forked from a-h/generate
-
Notifications
You must be signed in to change notification settings - Fork 3
/
output_test.go
91 lines (81 loc) · 2.14 KB
/
output_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
package generate
import (
"reflect"
"strings"
"testing"
)
func TestThatFieldNamesAreOrdered(t *testing.T) {
m := map[string]Field{
"z": {},
"b": {},
}
actual := getOrderedFieldNames(m)
expected := []string{"b", "z"}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("expected %s and actual %s should match in order", strings.Join(expected, ", "), strings.Join(actual, ","))
}
}
func TestThatStructNamesAreOrdered(t *testing.T) {
m := map[string]Struct{
"c": {},
"b": {},
"a": {},
}
actual := getOrderedStructNames(m)
expected := []string{"a", "b", "c"}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("expected %s and actual %s should match in order", strings.Join(expected, ", "), strings.Join(actual, ","))
}
}
func TestLineAndCharacterFromOffset(t *testing.T) {
tests := []struct {
In []byte
Offset int
ExpectedLine int
ExpectedCharacter int
ExpectedError bool
}{
{
In: []byte("Line 1\nLine 2"),
Offset: 6,
ExpectedLine: 2,
ExpectedCharacter: 1,
},
{
In: []byte("Line 1\r\nLine 2"),
Offset: 7,
ExpectedLine: 2,
ExpectedCharacter: 1,
},
{
In: []byte("Line 1\nLine 2"),
Offset: 0,
ExpectedLine: 1,
ExpectedCharacter: 1,
},
{
In: []byte("Line 1\nLine 2"),
Offset: 200,
ExpectedLine: 0,
ExpectedCharacter: 0,
ExpectedError: true,
},
{
In: []byte("Line 1\nLine 2"),
Offset: -1,
ExpectedLine: 0,
ExpectedCharacter: 0,
ExpectedError: true,
},
}
for _, test := range tests {
actualLine, actualCharacter, err := lineAndCharacter(test.In, test.Offset)
if err != nil && !test.ExpectedError {
t.Errorf("Unexpected error for input %s at offset %d: %v", test.In, test.Offset, err)
continue
}
if actualLine != test.ExpectedLine || actualCharacter != test.ExpectedCharacter {
t.Errorf("For '%s' at offset %d, expected %d:%d, but got %d:%d", test.In, test.Offset, test.ExpectedLine, test.ExpectedCharacter, actualLine, actualCharacter)
}
}
}