Exemplo n.º 1
0
    def test_dtab_read_ignores_comment_line(self):
        withComments = Dtab.read("""
# a comment
      /#foo => /biz  # another comment
             | ( /bliz & # yet another comment
                 /bluth ) # duh bluths
             ; #finalmente
      #/ignore=>/me;
    """)
        dtab = Dtab([
            Dentry(
                Path.Utf8("#foo"),
                NameTree.Alt(
                    NameTree.Leaf(Path.Utf8("biz")),
                    NameTree.Union(
                        NameTree.Weighted(
                            NameTree.Weighted.defaultWeight,
                            NameTree.Leaf(Path.Utf8("bliz")),
                        ),
                        NameTree.Weighted(
                            NameTree.Weighted.defaultWeight,
                            NameTree.Leaf(Path.Utf8("bluth")),
                        ),
                    ),
                ),
            )
        ])

        s = "Dtab(Label(#foo)=>NameTree.Leaf(Path(/biz)),"
        s += "NameTree.Union(NameTree.Weighted(1.0,NameTree.Leaf(Path(/bliz))),"
        s += "NameTree.Weighted(1.0,NameTree.Leaf(Path(/bluth)))))"

        self.assertEqual(repr(dtab), s)

        self.assertEqual(withComments, dtab)
Exemplo n.º 2
0
 def parse_weighted(self):
     self.eat_whitespace()
     weight = None
     if not self.is_number_char(self.peek):
         weight = NameTree.Weighted.defaultWeight
     else:
         weight = self.parse_number()
         self.eat_whitespace()
         self.eat('*')
         self.eat_whitespace()
     return NameTree.Weighted(weight, self.parse_simple())
Exemplo n.º 3
0
    def test_parseNameTree(self):
        defaultWeight = NameTree.Weighted.defaultWeight

        self.assertTrue(
            NameTreeParsers.parseNameTree("! | ~ | $") == NameTree.Alt(
                NameTree.Fail, NameTree.Neg, NameTree.Empty))

        self.assertTrue(
            NameTreeParsers.parseNameTree("/foo/bar") == NameTree.Leaf(
                Path.Utf8("foo", "bar")))

        self.assertTrue(
            NameTreeParsers.parseNameTree("  /foo & /bar  ") == NameTree.Union(
                NameTree.Weighted(defaultWeight, NameTree.Leaf(Path.Utf8(
                    "foo"))),
                NameTree.Weighted(defaultWeight, NameTree.Leaf(Path.Utf8(
                    "bar")))))

        self.assertTrue(
            NameTreeParsers.parseNameTree("  /foo | /bar  ") == NameTree.Alt(
                NameTree.Leaf(Path.Utf8("foo")), NameTree.Leaf(Path.Utf8(
                    "bar"))))

        self.assertTrue(
            NameTreeParsers.parseNameTree("/foo & /bar | /bar & /baz") ==
            NameTree.Alt(
                NameTree.Union(
                    NameTree.Weighted(defaultWeight,
                                      NameTree.Leaf(Path.Utf8("foo"))),
                    NameTree.Weighted(defaultWeight,
                                      NameTree.Leaf(Path.Utf8("bar")))),
                NameTree.Union(
                    NameTree.Weighted(defaultWeight,
                                      NameTree.Leaf(Path.Utf8("bar"))),
                    NameTree.Weighted(defaultWeight,
                                      NameTree.Leaf(Path.Utf8("baz"))))))

        self.assertTrue(
            NameTreeParsers.parseNameTree(
                "1 * /foo & 2 * /bar | .5 * /bar & .5 * /baz") ==
            NameTree.Alt(
                NameTree.Union(
                    NameTree.Weighted(1, NameTree.Leaf(Path.Utf8("foo"))),
                    NameTree.Weighted(2, NameTree.Leaf(Path.Utf8("bar")))),
                NameTree.Union(
                    NameTree.Weighted(0.5, NameTree.Leaf(Path.Utf8("bar"))),
                    NameTree.Weighted(0.5, NameTree.Leaf(Path.Utf8("baz"))))))

        with self.assertRaises(IllegalArgumentException):
            NameTreeParsers.parseNameTree("")

        with self.assertRaises(IllegalArgumentException):
            NameTreeParsers.parseNameTree("#")

        with self.assertRaises(IllegalArgumentException):
            NameTreeParsers.parseNameTree("/foo &")

        with self.assertRaises(IllegalArgumentException):
            NameTreeParsers.parseNameTree("/foo & 0.1.2 * /bar")

        with self.assertRaises(IllegalArgumentException):
            NameTreeParsers.parseNameTree("/foo & . * /bar")