[Zodb-checkins] CVS: Zope/lib/python/ZConfig/tests - testSubstitution.py:1.3.2.1 testConfig.py:1.6.4.1 testInterp.py:NONE

Andreas Jung andreas@andreas-jung.com
Sat, 9 Nov 2002 03:43:29 -0500


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

Modified Files:
      Tag: ajung-restructuredtext-integration-branch
	testConfig.py 
Added Files:
      Tag: ajung-restructuredtext-integration-branch
	testSubstitution.py 
Removed Files:
      Tag: ajung-restructuredtext-integration-branch
	testInterp.py 
Log Message:
merge from trunk


=== 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/testConfig.py 1.6 => 1.6.4.1 ===
--- Zope/lib/python/ZConfig/tests/testConfig.py:1.6	Mon Oct 21 14:59:55 2002
+++ Zope/lib/python/ZConfig/tests/testConfig.py	Sat Nov  9 03:42:58 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 ===