12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package parser
- import (
- "testing"
- "code.osinet.fr/fgm/waiig15/lexer"
- "code.osinet.fr/fgm/waiig15/ast"
- )
- func TestLetStatements(t *testing.T) {
- // Try removing the ident, the =, or both, to get human-readable errors.
- input := `
- let x = 5;
- let y = 10;
- let foobar = 838383;
- `
- l := lexer.New(input)
- p := New(l)
- program := p.ParseProgram()
- checkParserErrors(t, p)
- if program == nil {
- t.Fatalf("ParseProgram() returned nil.")
- }
- if len(program.Statements) != 3 {
- t.Fatalf("program.Statements does not contain 3 statements, got=%d",
- len(program.Statements))
- }
- tests := []struct {
- expectedIdentifier string
- }{
- {"x"},
- {"y"},
- {"foobar"},
- }
- for i, tt := range tests {
- stmt := program.Statements[i]
- if !testLetStatement(t, stmt, tt.expectedIdentifier) {
- return
- }
- }
- }
- func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
- if s.TokenLiteral() != "let" {
- t.Errorf("s.TokenLiteral not 'let', got=%q", s.TokenLiteral())
- return false
- }
- // Statement is an interface, we need a concrete type for the value, and we
- // just determined this looked like a LetStatement.
- letStmt, ok := s.(*ast.LetStatement)
- if !ok {
- t.Errorf("s not *ast.LetStatement{}, got=%T", s)
- }
- if letStmt.Name.Value != name {
- t.Errorf("letStmt.Name.Value not %s, got=%s",
- name, letStmt.Name.Value)
- return false
- }
- if letStmt.Name.TokenLiteral() != name {
- t.Errorf("letStmt.Name.TokenLiteral not %s, got=%s",
- name, letStmt.Name.TokenLiteral())
- return false
- }
- return true
- }
|