def test_alt_set(self): self.assertEqual(parse_expansion_string("a|b"), AlternativeSet("a", "b")) self.assertEqual(parse_expansion_string("a|b|c"), AlternativeSet("a", "b", "c")) # Also test invalid alt. sets self.assertRaises(ParseException, parse_expansion_string, "a|b|")
def test_sequence(self): # A sequence with optional and required literals. self.assertEqual(parse_expansion_string("[please] work"), Sequence(OptionalGrouping("please"), "work")) # A sequence with two alternative sets. self.assertEqual( parse_expansion_string("(a|b|c) (one|two|three)"), Sequence(AlternativeSet("a", "b", "c"), AlternativeSet("one", "two", "three")))
def test_kleene(self): # Test one literal self.assertEqual(parse_expansion_string("test*"), KleeneStar("test")) # Test a sequence of two literals, one repeating. self.assertEqual(parse_expansion_string("(please)* work"), Sequence(KleeneStar("please"), "work")) # Test a sequence of three literals, one repeating. self.assertEqual(parse_expansion_string("a* b [c]"), Sequence(KleeneStar("a"), "b", OptionalGrouping("c")))
def test_repeat(self): # Test one literal self.assertEqual(parse_expansion_string("test+"), Repeat("test")) # Test a sequence of two literals, one repeating. self.assertEqual(parse_expansion_string("(please)+ work"), Sequence(Repeat("please"), "work")) # Test a sequence of three literals, one repeating. self.assertEqual(parse_expansion_string("a+ b [c]"), Sequence(Repeat("a"), "b", OptionalGrouping("c")))
def test_alternative_weights(self): # Test with three weighted alternatives are parsed correctly. expected = AlternativeSet("a", "b", "c") expected.weights = {"a": 10, "b": 5.5, "c": 20} self.assertEqual(parse_expansion_string("/10/ a | /5.5/ b | /20/ c"), expected) # Test that groupings, references and sequences can also be weighted. alt1, alt2, alt3, alt4 = (OptionalGrouping("a"), "b", "c d", NamedRuleRef("test")) expected = AlternativeSet(alt1, alt2, alt3, alt4) expected.weights = {alt1: 10, alt2: 5.5, alt3: 20, alt4: 2.5} self.assertEqual(parse_expansion_string( "/10/ [a] | /5.5/ (b) | /20/ (c d) | /2.5/ <test>" ), expected)
def test_repeat(self): # Test one literal self.assertEqual(parse_expansion_string("test+"), Repeat("test")) # Test a sequence of two literals, one repeating. self.assertEqual(parse_expansion_string("(please)+ work"), Sequence(Repeat("please"), "work")) # Test a sequence of three literals, one repeating. self.assertEqual(parse_expansion_string("a+ b [c]"), Sequence(Repeat("a"), "b", OptionalGrouping("c"))) # Sequences within a repeated alternative set. expected = Repeat(AlternativeSet(Sequence("up", NamedRuleRef("n")), Sequence("left", NamedRuleRef("n")))) self.assertEqual(expected, parse_expansion_string("(up <n>|left <n>)+"))
def test_multiple_tags(self): # Test that required groupings are added if using the multiple tag syntax. expected = RequiredGrouping(RequiredGrouping("text")) e = parse_expansion_string("text {tag1} {tag2} {tag3}") self.assertEqual(e, expected) self.assertEqual(e.tag, "tag3") self.assertEqual(e.children[0].tag, "tag2") self.assertEqual(e.children[0].children[0].tag, "tag1")
def test_alt_set_weights_and_tags(self): a, b = map(Literal, "ab") expected = AlternativeSet(a, b) expected.weights = {a: 10, b: 5.5} expected.children[0].tag = "1" expected.children[1].tag = "2" self.assertEqual(parse_expansion_string("/10/ a {1} | /5.5/ b {2}"), expected)
def test_unary_alternatives(self): for cls, op in [(Repeat, "+"), (KleeneStar, "*")]: # Test unweighted. expected = AlternativeSet( cls("a"), cls(RequiredGrouping("b c")), cls(RequiredGrouping(Sequence("d", OptionalGrouping("e")))) ) exp_str = "a{op} | (b c){op} | (d [e]){op}".format(op=op) self.assertEqual(parse_expansion_string(exp_str), expected) # Test weighted. alt1, alt2 = cls("a"), cls(RequiredGrouping("b c")) alt3 = cls(RequiredGrouping(Sequence("d", OptionalGrouping("e")))) expected = AlternativeSet(alt1, alt2, alt3) expected.weights = {alt1: 10, alt2: 5, alt3: 2} exp_str = "/10/ a{op} | /5/ (b c){op} | /2/ (d [e]){op}".format(op=op) self.assertEqual(parse_expansion_string(exp_str), expected)
def test_sequence(self): # A sequence with optional and required literals. self.assertEqual(parse_expansion_string("[please] work"), Sequence(OptionalGrouping("please"), "work")) # A sequence with two alternative sets. self.assertEqual(parse_expansion_string("(a|b|c) (one|two|three)"), Sequence(AlternativeSet("a", "b", "c"), AlternativeSet("one", "two", "three"))) # A sequence with tags. expected = Sequence("this", RequiredGrouping("is"), "a", RequiredGrouping("test")) result = parse_expansion_string("this (is){tag1} a (test){tag2}") self.assertEqual(result, expected) self.assertEqual(result.children[1].tag, "tag1") self.assertEqual(result.children[3].tag, "tag2") # Sequences within an alternative set. expected = AlternativeSet(Sequence("up", NamedRuleRef("n")), Sequence("left", NamedRuleRef("n"))) self.assertEqual(expected, parse_expansion_string("up <n>|left <n>"))
def test_tags(self): # Test with one literal. e = parse_expansion_string("a {tag}") self.assertEqual(e, Literal("a")) self.assertEqual(e.tag, "tag") # Test with two literals. e = parse_expansion_string("a {1} b {2}") self.assertEqual(e, Sequence("a", "b")) self.assertEqual(e.children[0].tag, "1") self.assertEqual(e.children[1].tag, "2") # Test with an alternative set. e = parse_expansion_string("a {1} | b {2} | c {3}") self.assertEqual(e, AlternativeSet("a", "b", "c")) self.assertEqual(e.children[0].tag, "1") self.assertEqual(e.children[1].tag, "2") self.assertEqual(e.children[2].tag, "3") # Test with a required grouping. e = parse_expansion_string("(a b) {tag}") self.assertEqual(e, RequiredGrouping(Literal("a b"))) self.assertEqual(e.tag, "tag") # Test with an optional. e = parse_expansion_string("[a] {tag}") self.assertEqual(e, OptionalGrouping(Literal("a"))) self.assertEqual(e.tag, "tag") # Test with a sequence. e = parse_expansion_string("(a [b]) {tag}") self.assertEqual( e, RequiredGrouping(Sequence("a", OptionalGrouping("b")))) self.assertEqual(e.tag, "tag") # Test with references. e = parse_expansion_string("<action> {tag}") self.assertEqual(e, NamedRuleRef("action")) self.assertEqual(e.tag, "tag")
def test_optional(self): self.assertEqual(parse_expansion_string("[a]"), OptionalGrouping("a")) self.assertEqual(parse_expansion_string("([a])"), OptionalGrouping("a"))
def test_alt_set_within_sequence(self): self.assertEqual( parse_expansion_string("i (go | run) to school"), Sequence("i", AlternativeSet("go", "run"), "to school"))
def test_required_grouping(self): self.assertEqual(parse_expansion_string("(a)"), RequiredGrouping(Literal("a")))
def test_literal_ascii(self): self.assertEqual(parse_expansion_string("command"), Literal("command")) self.assertEqual(parse_expansion_string("a literal"), Literal("a literal"))
def test_literal_unicode(self): self.assertEqual(parse_expansion_string(u"комманде"), Literal(u"комманде"))
def test_rule_ref(self): self.assertEqual(parse_expansion_string("<rule>"), NamedRuleRef("rule"))
def test_required_grouping(self): # Test that redundant parenthesises are removed. self.assertEqual(parse_expansion_string("(a)"), Literal("a"))
def test_null_void_refs(self): self.assertEqual(parse_expansion_string("<NULL>"), NullRef()) self.assertEqual(parse_expansion_string("<VOID>"), VoidRef())