-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduration_test.go
More file actions
39 lines (35 loc) · 1.18 KB
/
duration_test.go
File metadata and controls
39 lines (35 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package configfile_test
import (
"encoding/json"
"testing"
"time"
"github.com/smarty/configfile/v2"
"github.com/smarty/configfile/v2/internal/should"
)
type DurationThing struct {
Timeout configfile.Duration `json:"timeout"`
}
func TestDurationUnmarshal_NullConvention(t *testing.T) {
var thing DurationThing
err := json.Unmarshal([]byte(`{"timeout":null}`), &thing)
should.So(t, err, should.BeNil)
should.So(t, thing.Timeout, should.Equal, configfile.Duration{})
}
func TestDurationUnmarshal_BadValue(t *testing.T) {
var thing DurationThing
err := json.Unmarshal([]byte(`{"timeout":42}`), &thing)
should.So(t, err, should.NOT.BeNil)
should.So(t, thing.Timeout, should.Equal, configfile.Duration{})
}
func TestDurationUnmarshal_BadJSON(t *testing.T) {
var thing DurationThing
err := json.Unmarshal([]byte(`{"timeout":"not a duration"}`), &thing)
should.So(t, err, should.NOT.BeNil)
should.So(t, thing.Timeout, should.Equal, configfile.Duration{})
}
func TestDurationUnmarshal_GoodJSON(t *testing.T) {
var thing DurationThing
err := json.Unmarshal([]byte(`{"timeout":"2s"}`), &thing)
should.So(t, err, should.BeNil)
should.So(t, thing.Timeout.Duration, should.Equal, time.Second*2)
}