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")
def read(cls, s): from dtab.parser import NameTreeParsers return NameTreeParsers.parseNameTree(s)
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")
def read(s): from dtab.parser import NameTreeParsers return NameTreeParsers.parseNameTree(s)