示例#1
0
    def testTreeBuilding(self):
        ''' Test that the tree builder constructs trees correctly when
        parsed. '''
        l = lexer.Lexer("(('foo' : 0.1, 'bar' : 1.0) : 2, baz)")
        handler = _TreeBuilder()
        p = parser._Parser(l, handler)
        p.parse()
        t = handler.get_result()

        self.assertEqual(len(t.get_edges()), 2)
        (t1, b1, l1), (t2, b2, l2) = t.get_edges()

        self.assertEqual(len(t1.get_edges()), 2)
        self.assertEqual(l1, 2.0)
        self.assertEqual(t2.__class__, Leaf)
        self.assertEqual(l2, None)
        self.assertEqual(t.leaves_identifiers, ['foo', 'bar', 'baz'])
示例#2
0
文件: treetest.py 项目: ptdtan/Ragout
    def testTreeBuilding(self):
        ''' Test that the tree builder constructs trees correctly when
        parsed. '''
        l = lexer.Lexer("(('foo' : 0.1, 'bar' : 1.0) : 2, baz)")
        handler = _TreeBuilder()
        p = parser._Parser(l,handler)
        p.parse()
        t = handler.get_result()

        self.assertEqual(len(t.get_edges()),2)
        (t1,b1,l1), (t2,b2,l2) = t.get_edges()

        self.assertEqual(len(t1.get_edges()),2)
        self.assertEqual(l1, 2.0)
        self.assertEqual(t2.__class__, Leaf)
        self.assertEqual(l2, None)
        self.assertEqual(t.leaves_identifiers, ['foo','bar','baz'])
示例#3
0
    id = lexer.read_token(tokens.ID)
    if id.get_name() != 'bar':
        raise "Unexpected name!"
    lexer.read_token(tokens.Colon)
    n = lexer.read_token(tokens.Number)
    if n.get_number() != 0.00:
        raise "Unexpected number!"
    lexer.read_token(tokens.Comma)
    lexer.read_token(tokens.SemiColon)
    print "Done"

    print "Testing parse...",
    import parser
    import tree
    lexer = Lexer("(('foo' : 0.1, 'bar' : 1.0) : 2, baz)")
    handler = tree._TreeBuilder()
    p = parser._Parser(lexer, handler)
    p.parse()
    t = handler.get_result()

    if len(t.get_edges()) != 2:
        raise "Unexpected number of edges"
    [(t1, b1, l1), (t2, b2, l2)] = t.get_edges()
    if len(t1.get_edges()) != 2:
        raise "Unexpected number of edges"
    if l1 != 2.0:
        raise "Unexpected edge length"

    if t2.__class__ != tree.Leaf:
        raise "Leaf expected"
    if l2 != None:
    if id.get_name() != "bar":
        raise "Unexpected name!"
    lexer.read_token(tokens.Colon)
    n = lexer.read_token(tokens.Number)
    if n.get_number() != 0.00:
        raise "Unexpected number!"
    lexer.read_token(tokens.Comma)
    lexer.read_token(tokens.SemiColon)
    print "Done"

    print "Testing parse...",
    import parser
    import tree

    lexer = Lexer("(('foo' : 0.1, 'bar' : 1.0) : 2, baz)")
    handler = tree._TreeBuilder()
    p = parser._Parser(lexer, handler)
    p.parse()
    t = handler.get_result()

    if len(t.get_edges()) != 2:
        raise "Unexpected number of edges"
    [(t1, b1, l1), (t2, b2, l2)] = t.get_edges()
    if len(t1.get_edges()) != 2:
        raise "Unexpected number of edges"
    if l1 != 2.0:
        raise "Unexpected edge length"

    if t2.__class__ != tree.Leaf:
        raise "Leaf expected"
    if l2 != None: