Skip to content

Commit

Permalink
Add charset related tests and functionality
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wneessen committed Feb 5, 2024
1 parent ef86a1a commit 6808857
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions part.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions part_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 6808857

Please sign in to comment.