parser_return_test.go 1003 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package parser
  2. import (
  3. "testing"
  4. "code.osinet.fr/fgm/waiig15/lexer"
  5. "code.osinet.fr/fgm/waiig15/ast"
  6. )
  7. func TestReturnStatements(t *testing.T) {
  8. // Try removing the ident, the =, or both, to get human-readable errors.
  9. input := `
  10. return 5;
  11. return 10;
  12. return 993322;
  13. `
  14. l := lexer.New(input)
  15. p := New(l)
  16. program := p.ParseProgram()
  17. checkParserErrors(t, p)
  18. if program == nil {
  19. t.Fatalf("ParseProgram() returned nil.")
  20. }
  21. if len(program.Statements) != 3 {
  22. t.Fatalf("program.Statements does not contain 3 statements, got=%d",
  23. len(program.Statements))
  24. }
  25. for _, stmt := range program.Statements {
  26. // Statement is an interface, we need a concrete type for the value, and
  27. // our test input only contains Let statements.
  28. returnStmt, ok := stmt.(*ast.ReturnStatement)
  29. if !ok {
  30. t.Errorf("s not *ast.ReturnStatement{}, got=%T", stmt)
  31. continue
  32. }
  33. if returnStmt.TokenLiteral() != "return" {
  34. t.Errorf("s.TokenLiteral not 'return', got=%q",
  35. stmt.TokenLiteral())
  36. }
  37. }
  38. }