diff tests/test.py @ 554:36d7aa000435

Implement a C-version of Configuration.interpolate_variables
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 03 Jan 2022 00:11:41 +0100
parents 9d2bd411f5c5
children 75ecbe07abff
line wrap: on
line diff
--- a/tests/test.py	Sun Jan 02 20:43:24 2022 +0100
+++ b/tests/test.py	Mon Jan 03 00:11:41 2022 +0100
@@ -1920,7 +1920,99 @@
     def test_split_filters_many(self):
         self.assertEqual((u"the-varname", [u"Empty", u"None"]),
                          self.split_filters(u"the-varname|Empty|None"))
-        
+
+    def test_None_filter_single(self):
+        cfg = configmix.load()
+        x = getattr(cfg, self.interpolate_meth)(u"{{non-existing|None}}")
+        self.assertIsNone(x)
+        # caching should have no effect
+        y = getattr(cfg, self.interpolate_meth)(u"{{non-existing|None}}")
+        self.assertIsNone(y)
+
+    def test_None_filter_embedded(self):
+        cfg = configmix.load()
+        x = getattr(cfg, self.interpolate_meth)(u"A{{non-existing|None}}Z")
+        self.assertEqual(u"AZ", x)
+        # caching should have no effect
+        y = getattr(cfg, self.interpolate_meth)(u"A{{non-existing|None}}Z")
+        self.assertEqual(u"AZ", y)
+
+    def test_Empty_filtersingle(self):
+        cfg = configmix.load()
+        x = getattr(cfg, self.interpolate_meth)(u"{{non-existing|Empty}}")
+        self.assertEqual("", x)
+        # caching should have no effect
+        y = getattr(cfg, self.interpolate_meth)(u"{{non-existing|Empty}}")
+        self.assertEqual("", y)
+
+    def test_None_filter_pass_through(self):
+        cfg = configmix.load(os.path.join(TESTDATADIR, "conf20.yml"),
+                             os.path.join(TESTDATADIR, "conf21.yml"),
+                             os.path.join(TESTDATADIR, "conf22.ini"),
+                             os.path.join(TESTDATADIR, "conf23.json"),
+                             os.path.join(TESTDATADIR, "conf24.toml"))
+        x = getattr(cfg, self.interpolate_meth)(u"{{intl.cache.items|None}}")
+        self.assertEqual(10, x)
+        # caching should have no effect
+        y = getattr(cfg, self.interpolate_meth)(u"{{intl.cache.items|None}}")
+        self.assertEqual(10, y)
+
+    def test_Empty_filter_pass_through(self):
+        cfg = configmix.load(os.path.join(TESTDATADIR, "conf20.yml"),
+                             os.path.join(TESTDATADIR, "conf21.yml"),
+                             os.path.join(TESTDATADIR, "conf22.ini"),
+                             os.path.join(TESTDATADIR, "conf23.json"),
+                             os.path.join(TESTDATADIR, "conf24.toml"))
+        x = getattr(cfg, self.interpolate_meth)(u"{{intl.cache.items|Empty}}")
+        self.assertEqual(10, x)
+        # caching should have no effect
+        y = getattr(cfg, self.interpolate_meth)(u"{{intl.cache.items|Empty}}")
+        self.assertEqual(10, y)
+
+    def test_Empty_filter_no_pass_through_2(self):
+        cfg = configmix.load(os.path.join(TESTDATADIR, "conf20.yml"),
+                             os.path.join(TESTDATADIR, "conf21.yml"),
+                             os.path.join(TESTDATADIR, "conf22.ini"),
+                             os.path.join(TESTDATADIR, "conf23.json"),
+                             os.path.join(TESTDATADIR, "conf24.toml"))
+        x = getattr(cfg, self.interpolate_meth)(u"{{intl.cache.items|Empty}}{{intl.cache.items}}")
+        self.assertEqual(u"1010", x)
+        # caching should have no effect
+        y = getattr(cfg, self.interpolate_meth)(u"{{intl.cache.items|Empty}}{{intl.cache.items}}")
+        self.assertEqual(u"1010", y)        
+
+    def test_interpolate_wrong_syntax(self):
+        cfg = configmix.load()
+        x1 = getattr(cfg, self.interpolate_meth)(u"{{the-variable}")
+        self.assertEqual(u"{{the-variable}", x1)
+        x2 = getattr(cfg, self.interpolate_meth)(u"A{{the-variable}Z")
+        self.assertEqual(u"A{{the-variable}Z", x2)
+        x3 = getattr(cfg, self.interpolate_meth)(u"A{{1{{2{{3}Z")
+        self.assertEqual(u"A{{1{{2{{3}Z", x3)
+        # caching should have no effect
+        y1 = getattr(cfg, self.interpolate_meth)(u"{{the-variable}")
+        self.assertEqual(u"{{the-variable}", y1)
+        y2 = getattr(cfg, self.interpolate_meth)(u"A{{the-variable}Z")
+        self.assertEqual(u"A{{the-variable}Z", y2)
+        y3 = getattr(cfg, self.interpolate_meth)(u"A{{1{{2{{3}Z")
+        self.assertEqual(u"A{{1{{2{{3}Z", y3)
+
+    def test_interpolate_empty_str(self):
+        cfg = configmix.load()
+        x = getattr(cfg, self.interpolate_meth)(u"")
+        self.assertEqual(u"", x)
+        # caching should have no effect
+        y = getattr(cfg, self.interpolate_meth)(u"")
+        self.assertEqual(u"", y)
+
+    def test_interpolate_no_interpolation(self):
+        cfg = configmix.load()
+        x = getattr(cfg, self.interpolate_meth)(u"no-interpolation-here")
+        self.assertEqual(u"no-interpolation-here", x)
+        # caching should have no effect
+        y = getattr(cfg, self.interpolate_meth)(u"no-interpolation-here")
+        self.assertEqual(u"no-interpolation-here", y)
+
 
 class T09Parser(_TParserMixin, unittest.TestCase):
 
@@ -1930,6 +2022,7 @@
         self.pathstr2path = configmix.config.py_pathstr2path
         self.split_ns = configmix.config._py_split_ns
         self.split_filters = configmix.config._py_split_filters
+        self.interpolate_meth = "py_interpolate_variables"
 
     def test_split_ns_wrong_type(self):
         self.assertRaises(
@@ -1959,6 +2052,7 @@
             self.pathstr2path = configmix.config.fast_pathstr2path
             self.split_ns = configmix.config._fast_split_ns
             self.split_filters = configmix.config._fast_split_filters
+            self.interpolate_meth = "fast_interpolate_variables"
 
         def test_split_ns_wrong_type(self):
             self.assertRaises(