From 04b8bb59b04e24600f94d3c69986a7738949d52c Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Sat, 21 Sep 2024 16:20:19 +0100 Subject: [PATCH] Fix FromAddress parsing and add tests --- fromaddress.go | 8 +------- fromaddress_test.go | 23 +++++++++++++++++++++++ go.mod | 7 +++++++ go.sum | 4 ++++ 4 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 fromaddress_test.go diff --git a/fromaddress.go b/fromaddress.go index ec14b4f..fbb695e 100644 --- a/fromaddress.go +++ b/fromaddress.go @@ -2,7 +2,6 @@ package simplemail import ( "encoding" - "encoding/json" "github.com/emersion/go-message/mail" ) @@ -13,12 +12,7 @@ type FromAddress struct { var _ encoding.TextUnmarshaler = &FromAddress{} func (f *FromAddress) UnmarshalText(b []byte) error { - var a string - err := json.Unmarshal(b, &a) - if err != nil { - return err - } - address, err := mail.ParseAddress(a) + address, err := mail.ParseAddress(string(b)) if err != nil { return err } diff --git a/fromaddress_test.go b/fromaddress_test.go new file mode 100644 index 0000000..7ad0263 --- /dev/null +++ b/fromaddress_test.go @@ -0,0 +1,23 @@ +package simplemail + +import ( + "encoding/json" + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" + "testing" +) + +func TestFromAddress_UnmarshalText(t *testing.T) { + t.Run("json", func(t *testing.T) { + var fromAddress FromAddress + assert.NoError(t, json.Unmarshal([]byte(`"Jane Doe "`), &fromAddress)) + assert.Equal(t, "Jane Doe", fromAddress.Address.Name) + assert.Equal(t, "jane@example.com", fromAddress.Address.Address) + }) + t.Run("yaml", func(t *testing.T) { + var fromAddress FromAddress + assert.NoError(t, yaml.Unmarshal([]byte(`"Jane Doe "`), &fromAddress)) + assert.Equal(t, "Jane Doe", fromAddress.Address.Name) + assert.Equal(t, "jane@example.com", fromAddress.Address.Address) + }) +} diff --git a/go.mod b/go.mod index 8b8be72..cef01a4 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,11 @@ require ( github.com/emersion/go-message v0.18.1 github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 github.com/emersion/go-smtp v0.21.3 + github.com/stretchr/testify v1.9.0 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index b086940..5fa32d2 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -44,5 +46,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=