-
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.
test(eval): suite for evaluation of integer literals
- Loading branch information
1 parent
54ec99e
commit d0fbd91
Showing
1 changed file
with
44 additions
and
0 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
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 | ||
} |