def TestInvalidScript(): tp = MockTokenParser('1completerubbish') ce = CompileEngine(tp, MockVariables(), MockScriptEngine()) try: ce.Process() AssertTrue('tis', False) except CompileError as e: AssertEqual(e.value, "01: Unrecognised token '1completerubbish'")
def TestEngineFunctionInvalid(): tp = MockTokenParser('engineFunction ;') try: script = MockScript('name') ce = CompileEngine(tp, MockVariables([], [], [['engineFunction', 0]]), None) ce.CompileSingleExecutionBlock(script) AssertTrue(False) except CompileError as e: pass
def TestPushAndPop(): os = OperatorStack() os.Push(Operator(1, 1)) AssertEqual('tpap1', os.GetPrecedence(), 1) os.Push(Operator(2, 2)) AssertEqual('tpap2', os.GetPrecedence(), 2) AssertEqual('tpap3', os.Pop().scriptcommand, 2) AssertEqual('tpap3', os.Pop().scriptcommand, 1) AssertTrue('tose1', os.IsEmpty()) AssertEqual('tose2', os.GetPrecedence(), 0)
def TestRequireNextToken(): tp = MockTokenParser('first second') ce = CompileEngine(tp, MockVariables(), MockScriptEngine()) ce.RequireNextToken('first', 'previous') try: ce.RequireNextToken('third', 'previous') AssertTrue('trtnp', False) except CompileError as e: AssertEqual('trnt1', e.value, "Expected 'third' instead of 'second' after 'previous'")
def TestEmptyStack(): os = OperatorStack() AssertTrue('tose1', os.IsEmpty()) AssertEqual('tose2', os.GetPrecedence(), 0)
def TestExpressionTerminators(): ep = ExpressionParser(None) AssertTrue('tet1', ep.IsExpressionTerminator(';')) AssertTrue('tet2', ep.IsExpressionTerminator(')')) AssertTrue('tet3', ep.IsExpressionTerminator(None))
def TestValidVariableChar(): tp = TokenParser('x') AssertTrue('tian1', ValidVariableChar('a')) AssertTrue('tian2', ValidVariableChar('z')) AssertTrue('tian3', ValidVariableChar('_')) AssertFalse('tian4', ValidVariableChar('?'))
def TestEmptyTokenParser(): tp = MockTokenParser('') ce = CompileEngine(tp, None, None) ce.Process() AssertTrue('tetp', tp.tokenfetched)
def TestGlobalVariableCanBeDeclared(): ve = VariableEngine() ve.AddGlobalVariable('globalvar') AssertTrue('tgvd', ve.IsGlobalVariable('globalvar'))