-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
160 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package parser | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/54L1m/mil-lang/ast" | ||
"github.com/54L1m/mil-lang/lexer" | ||
) | ||
|
||
func TestLetStatement(t *testing.T) { | ||
input := ` | ||
var x = 5; | ||
var y = 10; | ||
var foobar = 838383; | ||
` | ||
l := lexer.New(input) | ||
p := New(l) | ||
|
||
program := p.ParseProgram() | ||
checkParseErrors(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 !testVarStatement(t, stmt, tt.expectedIdentifier) { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func testVarStatement(t *testing.T, s ast.Statement, name string) bool { | ||
if s.TokenLiteral() != "var" { | ||
t.Errorf("s.TokenLiteral not 'var'. got=%q", s.TokenLiteral()) | ||
return false | ||
} | ||
varStmt, ok := s.(*ast.VarStatement) | ||
if !ok { | ||
t.Errorf("s not *ast.VarStatement. got=%T", s) | ||
return false | ||
} | ||
if varStmt.Name.Value != name { | ||
t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, varStmt.Name.Value) | ||
return false | ||
} | ||
if varStmt.Name.TokenLiteral() != name { | ||
t.Errorf("letStmt.Name.TokenLiteral() not '%s'. got=%s", | ||
name, varStmt.Name.TokenLiteral()) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func checkParseErrors(t *testing.T, p *Parser) { | ||
errors := p.Errors() | ||
if len(errors) == 0 { | ||
return | ||
} | ||
|
||
t.Errorf("parser has %d errors", len(errors)) | ||
for _, msg := range errors { | ||
t.Errorf("parser error: %q", msg) | ||
} | ||
t.FailNow() | ||
} |