def test_data_def_1(): #IGNORE:C01111
    msg = 'Parse data definition for single attribute.'
    #py.test.skip(msg)
    print msg
    
    from freeode.simlparser import Parser   
    from freeode.ast import RoleConstant
    
    parser = Parser()
    #For debugging: keep Pyparsing's  original parse results.
    # Exit immediately from all action functions
    #Parser.noTreeModification = 1

    test_prog = (
'''
data a: Float const
''' )
    print 'statement: --------------------------'
    print test_prog

    ast = parser.parseModuleStr(test_prog)
    print 'AST: --------------------------------'
    print ast 
    
    data_stmt = ast.statements[0]
    check_single_data_statement(data_stmt, 'a', 'Float', RoleConstant)
def test_class_def_1(): #IGNORE:C01111
    msg = 'Parse data definition for multiple attributes.'
    #py.test.skip(msg)
    print msg
    
    from freeode.simlparser import Parser   
    from freeode.ast import NodeClassDef, NodePassStmt
    
    parser = Parser()
    #For debugging: keep Pyparsing's  original parse results.
    # Exit immediately from all action functions
    #Parser.noTreeModification = 1

    test_prog = (
'''
class A:
    pass
''' )
    print 'statement: --------------------------'
    print test_prog

    ast = parser.parseModuleStr(test_prog)
    print 'AST: --------------------------------'
    print ast 
    
    class_stmt = ast.statements[0]
    assert isinstance(class_stmt, NodeClassDef)    
    assert class_stmt.name == 'A'
    #the single statement in the class body
    assert len(class_stmt.statements) == 1
    assert isinstance(class_stmt.statements[0], NodePassStmt)
def test_if_stmt_2(): #IGNORE:C01111
    msg = 'Test to parse an if statement. - Corner case, most simple if statement.'
    #py.test.skip(msg)
    print msg
    
    from freeode.simlparser import Parser   
    from freeode.ast import NodeIfStmt, NodeClause
    
    parser = Parser()
    #For debugging: keep Pyparsing's  original parse results.
    # Exit immediately from all action functions
    #Parser.noTreeModification = 1

    test_prog = (
'''
if a: b
''' )
    #print test_prog
    print
    ast = parser.parseModuleStr(test_prog)
    #print ast 
    
    if_stmt = ast.statements[0]
    assert isinstance(if_stmt, NodeIfStmt)
    assert len(if_stmt.clauses) == 1 
    if_clause = if_stmt.clauses[0]
    assert isinstance(if_clause, NodeClause)
    assert len(if_clause.statements) == 1
def test_data_def_2(): #IGNORE:C01111
    msg = 'Parse data definition for multiple attributes.'
    #py.test.skip(msg)
    print msg
    
    from freeode.simlparser import Parser   
    from freeode.ast import NodeDataDef, NodeStmtList, RoleConstant
    
    parser = Parser()
    #For debugging: keep Pyparsing's  original parse results.
    # Exit immediately from all action functions
    #Parser.noTreeModification = 1

    test_prog = (
'''
data a, b, c: Float const
''' )
    print 'statement: --------------------------'
    print test_prog

    ast = parser.parseModuleStr(test_prog)
    print 'AST: --------------------------------'
    print ast 
    
    #A data statement with multiple attributes is parsed into a list of 
    #data statements for one attribute
    data_stmt_list = ast.statements[0]
    assert isinstance(data_stmt_list, NodeStmtList)
    #test each data statement in the list
    for data_stmt, name in zip(data_stmt_list.statements, ['a', 'b', 'c']):
        assert isinstance(data_stmt, NodeDataDef)
        check_single_data_statement(data_stmt, name, 'Float', RoleConstant)
Example #5
0
def test_ifc_stmt_1(): #IGNORE:C01111
    msg = 'Test to parse a ifc statement. The ifc statement is executed at compile time,'
    #py.test.skip(msg)
    print msg
    
    from freeode.simlparser import Parser   
    from freeode.ast import NodeIfStmt, NodeClause, NodeFloat
    #from freeode.util import aa_make_tree
    
    parser = Parser()
    #For debugging: keep Pyparsing's  original parse results.
    # Exit immediately from all action functions
    #Parser.noTreeModification = 1

    test_prog = (
'''
ifc a==1:
    b = 1
    c = 1
elif a==2:
    b = 2
else:
    b = 3
''' )
    #print test_prog
    print
    ast = parser.parseModuleStr(test_prog)
    #print aa_make_tree(ast) 
    
    if_stmt = ast.statements[0]
    assert isinstance(if_stmt, NodeIfStmt)
    assert if_stmt.runtime_if == False
    assert len(if_stmt.clauses) == 3 
    if_clause = if_stmt.clauses[0]
    assert isinstance(if_clause, NodeClause)
    assert len(if_clause.statements) == 2
    elif_clause = if_stmt.clauses[1]
    assert isinstance(elif_clause, NodeClause)
    assert len(elif_clause.statements) == 1
    else_clause = if_stmt.clauses[2]
    assert isinstance(else_clause, NodeClause)
    assert len(else_clause.statements) == 1
    assert isinstance(else_clause.condition, NodeFloat)
def test_if_stmt_1():  # IGNORE:C01111
    msg = "Test to parse an if statement."
    # py.test.skip(msg)
    print msg

    from freeode.simlparser import Parser
    from freeode.ast import NodeIfStmt, NodeClause, NodeFloat

    parser = Parser()
    # For debugging: keep Pyparsing's  original parse results.
    # Exit immediately from all action functions
    # Parser.noTreeModification = 1

    test_prog = """
if a==1:
    b = 1
    c = 1
elif a==2:
    b = 2
else:
    b = 3
"""
    # print test_prog
    print
    ast = parser.parseModuleStr(test_prog)
    # print ast

    if_stmt = ast.statements[0]
    assert isinstance(if_stmt, NodeIfStmt)
    assert len(if_stmt.clauses) == 3
    if_clause = if_stmt.clauses[0]
    assert isinstance(if_clause, NodeClause)
    assert len(if_clause.statements) == 2
    elif_clause = if_stmt.clauses[1]
    assert isinstance(elif_clause, NodeClause)
    assert len(elif_clause.statements) == 1
    else_clause = if_stmt.clauses[2]
    assert isinstance(else_clause, NodeClause)
    assert len(else_clause.statements) == 1
    assert isinstance(else_clause.condition, NodeFloat)
def test_identifier_1(): #IGNORE:C01111
    msg = 'Parse an identifier.'
    #py.test.skip(msg)
    print msg
    
    from freeode.simlparser import Parser   
    from freeode.ast import NodeIdentifier
    
    parser = Parser()
    #For debugging: keep Pyparsing's  original parse results.
    # Exit immediately from all action functions
    #Parser.noTreeModification = 1

    test_expression = 'a'
    print 'expression: ', test_expression
    
    ast = parser.parseExpressionStr(test_expression)
    print 'AST:'
    print ast 
    
    node_id = ast
    assert isinstance(node_id, NodeIdentifier)
    assert isinstance(node_id.name, str)
    assert node_id.name == 'a'
Example #8
0
def test_parser_stack_overflow_bug(): #IGNORE:C01111
    msg = '''
    Some relatively short input made the the parser crash because of stack 
    exhaustion.
    
    This test is for fixed bug: #389924
        https://bugs.launchpad.net/freeode/+bug/389924
    '''
    #py.test.skip(msg)
    print msg
    
    from freeode.simlparser import Parser
    from freeode.util import UserException
    
    #--- first example ------------------------------------------------------
    #http://launchpadlibrarian.net/28144614/crash_parser3.siml
    prog_text = \
'''
#This file causes a compiler internal error which is difficult to debug:
#RuntimeError: maximum recursion depth exceeded
#Now trying illegal syntax

#func a():
#func a():
func a():

if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:  #There must be an if statement at the end for the error to happen
    a()
'''
    parser = Parser()
    assert_raises(UserException, None, 
                  parser.parseModuleStr, prog_text)
    
    #--- second example --------------------------------------------------
    #Modified version of:
    #http://launchpadlibrarian.net/28144614/crash_parser3.siml
    prog_text = \
'''
func a():
    pass

if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:
    a()
if a:  #There must be an if statement at the end for the error to happen
    a()
'''
    parser = Parser()
    _ast = parser.parseModuleStr(prog_text)
    
    #--- third example ------------------------------------------------------
    #http://launchpadlibrarian.net/28144624/crash_parser4.siml
    prog_text = \
'''
#This file causes a compiler internal error which is difficult to debug:
#RuntimeError: maximum recursion depth exceeded
#Now trying illegal syntax

func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():
func a():

'''
    
    parser = Parser()
    assert_raises(UserException, None, 
                  parser.parseModuleStr, prog_text)
    
    #--- fourth example ------------------------------------------------------
    #http://launchpadlibrarian.net/28144635/crash_parser5.siml
    prog_text = \
'''
#This file causes a compiler internal error which is difficult to debug:
#RuntimeError: maximum recursion depth exceeded
#Now trying illegal syntax

if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
if a:
'''
    
    parser = Parser()
    assert_raises(UserException, None, 
                  parser.parseModuleStr, prog_text)