-
Notifications
You must be signed in to change notification settings - Fork 75
/
writer_test.go
161 lines (139 loc) · 3.91 KB
/
writer_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
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
package mailyak
import (
"bytes"
"fmt"
"io"
"net/smtp"
"strings"
"testing"
)
// TestHTML ensures we can write to HTML as an io.Writer
func TestHTML(t *testing.T) {
t.Parallel()
tests := []struct {
name string
// Parameters.
data []string
}{
{
"Writer test",
[]string{"Worst idea since someone said ‘yeah let’s take this suspiciously large " +
"wooden horse into Troy, statues are all the rage this season’.",
},
},
{
"Writer test multiple",
[]string{
"Worst idea since someone said ‘yeah let’s take this suspiciously large " +
"wooden horse into Troy, statues are all the rage this season’.",
"Am I jumping the gun, Baldrick, or are the words 'I have a cunning plan' " +
"marching with ill-deserved confidence in the direction of this conversation?",
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mail := New("", smtp.PlainAuth("", "", "", ""))
for _, data := range tt.data {
if _, err := io.WriteString(mail.HTML(), data); err != nil {
t.Errorf("%q. HTML() error = %v", tt.name, err)
continue
}
}
if !bytes.Equal([]byte(strings.Join(tt.data, "")), mail.html.Bytes()) {
t.Errorf("%q. HTML() = %v, want %v", tt.name, mail.html.String(), tt.data)
}
})
}
}
// TestPlain ensures we can write to Plain as an io.Writer
func TestPlain(t *testing.T) {
t.Parallel()
tests := []struct {
name string
// Parameters.
data string
}{
{
"Writer test",
"Am I jumping the gun, Baldrick, or are the words 'I have a cunning plan' " +
"marching with ill-deserved confidence in the direction of this conversation?",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mail := New("", smtp.PlainAuth("", "", "", ""))
if _, err := io.WriteString(mail.Plain(), tt.data); err != nil {
t.Fatalf("%q. Plain() error = %v", tt.name, err)
}
if !bytes.Equal([]byte(tt.data), mail.plain.Bytes()) {
t.Errorf("%q. Plain() = %v, want %v", tt.name, mail.plain.String(), tt.data)
}
})
}
}
// TestWritableString ensures the writable type returns a string when called
// with fmt.Printx(), etc
func TestWritableString(t *testing.T) {
t.Parallel()
tests := []struct {
name string
// Parameters.
data string
}{
{
"String test",
"Baldrick, does it have to be this way? " +
"Our valued friendship ending with me cutting you up into strips and telling " +
"the prince that you walked over a very sharp cattle grid in an extremely heavy hat?",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mail := New("", smtp.PlainAuth("", "", "", ""))
if _, err := io.WriteString(mail.Plain(), tt.data); err != nil {
t.Fatalf("%q. Plain() error = %v", tt.name, err)
}
if tt.data != mail.plain.String() {
t.Errorf("%q. writable.String() = %v, want %v", tt.name, mail.plain.String(), tt.data)
}
if out := fmt.Sprintf("%v", mail.plain.String()); out != tt.data {
t.Errorf("%q. writable.String() via fmt.Sprintf = %v, want %v", tt.name, out, tt.data)
}
})
}
}
// TestPlain_String ensures we can use the string setter
func TestPlain_String(t *testing.T) {
t.Parallel()
tests := []struct {
name string
// Parameters.
data string
}{
{
"Writer test",
"Am I jumping the gun, Baldrick, or are the words 'I have a cunning plan' " +
"marching with ill-deserved confidence in the direction of this conversation?",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mail := New("", smtp.PlainAuth("", "", "", ""))
if _, err := io.WriteString(mail.Plain(), tt.data); err != nil {
t.Fatalf("%q. Plain() error = %v", tt.name, err)
}
if !bytes.Equal([]byte(tt.data), mail.plain.Bytes()) {
t.Errorf("%q. Plain() = %v, want %v", tt.name, mail.plain.String(), tt.data)
}
})
}
}