Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
test: empty/default optional durations
Browse files Browse the repository at this point in the history
does not crash if user restores default value and sets it to empty string ""
  • Loading branch information
lidel committed Oct 26, 2021
1 parent d62365e commit c9b7984
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
4 changes: 2 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ type Duration struct {

func (d *Duration) UnmarshalText(text []byte) error {
switch string(text) {
case "null", "undefined":
case "null", "undefined", "":
*d = Duration{}
return nil
default:
Expand Down Expand Up @@ -253,7 +253,7 @@ func (d Duration) MarshalText() ([]byte, error) {

func (d Duration) String() string {
if d.value == nil {
return "defaults"
return "default"
}
return d.value.String()
}
Expand Down
64 changes: 51 additions & 13 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,24 @@ func TestDuration(t *testing.T) {
})

t.Run("default value", func(t *testing.T) {
var d Duration
if !d.IsDefault() {
t.Fatal("expected value to be the default initially")
}
if err := json.Unmarshal([]byte("null"), &d); err != nil {
t.Fatal(err)
}
if dur := d.WithDefault(time.Hour); dur != time.Hour {
t.Fatalf("expected default value to be used, got %s", dur)
}
if !d.IsDefault() {
t.Fatal("expected value to be the default")
for _, jsonStr := range []string{"null", "\"\"", "\"null\""} {
var d Duration
if !d.IsDefault() {
t.Fatal("expected value to be the default initially")
}
if err := json.Unmarshal([]byte(jsonStr), &d); err != nil {
t.Fatal(err)
}
if dur := d.WithDefault(time.Hour); dur != time.Hour {
t.Fatalf("expected default value to be used, got %s", dur)
}
if !d.IsDefault() {
t.Fatal("expected value to be the default")
}
}
})

t.Run("omitempyt", func(t *testing.T) {
t.Run("omitempty", func(t *testing.T) {
type Foo struct {
D *Duration `json:",omitempty"`
}
Expand All @@ -56,6 +58,42 @@ func TestDuration(t *testing.T) {
t.Fatalf("expected omitempty to omit the duration, got %s", out)
}
})

for jsonStr, goValue := range map[string]Duration{
"\"\"": {},
"null": {},
"\"null\"": {},
"\"1s\"": {value: makeDurationPointer(time.Second)},
"\"42h1m3s\"": {value: makeDurationPointer(42*time.Hour + 1*time.Minute + 3*time.Second)},
} {
var d Duration
err := json.Unmarshal([]byte(jsonStr), &d)
if err != nil {
t.Fatal(err)
}

if goValue.value == nil && d.value == nil {
} else if goValue.value == nil && d.value != nil {
t.Errorf("expected nil for %s, got %s", jsonStr, d)
} else if *d.value != *goValue.value {
t.Fatalf("expected %s for %s, got %s", goValue, jsonStr, d)
}

// Test Reverse
out, err := json.Marshal(goValue)
if err != nil {
t.Fatal(err)
}
if goValue.value == nil {
if string(out) != "\"null\"" {
t.Fatalf("expected null string for %s, got %s", jsonStr, string(out))
}
continue
}
if string(out) != jsonStr {
t.Fatalf("expected %s, got %s", jsonStr, string(out))
}
}
}

func TestOneStrings(t *testing.T) {
Expand Down

0 comments on commit c9b7984

Please sign in to comment.