diff --git a/parser/print_test.go b/parser/print_test.go index 352b4e7..019c471 100644 --- a/parser/print_test.go +++ b/parser/print_test.go @@ -4,24 +4,46 @@ import ( "bufio" "bytes" "reflect" + "strings" "testing" ) +var testprogram = ` >>>>> + [-]+++++ + [->>+<<] + > + [ + , + [<<<] + ++ + [->>++<<] + >> + [->+>+<<] + > + [->+++>+++<<] + ] + >> + . + [->>-<<] +` + func TestPrint(t *testing.T) { program := []Instruction{ {opNoop, 0}, {opAddDp, 5}, - {opSetVal, 0}, - {opAddVal, 5}, + {opSetVal, 5}, {opMove, 2}, + {opAddDp, 1}, {opJmpZ, 7}, {opIn, 1}, {opSkip, -3}, {opSetVal, 2}, {opMulVal, 2}, {opNoop, 2}, + {opAddDp, 2}, {opDupVal, 1}, {opNoop, 2}, + {opAddDp, 1}, {opVec, 1}, {opNoop, 2}, {opNoop, 3}, @@ -34,7 +56,22 @@ func TestPrint(t *testing.T) { outputBuf := bufio.NewWriter(&buf) Print(program, outputBuf) got := buf.String() - want := " >>>>>\n [-]\n +++++\n [->>+<<]\n [\n\t ,\n\t [<<<]\n\t ++\n\t [->>++<<]\n\t [->+>+<<]\n\t [->+++>+++<<]\n ]\n >>\n .\n [->>-<<]\n" + want := testprogram + + if !reflect.DeepEqual(got, want) { + t.Errorf("got %v want %v", got, want) + } +} + +func TestParseThenPrint(t *testing.T) { + want := testprogram + + program, _ := Tokenise(bufio.NewReader(strings.NewReader(testprogram))) + + var buf bytes.Buffer + outputBuf := bufio.NewWriter(&buf) + Print(program, outputBuf) + got := buf.String() if !reflect.DeepEqual(got, want) { t.Errorf("got %v want %v", got, want)