Skip to content

Commit 15caea0

Browse files
committed
Closes #60. Added final test coverage and an additional SetContent method
1 parent dbe5874 commit 15caea0

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

part.go

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func (p *Part) GetWriteFunc() func(io.Writer) (int64, error) {
4343
return p.w
4444
}
4545

46+
// SetContent overrides the content of the Part with the given string
47+
func (p *Part) SetContent(c string) {
48+
buf := bytes.NewBufferString(c)
49+
p.w = writeFuncFromBuffer(buf)
50+
}
51+
4652
// SetContentType overrides the ContentType of the Part
4753
func (p *Part) SetContentType(c ContentType) {
4854
p.ctype = c

part_test.go

+122
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package mail
77
import (
88
"bytes"
99
"fmt"
10+
"io"
11+
"strings"
1012
"testing"
1113
)
1214

@@ -43,6 +45,35 @@ func TestPartEncoding(t *testing.T) {
4345
}
4446
}
4547

48+
// TestPartContentType tests Part.SetContentType
49+
func TestPart_SetContentType(t *testing.T) {
50+
tests := []struct {
51+
name string
52+
ct ContentType
53+
want string
54+
}{
55+
{"ContentType: text/plain", TypeTextPlain, "text/plain"},
56+
{"ContentType: text/html", TypeTextHTML, "text/html"},
57+
{"ContentType: application/json", "application/json", "application/json"},
58+
}
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
m := NewMsg()
62+
m.SetBodyString(TypeTextPlain, "This is a test with ümläutß")
63+
pl, err := getPartList(m)
64+
if err != nil {
65+
t.Errorf("failed: %s", err)
66+
return
67+
}
68+
pl[0].SetContentType(tt.ct)
69+
ct := pl[0].GetContentType()
70+
if string(ct) != tt.want {
71+
t.Errorf("SetContentType failed. Got: %s, expected: %s", string(ct), tt.want)
72+
}
73+
})
74+
}
75+
}
76+
4677
// TestPartEncoding tests Part.GetEncoding
4778
func TestPart_GetEncoding(t *testing.T) {
4879
tests := []struct {
@@ -119,6 +150,97 @@ func TestPart_GetWriteFunc(t *testing.T) {
119150
}
120151
}
121152

153+
// TestPart_GetContent tests Part.GetContent
154+
func TestPart_GetContent(t *testing.T) {
155+
c := "This is a test with ümläutß"
156+
m := NewMsg()
157+
m.SetBodyString(TypeTextPlain, c)
158+
pl, err := getPartList(m)
159+
if err != nil {
160+
t.Errorf("failed: %s", err)
161+
return
162+
}
163+
cb, err := pl[0].GetContent()
164+
if err != nil {
165+
t.Errorf("Part.GetContent failed: %s", err)
166+
}
167+
if string(cb) != c {
168+
t.Errorf("Part.GetContent failed. Expected: %s, got: %s", c, string(cb))
169+
}
170+
}
171+
172+
// TestPart_GetContentBroken tests Part.GetContent
173+
func TestPart_GetContentBroken(t *testing.T) {
174+
c := "This is a test with ümläutß"
175+
m := NewMsg()
176+
m.SetBodyString(TypeTextPlain, c)
177+
pl, err := getPartList(m)
178+
if err != nil {
179+
t.Errorf("failed: %s", err)
180+
return
181+
}
182+
pl[0].w = func(io.Writer) (int64, error) {
183+
return 0, fmt.Errorf("broken")
184+
}
185+
_, err = pl[0].GetContent()
186+
if err == nil {
187+
t.Errorf("Part.GetContent was supposed to failed, but didn't")
188+
}
189+
}
190+
191+
// TestPart_SetWriteFunc tests Part.SetWriteFunc
192+
func TestPart_SetWriteFunc(t *testing.T) {
193+
c := "This is a test with ümläutß"
194+
m := NewMsg()
195+
m.SetBodyString(TypeTextPlain, c)
196+
pl, err := getPartList(m)
197+
if err != nil {
198+
t.Errorf("failed: %s", err)
199+
return
200+
}
201+
cb, err := pl[0].GetContent()
202+
if err != nil {
203+
t.Errorf("Part.GetContent failed: %s", err)
204+
}
205+
pl[0].SetWriteFunc(func(w io.Writer) (int64, error) {
206+
ns := strings.ToUpper(string(cb))
207+
buf := bytes.NewBufferString(ns)
208+
nb, err := w.Write(buf.Bytes())
209+
return int64(nb), err
210+
})
211+
nc, err := pl[0].GetContent()
212+
if err != nil {
213+
t.Errorf("Part.GetContent failed: %s", err)
214+
}
215+
if string(nc) != strings.ToUpper(c) {
216+
t.Errorf("SetWriteFunc failed. Expected: %s, got: %s", strings.ToUpper(c), string(nc))
217+
}
218+
}
219+
220+
// TestPart_SetContent tests Part.SetContent
221+
func TestPart_SetContent(t *testing.T) {
222+
c := "This is a test with ümläutß"
223+
m := NewMsg()
224+
m.SetBodyString(TypeTextPlain, c)
225+
pl, err := getPartList(m)
226+
if err != nil {
227+
t.Errorf("failed: %s", err)
228+
return
229+
}
230+
cb, err := pl[0].GetContent()
231+
if err != nil {
232+
t.Errorf("Part.GetContent failed: %s", err)
233+
}
234+
pl[0].SetContent(strings.ToUpper(string(cb)))
235+
nc, err := pl[0].GetContent()
236+
if err != nil {
237+
t.Errorf("Part.GetContent failed: %s", err)
238+
}
239+
if string(nc) != strings.ToUpper(c) {
240+
t.Errorf("SetContent failed. Expected: %s, got: %s", strings.ToUpper(c), string(nc))
241+
}
242+
}
243+
122244
// getPartList is a helper function
123245
func getPartList(m *Msg) ([]*Part, error) {
124246
pl := m.GetParts()

0 commit comments

Comments
 (0)