Mercurial > hgrepos > Python > libs > ConfigMix
comparison tests/test.py @ 656:2b1c7a68f913
Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 30 May 2022 09:31:29 +0200 |
| parents | fe1299825a9a |
| children | 213f0ec3bbbc |
comparison
equal
deleted
inserted
replaced
| 655:b74f20e19c01 | 656:2b1c7a68f913 |
|---|---|
| 942 for i in range(4): | 942 for i in range(4): |
| 943 self.assertEqual( | 943 self.assertEqual( |
| 944 i, | 944 i, |
| 945 cfg.getvarl_s(u"test", u"List", i)) | 945 cfg.getvarl_s(u"test", u"List", i)) |
| 946 | 946 |
| 947 def test48_index_access_to_lists_with_subdicts(self): | 947 def test48_indexed_access_to_lists(self): |
| 948 cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml")) | |
| 949 for i in range(4): | |
| 950 self.assertEqual( | |
| 951 i, | |
| 952 cfg.getvar_s(u"test.List.~%d~" % (i, ))) | |
| 953 | |
| 954 def test49_index_access_to_lists_with_subdicts(self): | |
| 948 cfg = self._load(os.path.join(TESTDATADIR, | 955 cfg = self._load(os.path.join(TESTDATADIR, |
| 949 "index-access-for-jails.yml")) | 956 "index-access-for-jails.yml")) |
| 950 for idx in range(len(cfg.getvarl(u"the-list"))): | 957 for idx in range(len(cfg.getvarl(u"the-list"))): |
| 951 self.assertEqual( | 958 self.assertEqual( |
| 952 idx, | 959 idx, |
| 953 cfg.getvarl_s(u"the-list", idx, u"entry")) | 960 cfg.getvarl_s(u"the-list", idx, u"entry")) |
| 961 | |
| 962 def test50_index_access_to_lists_with_subdicts(self): | |
| 963 cfg = self._load(os.path.join(TESTDATADIR, | |
| 964 "index-access-for-jails.yml")) | |
| 965 for idx in range(len(cfg.getvarl(u"the-list"))): | |
| 966 self.assertEqual( | |
| 967 idx, | |
| 968 cfg.getvar_s(u"the-list.~%d~.entry" % (idx, ))) | |
| 954 | 969 |
| 955 | 970 |
| 956 class T02LoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase): | 971 class T02LoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase): |
| 957 | 972 |
| 958 def setUp(self): | 973 def setUp(self): |
| 1895 TESTDATADIR, "index-access-for-jails.yml")) | 1910 TESTDATADIR, "index-access-for-jails.yml")) |
| 1896 for idx in range(len(cfg.getvarl(u"the-list"))): | 1911 for idx in range(len(cfg.getvarl(u"the-list"))): |
| 1897 jcfg = cfg.jailed(rootpath=(u"the-list", idx)) | 1912 jcfg = cfg.jailed(rootpath=(u"the-list", idx)) |
| 1898 self.assertEqual(1, len(jcfg)) | 1913 self.assertEqual(1, len(jcfg)) |
| 1899 self.assertEqual(idx, jcfg.getvarl_s(u"entry")) | 1914 self.assertEqual(idx, jcfg.getvarl_s(u"entry")) |
| 1915 self.assertEqual((u"the-list", idx), jcfg._path) | |
| 1916 self.assertEqual(u"the-list.~%d~." % (idx, ), jcfg._pathstr) | |
| 1917 | |
| 1918 def test_index_jail_access_with_strpath(self): | |
| 1919 cfg = configmix.load(os.path.join( | |
| 1920 TESTDATADIR, "index-access-for-jails.yml")) | |
| 1921 for idx in range(len(cfg.getvarl(u"the-list"))): | |
| 1922 jcfg = cfg.jailed(root=u"the-list.~%d~" % (idx, )) | |
| 1923 self.assertEqual(1, len(jcfg)) | |
| 1924 self.assertEqual(idx, jcfg.getvarl_s(u"entry")) | |
| 1925 self.assertEqual((u"the-list", idx), jcfg._path) | |
| 1926 self.assertEqual(u"the-list.~%d~." % (idx, ), jcfg._pathstr) | |
| 1900 | 1927 |
| 1901 | 1928 |
| 1902 class _TParserMixin: | 1929 class _TParserMixin: |
| 1903 def test_quote_and_unquote_empty(self): | 1930 def test_quote_and_unquote_empty(self): |
| 1904 e = self.quote(u"") | 1931 e = self.quote(u"") |
| 1905 self.assertEqual(u"", e) | 1932 self.assertEqual(u"", e) |
| 1906 self.assertEqual(u"", self.unquote(e)) | 1933 self.assertEqual(u"", self.unquote(e)) |
| 1907 | 1934 |
| 1908 def test_quoting_and_unquoting_are_inverse(self): | 1935 def test_quoting_and_unquoting_are_inverse(self): |
| 1909 for c in u"""%.:#|"'{}[]""": | 1936 for c in u"""%.:#|"'{}[]~""": |
| 1910 qc = self.quote(c) | 1937 qc = self.quote(c) |
| 1911 self.assertTrue(qc.startswith(u"%x") and len(qc) == 4) | 1938 self.assertTrue(qc.startswith(u"%x") and len(qc) == 4) |
| 1912 self.assertEqual(c, self.unquote(qc)) | 1939 self.assertEqual(c, self.unquote(qc)) |
| 1913 | 1940 |
| 1914 def test_quoting_and_unquoting_are_inverse_all(self): | 1941 def test_quoting_and_unquoting_are_inverse_all(self): |
| 1915 c = u"""%.:#|"'{}[]""" | 1942 c = u"""%.:#|"'{}[]~""" |
| 1916 qc = self.quote(c) | 1943 qc = self.quote(c) |
| 1917 self.assertEqual(len(c)*4, len(qc)) | 1944 self.assertEqual(len(c)*4, len(qc)) |
| 1918 self.assertEqual(c, self.unquote(qc)) | 1945 self.assertEqual(c, self.unquote(qc)) |
| 1919 | 1946 |
| 1920 def test_quoting_and_unquoting_are_identical(self): | 1947 def test_quoting_and_unquoting_are_identical(self): |
| 1928 # other characters | 1955 # other characters |
| 1929 qc = self.quote(configmix.config._QUOTE_SAFE) | 1956 qc = self.quote(configmix.config._QUOTE_SAFE) |
| 1930 self.assertEqual(configmix.config._QUOTE_SAFE, qc) | 1957 self.assertEqual(configmix.config._QUOTE_SAFE, qc) |
| 1931 self.assertEqual(configmix.config._QUOTE_SAFE, self.unquote(qc)) | 1958 self.assertEqual(configmix.config._QUOTE_SAFE, self.unquote(qc)) |
| 1932 | 1959 |
| 1960 def test_quote_index_to_tilde(self): | |
| 1961 self.assertEqual(u"~4~", self.quote(4)) | |
| 1962 | |
| 1963 def test_unquote_index_with_tilde(self): | |
| 1964 self.assertEqual(4, self.unquote(u"~4~")) | |
| 1965 | |
| 1966 def test_unquote_empty_tilde(self): | |
| 1967 self.assertEqual(u"~~", self.unquote(u"~~")) | |
| 1968 | |
| 1969 def test_unquote_invalid_number_tilde(self): | |
| 1970 self.assertEqual(u"~0x4~", self.unquote(u"~0x4~")) | |
| 1971 | |
| 1972 def test_unquote_invalid_number_tilde_2(self): | |
| 1973 self.assertEqual(u"~\U00019001~", self.unquote(u"~%U00019001~")) | |
| 1974 | |
| 1975 def test_quote_unquote_indexes(self): | |
| 1976 for idx in range(0, 10000): | |
| 1977 self.assertEqual(idx, self.unquote(self.quote(idx))) | |
| 1978 | |
| 1979 def test_quote_unquote_negative_index(self): | |
| 1980 for idx in (-1, -2, -3): | |
| 1981 self.assertEqual(idx, self.unquote(self.quote(idx))) | |
| 1982 | |
| 1983 def test_index_overflow_border(self): | |
| 1984 self.assertEqual(32759, self.unquote(u"~32759~")) | |
| 1985 self.assertEqual(u"~32760~", self.unquote(u"~32760~")) | |
| 1986 | |
| 1933 def test_unquote_unimax(self): | 1987 def test_unquote_unimax(self): |
| 1934 self.assertEqual(u"\U00019001", self.unquote(u"%U00019001")) | 1988 self.assertEqual(u"\U00019001", self.unquote(u"%U00019001")) |
| 1935 self.assertEqual(u"X\U00019AF1Z", self.unquote(u"X%U00019aF1Z")) | 1989 self.assertEqual(u"X\U00019AF1Z", self.unquote(u"X%U00019aF1Z")) |
| 1936 | 1990 |
| 1937 def test_unquote_base_plane(self): | 1991 def test_unquote_base_plane(self): |
| 2010 self.assertEqual((u"1", u"2", u""), p) | 2064 self.assertEqual((u"1", u"2", u""), p) |
| 2011 | 2065 |
| 2012 def test_split_unquote(self): | 2066 def test_split_unquote(self): |
| 2013 p = self.pathstr2path(u"a%x2Eb.c%u002Ed.e%U0000002Ef") | 2067 p = self.pathstr2path(u"a%x2Eb.c%u002Ed.e%U0000002Ef") |
| 2014 self.assertEqual((u"a.b", u"c.d", u"e.f"), p) | 2068 self.assertEqual((u"a.b", u"c.d", u"e.f"), p) |
| 2069 | |
| 2070 def test_split_unquote_with_index(self): | |
| 2071 p = self.pathstr2path(u"a%x2Eb.~555~.c%u002Ed.e%U0000002Ef.~6~") | |
| 2072 self.assertEqual((u"a.b", 555, u"c.d", u"e.f", 6), p) | |
| 2015 | 2073 |
| 2016 def test_split_ns_empty(self): | 2074 def test_split_ns_empty(self): |
| 2017 self.assertEqual((None, u""), self.split_ns(u"")) | 2075 self.assertEqual((None, u""), self.split_ns(u"")) |
| 2018 | 2076 |
| 2019 def test_split_ns_empty_parts(self): | 2077 def test_split_ns_empty_parts(self): |
| 2241 | 2299 |
| 2242 def test_quote_wrong_type(self): | 2300 def test_quote_wrong_type(self): |
| 2243 self.assertRaises( | 2301 self.assertRaises( |
| 2244 AttributeError, # no .lstrip | 2302 AttributeError, # no .lstrip |
| 2245 self.quote, | 2303 self.quote, |
| 2246 1) | 2304 1.0) |
| 2247 | 2305 |
| 2248 def test_unquote_wrong_type(self): | 2306 def test_unquote_wrong_type(self): |
| 2249 self.assertRaises( | 2307 self.assertRaises( |
| 2250 TypeError, # argument of type "int" is not iterable | 2308 TypeError, # argument of type "int" is not iterable |
| 2251 self.unquote, | 2309 self.unquote, |
