def TestScriptDefinitions(): tp = TokenParser('scriptname(){}') AssertEqual(tp.GetToken(), 'scriptname') AssertEqual(tp.GetToken(), '(') AssertEqual(tp.GetToken(), ')') AssertEqual(tp.GetToken(), '{') AssertEqual(tp.GetToken(), '}')
def TestReplaceToken(): tp = TokenParser('one two three') AssertEqual('trt1', tp.GetToken(), 'one') AssertEqual('trt2', tp.GetToken(), 'two') tp.ReplaceToken('four') AssertEqual('trt3', tp.GetToken(), 'four') AssertEqual('trt4', tp.GetToken(), 'three')
def TestSetToken(): se = ScriptEngine() script = se.CreateScript('test') script.AddTokenInt(1) script.AddTokenInt(2) script.AddTokenInt(3) AssertEqual('tst1', script.GetScriptCode(), [1, 2, 3]) script.SetToken(1, 4) AssertEqual('tst1', script.GetScriptCode(), [1, 4, 3])
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 TestMissingScriptTerminator(): tp = MockTokenParser('scriptname ( ) {') ce = CompileEngine(tp, VariableEngine(), MockScriptEngine()) try: ce.Process() AssertFalse('sef', True) except CompileError as e: AssertEqual('tmst', e.value, "10: Unexpected end of script")
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 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 TestAddStuffToScript(): se = ScriptEngine() script = se.CreateScript('test') script.AddTokenInt(1) AssertEqual(1, script.GetEndIndex2()) AssertEqual(4, script.GetEndPC()) script.AddTokenInt(2) AssertEqual(2, script.GetEndIndex2()) AssertEqual(8, script.GetEndPC()) script.AddTokenString('fried') AssertEqual(3, script.GetEndIndex2()) AssertEqual(14, script.GetEndPC())
def TestEmptyStack(): os = OperatorStack() AssertTrue('tose1', os.IsEmpty()) AssertEqual('tose2', os.GetPrecedence(), 0)
def TestUnaryOperators(): ep = ExpressionParser(None) AssertNEqual('tuo1', ep.GetUnaryOperator('-'), None) AssertNEqual('tuo2', ep.GetUnaryOperator('!'), None) AssertEqual(ep.GetUnaryOperator('/'), None)
def TestComments(): tp = TokenParser('one; // comment\ntwo') AssertEqual('tc1', tp.GetToken(), 'one') AssertEqual('tc2', tp.GetToken(), ';') AssertEqual('tc3', tp.GetToken(), 'two')
def TestStrings(): tp = TokenParser('bla"string"bla') AssertEqual(tp.GetToken(), 'bla') AssertEqual(tp.GetToken(), '"string"') AssertEqual(tp.GetToken(), 'bla')
def TestOr(): tp = TokenParser("| || |||") AssertEqual(tp.GetToken(), '|') AssertEqual(tp.GetToken(), '||') AssertEqual(tp.GetToken(), '||') AssertEqual(tp.GetToken(), '|')
def TestMoreTokens(): tp = TokenParser("&&&") AssertEqual(tp.GetToken(), '&&') AssertEqual(tp.GetToken(), '&')
def TestBasicTokens(): tp = TokenParser("var _var1_23\n\n345 // comment\n1234{ }{} +=+-==/\n[hash];\n \r\n\t") AssertEqual('ttp1', tp.GetToken(), 'var') AssertEqual('ttp2', tp.GetToken(), '_var1_23') AssertEqual('ttp3', tp.GetToken(), '345') AssertEqual('ttp5', tp.GetToken(), '1234') AssertEqual('ttp7', tp.GetToken(), '{') AssertEqual('ttp8', tp.GetToken(), '}') AssertEqual('ttp7', tp.GetToken(), '{') AssertEqual('ttp8', tp.GetToken(), '}') AssertEqual('ttp9', tp.GetToken(), '+') AssertEqual('ttp10', tp.GetToken(), '=') AssertEqual('ttp9', tp.GetToken(), '+') AssertEqual('ttp11', tp.GetToken(), '-') AssertEqual('ttp12', tp.GetToken(), '==') AssertEqual('ttp13', tp.GetToken(), '/') AssertEqual('ttp14', tp.GetToken(), '[') AssertEqual('ttp15', tp.GetToken(), 'hash') AssertEqual('ttp16', tp.GetToken(), ']') AssertEqual('ttp16', tp.GetToken(), ';') AssertEqual(tp.GetToken(), None) AssertEqual('ttpend', tp.GetLineNumber(), 8)