reassign values
This commit is contained in:
@@ -921,6 +921,40 @@ func TestWhileExpression(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssignmentStatements(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expectedIdentifier string
|
||||
expectedValue interface{}
|
||||
}{
|
||||
{"x = 5;", "x", 5},
|
||||
{"y = true;", "y", true},
|
||||
{"foobar = y;", "foobar", "y"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
program := p.ParseProgram()
|
||||
checkParserErrors(t, p)
|
||||
|
||||
if len(program.Statements) != 1 {
|
||||
t.Fatalf("program.Statements does not contain 1 statements. got=%d",
|
||||
len(program.Statements))
|
||||
}
|
||||
|
||||
stmt := program.Statements[0]
|
||||
if !testAssignmentStatement(t, stmt, tt.expectedIdentifier) {
|
||||
return
|
||||
}
|
||||
|
||||
val := stmt.(*ast.AssignmentStatement).Value
|
||||
if !testLiteralExpression(t, val, tt.expectedValue) {
|
||||
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())
|
||||
@@ -1066,3 +1100,29 @@ func checkParserErrors(t *testing.T, p *Parser) {
|
||||
}
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
func testAssignmentStatement(t *testing.T, s ast.Statement, name string) bool {
|
||||
if s.TokenLiteral() != "=" {
|
||||
t.Errorf("s.TokenLiteral not '='. got=%q", s.TokenLiteral())
|
||||
return false
|
||||
}
|
||||
|
||||
assignStmt, ok := s.(*ast.AssignmentStatement)
|
||||
if !ok {
|
||||
t.Errorf("s not *ast.AssignmentStatement. got=%T", s)
|
||||
return false
|
||||
}
|
||||
|
||||
if assignStmt.Name.Value != name {
|
||||
t.Errorf("assignStmt.Name.Value not '%s'. got=%s", name, assignStmt.Name.Value)
|
||||
return false
|
||||
}
|
||||
|
||||
if assignStmt.Name.TokenLiteral() != name {
|
||||
t.Errorf("assignStmt.Name.TokenLiteral() not '%s'. got=%s",
|
||||
name, assignStmt.Name.TokenLiteral())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user