| 1 |
package mailyak |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"net/smtp" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
// TestHTML ensures we can write to HTML as an io.Writer |
| 13 |
func TestHTML(t *testing.T) { |
| 14 |
t.Parallel() |
| 15 |
|
| 16 |
tests := []struct { |
| 17 |
name string |
| 18 |
// Parameters. |
| 19 |
data []string |
| 20 |
}{ |
| 21 |
{ |
| 22 |
"Writer test", |
| 23 |
[]string{"Worst idea since someone said ‘yeah let’s take this suspiciously large " + |
| 24 |
"wooden horse into Troy, statues are all the rage this season’.", |
| 25 |
}, |
| 26 |
}, |
| 27 |
{ |
| 28 |
"Writer test multiple", |
| 29 |
[]string{ |
| 30 |
"Worst idea since someone said ‘yeah let’s take this suspiciously large " + |
| 31 |
"wooden horse into Troy, statues are all the rage this season’.", |
| 32 |
"Am I jumping the gun, Baldrick, or are the words 'I have a cunning plan' " + |
| 33 |
"marching with ill-deserved confidence in the direction of this conversation?", |
| 34 |
}, |
| 35 |
}, |
| 36 |
} |
| 37 |
for _, tt := range tests { |
| 38 |
tt := tt |
| 39 |
t.Run(tt.name, func(t *testing.T) { |
| 40 |
t.Parallel() |
| 41 |
|
| 42 |
mail := New("", smtp.PlainAuth("", "", "", "")) |
| 43 |
|
| 44 |
for _, data := range tt.data { |
| 45 |
if _, err := io.WriteString(mail.HTML(), data); err != nil { |
| 46 |
t.Errorf("%q. HTML() error = %v", tt.name, err) |
| 47 |
continue |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if !bytes.Equal([]byte(strings.Join(tt.data, "")), mail.html.Bytes()) { |
| 52 |
t.Errorf("%q. HTML() = %v, want %v", tt.name, mail.html.String(), tt.data) |
| 53 |
} |
| 54 |
}) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// TestPlain ensures we can write to Plain as an io.Writer |
| 59 |
func TestPlain(t *testing.T) { |
| 60 |
t.Parallel() |
| 61 |
|
| 62 |
tests := []struct { |
| 63 |
name string |
| 64 |
// Parameters. |
| 65 |
data string |
| 66 |
}{ |
| 67 |
{ |
| 68 |
"Writer test", |
| 69 |
"Am I jumping the gun, Baldrick, or are the words 'I have a cunning plan' " + |
| 70 |
"marching with ill-deserved confidence in the direction of this conversation?", |
| 71 |
}, |
| 72 |
} |
| 73 |
for _, tt := range tests { |
| 74 |
tt := tt |
| 75 |
t.Run(tt.name, func(t *testing.T) { |
| 76 |
t.Parallel() |
| 77 |
|
| 78 |
mail := New("", smtp.PlainAuth("", "", "", "")) |
| 79 |
|
| 80 |
if _, err := io.WriteString(mail.Plain(), tt.data); err != nil { |
| 81 |
t.Fatalf("%q. Plain() error = %v", tt.name, err) |
| 82 |
} |
| 83 |
|
| 84 |
if !bytes.Equal([]byte(tt.data), mail.plain.Bytes()) { |
| 85 |
t.Errorf("%q. Plain() = %v, want %v", tt.name, mail.plain.String(), tt.data) |
| 86 |
} |
| 87 |
}) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// TestWritableString ensures the writable type returns a string when called |
| 92 |
// with fmt.Printx(), etc |
| 93 |
func TestWritableString(t *testing.T) { |
| 94 |
t.Parallel() |
| 95 |
|
| 96 |
tests := []struct { |
| 97 |
name string |
| 98 |
// Parameters. |
| 99 |
data string |
| 100 |
}{ |
| 101 |
{ |
| 102 |
"String test", |
| 103 |
"Baldrick, does it have to be this way? " + |
| 104 |
"Our valued friendship ending with me cutting you up into strips and telling " + |
| 105 |
"the prince that you walked over a very sharp cattle grid in an extremely heavy hat?", |
| 106 |
}, |
| 107 |
} |
| 108 |
for _, tt := range tests { |
| 109 |
tt := tt |
| 110 |
t.Run(tt.name, func(t *testing.T) { |
| 111 |
t.Parallel() |
| 112 |
|
| 113 |
mail := New("", smtp.PlainAuth("", "", "", "")) |
| 114 |
|
| 115 |
if _, err := io.WriteString(mail.Plain(), tt.data); err != nil { |
| 116 |
t.Fatalf("%q. Plain() error = %v", tt.name, err) |
| 117 |
} |
| 118 |
|
| 119 |
if tt.data != mail.plain.String() { |
| 120 |
t.Errorf("%q. writable.String() = %v, want %v", tt.name, mail.plain.String(), tt.data) |
| 121 |
} |
| 122 |
|
| 123 |
if out := fmt.Sprintf("%v", mail.plain.String()); out != tt.data { |
| 124 |
t.Errorf("%q. writable.String() via fmt.Sprintf = %v, want %v", tt.name, out, tt.data) |
| 125 |
} |
| 126 |
}) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// TestPlain_String ensures we can use the string setter |
| 131 |
func TestPlain_String(t *testing.T) { |
| 132 |
t.Parallel() |
| 133 |
|
| 134 |
tests := []struct { |
| 135 |
name string |
| 136 |
// Parameters. |
| 137 |
data string |
| 138 |
}{ |
| 139 |
{ |
| 140 |
"Writer test", |
| 141 |
"Am I jumping the gun, Baldrick, or are the words 'I have a cunning plan' " + |
| 142 |
"marching with ill-deserved confidence in the direction of this conversation?", |
| 143 |
}, |
| 144 |
} |
| 145 |
for _, tt := range tests { |
| 146 |
tt := tt |
| 147 |
t.Run(tt.name, func(t *testing.T) { |
| 148 |
t.Parallel() |
| 149 |
|
| 150 |
mail := New("", smtp.PlainAuth("", "", "", "")) |
| 151 |
|
| 152 |
if _, err := io.WriteString(mail.Plain(), tt.data); err != nil { |
| 153 |
t.Fatalf("%q. Plain() error = %v", tt.name, err) |
| 154 |
} |
| 155 |
|
| 156 |
if !bytes.Equal([]byte(tt.data), mail.plain.Bytes()) { |
| 157 |
t.Errorf("%q. Plain() = %v, want %v", tt.name, mail.plain.String(), tt.data) |
| 158 |
} |
| 159 |
}) |
| 160 |
} |
| 161 |
} |