Add charset related tests and functionality

New unit tests have been introduced for WithPartCharset and SetCharset methods in part_test.go. Also, a GetCharset method has been implemented in part.go. These modifications ensure the robustness of charset handling within the application.
This commit is contained in:
Winni Neessen 2024-02-05 11:41:10 +01:00
parent ef86a1a1fb
commit 68088577ba
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
2 changed files with 61 additions and 0 deletions

View file

@ -31,6 +31,11 @@ func (p *Part) GetContent() ([]byte, error) {
return b.Bytes(), nil
}
// GetCharset returns the currently set Charset of the Part
func (p *Part) GetCharset() Charset {
return p.cset
}
// GetContentType returns the currently set ContentType of the Part
func (p *Part) GetContentType() ContentType {
return p.ctype

View file

@ -45,6 +45,33 @@ func TestPartEncoding(t *testing.T) {
}
}
// TestWithPartCharset tests the WithPartCharset method
func TestWithPartCharset(t *testing.T) {
tests := []struct {
name string
cs Charset
want string
}{
{"Part charset: UTF-8", CharsetUTF8, "UTF-8"},
{"Part charset: ISO-8859-1", CharsetISO88591, "ISO-8859-1"},
{"Part charset: empty", "", ""},
}
for _, tt := range tests {
m := NewMsg()
t.Run(tt.name, func(t *testing.T) {
part := m.newPart(TypeTextPlain, WithPartCharset(tt.cs), nil)
if part == nil {
t.Errorf("newPart() WithPartCharset() failed: no part returned")
return
}
if part.cset.String() != tt.want {
t.Errorf("newPart() WithPartCharset() failed: expected charset: %s, got: %s",
tt.want, part.cset.String())
}
})
}
}
// TestPart_WithPartContentDescription tests the WithPartContentDescription method
func TestPart_WithPartContentDescription(t *testing.T) {
tests := []struct {
@ -320,3 +347,32 @@ func getPartList(m *Msg) ([]*Part, error) {
}
return pl, nil
}
// TestPart_SetCharset tests Part.SetCharset method
func TestPart_SetCharset(t *testing.T) {
tests := []struct {
name string
cs Charset
want string
}{
{"Charset: UTF-8", CharsetUTF8, "UTF-8"},
{"Charset: ISO-8859-1", CharsetISO88591, "ISO-8859-1"},
{"Charset: empty", "", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewMsg()
m.SetBodyString(TypeTextPlain, "This is a test with ümläutß")
pl, err := getPartList(m)
if err != nil {
t.Errorf("failed: %s", err)
return
}
pl[0].SetCharset(tt.cs)
cs := pl[0].GetCharset()
if string(cs) != tt.want {
t.Errorf("SetCharset failed. Got: %s, expected: %s", string(cs), tt.want)
}
})
}
}