Skip to content

Commit 4ac9c7e

Browse files
committed
Rewrite test a bit
1 parent ee1f93a commit 4ac9c7e

2 files changed

Lines changed: 42 additions & 28 deletions

File tree

decode.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,13 @@ func (md *MetaData) unify(data interface{}, rv reflect.Value) error {
199199
}
200200

201201
rvi := rv.Interface()
202-
// Special case. Unmarshaler Interface support.
203202
if v, ok := rvi.(Unmarshaler); ok {
204203
return v.UnmarshalTOML(data)
205204
}
206-
207-
// Special case. Look for a value satisfying the TextUnmarshaler interface.
208205
if v, ok := rvi.(encoding.TextUnmarshaler); ok {
209206
return md.unifyText(data, v)
210207
}
208+
211209
// TODO:
212210
// The behavior here is incorrect whenever a Go type satisfies the
213211
// encoding.TextUnmarshaler interface but also corresponds to a TOML hash or

decode_test.go

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -755,34 +755,50 @@ func TestDecodeDatetime(t *testing.T) {
755755
}
756756

757757
func TestDecodeTextUnmarshaler(t *testing.T) {
758-
type Dict struct {
759-
Time time.Time
760-
TimeP *time.Time
761-
TimeMap map[string]time.Time
762-
}
763-
764-
tm := time.Date(1987, 7, 5, 5, 45, 0, 0, time.UTC)
765-
expected := &Dict{
766-
Time: tm,
767-
TimeP: &tm,
768-
TimeMap: map[string]time.Time{"foo": tm},
758+
tests := []struct {
759+
name string
760+
t interface{}
761+
toml string
762+
want string
763+
}{
764+
{
765+
"time.Time",
766+
struct{ Time time.Time }{},
767+
"Time = 1987-07-05T05:45:00Z",
768+
"map[Time:1987-07-05 05:45:00 +0000 UTC]",
769+
},
770+
{
771+
"*time.Time",
772+
struct{ Time *time.Time }{},
773+
"Time = 1988-07-05T05:45:00Z",
774+
"map[Time:1988-07-05 05:45:00 +0000 UTC]",
775+
},
776+
{
777+
"map[string]time.Time",
778+
struct{ Times map[string]time.Time }{},
779+
"Times.one = 1989-07-05T05:45:00Z\nTimes.two = 1990-07-05T05:45:00Z",
780+
"map[Times:map[one:1989-07-05 05:45:00 +0000 UTC two:1990-07-05 05:45:00 +0000 UTC]]",
781+
},
782+
{
783+
"map[string]*time.Time",
784+
struct{ Times map[string]*time.Time }{},
785+
"Times.one = 1989-07-05T05:45:00Z\nTimes.two = 1990-07-05T05:45:00Z",
786+
"map[Times:map[one:1989-07-05 05:45:00 +0000 UTC two:1990-07-05 05:45:00 +0000 UTC]]",
787+
},
769788
}
770789

771-
ex1 := `
772-
Time = 1987-07-05T05:45:00Z
773-
TimeP = 1987-07-05T05:45:00Z
774-
775-
[TimeMap]
776-
foo = 1987-07-05T05:45:00Z
777-
`
790+
for _, tt := range tests {
791+
t.Run(tt.name, func(t *testing.T) {
792+
_, err := Decode(tt.toml, &tt.t)
793+
if err != nil {
794+
t.Fatal(err)
795+
}
778796

779-
dict := new(Dict)
780-
_, err := Decode(ex1, dict)
781-
if err != nil {
782-
t.Errorf("Decode error: %v", err)
783-
}
784-
if !reflect.DeepEqual(expected, dict) {
785-
t.Fatalf("\n%#v\n!=\n%#v\n", expected, dict)
797+
have := fmt.Sprintf("%v", tt.t)
798+
if have != tt.want {
799+
t.Errorf("\nhave: %s\nwant: %s", have, tt.want)
800+
}
801+
})
786802
}
787803
}
788804

0 commit comments

Comments
 (0)