-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_test.go
161 lines (155 loc) · 3.7 KB
/
range_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"io"
"os"
"testing"
)
var dummyFile = []string{
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
}
// TestRange() runs tests on the range parser and verifies the start, end and
// dot position. It also compares the output.
func TestRange(t *testing.T) {
var ted *Editor = NewEditor(nil, io.Discard, io.Discard)
ted.setupTestFile(dummyFile)
tests := []struct {
input []byte
expectedStart int
expectedEnd int
expectedDot int
expectedAddrCount int
expectedOutput string
}{
{
input: []byte("8"),
expectedStart: 8,
expectedEnd: 8,
expectedAddrCount: 1,
expectedOutput: "H\n",
},
{
input: []byte("1,5"),
expectedStart: 1,
expectedEnd: 5,
expectedAddrCount: 2,
expectedOutput: "E\n",
},
{
input: []byte("+"),
expectedStart: 6,
expectedEnd: 6,
expectedAddrCount: 1,
expectedOutput: "F\n",
},
{
input: []byte("-"),
expectedStart: 5,
expectedEnd: 5,
expectedAddrCount: 1,
expectedOutput: "E\n",
},
{
input: []byte("^"),
expectedStart: 4,
expectedEnd: 4,
expectedAddrCount: 1,
expectedOutput: "D\n",
},
{
input: []byte(".,+5"),
expectedStart: 4,
expectedEnd: 9,
expectedAddrCount: 2,
expectedOutput: "I\n",
},
{
input: []byte("-2,+5"),
expectedStart: 7,
expectedEnd: 14,
expectedAddrCount: 2,
expectedOutput: "N\n",
},
{
input: []byte(","),
expectedStart: 1,
expectedEnd: 26,
expectedAddrCount: 2,
expectedOutput: "Z\n",
},
{
input: []byte("6,%"),
expectedStart: 6,
expectedEnd: 26,
expectedAddrCount: 2,
expectedOutput: "Z\n",
},
{
input: []byte("3;"),
expectedStart: 3,
expectedEnd: 26,
expectedAddrCount: 2,
expectedOutput: "Z\n",
},
}
for _, test := range tests {
t.Run(string(test.input), func(t *testing.T) {
var b bytes.Buffer
ted.ReadInput(bytes.NewBuffer(test.input))
ted.out = &b
if err := ted.DoRange(); err != nil {
t.Fatalf("expected the range to be valid but failed: %s", err)
}
if err := ted.DoCommand(); err != nil {
t.Fatalf("expected the command to be valid but failed: %s", err)
}
if ted.Start != test.expectedStart {
t.Errorf("expected start position %d, got %d",
test.expectedStart, ted.Start)
}
if ted.End != test.expectedEnd {
t.Errorf("expected end position %d, got %d", test.expectedEnd, ted.End)
}
// if ted.Dot != test.expectedDot {
// t.Errorf("expected dot position %d, got %d", test.expectedDot, ted.Dot)
// }
if ted.addrCount != test.expectedAddrCount {
t.Errorf("expected internal address count %d, got %d",
test.expectedAddrCount, ted.addrCount)
}
if b.String() != test.expectedOutput {
t.Fatalf("expected output '%s', got '%s'", test.expectedOutput, b.String())
}
})
}
}
func (ed *Editor) setupTestFile(buf []string) {
ed.Lines = buf
ed.Path = "test"
ed.Dot = len(buf)
ed.Start = ed.Dot
ed.End = ed.Dot
ed.addr = -1
}
func (ed *Editor) removeDummyFile(fname string) {
if _, err := os.Stat(fname); err == nil {
if err := os.Remove(fname); err != nil {
panic(err)
}
}
}
func (ed *Editor) createDummyFile(fname string) {
ed.removeDummyFile(fname)
file, err := os.Create(fname)
if err != nil {
panic(err)
}
defer file.Close()
for _, ln := range dummyFile {
_, err := file.WriteString(ln+"\n")
if err != nil {
panic(err)
}
}
}