[Zodb-checkins] CVS: Zope/lib/python/ZConfig/tests - testSubstitution.py:1.3.6.1 __init__.py:1.1.4.2 testConfig.py:1.1.4.4 testInterp.py:NONE

Chris McDonough chrism@zope.com
Fri, 15 Nov 2002 20:26:18 -0500


Update of /cvs-repository/Zope/lib/python/ZConfig/tests
In directory cvs.zope.org:/tmp/cvs-serv25541/tests

Modified Files:
      Tag: chrism-install-branch
	__init__.py testConfig.py 
Added Files:
      Tag: chrism-install-branch
	testSubstitution.py 
Removed Files:
      Tag: chrism-install-branch
	testInterp.py 
Log Message:
Updating chrism-install-branch ZConfig to HEAD.


=== Added File Zope/lib/python/ZConfig/tests/testSubstitution.py ===
"""Tests of the string interpolation module."""

# This is needed to support Python 2.1.
from __future__ import nested_scopes

import unittest

from UserDict import UserDict

from ZConfig.Substitution import get, substitute
from ZConfig.Substitution import SubstitutionRecursionError
from ZConfig.Substitution import SubstitutionSyntaxError


class ContainerDict(UserDict):
    def __init__(self, mapping=None, container=None):
        self.container = container
        UserDict.__init__(self, mapping)


class SubstitutionTestCase(unittest.TestCase):
    def test_simple_names(self):
        d = {"name": "value",
             "name1": "abc",
             "name_": "def",
             "_123": "ghi"}
        def check(s, v):
            self.assertEqual(substitute(s, d), v)
        check("$name", "value")
        check(" $name ", " value ")
        check("${name}", "value")
        check(" ${name} ", " value ")
        check("$name$name", "valuevalue")
        check("$name1$name", "abcvalue")
        check("$name_$name", "defvalue")
        check("$_123$name", "ghivalue")
        check("$name $name", "value value")
        check("$name1 $name", "abc value")
        check("$name_ $name", "def value")
        check("$_123 $name", "ghi value")
        check("splat", "splat")
        check("$$", "$")
        check("$$$name$$", "$value$")

    def test_undefined_names(self):
        d = {"name": "value"}
        self.assertEqual(substitute("$splat", d), "")
        self.assertEqual(substitute("$splat1", d), "")
        self.assertEqual(substitute("$splat_", d), "")

    def test_syntax_errors(self):
        d = {"name": "value"}
        def check(s):
            self.assertRaises(SubstitutionSyntaxError,
                              substitute, s, d)
        check("${")
        check("${name")
        check("${1name}")
        check("${ name}")

    def test_edge_cases(self):
        # It's debatable what should happen for these cases, so we'll
        # follow the lead of the Bourne shell here.
        def check(s):
            self.assertEqual(substitute(s, {}), s)
        check("$1")
        check("$")
        check("$ stuff")

    def test_non_nesting(self):
        d = {"name": "$value"}
        self.assertEqual(substitute("$name", d), "$value")

    def test_simple_nesting(self):
        d = {"name": "value",
             "nest": "$splat",
             "splat": "nested"}
        def check(name, value):
            self.assertEqual(get(d, name), value)
        check("name", "value")
        check("nest", "nested")

    def test_nesting_errors(self):
        d = {"name": "$splat",
             "splat": "$name"}
        self.assertRaises(SubstitutionRecursionError,
                          get, d, "name")
        d = {"name": "$splat",
             "splat": "$splat"}
        self.assertRaises(SubstitutionRecursionError,
                          get, d, "name")

    def test_container_search(self):
        d1 = {"outer": "outervalue",
              "inner": "inner-from-outer"}
        d2 = ContainerDict({"inner": "inner-from-inner",
                            "bogus": "${nothere}",
                            "both": "${inner} ${outer}"}, d1)
        self.assertEqual(get(d2, "both"),
                         "inner-from-inner outervalue")
        self.assertEqual(get(d2, "bogus"), "")


def test_suite():
    return unittest.makeSuite(SubstitutionTestCase)

if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')


=== Zope/lib/python/ZConfig/tests/__init__.py 1.1.4.1 => 1.1.4.2 ===


=== Zope/lib/python/ZConfig/tests/testConfig.py 1.1.4.3 => 1.1.4.4 ===
--- Zope/lib/python/ZConfig/tests/testConfig.py:1.1.4.3	Sat Oct 26 15:51:48 2002
+++ Zope/lib/python/ZConfig/tests/testConfig.py	Fri Nov 15 20:26:18 2002
@@ -14,6 +14,7 @@
 """Tests of the configuration data structures and loader."""
 
 import os
+import StringIO
 import tempfile
 import unittest
 import urllib
@@ -21,6 +22,7 @@
 import warnings
 
 import ZConfig
+
 from ZConfig.Context import Context
 from ZConfig.Common import ConfigurationError, ConfigurationTypeError
 
@@ -237,6 +239,15 @@
         finally:
             os.chdir(pwd)
             os.unlink(fn)
+
+    def test_load_from_fileobj(self):
+        sio = StringIO.StringIO("name value\n"
+                                "<section>\n"
+                                "  name value2\n"
+                                "</section>\n")
+        cf = ZConfig.loadfile(sio)
+        self.assertEqual(cf.get("name"), "value")
+        self.assertEqual(cf.getSection("section").get("name"), "value2")
 
     def write_tempfile(self):
         fn = tempfile.mktemp()

=== Removed File Zope/lib/python/ZConfig/tests/testInterp.py ===