Skip to content

Commit

Permalink
test: add xml unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
francesconi committed Oct 29, 2024
1 parent 614747b commit 2d62b32
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions timex/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package timex

import (
"encoding/json"
"encoding/xml"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -35,8 +36,24 @@ func TestMarshalJSON(t *testing.T) {
}

func TestUnmarshalJSON(t *testing.T) {
var r Date
err := json.Unmarshal([]byte(`"2023-05-04"`), &r)
var d Date
err := json.Unmarshal([]byte(`"2023-05-04"`), &d)
assert.Nil(t, err)
assert.Equal(t, Date{2023, 5, 4}, r)
assert.Equal(t, Date{2023, 5, 4}, d)
}

func TestMarshalXMLAttr(t *testing.T) {
type Foo struct {
Bar Date `xml:"bar,attr"`
}
got, err := xml.Marshal(Foo{Date{2023, 5, 4}})
assert.Nil(t, err)
assert.Equal(t, `<Foo bar="2023-05-04"></Foo>`, string(got))
}

func TestUnmarshalXML(t *testing.T) {
var d Date
err := xml.Unmarshal([]byte(`<Date>2023-05-04</Date>`), &d)
assert.Nil(t, err)
assert.Equal(t, Date{2023, 5, 4}, d)
}

0 comments on commit 2d62b32

Please sign in to comment.