diff options
author | Paul Duncan <pabs@pablotron.org> | 2022-03-13 07:33:12 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2022-03-13 07:33:12 -0400 |
commit | 9160c2fd5dcf3db9a44539be0ea41eb69eb06b21 (patch) | |
tree | 0a02f4fe2dae19dfb4e4cc1bddee587606cccbaa /cisa/date_test.go | |
parent | 8ffd821fd1d5e227f15ca3e6a0f428cfdbd45398 (diff) | |
download | cvez-9160c2fd5dcf3db9a44539be0ea41eb69eb06b21.tar.bz2 cvez-9160c2fd5dcf3db9a44539be0ea41eb69eb06b21.zip |
cisa: add date json marshall/unmarshall
Diffstat (limited to 'cisa/date_test.go')
-rw-r--r-- | cisa/date_test.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/cisa/date_test.go b/cisa/date_test.go index c64f618..47e152e 100644 --- a/cisa/date_test.go +++ b/cisa/date_test.go @@ -1,6 +1,7 @@ package cisa import ( + "encoding/json" "fmt" "reflect" "testing" @@ -308,3 +309,66 @@ func TestString(t *testing.T) { }) } } + +func TestDateUnmarshalJSON(t *testing.T) { + passTests := []struct { + val string + exp string + } { + { `"2022-02-03"`, "2022-02-03" }, + } + + for _, test := range(passTests) { + var got Date + if err := json.Unmarshal([]byte(test.val), &got); err != nil { + t.Error(err) + } else if got.String() != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", got.String(), test.exp) + } + } + + failTests := []struct { + name string + val string + } { + { "fail-str", "asdf" }, + { "fail-parse", "\"asdf\"" }, + } + + for _, test := range(failTests) { + t.Run(test.name, func(t *testing.T) { + var got Date + if err := got.UnmarshalJSON([]byte(test.val)); err == nil { + t.Errorf("got \"%v\" exp error", got) + } + }) + } + +} + +func TestDateMarshalJSON(t *testing.T) { + tests := []struct { + val string + exp string + } { + { "2022-10-31", `"2022-10-31"` }, + } + + for _, test := range(tests) { + t.Run(test.val, func(t *testing.T) { + // create date + dt, err := NewDate([]byte(test.val)) + if err != nil { + t.Error(err) + return + } + + // get/check string + if got, err := dt.MarshalJSON(); err != nil { + t.Error(err) + } else if string(got) != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", got, test.exp) + } + }) + } +} |