registry: Normalize values before checking they are valid

Otherwise, normalization is useless, and gives a surprising error message,
such as:

```
<user> config plugins.rss.sortfeeditems oldestfirst
<bot> Error: Valid values include 'asInFeed', 'oldestFirst', 'newestFirst', 'outdatedFirst', and 'updatedFirst', not 'oldestFirst'.
```
This commit is contained in:
Valentin Lorentz 2022-09-07 12:31:19 +02:00
parent 86b389618f
commit 200acdfa93
2 changed files with 21 additions and 2 deletions

View File

@ -1,7 +1,7 @@
### ###
# Copyright (c) 2002-2005, Jeremiah Fincher # Copyright (c) 2002-2005, Jeremiah Fincher
# Copyright (c) 2009, James McCoy # Copyright (c) 2009, James McCoy
# Copyright (c) 2010-2021, Valentin Lorentz # Copyright (c) 2010-2022, Valentin Lorentz
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -33,11 +33,20 @@ import random
from supybot.test import * from supybot.test import *
import supybot.conf as conf import supybot.conf as conf
import supybot.registry as registry
_letters = 'abcdefghijklmnopqrstuvwxyz' _letters = 'abcdefghijklmnopqrstuvwxyz'
def random_string(): def random_string():
return ''.join(random.choice(_letters) for _ in range(16)) return ''.join(random.choice(_letters) for _ in range(16))
class Fruit(registry.OnlySomeStrings):
validStrings = ('Apple', 'Orange')
group = conf.registerGroup(conf.supybot.plugins.Config, 'test')
conf.registerGlobalValue(group, 'fruit',
Fruit('Orange', '''Must be a fruit'''))
class ConfigTestCase(ChannelPluginTestCase): class ConfigTestCase(ChannelPluginTestCase):
# We add utilities so there's something in supybot.plugins. # We add utilities so there's something in supybot.plugins.
plugins = ('Config', 'User', 'Utilities', 'Web') plugins = ('Config', 'User', 'Utilities', 'Web')
@ -50,6 +59,16 @@ class ConfigTestCase(ChannelPluginTestCase):
self.assertNotRegexp('config get supybot.reply', r'registry\.Group') self.assertNotRegexp('config get supybot.reply', r'registry\.Group')
self.assertResponse('config supybot.protocols.irc.throttleTime', '0.0') self.assertResponse('config supybot.protocols.irc.throttleTime', '0.0')
def testSetOnlysomestrings(self):
self.assertResponse('config supybot.plugins.Config.test.fruit Apple',
'The operation succeeded.')
self.assertResponse('config supybot.plugins.Config.test.fruit orange',
'The operation succeeded.')
self.assertResponse('config supybot.plugins.Config.test.fruit Tomatoe',
"Error: Valid values include 'Apple' and "
"'Orange', not 'Tomatoe'.")
def testList(self): def testList(self):
self.assertError('config list asldfkj') self.assertError('config list asldfkj')
self.assertError('config list supybot.asdfkjsldf') self.assertError('config list supybot.asdfkjsldf')

View File

@ -710,7 +710,7 @@ class OnlySomeStrings(String):
def setValue(self, s): def setValue(self, s):
v = self.normalize(s) v = self.normalize(s)
if s in self.validStrings: if v in self.validStrings:
self.__parent.setValue(v) self.__parent.setValue(v)
else: else:
self.error(v) self.error(v)