Fixed/added some config parsing tests
This commit is contained in:
@@ -253,20 +253,19 @@ class MultiWorldParser(object):
|
|||||||
for key in settingsDefinition.render:
|
for key in settingsDefinition.render:
|
||||||
option = settingsDefinition.render[key]
|
option = settingsDefinition.render[key]
|
||||||
if option.has_key("default"):
|
if option.has_key("default"):
|
||||||
self.defaults[key] = option.get("default")
|
self.defaults[key] = option["default"]
|
||||||
|
|
||||||
|
|
||||||
self.defaults.update(glob)
|
self.defaults.update(glob)
|
||||||
|
|
||||||
|
|
||||||
import pprint
|
|
||||||
pprint.pprint(glob, indent=2)
|
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
|
|
||||||
|
|
||||||
for worldname in self.render:
|
for worldname in self.render:
|
||||||
world = self.render[worldname]
|
world = dict()
|
||||||
world.update(self.defaults)
|
world.update(self.defaults)
|
||||||
|
world.update(self.render[worldname])
|
||||||
|
|
||||||
for key in world:
|
for key in world:
|
||||||
if key not in settingsDefinition.render:
|
if key not in settingsDefinition.render:
|
||||||
@@ -278,8 +277,9 @@ class MultiWorldParser(object):
|
|||||||
val = definition['validator'](world[key])
|
val = definition['validator'](world[key])
|
||||||
world[key] = val
|
world[key] = val
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print "Error: %r" % e
|
#print "Error validating %s: %r" % (key, e)
|
||||||
next
|
raise e
|
||||||
|
self.render[worldname] = world
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -62,15 +62,22 @@ def validateBGColor(color):
|
|||||||
"""BG color must be an HTML color, with an option leading # (hash symbol)
|
"""BG color must be an HTML color, with an option leading # (hash symbol)
|
||||||
returns an (r,b,g) 3-tuple
|
returns an (r,b,g) 3-tuple
|
||||||
"""
|
"""
|
||||||
if color[0] != "#":
|
if type(color) == str:
|
||||||
color = "#%s" % color
|
if color[0] != "#":
|
||||||
if len(color) != 7:
|
color = "#" + color
|
||||||
raise ValidationException("%r is not a valid color. Expected HTML color syntax (i.e. #RRGGBB)" % color)
|
if len(color) != 7:
|
||||||
|
raise ValidationException("%r is not a valid color. Expected HTML color syntax (i.e. #RRGGBB)" % color)
|
||||||
r = int(color[1:3], 16)
|
try:
|
||||||
g = int(color[3:5], 16)
|
r = int(color[1:3], 16)
|
||||||
b = int(color[5:7], 16)
|
g = int(color[3:5], 16)
|
||||||
return (r,g,b,0)
|
b = int(color[5:7], 16)
|
||||||
|
return (r,g,b,0)
|
||||||
|
except ValueError:
|
||||||
|
raise ValidationException("%r is not a valid color. Expected HTML color syntax (i.e. #RRGGBB)" % color)
|
||||||
|
elif type(color) == tuple:
|
||||||
|
if len(color) != 4:
|
||||||
|
raise ValidationException("%r is not a valid color. Expected a 4-tuple" % (color,))
|
||||||
|
return color
|
||||||
|
|
||||||
|
|
||||||
def validateOptImg(opt):
|
def validateOptImg(opt):
|
||||||
|
|||||||
@@ -7,5 +7,6 @@ render["world"] = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render["otherworld"] = {
|
render["otherworld"] = {
|
||||||
"rendermode": "foo"
|
"rendermode": "foo",
|
||||||
|
"bgcolor": "#ffffff"
|
||||||
}
|
}
|
||||||
|
|||||||
6
test/data/settings/settings_test_bgcolor.py
Normal file
6
test/data/settings/settings_test_bgcolor.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
bgcolor="#000000"
|
||||||
|
|
||||||
|
render["world"] = {
|
||||||
|
"worldpath": "test/data/settings/test_world",
|
||||||
|
"bgcolor":"ffff"
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ class SettingsTest(unittest.TestCase):
|
|||||||
things = s.get_render_things()
|
things = s.get_render_things()
|
||||||
# no exceptions so far. that's good
|
# no exceptions so far. that's good
|
||||||
self.assertEquals(things['world']['bgcolor'], (26,26,26,0))
|
self.assertEquals(things['world']['bgcolor'], (26,26,26,0))
|
||||||
|
self.assertEquals(things['otherworld']['bgcolor'], (255,255,255,0))
|
||||||
|
|
||||||
def test_rendermode_validation(self):
|
def test_rendermode_validation(self):
|
||||||
s = configParser.MultiWorldParser("test/data/settings/settings_test_rendermode.py")
|
s = configParser.MultiWorldParser("test/data/settings/settings_test_rendermode.py")
|
||||||
@@ -22,5 +23,12 @@ class SettingsTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertRaises(ValidationException,s.validate)
|
self.assertRaises(ValidationException,s.validate)
|
||||||
|
|
||||||
|
def test_bgcolor_validation(self):
|
||||||
|
s = configParser.MultiWorldParser("test/data/settings/settings_test_bgcolor.py")
|
||||||
|
s.parse()
|
||||||
|
|
||||||
|
self.assertRaises(ValidationException, s.validate)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user