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)
def parse_tree1(self): trees = [] while True: trees.append(self.parse_weighted()) self.eat_whitespace() if not self.maybe_eat('&'): break if len(trees) > 1: return NameTree.Union(*trees) return trees[0].tree
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")