|
|
@@ -85,6 +85,29 @@ func TestIntegerArithmetic(t *testing.T) {
|
|
|
runCompilerTests(t, tests)
|
|
|
}
|
|
|
|
|
|
+func TestBooleanExpression(t *testing.T) {
|
|
|
+ tests := []compilerTestCase{
|
|
|
+ {
|
|
|
+ input: "true",
|
|
|
+ expectedConstants: []interface{}{},
|
|
|
+ expectedInstructions: []code.Instructions{
|
|
|
+ code.Make(code.OpTrue),
|
|
|
+ code.Make(code.OpPop),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ input: "false",
|
|
|
+ expectedConstants: []interface{}{},
|
|
|
+ expectedInstructions: []code.Instructions{
|
|
|
+ code.Make(code.OpFalse),
|
|
|
+ code.Make(code.OpPop),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ runCompilerTests(t, tests)
|
|
|
+}
|
|
|
+
|
|
|
func runCompilerTests(t *testing.T, tests []compilerTestCase) {
|
|
|
t.Helper()
|
|
|
|
|
|
@@ -154,12 +177,12 @@ func testInstructions(
|
|
|
concatted := concatInstructions(expected)
|
|
|
|
|
|
if len(actual) != len(concatted) {
|
|
|
- return fmt.Errorf("wrong instructions length.\nwant=%q\ngot=%q", concatted, actual)
|
|
|
+ return fmt.Errorf("wrong instructions length.\nwant=%q\n got=%q", concatted, actual)
|
|
|
}
|
|
|
|
|
|
for i, ins := range concatted {
|
|
|
if actual[i] != ins {
|
|
|
- return fmt.Errorf("wrong instructions at %d.\nwant=%q\ngot=%q", i, concatted, actual)
|
|
|
+ return fmt.Errorf("wrong instructions at %d.\nwant=%q\n got=%q", i, concatted, actual)
|
|
|
}
|
|
|
}
|
|
|
return nil
|