-
Notifications
You must be signed in to change notification settings - Fork 5
/
testHelpers.go
185 lines (151 loc) · 4.54 KB
/
testHelpers.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package dxf
import (
"fmt"
"reflect"
"strings"
"testing"
)
func roundTripDrawing(t *testing.T, d *Drawing) (result Drawing) {
s := d.String()
result = parse(t, s)
return
}
func assert(t *testing.T, condition bool, message string) {
if !condition {
t.Error(message)
}
}
func assertContains(t *testing.T, expected, actual string) {
if !strings.Contains(actual, expected) {
t.Errorf("Unable to find '%s' in '%s'", expected, actual)
}
}
func assertNotContains(t *testing.T, notExpected, actual string) {
if strings.Contains(actual, notExpected) {
t.Errorf("Unexpectedly found '%s' in '%s'", notExpected, actual)
}
}
func assertEqCodePairs(t *testing.T, expected, actual []CodePair) {
isCorrect := len(actual) == len(expected)
actualText := ""
for _, p := range actual {
actualText += p.String() + "\n"
}
expectedText := ""
for _, p := range expected {
expectedText += p.String() + "\n"
}
for i := 0; i < len(actual); i++ {
isCorrect = isCorrect && reflect.DeepEqual(actual[i], expected[i])
}
if !isCorrect {
t.Errorf("Expected code pairs [\n%s] but found [\n%s]", expectedText, actualText)
}
}
func assertContainsCodePairs(t *testing.T, expected, actual []CodePair) {
for i := 0; i < len(actual)-len(expected)+1; i++ {
match := true
for j := 0; j < len(expected) && match; j++ {
match = match && reflect.DeepEqual(actual[i+j], expected[j])
}
if match {
return
}
}
expectedText := "\n"
for _, p := range expected {
expectedText += p.String() + "\n"
}
actualText := "\n"
for _, p := range actual {
actualText += p.String() + "\n"
}
t.Errorf("Unable to find %s in %s", expectedText, actualText)
}
func assertNotContainsCodePairs(t *testing.T, notExpected, actual []CodePair) {
for i := 0; i < len(actual)-len(notExpected)+1; i++ {
match := false
for j := 0; j < len(notExpected) && !match; j++ {
match = match || reflect.DeepEqual(actual[i+j], notExpected[j])
}
if match {
notExpectedText := "\n"
for _, p := range notExpected {
notExpectedText += p.String() + "\n"
}
actualText := "\n"
for _, p := range actual {
actualText += p.String() + "\n"
}
t.Errorf("Unexpectedly found %s in %s", notExpectedText, actualText)
}
}
}
func assertEqShort(t *testing.T, expected, actual int16) {
assert(t, expected == actual, fmt.Sprintf(expectedActualString("d"), expected, actual))
}
func assertEqInt(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("Expected: %d\nActual: %d", expected, actual)
}
}
func assertEqFloat64(t *testing.T, expected, actual float64) {
assert(t, expected == actual, fmt.Sprintf(expectedActualString("f"), expected, actual))
}
func assertEqPoint(t *testing.T, expected, actual Point) {
if expected != actual {
t.Errorf("Expected: %s\nActual: %s", expected.String(), actual.String())
}
}
func assertEqVector(t *testing.T, expected, actual Vector) {
if expected != actual {
t.Errorf("Expected: %s\nActual: %s", expected.String(), actual.String())
}
}
func assertEqString(t *testing.T, expected, actual string) {
if expected != actual {
t.Errorf("Expected: %s\nActual: %s", expected, actual)
}
}
func assertEqUInt64(t *testing.T, expected, actual uint64) {
assert(t, expected == actual, fmt.Sprintf(expectedActualString("X"), expected, actual))
}
func assertEqByteArray(t *testing.T, expected, actual []byte) {
assert(t, len(expected) == len(actual), fmt.Sprintf(expectedActualString("d"), len(expected), len(actual)))
for i := range expected {
assert(t, expected[i] == actual[i], fmt.Sprintf("Difference at offset %d: "+expectedActualString("x"), i, expected[i], actual[i]))
}
}
func join(vals ...string) string {
return strings.Join(vals, "\r\n")
}
func parse(t *testing.T, content string) Drawing {
drawing, err := ParseDrawing(strings.TrimSpace(content))
if err != nil {
t.Error(err)
}
return drawing
}
func parseFromCodePairs(t *testing.T, codePairs ...CodePair) Drawing {
drawing, err := ParseDrawingFromCodePairs(codePairs...)
if err != nil {
t.Error(err)
}
return drawing
}
func expectedActualString(placeholder string) string {
return fmt.Sprintf("Expected: %%%s\nActual %%%s", placeholder, placeholder)
}
func drawingCodePairs(t *testing.T, drawing Drawing) (codePairs []CodePair) {
codePairs, err := drawing.CodePairs()
if err != nil {
t.Error(err)
}
return
}
func drawingCodePairsFromEntity(t *testing.T, entity Entity, version AcadVersion) (codePairs []CodePair) {
drawing := *NewDrawing()
drawing.Header.Version = version
drawing.Entities = append(drawing.Entities, entity)
return drawingCodePairs(t, drawing)
}