[Zope3-checkins] CVS: Packages/ZConfig/tests - test_schema.py:1.1.2.17

Fred L. Drake, Jr. fred@zope.com
Fri, 13 Dec 2002 13:44:54 -0500


Update of /cvs-repository/Packages/ZConfig/tests
In directory cvs.zope.org:/tmp/cvs-serv315

Modified Files:
      Tag: zconfig-schema-devel-branch
	test_schema.py 
Log Message:
Add several tests of the datatype converters.


=== Packages/ZConfig/tests/test_schema.py 1.1.2.16 => 1.1.2.17 ===
--- Packages/ZConfig/tests/test_schema.py:1.1.2.16	Fri Dec 13 12:28:11 2002
+++ Packages/ZConfig/tests/test_schema.py	Fri Dec 13 13:44:54 2002
@@ -14,10 +14,12 @@
 """Tests of ZConfig schemas."""
 
 import StringIO
+import sys
 import unittest
 import urlparse
 
 import ZConfig
+import ZConfig.datatypes
 
 from ZConfig.loader import ConfigLoader
 from ZConfig.tests.test_config import CONFIG_BASE
@@ -248,6 +250,102 @@
         eq(conf.g[1].k1, "value1")
         eq(conf.g[2].k2, "default2")
         eq(conf.g[3].k2, "value2")
+
+    # datatype tests
+
+    def test_datatype_basickey(self):
+        convert = ZConfig.datatypes.get("basic-key")
+        eq = self.assertEqual
+        raises = self.assertRaises
+
+        eq(convert("abc"), "abc")
+        eq(convert("ABC_DEF.123"), "abc_def.123")
+        eq(convert("Abc-Def-456"), "abc-def-456")
+        eq(convert("Abc.Def"), "abc.def")
+
+        raises(ValueError, convert, "_abc")
+        raises(ValueError, convert, "-abc")
+        raises(ValueError, convert, "123")
+        raises(ValueError, convert, "")
+
+    def test_datatype_boolean(self):
+        convert = ZConfig.datatypes.get("boolean")
+        check = self.assert_
+        raises = self.assertRaises
+
+        check(convert("on"))
+        check(convert("true"))
+        check(convert("yes"))
+        check(not convert("off"))
+        check(not convert("false"))
+        check(not convert("no"))
+        raises(ValueError, convert, '0')
+        raises(ValueError, convert, '1')
+        raises(ValueError, convert, '')
+        raises(ValueError, convert, 'junk')
+
+    def test_datatype_identifier(self):
+        convert = ZConfig.datatypes.get("identifier")
+        eq = self.assertEqual
+        raises = self.assertRaises
+
+        eq(convert("AbcDef"), "AbcDef")
+        eq(convert("a________"), "a________")
+        eq(convert("abc_def"), "abc_def")
+        eq(convert("int123"), "int123")
+        eq(convert("_abc"), "_abc")
+        eq(convert("_123"), "_123")
+        eq(convert("__dict__"), "__dict__")
+        raises(ValueError, convert, "2345")
+        raises(ValueError, convert, "-abc")
+        raises(ValueError, convert, "-123")
+        raises(ValueError, convert, "")
+
+    def test_datatype_integer(self):
+        convert = ZConfig.datatypes.get("integer")
+        eq = self.assertEqual
+        raises = self.assertRaises
+
+        eq(convert('-100'), -100)
+        eq(convert('-1'), -1)
+        eq(convert('-0'), 0)
+        eq(convert('0'), 0)
+        eq(convert('1'), 1)
+        eq(convert('100'), 100)
+        eq(convert('65535'), 65535)
+        eq(convert('65536'), 65536)
+
+        big = sys.maxint + 1L  # Python 2.1 needs the L suffix here
+        s = str(big)           # s won't have the suffix
+        eq(convert(s), big)
+        eq(convert("-" + s), -big)
+
+        raises(ValueError, convert, 'abc')
+        raises(ValueError, convert, '-0xabc')
+        raises(ValueError, convert, '')
+        raises(ValueError, convert, '123 456')
+        raises(ValueError, convert, '123-')
+
+    def test_datatype_locale(self):
+        convert = ZConfig.datatypes.get("locale")
+        # Python supports "C" even when the _locale module is not available
+        self.assertEqual(convert("C"), "C")
+        self.assertRaises(ValueError, convert, "no-such-locale")
+
+    def test_datatype_port(self):
+        convert = ZConfig.datatypes.get("port-number")
+        eq = self.assertEqual
+        raises = self.assertRaises
+
+        raises(ValueError, convert, '-1')
+        raises(ValueError, convert, '0')
+        eq(convert('1'), 1)
+        eq(convert('80'), 80)
+        eq(convert('1023'), 1023)
+        eq(convert('1024'), 1024)
+        eq(convert('60000'), 60000)
+        eq(convert('65535'), 0xffff)
+        raises(ValueError, convert, '65536')
 
     # utilities