-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
42 lines (37 loc) · 1 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package parse
import (
"bytes"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/prog-lang/brainf-go/cpu"
)
func TestParser(t *testing.T) {
source := "+++ [ > +++ [ .- ] < - ]"
code, err := FromReader(strings.NewReader(source))
if err != nil {
t.Fatalf("unexpected parsing error: %s", err)
}
buf := bytes.NewBuffer([]byte{})
vm := cpu.New(code, nil, buf)
if err := vm.Start().Error(); err != nil {
t.Fatalf("panic during execution: %v", err)
}
expect := []byte{3, 2, 1, 3, 2, 1, 3, 2, 1}
got := buf.Bytes()
if !cmp.Equal(expect, got) {
t.Fatalf("invalid bytes written to buffer:\n%s", cmp.Diff(expect, got))
}
}
func TestUnmatchedOpenBracket(t *testing.T) {
source := "["
if _, err := FromReader(strings.NewReader(source)); err == nil {
t.Fatalf("expected parsing error, but got %v", err)
}
}
func TestUnmatchedClosingBracket(t *testing.T) {
source := "]"
if _, err := FromReader(strings.NewReader(source)); err == nil {
t.Fatalf("expected parsing error, but got %v", err)
}
}