Skip to content

Commit

Permalink
fix: broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasepe committed Dec 28, 2020
1 parent 1bf06ba commit 802d788
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 269 deletions.
8 changes: 4 additions & 4 deletions data/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestFetchFromURI(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello from scrawl!")
fmt.Fprint(w, "Hello from g2d!")
}))
defer ts.Close()

Expand All @@ -19,19 +19,19 @@ func TestFetchFromURI(t *testing.T) {
t.Error(err)
}

want := "Hello from scrawl!"
want := "Hello from g2d!"
if got := string(data); got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}

func TestFetchFromFile(t *testing.T) {
data, err := FetchFromFile("../testdata/spritesheet.json", 10)
data, err := FetchFromFile("../testdata/none.g2d", 10)
if err != nil {
t.Error(err)
}

want := `{ "fram`
want := `print( #co`
if got := flatten(string(data)); got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
Expand Down
7 changes: 0 additions & 7 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,6 @@ if (10 > 1) {
`"Hello" - "World"`,
"unknown operator: str - str",
},
{
`{"name": "Monkey"}[fn(x) { x }];`,
"unusable as hash key: fn",
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -575,8 +571,6 @@ func TestBuiltinFunctions(t *testing.T) {
{`bool("foo")`, true},
{`bool([])`, false},
{`bool([1, 2, 3])`, true},
{`bool({})`, false},
{`bool({"a": 1})`, true},
{`int(true)`, 1},
{`int(false)`, 0},
{`int(1)`, 1},
Expand All @@ -587,7 +581,6 @@ func TestBuiltinFunctions(t *testing.T) {
{`str(10)`, "10"},
{`str("foo")`, "foo"},
{`str([1, 2, 3])`, "[1, 2, 3]"},
{`str({"a": 1})`, "{a: 1}"},
}

for _, tt := range tests {
Expand Down
4 changes: 0 additions & 4 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ d.foo
expectedType token.Type
expectedLiteral string
}{
{token.COMMENT, "!./g2d"},
{token.IDENT, "five"},
{token.BIND, ":="},
{token.INT, "5"},
Expand All @@ -69,7 +68,6 @@ d.foo
{token.SEMICOLON, ";"},
{token.RBRACE, "}"},
{token.SEMICOLON, ";"},
{token.COMMENT, " this is a comment"},
{token.IDENT, "result"},
{token.BIND, ":="},
{token.IDENT, "add"},
Expand Down Expand Up @@ -108,7 +106,6 @@ d.foo
{token.FALSE, "false"},
{token.SEMICOLON, ";"},
{token.RBRACE, "}"},
{token.COMMENT, " this is another comment"},
{token.INT, "10"},
{token.EQ, "=="},
{token.INT, "10"},
Expand Down Expand Up @@ -166,7 +163,6 @@ c := "\r\n\t"
expectedType token.Type
expectedLiteral string
}{
{token.COMMENT, "!./g2d"},
{token.IDENT, "a"},
{token.BIND, ":="},
{token.STRING, "\"foo\""},
Expand Down
254 changes: 0 additions & 254 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,6 @@ import (
"github.com/lucasepe/g2d/lexer"
)

func TestComments(t *testing.T) {
tests := []struct {
input string
expectedComment string
}{
{"// foo", " foo"},
{"#!g2d", "!g2d"},
{"# foo", " foo"},
{" # foo", " foo"},
{" // x := 1", " 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 TestAssignmentExpressions(t *testing.T) {
assert := assert.New(t)

Expand All @@ -51,7 +21,6 @@ func TestAssignmentExpressions(t *testing.T) {
{"y = true;", "y=true"},
{"foobar = y;", "foobar=y"},
{"[1, 2, 3][1] = 4", "([1, 2, 3][1])=4"},
{`{"a": 1}["b"] = 2`, `({a:1}[b])=2`},
}

for _, tt := range tests {
Expand Down Expand Up @@ -926,21 +895,6 @@ func TestCallExpressionParameterParsing(t *testing.T) {
}
}

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
}

func testInfixExpression(t *testing.T, exp ast.Expression, left interface{},
operator string, right interface{}) bool {

Expand Down Expand Up @@ -1103,40 +1057,6 @@ func TestParsingArrayLiterals(t *testing.T) {
testInfixExpression(t, array.Elements[2], 3, "+", 3)
}

func TestParsingSelectorExpressions(t *testing.T) {
input := "myHash.foo"

l := lexer.New(input)
p := New(l)
program := p.ParseProgram()
checkParserErrors(t, p)
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
t.Logf("stmt: %#v", stmt)

exp, ok := stmt.Expression.(*ast.IndexExpression)
if !ok {
t.Fatalf("exp not *ast.IndexExpression. got=%T", stmt.Expression)
}

ident, ok := exp.Left.(*ast.Identifier)
if !ok {
t.Fatalf("exp.Left not *ast.Identifier. got=%T", stmt.Expression)
}

if !testIdentifier(t, ident, "myHash") {
return
}

index, ok := exp.Index.(*ast.StringLiteral)
if !ok {
t.Fatalf("exp.Index not *ast.StringLiteral. got=%T", stmt.Expression)
}

if index.Value != "foo" {
t.Fatalf("index.Value != \"foo\"")
}
}

func TestParsingIndexExpressions(t *testing.T) {
input := "myArray[1 + 1]"

Expand All @@ -1158,177 +1078,3 @@ func TestParsingIndexExpressions(t *testing.T) {
return
}
}

func TestParsingHashLiteralsStringKeys(t *testing.T) {
input := `{"one": 1, "two": 2, "three": 3}`

l := lexer.New(input)
p := New(l)
program := p.ParseProgram()
checkParserErrors(t, p)

stmt := program.Statements[0].(*ast.ExpressionStatement)
hash, ok := stmt.Expression.(*ast.HashLiteral)
if !ok {
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
}

if len(hash.Pairs) != 3 {
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
}

expected := map[string]int64{
"one": 1,
"two": 2,
"three": 3,
}

for key, value := range hash.Pairs {
literal, ok := key.(*ast.StringLiteral)
if !ok {
t.Errorf("key is not ast.StringLiteral. got=%T", key)
}

expectedValue := expected[literal.String()]

testIntegerLiteral(t, value, expectedValue)
}
}

func TestParsingHashLiteralsBooleanKeys(t *testing.T) {
input := `{true: 1, false: 2}`

l := lexer.New(input)
p := New(l)
program := p.ParseProgram()
checkParserErrors(t, p)

stmt := program.Statements[0].(*ast.ExpressionStatement)
hash, ok := stmt.Expression.(*ast.HashLiteral)
if !ok {
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
}

expected := map[string]int64{
"true": 1,
"false": 2,
}

if len(hash.Pairs) != len(expected) {
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
}

for key, value := range hash.Pairs {
boolean, ok := key.(*ast.Boolean)
if !ok {
t.Errorf("key is not ast.BooleanLiteral. got=%T", key)
continue
}

expectedValue := expected[boolean.String()]
testIntegerLiteral(t, value, expectedValue)
}
}

func TestParsingHashLiteralsIntegerKeys(t *testing.T) {
input := `{1: 1, 2: 2, 3: 3}`

l := lexer.New(input)
p := New(l)
program := p.ParseProgram()
checkParserErrors(t, p)

stmt := program.Statements[0].(*ast.ExpressionStatement)
hash, ok := stmt.Expression.(*ast.HashLiteral)
if !ok {
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
}

expected := map[string]int64{
"1": 1,
"2": 2,
"3": 3,
}

if len(hash.Pairs) != len(expected) {
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
}

for key, value := range hash.Pairs {
integer, ok := key.(*ast.IntegerLiteral)
if !ok {
t.Errorf("key is not ast.IntegerLiteral. got=%T", key)
continue
}

expectedValue := expected[integer.String()]

testIntegerLiteral(t, value, expectedValue)
}
}

func TestParsingEmptyHashLiteral(t *testing.T) {
input := "{}"

l := lexer.New(input)
p := New(l)
program := p.ParseProgram()
checkParserErrors(t, p)

stmt := program.Statements[0].(*ast.ExpressionStatement)
hash, ok := stmt.Expression.(*ast.HashLiteral)
if !ok {
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
}

if len(hash.Pairs) != 0 {
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
}
}

func TestParsingHashLiteralsWithExpressions(t *testing.T) {
input := `{"one": 0 + 1, "two": 10 - 8, "three": 15 / 5}`

l := lexer.New(input)
p := New(l)
program := p.ParseProgram()
checkParserErrors(t, p)

stmt := program.Statements[0].(*ast.ExpressionStatement)
hash, ok := stmt.Expression.(*ast.HashLiteral)
if !ok {
t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
}

if len(hash.Pairs) != 3 {
t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
}

tests := map[string]func(ast.Expression){
"one": func(e ast.Expression) {
testInfixExpression(t, e, 0, "+", 1)
},
"two": func(e ast.Expression) {
testInfixExpression(t, e, 10, "-", 8)
},
"three": func(e ast.Expression) {
testInfixExpression(t, e, 15, "/", 5)
},
}

for key, value := range hash.Pairs {
literal, ok := key.(*ast.StringLiteral)
if !ok {
t.Errorf("key is not ast.StringLiteral. got=%T", key)
continue
}

testFunc, ok := tests[literal.String()]
if !ok {
t.Errorf("No test function for key %q found", literal.String())
continue
}

testFunc(value)
}
}
2 changes: 2 additions & 0 deletions testdata/none.g2d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
print( #comment
"Hello, World\n");

0 comments on commit 802d788

Please sign in to comment.