reassign values
Some checks failed
Build / build (push) Failing after 1m46s
Test / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-15 15:24:48 -04:00
parent c818591f79
commit 2988719c9c
13 changed files with 327 additions and 17 deletions

View File

@@ -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
}