|
@@ -0,0 +1,64 @@
|
|
|
+package parser
|
|
|
+
|
|
|
+import (
|
|
|
+ "testing"
|
|
|
+
|
|
|
+ "code.osinet.fr/fgm/waiig15/ast"
|
|
|
+ "code.osinet.fr/fgm/waiig15/lexer"
|
|
|
+)
|
|
|
+
|
|
|
+func TestParsingInfixExpressions(t *testing.T) {
|
|
|
+ infixTests := []struct {
|
|
|
+ input string
|
|
|
+ leftValue int64
|
|
|
+ operator string
|
|
|
+ rightValue int64
|
|
|
+ }{
|
|
|
+ {"5 + 5", 5, "+", 5},
|
|
|
+ {"5 - 5", 5, "-", 5},
|
|
|
+ {"5 * 5", 5, "*", 5},
|
|
|
+ {"5 / 5", 5, "/", 5},
|
|
|
+ {"5 > 5", 5, ">", 5},
|
|
|
+ {"5 < 5", 5, "<", 5},
|
|
|
+ {"5 == 5", 5, "==", 5},
|
|
|
+ {"5 != 5", 5, "!=", 5},
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, tt := range infixTests {
|
|
|
+ l := lexer.New(tt.input)
|
|
|
+ p := New(l)
|
|
|
+ program := p.ParseProgram()
|
|
|
+ checkParserErrors(t, p)
|
|
|
+
|
|
|
+ if len(program.Statements) != 1 {
|
|
|
+ t.Fatalf("program.Statements does not contain %d statements. got=%d",
|
|
|
+ 1, len(program.Statements))
|
|
|
+ }
|
|
|
+
|
|
|
+ stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
|
|
+ if !ok {
|
|
|
+ t.Fatalf("program.Statements[0] is not expressionStatement. got=%T",
|
|
|
+ program.Statements[0])
|
|
|
+ }
|
|
|
+
|
|
|
+ exp, ok := stmt.Expression.(*ast.InfixExpression)
|
|
|
+ if !ok {
|
|
|
+ t.Fatalf("exp is not infixExpression. got=%T", stmt.Expression)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if !testIntegerLiteral(t, exp.Left, tt.leftValue) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if exp.Operator != tt.operator {
|
|
|
+ t.Fatalf("exp.Operator is not '%s'. got=%s", tt.operator,
|
|
|
+ exp.Operator)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if !testIntegerLiteral(t, exp.Right, tt.rightValue) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|