Skip to content

Commit

Permalink
test(eval): suite for evaluation of integer literals
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavodiasag committed Feb 1, 2024
1 parent 54ec99e commit d0fbd91
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package eval

import (
"testing"

"monkey/lexer"
"monkey/object"
"monkey/parser"
)

func TestEvalIntegerExpresion(t *testing.T) {
for _, tt := range []struct {
input string
expected int64
}{
{"5", 5},
{"10", 10},
} {
evaluated := testEval(tt.input)
testIntegerObject(t, evaluated, tt.expected)
}
}

func testEval(input string) object.Object {
l := lexer.New(input)
p := parser.New(l)
program := p.ParseProgram()

return Eval(program)
}

func testIntegerObject(t *testing.T, obj object.Object, expected int64) bool {
result, ok := obj.(*object.Integer)
if !ok {
t.Errorf("Object not Integer. Got %T (%+v).", obj, obj)
return false
}
if result.Value != expected {
t.Errorf("Object.Value mismatch. Expected %d, got %d",
result.Value, expected)
return false
}
return true
}

0 comments on commit d0fbd91

Please sign in to comment.