comments
This commit is contained in:
@@ -1126,3 +1126,48 @@ func testAssignmentStatement(t *testing.T, s ast.Statement, name string) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func TestComments(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expectedComment string
|
||||
}{
|
||||
{"// foo", " foo"},
|
||||
{"#!monkey", "!monkey"},
|
||||
{"# foo", " foo"},
|
||||
{" # foo", " foo"},
|
||||
{" // let x = 1", " let x = 1"},
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
comment := program.Statements[0]
|
||||
if !testComment(t, comment, tt.expectedComment) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testComment(t *testing.T, s ast.Statement, expected string) bool {
|
||||
comment, ok := s.(*ast.Comment)
|
||||
if !ok {
|
||||
t.Errorf("s not *ast.Comment. got=%T", s)
|
||||
return false
|
||||
}
|
||||
|
||||
if comment.Value != expected {
|
||||
t.Errorf("comment.Value not '%s'. got=%s", expected, comment.Value)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user