From 07390eeb960df3ca10b758c9f62459cf2ee9aa3f Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 1 Feb 2022 01:21:45 -0500 Subject: add internal/feed/dataformat.go --- internal/feed/dataformat_test.go | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 internal/feed/dataformat_test.go (limited to 'internal/feed/dataformat_test.go') diff --git a/internal/feed/dataformat_test.go b/internal/feed/dataformat_test.go new file mode 100644 index 0000000..efb4986 --- /dev/null +++ b/internal/feed/dataformat_test.go @@ -0,0 +1,65 @@ +package feed + +import ( + "encoding/json" + "testing" +) + +func TestDataFormatUnmarshalInvalidData(t *testing.T) { + test := []byte(`{}`) + var val DataFormat + + if err := json.Unmarshal(test, &val); err == nil { + t.Errorf("got \"%s\", exp error", val) + } +} + +func TestDataFormatUnmarshalUnknown(t *testing.T) { + test := []byte(`"foo"`) + exp := "unknown data format: foo" + var val DataFormat + + err := json.Unmarshal(test, &val) + if err == nil { + t.Errorf("got \"%s\", exp error", val) + return + } + + if err.Error() != exp { + t.Errorf("got \"%s\", exp \"%s\"", err.Error(), exp) + } +} + +func TestDataFormatUnmarshalValid(t *testing.T) { + test := []byte(`"MITRE"`) + exp := MitreFormat + var got DataFormat + + if err := json.Unmarshal(test, &got); err != nil { + t.Error(err) + return + } + + if got != exp { + t.Errorf("got \"%s\", exp \"%s\"", got, exp) + } +} + +func TestDataFormatString(t *testing.T) { + tests := []struct { + val DataFormat + exp string + } { + { MitreFormat, "MITRE" }, + { DataFormat(255), "DataFormat(255)" }, + } + + for _, test := range(tests) { + t.Run(test.val.String(), func(t *testing.T) { + got := test.val.String() + if got != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", got, test.exp) + } + }) + } +} -- cgit v1.2.3