changeset 373:0c65aac81807

Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 11 Jul 2021 17:08:06 +0200
parents ac3e3cd6faae
children 4d7ad20cb8f9
files configmix/config.py tests/test.py
diffstat 2 files changed, 92 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/configmix/config.py	Sun Jul 11 16:43:14 2021 +0200
+++ b/configmix/config.py	Sun Jul 11 17:08:06 2021 +0200
@@ -221,6 +221,17 @@
         else:
             return s
 
+    def getfirstintvar_s(self, *varnames, **kwds):
+        """Get a (possibly substituted) variable and coerce text to a
+        number.
+
+        """
+        s = self.getfirstvar_s(*varnames, **kwds)
+        if isinstance(s, self._TEXTTYPE):
+            return int(s, 0)
+        else:
+            return s
+
     def getboolvarl_s(self, *names, **kwds):
         """Get a (possibly substituted) variable and convert text to a
         boolean
@@ -249,6 +260,20 @@
         else:
             return s
 
+    def getfirstboolvar_s(self, *varnames, **kwds):
+        """Get a (possibly substituted) variable and convert text to a
+        boolean
+
+        """
+        s = self.getfirstvar_s(*varnames, **kwds)
+        if isinstance(s, self._TEXTTYPE):
+            sl = s.strip().lower()
+            if sl not in self._BOOL_CVT:
+                raise ValueError("Not a boolean: %r" % s)
+            return self._BOOL_CVT[sl]
+        else:
+            return s
+
     # Conversion of booleans
     _BOOL_CVT = {
         u('1'): True, u('yes'): True, u('true'): True, u('on'): True,
--- a/tests/test.py	Sun Jul 11 16:43:14 2021 +0200
+++ b/tests/test.py	Sun Jul 11 17:08:06 2021 +0200
@@ -514,6 +514,73 @@
             "intl.non.existing",
             "intl.non.existing2")
 
+    def test23_getfirstintvar_s_nonexisting(self):
+        cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
+        self.assertIsNone(cfg.getfirstintvar_s("db.non.existing.key",
+                                               "db.non.existing.key2",
+                                               "intl.non.existing",
+                                               default=None))
+        self.assertRaises(
+            KeyError,
+            cfg.getfirstintvar_s,
+            "db.non.existing.key",
+            "db.non.exksting.key2",
+            "intl.non.existing")
+
+    def test23_getfirstintvar_s_nonexisting(self):
+        cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
+        self.assertEqual(20,
+                         cfg.getfirstintvar_s("db.non.existing.key",
+                                              "db.non.existing.key2",
+                                              "intl.non.existing",
+                                              default=u("20")))
+        self.assertEqual(30,
+                         cfg.getfirstintvar_s("db.non.existing.key",
+                                              "db.non.existing.key2",
+                                              "intl.non.existing",
+                                              default=30))
+        self.assertRaises(
+            KeyError,
+            cfg.getfirstintvar_s,
+            "db.non.existing.key",
+            "db.non.exksting.key2",
+            "intl.non.existing")
+
+    def test24_getfirstintvar_s_existing(self):
+        cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
+        self.assertEqual(10,
+                         cfg.getfirstintvar_s("db.non.existing.key",
+                                              "intl.cache.items",
+                                              "db.non.existing.key2",
+                                              default=u("20")))
+
+    def test25_getfirstboolvar_s_nonexisting(self):
+        cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
+        self.assertFalse(
+            cfg.getfirstboolvar_s("db.non.existing.key",
+                                  "db.non.existing.key2",
+                                  "db.engines.rw.no-echo",
+                                  default=u("false")))
+        self.assertTrue(
+            cfg.getfirstboolvar_s("db.non.existing.key",
+                                  "db.non.existing.key2",
+                                  "db.engines.rw.no-echo",
+                                  default=True))
+        self.assertRaises(
+            KeyError,
+            cfg.getfirstboolvar_s,
+            "db.non.existing.key",
+            "db.non.exksting.key2",
+            "b.engines.rw.no-echo")
+
+    def test26_getfirstboolvar_s_existing(self):
+        cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml"))
+        self.assertFalse(
+            cfg.getfirstboolvar_s("db.non.existing.key",
+                                  "db.engines.rw.echo",
+                                  "db.non.existing.key2",
+                                  default=u("true")))
+
 
 class T02LoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase):