def test_pattern_list_matcher(self): backRef = NdnRegexBackrefManager() cm = NdnRegexPatternListMatcher("<a>[<a><b>]", backRef) res = cm.match(Name("/a/b/c"), 0, 2) self.assertEqual(True, res) self.assertEqual(2, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) backRef = NdnRegexBackrefManager() cm = NdnRegexPatternListMatcher("<>*<a>", backRef) res = cm.match(Name("/a/b/c"), 0, 1) self.assertEqual(True, res) self.assertEqual(1, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) backRef = NdnRegexBackrefManager() cm = NdnRegexPatternListMatcher("<>*<a>", backRef) res = cm.match(Name("/a/b/c"), 0, 2) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) backRef = NdnRegexBackrefManager() cm = NdnRegexPatternListMatcher("<>*<a><>*", backRef) res = cm.match(Name("/a/b/c"), 0, 3) self.assertEqual(True, res) self.assertEqual(3, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) self.assertEqual("c", cm.getMatchResult()[2].toEscapedString())
def test_component_matcher(self): backRef = NdnRegexBackrefManager() cm = NdnRegexComponentMatcher("a", backRef) res = cm.match(Name("/a/b/"), 0, 1) self.assertEqual(True, res) self.assertEqual(1, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) backRef = NdnRegexBackrefManager() cm = NdnRegexComponentMatcher("a", backRef) res = cm.match(Name("/a/b/"), 1, 1) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) backRef = NdnRegexBackrefManager() cm = NdnRegexComponentMatcher("(c+)\\.(cd)", backRef) res = cm.match(Name("/ccc.cd/b/"), 0, 1) self.assertEqual(True, res) self.assertEqual(1, len(cm.getMatchResult())) self.assertEqual("ccc.cd", cm.getMatchResult()[0].toEscapedString()) self.assertEqual(2, backRef.size()) self.assertEqual("ccc", backRef.getBackref(0).getMatchResult()[0].toEscapedString()) self.assertEqual("cd", backRef.getBackref(1).getMatchResult()[0].toEscapedString())
def test_backref_matcher_advanced(self): backRef = NdnRegexBackrefManager() cm = NdnRegexRepeatMatcher("([<a><b>])+", backRef, 10) res = cm.match(Name("/a/b/c"), 0, 2) self.assertEqual(True, res) self.assertEqual(2, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) self.assertEqual(1, backRef.size()) self.assertEqual("b", backRef.getBackref(0).getMatchResult()[0].toEscapedString())
def __init__(self, expr, expand=""): super(NdnRegexTopMatcher, self).__init__(expr, NdnRegexMatcherBase.NdnRegexExprType.TOP) self._primaryMatcher = None self._secondaryMatcher = None self._primaryBackrefManager = NdnRegexBackrefManager() self._secondaryBackrefManager = NdnRegexBackrefManager() self._isSecondaryUsed = False self._expand = expand self._compile()
def test_component_set_matcher(self): backRef = NdnRegexBackrefManager() cm = NdnRegexComponentSetMatcher("<a>", backRef) res = cm.match(Name("/a/b/"), 0, 1) self.assertEqual(True, res) self.assertEqual(1, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) res = cm.match(Name("/a/b/"), 1, 1) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) res = cm.match(Name("/a/b/"), 0, 2) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) backRef = NdnRegexBackrefManager() cm = NdnRegexComponentSetMatcher("[<a><b><c>]", backRef) res = cm.match(Name("/a/b/d"), 1, 1) self.assertEqual(True, res) self.assertEqual(1, len(cm.getMatchResult())) self.assertEqual("b", cm.getMatchResult()[0].toEscapedString()) res = cm.match(Name("/a/b/d"), 2, 1) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) backRef = NdnRegexBackrefManager() cm = NdnRegexComponentSetMatcher("[^<a><b><c>]", backRef) res = cm.match(Name("/b/d"), 1, 1) self.assertEqual(True, res) self.assertEqual(1, len(cm.getMatchResult())) self.assertEqual("d", cm.getMatchResult()[0].toEscapedString()) backRef = NdnRegexBackrefManager() try: NdnRegexComponentSetMatcher("[<a]", backRef) self.fail("Did not throw the expected exception") except NdnRegexMatcherBase.Error: pass else: self.fail("Did not throw the expected exception")
def __init__(self, expr, type, backrefManager=None): # Array of NdnRegexMatcherBase self._matchers = [] # Array of Name.Component self._matchResult = [] self._expr = expr self._type = type if backrefManager == None: backrefManager = NdnRegexBackrefManager() self._backrefManager = backrefManager
def test_backref_matcher_advanced2(self): backRef = NdnRegexBackrefManager() cm = NdnRegexPatternListMatcher("(<a>(<b>))<c>", backRef) res = cm.match(Name("/a/b/c"), 0, 3) self.assertEqual(True, res) self.assertEqual(3, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) self.assertEqual("c", cm.getMatchResult()[2].toEscapedString()) self.assertEqual(2, backRef.size()) self.assertEqual("a", backRef.getBackref(0).getMatchResult()[0].toEscapedString()) self.assertEqual("b", backRef.getBackref(0).getMatchResult()[1].toEscapedString()) self.assertEqual("b", backRef.getBackref(1).getMatchResult()[0].toEscapedString())
def test_backref_matcher(self): backRef = NdnRegexBackrefManager() cm = NdnRegexBackrefMatcher("(<a><b>)", backRef) backRef.pushRef(cm) cm.lateCompile() res = cm.match(Name("/a/b/c"), 0, 2) self.assertEqual(True, res) self.assertEqual(2, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) self.assertEqual(1, backRef.size()) backRef = NdnRegexBackrefManager() cm = NdnRegexBackrefMatcher("(<a>(<b>))", backRef) backRef.pushRef(cm) cm.lateCompile() res = cm.match(Name("/a/b/c"), 0, 2) self.assertEqual(True, res) self.assertEqual(2, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) self.assertEqual(2, backRef.size()) self.assertEqual("a", backRef.getBackref(0).getMatchResult()[0].toEscapedString()) self.assertEqual("b", backRef.getBackref(0).getMatchResult()[1].toEscapedString()) self.assertEqual("b", backRef.getBackref(1).getMatchResult()[0].toEscapedString())
def test_repeat_matcher(self): backRef = NdnRegexBackrefManager() cm = NdnRegexRepeatMatcher("[<a><b>]*", backRef, 8) res = cm.match(Name("/a/b/c"), 0, 0) self.assertEqual(True, res) self.assertEqual(0, len(cm.getMatchResult())) res = cm.match(Name("/a/b/c"), 0, 2) self.assertEqual(True, res) self.assertEqual(2, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) backRef = NdnRegexBackrefManager() cm = NdnRegexRepeatMatcher("[<a><b>]+", backRef, 8) res = cm.match(Name("/a/b/c"), 0, 0) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) res = cm.match(Name("/a/b/c"), 0, 2) self.assertEqual(True, res) self.assertEqual(2, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) backRef = NdnRegexBackrefManager() cm = NdnRegexRepeatMatcher("<.*>*", backRef, 4) res = cm.match(Name("/a/b/c/d/e/f/"), 0, 6) self.assertEqual(True, res) self.assertEqual(6, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) self.assertEqual("c", cm.getMatchResult()[2].toEscapedString()) self.assertEqual("d", cm.getMatchResult()[3].toEscapedString()) self.assertEqual("e", cm.getMatchResult()[4].toEscapedString()) self.assertEqual("f", cm.getMatchResult()[5].toEscapedString()) backRef = NdnRegexBackrefManager() cm = NdnRegexRepeatMatcher("<>*", backRef, 2) res = cm.match(Name("/a/b/c/d/e/f/"), 0, 6) self.assertEqual(True, res) self.assertEqual(6, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) self.assertEqual("c", cm.getMatchResult()[2].toEscapedString()) self.assertEqual("d", cm.getMatchResult()[3].toEscapedString()) self.assertEqual("e", cm.getMatchResult()[4].toEscapedString()) self.assertEqual("f", cm.getMatchResult()[5].toEscapedString()) backRef = NdnRegexBackrefManager() cm = NdnRegexRepeatMatcher("<a>?", backRef, 3) res = cm.match(Name("/a/b/c"), 0, 0) self.assertEqual(True, res) self.assertEqual(0, len(cm.getMatchResult())) cm = NdnRegexRepeatMatcher("<a>?", backRef, 3) res = cm.match(Name("/a/b/c"), 0, 1) self.assertEqual(True, res) self.assertEqual(1, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) cm = NdnRegexRepeatMatcher("<a>?", backRef, 3) res = cm.match(Name("/a/b/c"), 0, 2) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) backRef = NdnRegexBackrefManager() cm = NdnRegexRepeatMatcher("[<a><b>]{3}", backRef, 8) res = cm.match(Name("/a/b/a/d/"), 0, 2) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) res = cm.match(Name("/a/b/a/d/"), 0, 3) self.assertEqual(True, res) self.assertEqual(3, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) self.assertEqual("a", cm.getMatchResult()[2].toEscapedString()) res = cm.match(Name("/a/b/a/d/"), 0, 4) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) backRef = NdnRegexBackrefManager() cm = NdnRegexRepeatMatcher("[<a><b>]{2,3}", backRef, 8) res = cm.match(Name("/a/b/a/d/e/"), 0, 2) self.assertEqual(True, res) self.assertEqual(2, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) res = cm.match(Name("/a/b/a/d/e/"), 0, 3) self.assertEqual(True, res) self.assertEqual(3, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) self.assertEqual("a", cm.getMatchResult()[2].toEscapedString()) res = cm.match(Name("/a/b/a/b/e/"), 0, 4) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) res = cm.match(Name("/a/b/a/d/e/"), 0, 1) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) backRef = NdnRegexBackrefManager() cm = NdnRegexRepeatMatcher("[<a><b>]{2,}", backRef, 8) res = cm.match(Name("/a/b/a/d/e/"), 0, 2) self.assertEqual(True, res) self.assertEqual(2, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) res = cm.match(Name("/a/b/a/b/e/"), 0, 4) self.assertEqual(True, res) self.assertEqual(4, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) self.assertEqual("a", cm.getMatchResult()[2].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[3].toEscapedString()) res = cm.match(Name("/a/b/a/d/e/"), 0, 1) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) backRef = NdnRegexBackrefManager() cm = NdnRegexRepeatMatcher("[<a><b>]{,2}", backRef, 8) res = cm.match(Name("/a/b/a/b/e/"), 0, 3) self.assertEqual(False, res) self.assertEqual(0, len(cm.getMatchResult())) res = cm.match(Name("/a/b/a/b/e/"), 0, 2) self.assertEqual(True, res) self.assertEqual(2, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) self.assertEqual("b", cm.getMatchResult()[1].toEscapedString()) res = cm.match(Name("/a/b/a/d/e/"), 0, 1) self.assertEqual(True, res) self.assertEqual(1, len(cm.getMatchResult())) self.assertEqual("a", cm.getMatchResult()[0].toEscapedString()) res = cm.match(Name("/a/b/a/d/e/"), 0, 0) self.assertEqual(True, res) self.assertEqual(0, len(cm.getMatchResult()))