parser_infix_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package parser
  2. import (
  3. "testing"
  4. "code.osinet.fr/fgm/waiig15/ast"
  5. "code.osinet.fr/fgm/waiig15/lexer"
  6. )
  7. func TestParsingInfixExpressions(t *testing.T) {
  8. infixTests := []struct {
  9. input string
  10. leftValue int64
  11. operator string
  12. rightValue int64
  13. }{
  14. {"5 + 5", 5, "+", 5},
  15. {"5 - 5", 5, "-", 5},
  16. {"5 * 5", 5, "*", 5},
  17. {"5 / 5", 5, "/", 5},
  18. {"5 > 5", 5, ">", 5},
  19. {"5 < 5", 5, "<", 5},
  20. {"5 == 5", 5, "==", 5},
  21. {"5 != 5", 5, "!=", 5},
  22. }
  23. for _, tt := range infixTests {
  24. l := lexer.New(tt.input)
  25. p := New(l)
  26. program := p.ParseProgram()
  27. checkParserErrors(t, p)
  28. if len(program.Statements) != 1 {
  29. t.Fatalf("program.Statements does not contain %d statements. got=%d",
  30. 1, len(program.Statements))
  31. }
  32. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  33. if !ok {
  34. t.Fatalf("program.Statements[0] is not expressionStatement. got=%T",
  35. program.Statements[0])
  36. }
  37. exp, ok := stmt.Expression.(*ast.InfixExpression)
  38. if !ok {
  39. t.Fatalf("exp is not infixExpression. got=%T", stmt.Expression)
  40. }
  41. // Why no error ?
  42. if !testIntegerLiteral(t, exp.Left, tt.leftValue) {
  43. return
  44. }
  45. if exp.Operator != tt.operator {
  46. t.Fatalf("exp.Operator is not '%s'. got=%s", tt.operator,
  47. exp.Operator)
  48. }
  49. // Why no error ?
  50. if !testIntegerLiteral(t, exp.Right, tt.rightValue) {
  51. return
  52. }
  53. }
  54. }