parset_identifier_test.go 959 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package parser
  2. import (
  3. "testing"
  4. "code.osinet.fr/fgm/waiig15/ast"
  5. "code.osinet.fr/fgm/waiig15/lexer"
  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. }