summaryrefslogtreecommitdiff
path: root/internal/render/instructions_test.go
blob: b62320d533a50547dbe8f37fc5d084fc7dde68e8 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package render

import (
	"bytes"
	"testing"
)

const (
	errFmt  = "Failed: '%v'\nExpected %v, got %v"
	passFmt = "Passed: '%v'\n"
)

type testTrimWrite struct {
	title        string
	input        Instruction
	n            int     // number of characters to trim
	d            trimDir // FROM which side to trim
	expectedTrim Instruction
	expectedBuf  string
}

var TrimWriteCases []testTrimWrite = []testTrimWrite{
	{
		"Trim 2 characters from left",
		DrawInstruction{X: 5, Y: 5, Content: "hello"},
		2,
		TrimLeft,
		DrawInstruction{X: 7, Y: 5, Content: "llo"},
		"\033[5;5Hhello",
	},
	{
		"Trim 2 characters from the right of a bold Draw",
		DrawInstruction{
			X:          43,
			Y:          6,
			Content:    "hello",
			Decorators: []string{"\033[1m"},
		},
		2,
		TrimRight,
		DrawInstruction{X: 43, Y: 6, Content: "hel"},
		"\033[6;43H\033[1mhello",
	},
	{
		"Trim 2 characters from the left of a Clear",
		ClearInstruction{X: 8, Y: 4, Size: 5},
		2,
		TrimLeft,
		ClearInstruction{X: 10, Y: 4, Size: 3},
		"\033[4;8H\033[0m     ",
	},
	{
		"Trim 2 characters from the right of a Clear",
		ClearInstruction{X: 98, Y: 76, Size: 5},
		2,
		TrimRight,
		ClearInstruction{X: 98, Y: 76, Size: 3},
		"\033[76;98H\033[0m     ",
	},
	{
		"Trim 10 characters from the left of a Clear of size 5",
		ClearInstruction{X: 42, Y: 69, Size: 5},
		10,
		TrimLeft,
		nil,
		"\033[69;42H\033[0m     ",
	},
	{
		"Trim 10 characters from the right of a Colored Draw of size 5",
		DrawInstruction{
			X:          420,
			Y:          1337,
			Content:    "hello",
			Decorators: []string{"\033[38;2;12;34;56m", "\033[48;2;98;76;54m"},
		},
		10,
		TrimRight,
		nil,
		"\033[1337;420H\033[38;2;12;34;56m\033[48;2;98;76;54mhello",
	},
}

type testOverlap struct {
	title       string
	over, under Instruction
	overlap     overlapType
	expected    []Instruction
}

var OverlapCases []testOverlap = []testOverlap{
	{
		"Two draws that perfectly overlap",
		DrawInstruction{X: 5, Y: 5, Content: "hello"},
		DrawInstruction{X: 5, Y: 5, Content: "wadup"},
		CoverTotal,
		[]Instruction{},
	},
	{
		"Draw + Clear that perfectly overlap",
		DrawInstruction{X: 5, Y: 5, Content: "hello"},
		ClearInstruction{X: 5, Y: 5, Size: 5},
		CoverTotal,
		[]Instruction{},
	},
	{
		"Two Clears that perfectly overlap",
		ClearInstruction{X: 5, Y: 5, Size: 5},
		ClearInstruction{X: 5, Y: 5, Size: 5},
		CoverTotal,
		[]Instruction{},
	},
	{
		"Draw 'More than covers' Clear",
		DrawInstruction{X: 5, Y: 5, Content: "hello"},
		ClearInstruction{X: 7, Y: 5, Size: 3},
		CoverTotal,
		[]Instruction{},
	},
	{
		"Clear Squashes Draw on the left",
		ClearInstruction{X: 2, Y: 5, Size: 5},
		DrawInstruction{X: 5, Y: 5, Content: "qwertyuiop"},
		CoverLeft,
		[]Instruction{
			DrawInstruction{X: 7, Y: 5, Content: "ertyuiop"},
		},
	},
	{
		"Clear Squashes Draw on the right",
		ClearInstruction{X: 5, Y: 5, Size: 10},
		DrawInstruction{X: 2, Y: 5, Content: "qwertyuiop"},
		CoverRight,
		[]Instruction{
			DrawInstruction{X: 2, Y: 5, Content: "qwe"},
		},
	},
	{
		"Draw is within another Draw",
		DrawInstruction{X: 7, Y: 5, Content: "yo"},
		DrawInstruction{X: 5, Y: 5, Content: "hello"},
		CoverWithin,
		[]Instruction{
			DrawInstruction{X: 5, Y: 5, Content: "he"},
			DrawInstruction{X: 9, Y: 5, Content: "o"},
		},
	},
	{
		"Clear is within another Draw",
		ClearInstruction{X: 10, Y: 5, Size: 5},
		DrawInstruction{X: 5, Y: 5, Content: "TheQuickBrownFoxJumpsOverTheLazyDog"},
		CoverWithin,
		[]Instruction{
			DrawInstruction{X: 5, Y: 5, Content: "TheQu"},
			DrawInstruction{X: 15, Y: 5, Content: "ownFoxJumpsOverTheLazyDog"},
		},
	},

	{
		"Clear split in half by a single character",
		DrawInstruction{X: 8, Y: 8, Content: "X"},
		ClearInstruction{X: 0, Y: 8, Size: 17},
		CoverWithin,
		[]Instruction{
			ClearInstruction{X: 0, Y: 8, Size: 8},
			ClearInstruction{X: 9, Y: 8, Size: 8},
		},
	},

	{
		"Two Draws that don't overlap but are on the same row",
		DrawInstruction{X: 5, Y: 5, Content: "hello"},
		DrawInstruction{X: 55, Y: 5, Content: "greetings"},
		CoverNone,
		[]Instruction{
			DrawInstruction{X: 55, Y: 5, Content: "greetings"},
		},
	},
	{
		"Two Clears on different rows",
		ClearInstruction{X: 5, Y: 5, Size: 5},
		ClearInstruction{X: 5, Y: 12, Size: 5},
		CoverNone,
		[]Instruction{
			ClearInstruction{X: 5, Y: 12, Size: 5},
		},
	},
}

func InstructionEquals(a, b Instruction) bool {
	var drwA, drwB DrawInstruction
	drwAOk, clrAOk := false, false
	var clrA, clrB ClearInstruction
	switch a := a.(type) {
	case DrawInstruction:
		drwA = a
		drwAOk = true
	case ClearInstruction:
		clrA = a
		clrAOk = true

	}
	switch b := b.(type) {
	case DrawInstruction:
		drwB = b
		if clrAOk { // A is of opposite type (clear)
			return false
		}
	case ClearInstruction:
		clrB = b
		if drwAOk { // A is of opposite type (draw)
			return false
		}
	}
	// If both are draws
	if drwAOk {
		if drwA.Content != drwB.Content {
			return false
		}
		if drwA.X != drwB.X || drwA.Y != drwB.Y {
			return false
		}
	}
	// If both are clears
	if clrAOk {
		if clrA.Size != clrB.Size {
			return false
		}
		if clrA.X != clrB.X || clrA.Y != clrB.Y {
			return false
		}
	}
	return true
}

func TestTrim(t *testing.T) {
	for _, tc := range TrimWriteCases {
		result := tc.input.Trim(tc.n, tc.d)
		if !InstructionEquals(result, tc.expectedTrim) {
			t.Errorf(errFmt, tc.title, tc.expectedTrim, result)
		}
		t.Logf(passFmt, tc.title)
	}
}

func TestOverlap(t *testing.T) {
	for _, tc := range OverlapCases {
		if ot := overlap(tc.over, tc.under); ot != tc.overlap {
			t.Errorf(errFmt, tc.title, tc.overlap, ot)
		}
		t.Logf(passFmt, tc.title)
	}
}

func TestSquash(t *testing.T) {
	for _, tc := range OverlapCases {
		res := squash(tc.over, tc.under, tc.overlap)
		if len(res) != len(tc.expected) {
			t.Errorf(errFmt, tc.title, len(tc.expected), len(res))
		}
		for i, r := range res {
			if !InstructionEquals(r, tc.expected[i]) {
				t.Errorf(errFmt, tc.title, tc.expected[i], r)
			}
		}
		t.Logf(passFmt, tc.title)
	}
}

func TestWrite(t *testing.T) {
	for _, tt := range TrimWriteCases {
		var buf bytes.Buffer
		tt.input.Write(&buf)
		if buf.String() != tt.expectedBuf {
			t.Errorf(errFmt, tt.title, tt.expectedBuf, buf.String())
		}
	}
}