parset_identifier_test.go 959 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package parser
  2. import (
  3. "testing"
  4. "code.osinet.fr/fgm/waiig15/lexer"
  5. "code.osinet.fr/fgm/waiig15/ast"
  6. )
  7. func TestIdentifierExpression(t *testing.T) {
  8. const input = "foobar"
  9. l := lexer.New(input)
  10. p := New(l)
  11. program := p.ParseProgram()
  12. checkParserErrors(t, p)
  13. if len(program.Statements) != 1 {
  14. t.Fatalf("program has not enough statements. got=%d",
  15. len(program.Statements))
  16. }
  17. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  18. if !ok {
  19. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. Got=%T",
  20. program.Statements)
  21. }
  22. ident, ok := stmt.Expression.(*ast.Identifier)
  23. if !ok {
  24. t.Fatalf("exp not *ast.Identifier. got=%T", stmt.Expression)
  25. }
  26. // Why not use input instead of inline strings ?
  27. if ident.Value != input {
  28. t.Errorf("ident.Value not %s. got=%s", input,
  29. ident.Value)
  30. }
  31. if ident.TokenLiteral() != input {
  32. t.Errorf("ident.TokenLiteral not %s. got=%s", input,
  33. ident.TokenLiteral())
  34. }
  35. }