def test_merge_regxs(self): pm1 = Pmod(_alias='pm1', _regxs=[ ('e', Pmod(_alias='E')), ('a', Pmod(_alias='A')), ('c', Pmod(_alias='C'))]) pm2 = Pmod(_alias='pm2', _regxs=[ ('b', Pmod(_alias='B')), ('a', Pmod(_alias='AA')), ('d', Pmod(_alias='DD')), ]) pm = pm1._merge(pm2) self.assertSequenceEqual(pmod2regexstrs(pm), list('ecbad')) self.assertEqual(pm._regxs[re.compile('a')]._alias, 'AA') self.assertSequenceEqual(pmod2regexstrs(pm1), list('eac')) self.assertSequenceEqual(pmod2regexstrs(pm2), list('bad')) self.assertEqual(pm._steps, {}) self.assertEqual(pm1._steps, {}) self.assertEqual(pm2._steps, {}) pm = pm2._merge(pm1) self.assertSequenceEqual(pmod2regexstrs(pm), list('bdeac')) self.assertEqual(pm._regxs[re.compile('a')]._alias, 'A') self.assertSequenceEqual(pmod2regexstrs(pm1), list('eac')) self.assertSequenceEqual(pmod2regexstrs(pm2), list('bad')) self.assertEqual(pm._steps, {}) self.assertEqual(pm1._steps, {}) self.assertEqual(pm2._steps, {})
def test_pmods_from_tuples_rootMapping(self): self.assertEqual( pmods_from_tuples([("", "A/B")]), Pmod(_steps={"": Pmod(_alias="A/B")}) ) self.assertEqual( pmods_from_tuples([("/", "A/B")]), Pmod(_steps={"": Pmod(_steps={"": Pmod(_alias="A/B")})}), )
def test_pmods_from_tuple_ignores_empties(self): # Ignored by :func:`pmods_from_tuples(). pmods = pmods_from_tuples([("", "")]) self.assertEqual(pmods, Pmod()) pmods = pmods_from_tuples([("spam", None)]) self.assertEqual(pmods, Pmod()) pmods = pmods_from_tuples([(None, "spam")]) self.assertEqual(pmods, Pmod())
def test_alias_invalid_group_ref_(self): pm = Pmod(_regxs=[ ('a', Pmod(_alias='A\\1')), ('b(.*)', Pmod(_alias='B\\2')), ]) with self.assertRaises(sre_constants.error): pm.alias('a') with self.assertRaises(sre_constants.error): pm.alias('bach')
def test_descend_None(self): pm = Pmod() self.assert_descend_stops(pm.descend("a")) pm = Pmod(_alias="a") self.assert_descend_stops(pm.descend("a")) pm = Pmod(_steps={"a": None}) self.assert_descend_stops(pm.descend("a"))
def test_alias_None(self): pm = Pmod() self.assertIsNone(pm.alias("a")) pm = Pmod(_alias="a") self.assertIsNone(pm.alias("a")) pm = Pmod(_steps={"a": None}) self.assertIsNone(pm.alias("a"))
def test_alias_invalid_group_ref_(self): pm = Pmod(_regxs=[("a", Pmod(_alias="A\\1")), ("b(.*)", Pmod(_alias="B\\2"))]) with self.assertRaises(sre_constants.error): pm.alias("a") with self.assertRaises(sre_constants.error): pm.alias("bach")
def test_merge_name_recurse(self): pm1 = Pmod(_alias='pm1', _steps={'a': Pmod(_alias='R1')}) pm2 = Pmod(_alias='pm2', _steps={'a': Pmod(_alias='R2'), 'b': Pmod(_alias='R22')}) pm = pm1._merge(pm2) self.assertEqual(pm._steps['a']._alias, 'R2') self.assertEqual(pm1._steps['a']._alias, 'R1') self.assertEqual(pm2._steps['a']._alias, 'R2') self.assertEqual(len(pm1._steps), 1) self.assertEqual(len(pm2._steps), 2) pm = pm2._merge(pm1) self.assertEqual(pm._steps['a']._alias, 'R1') self.assertEqual(pm1._steps['a']._alias, 'R1') self.assertEqual(pm2._steps['a']._alias, 'R2') self.assertEqual(len(pm1._steps), 1) self.assertEqual(len(pm2._steps), 2)
def test_descend(self): pm = Pmod( _steps={'a': Pmod(_alias='A', _steps={1: 11})}, _regxs=[ ('a\w*', Pmod(_alias='AWord', _steps={2: 22})), ('a\d*', Pmod(_alias='ADigit', _steps={3: 33})), ]) # All children and regexps match. self.assertDictEqual(pm.descend('a')[0]._steps, {1: 11, 2: 22, 3: 33}) # Only 'a\w*' matches. self.assertDictEqual(pm.descend('aa')[0]._steps, {2: 22}) # Both regexps matches. self.assertDictEqual(pm.descend('a1')[0]._steps, {2: 22, 3: 33})
def test_merge_name_recurse(self): pm1 = Pmod(_alias="pm1", _steps={"a": Pmod(_alias="R1")}) pm2 = Pmod( _alias="pm2", _steps={"a": Pmod(_alias="R2"), "b": Pmod(_alias="R22")} ) pm = pm1._merge(pm2) self.assertEqual(pm._steps["a"]._alias, "R2") self.assertEqual(pm1._steps["a"]._alias, "R1") self.assertEqual(pm2._steps["a"]._alias, "R2") self.assertEqual(len(pm1._steps), 1) self.assertEqual(len(pm2._steps), 2) pm = pm2._merge(pm1) self.assertEqual(pm._steps["a"]._alias, "R1") self.assertEqual(pm1._steps["a"]._alias, "R1") self.assertEqual(pm2._steps["a"]._alias, "R2") self.assertEqual(len(pm1._steps), 1) self.assertEqual(len(pm2._steps), 2)
def test_alias_None(self): pm = Pmod() self.assertIsNone(pm.alias('a')) pm = Pmod(_alias='a') self.assertIsNone(pm.alias('a')) pm = Pmod(_steps={'a': None}) self.assertIsNone(pm.alias('a'))
def test_descend_None(self): pm = Pmod() self.assert_descend_stops(pm.descend('a')) pm = Pmod(_alias='a') self.assert_descend_stops(pm.descend('a')) pm = Pmod(_steps={'a': None}) self.assert_descend_stops(pm.descend('a'))
def test_descend(self): pm = Pmod( _steps={"a": Pmod(_alias="A", _steps={1: 11})}, _regxs=[ (r"a\w*", Pmod(_alias="AWord", _steps={2: 22})), (r"a\d*", Pmod(_alias="ADigit", _steps={3: 33})), ], ) # All children and regexps match. self.assertDictEqual(pm.descend("a")[0]._steps, {1: 11, 2: 22, 3: 33}) # Only 'a\w*' matches. self.assertDictEqual(pm.descend("aa")[0]._steps, {2: 22}) # Both regexps matches. self.assertDictEqual(pm.descend("a1")[0]._steps, {2: 22, 3: 33})
def test_merge_name(self): pm1 = Pmod(_alias='pm1') pm2 = Pmod(_alias='pm2') pm = pm1._merge(pm2) self.assertEqual(pm._alias, 'pm2') self.assertEqual(pm1._alias, 'pm1') self.assertEqual(pm2._alias, 'pm2') pm = pm2._merge(pm1) self.assertEqual(pm._alias, 'pm1') self.assertEqual(pm1._alias, 'pm1') self.assertEqual(pm2._alias, 'pm2') pm1 = Pmod() pm2 = Pmod(_alias='pm2') pm = pm1._merge(pm2) self.assertEqual(pm._alias, 'pm2') self.assertEqual(pm1._alias, None) self.assertEqual(pm2._alias, 'pm2') pm = pm2._merge(pm1) self.assertEqual(pm._alias, 'pm2') self.assertEqual(pm1._alias, None) self.assertEqual(pm2._alias, 'pm2')
def test_merge_steps(self): pm1 = Pmod(_alias='pm1', _steps={'a': Pmod(_alias='A'), 'c': Pmod(_alias='C')}) pm2 = Pmod(_alias='pm2', _steps={'b': Pmod(_alias='B'), 'a': Pmod(_alias='AA'), 'd': Pmod(_alias='DD'), }) pm = pm1._merge(pm2) self.assertEqual(sorted(pm._steps.keys()), list('abcd')) pm = pm2._merge(pm1) self.assertEqual(sorted(pm._steps.keys()), list('abcd')) self.assertEqual(len(pm1._steps), 2) self.assertEqual(len(pm2._steps), 3) pm1 = Pmod(_steps={'a': Pmod(_alias='A'), 'c': Pmod(_alias='C')}) pm2 = Pmod(_alias='pm2', _steps={'b': Pmod(_alias='B'), 'a': Pmod(_alias='AA'), 'd': Pmod(_alias='DD'), }) pm = pm1._merge(pm2) self.assertEqual(sorted(pm._steps.keys()), list('abcd')) self.assertEqual(pm._steps['a']._alias, 'AA') self.assertEqual(len(pm1._steps), 2) self.assertEqual(len(pm2._steps), 3) self.assertEqual(pm._regxs, {}) self.assertEqual(pm1._regxs, {}) self.assertEqual(pm2._regxs, {}) pm = pm2._merge(pm1) self.assertEqual(sorted(pm._steps.keys()), list('abcd')) self.assertEqual(pm._steps['a']._alias, 'A') self.assertEqual(len(pm1._steps), 2) self.assertEqual(len(pm2._steps), 3) self.assertEqual(pm._regxs, {}) self.assertEqual(pm1._regxs, {}) self.assertEqual(pm2._regxs, {})
def test_merge_steps(self): pm1 = Pmod(_alias="pm1", _steps={"a": Pmod(_alias="A"), "c": Pmod(_alias="C")}) pm2 = Pmod( _alias="pm2", _steps={ "b": Pmod(_alias="B"), "a": Pmod(_alias="AA"), "d": Pmod(_alias="DD"), }, ) pm = pm1._merge(pm2) self.assertEqual(sorted(pm._steps.keys()), list("abcd")) pm = pm2._merge(pm1) self.assertEqual(sorted(pm._steps.keys()), list("abcd")) self.assertEqual(len(pm1._steps), 2) self.assertEqual(len(pm2._steps), 3) pm1 = Pmod(_steps={"a": Pmod(_alias="A"), "c": Pmod(_alias="C")}) pm2 = Pmod( _alias="pm2", _steps={ "b": Pmod(_alias="B"), "a": Pmod(_alias="AA"), "d": Pmod(_alias="DD"), }, ) pm = pm1._merge(pm2) self.assertEqual(sorted(pm._steps.keys()), list("abcd")) self.assertEqual(pm._steps["a"]._alias, "AA") self.assertEqual(len(pm1._steps), 2) self.assertEqual(len(pm2._steps), 3) self.assertEqual(pm._regxs, {}) self.assertEqual(pm1._regxs, {}) self.assertEqual(pm2._regxs, {}) pm = pm2._merge(pm1) self.assertEqual(sorted(pm._steps.keys()), list("abcd")) self.assertEqual(pm._steps["a"]._alias, "A") self.assertEqual(len(pm1._steps), 2) self.assertEqual(len(pm2._steps), 3) self.assertEqual(pm._regxs, {}) self.assertEqual(pm1._regxs, {}) self.assertEqual(pm2._regxs, {})
def test_map_path_rootUnmapped_emptyPath(self): pm = Pmod() self.assertEqual(pm.map_path(""), "")
def test_descend_group_zero_(self): pm = Pmod(_regxs=[('abc', Pmod(_alias='A\\g<0>'))]) self.assertEqual(pm.descend('abc')[1], 'Aabc') pm = Pmod(_regxs=[('a(.*)', Pmod(_alias='A\\g<0>'))]) self.assertEqual(pm.descend('abc')[1], 'Aabc')
def test_map_path_rootUnmapped_slashPath(self): pm = Pmod() self.assertEqual(pm.map_path("/"), "")
def test_descend_group_zero_(self): pm = Pmod(_regxs=[("abc", Pmod(_alias="A\\g<0>"))]) self.assertEqual(pm.descend("abc")[1], "Aabc") pm = Pmod(_regxs=[("a(.*)", Pmod(_alias="A\\g<0>"))]) self.assertEqual(pm.descend("abc")[1], "Aabc")
def test_alias_group_zero_(self): pm = Pmod(_regxs=[("abc", Pmod(_alias="A\\g<0>"))]) self.assertEqual(pm.alias("abc"), "Aabc") pm = Pmod(_regxs=[("a(.*)", Pmod(_alias="A\\g<0>"))]) self.assertEqual(pm.alias("abc"), "Aabc")
def _build_pmod_manually(self): return Pmod( _steps={"a": Pmod(_alias="A")}, _regxs=[(r"a(\w*)", Pmod("AWord")), ("a(\d*)", Pmod(_alias="A_\\1"))], )
def test_alias_group_zero_(self): pm = Pmod(_regxs=[('abc', Pmod(_alias='A\\g<0>'))]) self.assertEqual(pm.alias('abc'), 'Aabc') pm = Pmod(_regxs=[('a(.*)', Pmod(_alias='A\\g<0>'))]) self.assertEqual(pm.alias('abc'), 'Aabc')
def test_map_path_rootUnmapped_slashPath(self): pm = Pmod() self.assertEqual(pm.map_path('/'), '')
def test_map_path_rootUnmapped_emptyPath(self): pm = Pmod() self.assertEqual(pm.map_path(''), '')
def test_merge_all(self): def merge_all(pmods): return ft.reduce(Pmod._merge, pmods) pm1 = Pmod(_steps={"a": Pmod(_alias="A")}) pm2 = Pmod( _alias="pm2", _regxs=[("b", Pmod(_alias="BB")), ("a", Pmod(_alias="AA"))] ) pm3 = Pmod( _alias="PM3", _steps={"c": Pmod(_alias="CCC"), "b": Pmod(_alias="BBB")}, _regxs=[ ("b", Pmod(_alias="AAA")), ("a", Pmod(_alias="BBB")), ("c", Pmod(_alias="CCC")), ], ) pm = merge_all([pm1, pm2, pm3]) self.assertSetEqual(set(pm._steps.keys()), set(list("abc"))) self.assertSetEqual(set(pm1._steps.keys()), set(list("a"))) self.assertEqual(pm2._steps, {}) self.assertSetEqual(set(pm3._steps.keys()), set(list("bc"))) self.assertEqual(pmod2regexstrs(pm), list("bac")) self.assertEqual(pm1._regxs, {}) self.assertEqual(pmod2regexstrs(pm2), list("ba")) self.assertEqual(pmod2regexstrs(pm3), list("bac"))
def test_merge_regxs(self): pm1 = Pmod( _alias="pm1", _regxs=[ ("e", Pmod(_alias="E")), ("a", Pmod(_alias="A")), ("c", Pmod(_alias="C")), ], ) pm2 = Pmod( _alias="pm2", _regxs=[ ("b", Pmod(_alias="B")), ("a", Pmod(_alias="AA")), ("d", Pmod(_alias="DD")), ], ) pm = pm1._merge(pm2) self.assertSequenceEqual(pmod2regexstrs(pm), list("ecbad")) self.assertEqual(pm._regxs[re.compile("a")]._alias, "AA") self.assertSequenceEqual(pmod2regexstrs(pm1), list("eac")) self.assertSequenceEqual(pmod2regexstrs(pm2), list("bad")) self.assertEqual(pm._steps, {}) self.assertEqual(pm1._steps, {}) self.assertEqual(pm2._steps, {}) pm = pm2._merge(pm1) self.assertSequenceEqual(pmod2regexstrs(pm), list("bdeac")) self.assertEqual(pm._regxs[re.compile("a")]._alias, "A") self.assertSequenceEqual(pmod2regexstrs(pm1), list("eac")) self.assertSequenceEqual(pmod2regexstrs(pm2), list("bad")) self.assertEqual(pm._steps, {}) self.assertEqual(pm1._steps, {}) self.assertEqual(pm2._steps, {})
def test_merge_name(self): pm1 = Pmod(_alias="pm1") pm2 = Pmod(_alias="pm2") pm = pm1._merge(pm2) self.assertEqual(pm._alias, "pm2") self.assertEqual(pm1._alias, "pm1") self.assertEqual(pm2._alias, "pm2") pm = pm2._merge(pm1) self.assertEqual(pm._alias, "pm1") self.assertEqual(pm1._alias, "pm1") self.assertEqual(pm2._alias, "pm2") pm1 = Pmod() pm2 = Pmod(_alias="pm2") pm = pm1._merge(pm2) self.assertEqual(pm._alias, "pm2") self.assertEqual(pm1._alias, None) self.assertEqual(pm2._alias, "pm2") pm = pm2._merge(pm1) self.assertEqual(pm._alias, "pm2") self.assertEqual(pm1._alias, None) self.assertEqual(pm2._alias, "pm2")