[pypy-svn] r29763 - in pypy/dist/pypy/config: . test

guido at codespeak.net guido at codespeak.net
Fri Jul 7 18:29:28 CEST 2006


Author: guido
Date: Fri Jul  7 18:29:27 2006
New Revision: 29763

Modified:
   pypy/dist/pypy/config/config.py
   pypy/dist/pypy/config/test/test_config.py
Log:
(cfbolz, guido) added BoolOption and ListOption


Modified: pypy/dist/pypy/config/config.py
==============================================================================
--- pypy/dist/pypy/config/config.py	(original)
+++ pypy/dist/pypy/config/config.py	Fri Jul  7 18:29:27 2006
@@ -32,8 +32,15 @@
 
     def _freeze_(self):
         return True
-        
+
 class Option(object):
+    def validate(self, value):
+        raise NotImplementedError('abstract base class')
+
+    def getdefault(self):
+        return self.default
+
+class ChoiceOption(Option):
     def __init__(self, name, doc, values, default):
         self._name = name
         self.doc = doc
@@ -43,6 +50,27 @@
     def validate(self, value):
         return value in self.values
 
+class BoolOption(ChoiceOption):
+    def __init__(self, name, doc, default=True):
+        super(BoolOption, self).__init__(name, doc, [True, False], default)
+
+class ListOption(Option):
+    def __init__(self, name, doc, allowed_values, default=None):
+        self._name = name
+        self.doc = doc
+        self.allowed_values = allowed_values
+        self.default = default or []
+
+    def validate(self, value):
+        assert isinstance(value, list)
+        for item in value:
+            if item not in self.allowed_values:
+                return False
+        return True
+
+    def getdefault(self):
+        return self.default[:]
+
 class OptionDescription(object):
     def __init__(self, name, children):
         self._name = name

Modified: pypy/dist/pypy/config/test/test_config.py
==============================================================================
--- pypy/dist/pypy/config/test/test_config.py	(original)
+++ pypy/dist/pypy/config/test/test_config.py	Fri Jul  7 18:29:27 2006
@@ -2,31 +2,46 @@
 import py
 
 def test_base_config():
-    gcoption = Option('name', 'GC name', ['ref', 'framework'], 'ref')
-    objspaceoption = Option('objspace', 'Object space', 
+    gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
+    objspaceoption = ChoiceOption('objspace', 'Object space', 
                                 ['std', 'logic'], 'std')
+    booloption = BoolOption('bool', 'Test Boolean option')
+    listoption = ListOption('list', 'Test list valued option', ['foo', 'bar'],
+                                ['foo'])
+    
     gcgroup = OptionDescription('gc', [gcoption])
-    descr = OptionDescription('pypy', [gcgroup, objspaceoption])
+    descr = OptionDescription('pypy', [gcgroup, booloption, objspaceoption,
+                                        listoption])
     config = Config(descr)
     
     assert config.gc.name == 'ref'
     config.gc.name = 'framework'
     assert config.gc.name == 'framework'
+
     assert config.objspace == 'std'
     config.objspace = 'logic'
     assert config.objspace == 'logic'
+    
+    assert config.bool
+    config.bool = False
+    assert not config.bool
+
+    assert config.list == ['foo']
+    config.list = ['bar']
+    assert config.list == ['bar']
+
     py.test.raises(ValueError, 'config.objspace = "foo"')
     py.test.raises(ValueError, 'config.gc.name = "foo"')
     py.test.raises(ValueError, 'config.gc.foo = "bar"')
+    py.test.raises(ValueError, 'config.bool = 123')
+    py.test.raises(ValueError, 'config.list = ["baz"]')
 
 def test_annotator_folding():
     from pypy.translator.interactive import Translation
 
-    gcoption = Option('name', 'GC name', ['ref', 'framework'], 'ref')
-    objspaceoption = Option('objspace', 'Object space', 
-                                ['std', 'logic'], 'std')
+    gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
     gcgroup = OptionDescription('gc', [gcoption])
-    descr = OptionDescription('pypy', [gcgroup, objspaceoption])
+    descr = OptionDescription('pypy', [gcgroup])
     config = Config(descr)
     
     def f(x):
@@ -43,3 +58,4 @@
     assert len(block.operations) == 1
     assert len(block.exits) == 1
     assert block.operations[0].opname == 'int_add'
+



More information about the Pypy-commit mailing list