Debian Codegraph @

internal/test/golden_test.go

raw
1 package test
2
3 import "testing"
4
5 func TestEqualStrings(t *testing.T) {
6 got := make([]string, 0)
7 want := make([]string, 0)
8 mockt := &testing.T{}
9 DiffStrings(mockt, got, want)
10 if mockt.Failed() {
11 t.Error("Two empty slices should succeed")
12 }
13
14 got = []string{"foo"}
15 want = []string{"foo"}
16 mockt = &testing.T{}
17 DiffStrings(mockt, got, want)
18 if mockt.Failed() {
19 t.Error("Two equal single line slices should succeed")
20 }
21
22 got = []string{"foo", "bar", "baz", "enmime", "1", "2", "3", "4", "5", ""}
23 want = []string{"foo", "bar", "baz", "enmime", "1", "2", "3", "4", "5", ""}
24 mockt = &testing.T{}
25 DiffStrings(mockt, got, want)
26 if mockt.Failed() {
27 t.Error("Two equal multiline slices should succeed")
28 }
29 }
30
31 func TestDifferingStrings(t *testing.T) {
32 got := []string{"foo"}
33 want := []string{"bar"}
34 mockt := &testing.T{}
35 DiffStrings(mockt, got, want)
36 if !mockt.Failed() {
37 t.Error("Two differing single line slices should fail")
38 }
39
40 got = []string{"foo", "bar", "baz", "enmime", "1", "2", "3", "4", "5", ""}
41 want = []string{"foo", "bar", "baz", "inbucket", "1", "2", "3", "4", "5", ""}
42 mockt = &testing.T{}
43 DiffStrings(mockt, got, want)
44 if !mockt.Failed() {
45 t.Error("Two differing multiline slices should fail")
46 }
47 }
48
49 func TestEqualLines(t *testing.T) {
50 got := make([]byte, 0)
51 want := make([]byte, 0)
52 mockt := &testing.T{}
53 DiffLines(mockt, got, want)
54 if mockt.Failed() {
55 t.Error("Two empty slices should succeed")
56 }
57
58 got = []byte("foo\n")
59 want = []byte("foo\n")
60 mockt = &testing.T{}
61 DiffLines(mockt, got, want)
62 if mockt.Failed() {
63 t.Error("Two equal single line slices should succeed")
64 }
65
66 got = []byte("foo\nbar\nbaz\nenmime\n1\n2\n3\n4\n5\n")
67 want = []byte("foo\nbar\nbaz\nenmime\n1\n2\n3\n4\n5\n")
68 mockt = &testing.T{}
69 DiffLines(mockt, got, want)
70 if mockt.Failed() {
71 t.Error("Two equal multiline slices should succeed")
72 }
73 }
74
75 func TestDifferingLines(t *testing.T) {
76 got := []byte("foo")
77 want := []byte("bar")
78 mockt := &testing.T{}
79 DiffLines(mockt, got, want)
80 if !mockt.Failed() {
81 t.Error("Two differing single line slices should fail")
82 }
83
84 got = []byte("foo\n")
85 want = []byte("bar\n")
86 mockt = &testing.T{}
87 DiffLines(mockt, got, want)
88 if !mockt.Failed() {
89 t.Error("Two differing single line slices should fail")
90 }
91
92 got = []byte("foo\nbar\nbaz\nenmime\n1\n2\n3\n4\n5\n")
93 want = []byte("foo\nbar\nbaz\ninbucket\n1\n2\n3\n4\n5\n")
94 mockt = &testing.T{}
95 DiffLines(mockt, got, want)
96 if !mockt.Failed() {
97 t.Error("Two differing multiline slices should fail")
98 }
99
100 // Test missing EOL
101 got = []byte("foo\nenmime")
102 want = []byte("foo\ninbucket")
103 mockt = &testing.T{}
104 DiffLines(mockt, got, want)
105 if !mockt.Failed() {
106 t.Error("Two differing no-EOL slices should fail")
107 }
108 }
109
110 func TestGoldenMissing(t *testing.T) {
111 mockt := &testing.T{}
112 done := make(chan struct{})
113 go func() {
114 // t.Fatal exits current goroutine, calling deferrals
115 defer close(done)
116 DiffGolden(mockt, []byte{}, "zzzDOESNTEXIST")
117 }()
118 <-done
119 if !mockt.Failed() {
120 t.Error("Missing golden file should fail test")
121 }
122 }
123
124 func TestGolden(t *testing.T) {
125 mockt := &testing.T{}
126 DiffGolden(mockt, []byte("one\n"), "testdata", "test.golden")
127 if !mockt.Failed() {
128 t.Error("Differing bytes in golden file should fail test")
129 }
130
131 mockt = &testing.T{}
132 DiffGolden(mockt, []byte("one\ntwo\nthree\n"), "testdata", "test.golden")
133 if mockt.Failed() {
134 t.Error("Same bytes in golden file should not fail test")
135 }
136 }