Debian Codegraph @

internal/test/testing.go

raw
1 package test
2
3 import (
4 "bytes"
5 "io"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "github.com/jhillyerd/enmime"
12 )
13
14 // PartExists indicates to ComparePart that this part is expect to exist
15 var PartExists = &enmime.Part{}
16
17 // OpenTestData is a utility function to open a file in testdata for reading, it will panic if there
18 // is an error.
19 func OpenTestData(subdir, filename string) io.Reader {
20 // Open test part for parsing
21 raw, err := os.Open(filepath.Join("testdata", subdir, filename))
22 if err != nil {
23 // err already contains full path to file
24 panic(err)
25 }
26 return raw
27 }
28
29 // ComparePart test helper compares the attributes of two parts, returning true if they are equal.
30 // t.Errorf() will be called for each field that is not equal. The presence of child and siblings
31 // will be checked, but not the attributes of them. Header, Errors and unexported fields are
32 // ignored.
33 func ComparePart(t *testing.T, got *enmime.Part, want *enmime.Part) (equal bool) {
34 t.Helper()
35 if got == nil && want != nil {
36 t.Error("Part == nil, want not nil")
37 return
38 }
39 if got != nil && want == nil {
40 t.Error("Part != nil, want nil")
41 return
42 }
43 equal = true
44 if got == nil && want == nil {
45 return
46 }
47 if (got.Parent == nil) != (want.Parent == nil) {
48 equal = false
49 gs := "nil"
50 ws := "nil"
51 if got.Parent != nil {
52 gs = "present"
53 }
54 if want.Parent != nil {
55 ws = "present"
56 }
57 t.Errorf("Part.Parent == %q, want: %q", gs, ws)
58 }
59 if (got.FirstChild == nil) != (want.FirstChild == nil) {
60 equal = false
61 gs := "nil"
62 ws := "nil"
63 if got.FirstChild != nil {
64 gs = "present"
65 }
66 if want.FirstChild != nil {
67 ws = "present"
68 }
69 t.Errorf("Part.FirstChild == %q, want: %q", gs, ws)
70 }
71 if (got.NextSibling == nil) != (want.NextSibling == nil) {
72 equal = false
73 gs := "nil"
74 ws := "nil"
75 if got.NextSibling != nil {
76 gs = "present"
77 }
78 if want.NextSibling != nil {
79 ws = "present"
80 }
81 t.Errorf("Part.NextSibling == %q, want: %q", gs, ws)
82 }
83 if got.ContentType != want.ContentType {
84 equal = false
85 t.Errorf("Part.ContentType == %q, want: %q", got.ContentType, want.ContentType)
86 }
87 if got.Disposition != want.Disposition {
88 equal = false
89 t.Errorf("Part.Disposition == %q, want: %q", got.Disposition, want.Disposition)
90 }
91 if got.FileName != want.FileName {
92 equal = false
93 t.Errorf("Part.FileName == %q, want: %q", got.FileName, want.FileName)
94 }
95 if got.Charset != want.Charset {
96 equal = false
97 t.Errorf("Part.Charset == %q, want: %q", got.Charset, want.Charset)
98 }
99 if got.PartID != want.PartID {
100 equal = false
101 t.Errorf("Part.PartID == %q, want: %q", got.PartID, want.PartID)
102 }
103
104 return
105 }
106
107 // ContentContainsString checks if the provided readers content contains the specified substring
108 func ContentContainsString(t *testing.T, b []byte, substr string) {
109 t.Helper()
110 got := string(b)
111 if !strings.Contains(got, substr) {
112 t.Errorf("content == %q, should contain: %q", got, substr)
113 }
114 }
115
116 // ContentEqualsString checks if the provided readers content is the specified string
117 func ContentEqualsString(t *testing.T, b []byte, str string) {
118 t.Helper()
119 got := string(b)
120 if got != str {
121 t.Errorf("content == %q, want: %q", got, str)
122 }
123 }
124
125 // ContentEqualsBytes checks if the provided readers content is the specified []byte
126 func ContentEqualsBytes(t *testing.T, b []byte, want []byte) {
127 t.Helper()
128 if !bytes.Equal(b, want) {
129 t.Errorf("content:\n%v, want:\n%v", b, want)
130 }
131 }
132
133 // CompareEnvelope test helper compares the attributes of two envelopes, returning true if they are equal.
134 // t.Errorf() will be called for each field that is not equal. The presence of child and siblings
135 // will be checked, but not the attributes of them. Unexported fields are
136 // ignored.
137 func CompareEnvelope(t *testing.T, got *enmime.Envelope, want *enmime.Envelope) (equal bool) {
138 t.Helper()
139 if got == nil && want != nil {
140 t.Error("Envelope == nil, want not nil")
141 return
142 }
143 if got != nil && want == nil {
144 t.Error("Envelope != nil, want nil")
145 return
146 }
147 equal = true
148 if got == nil && want == nil {
149 return
150 }
151 if !ComparePart(t, got.Root, want.Root) {
152 equal = false
153 t.Error("Envelope.Root mismatch between envelopes")
154 }
155 if got.Text != want.Text {
156 equal = false
157 t.Errorf("Envelope.Text == %q, want: %q", got.Text, want.Text)
158 }
159 if got.HTML != want.HTML {
160 equal = false
161 t.Errorf("Envelope.HTML == %q, want: %q", got.HTML, want.HTML)
162 }
163 if len(got.Attachments) != len(want.Attachments) {
164 equal = false
165 t.Errorf("Envelope.Attachments has %q elements, want: %q", len(got.Attachments), len(want.Attachments))
166 }
167 if len(got.Inlines) != len(want.Inlines) {
168 equal = false
169 t.Errorf("Envelope.Inlines has %q elements, want: %q", len(got.Inlines), len(want.Inlines))
170 }
171 if len(got.OtherParts) != len(want.OtherParts) {
172 equal = false
173 t.Errorf("Envelope.OtherParts has %q elements, want: %q", len(got.OtherParts), len(want.OtherParts))
174 }
175 if len(got.Errors) != len(want.Errors) {
176 equal = false
177 t.Errorf("Envelope.Errors has %q elements, want: %q", len(got.Errors), len(want.Errors))
178 }
179
180 return
181 }