package parser import ( "code.osinet.fr/fgm/waiig15/ast" "code.osinet.fr/fgm/waiig15/lexer" "testing" ) func TestLetStatements(t *testing.T) { input := ` let x = 5; let y = 10; let foobar = 838383; ` l := lexer.New(input) p := New(l) program := p.ParseProgram() 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 }