From 988ce7a01e322077c11a0331f06b7f46e7dab45a Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sun, 29 Jan 2023 19:32:03 +0800 Subject: [PATCH] Missing tests for JSON/XML outputs (#280) --- .snapshots/TestMarshalErrorXMLPageResult | 5 +++ .snapshots/TestMarshalSuccessJSONPageResult | 2 +- .snapshots/TestMarshalSuccessXMLPageResult | 3 ++ .../TestMarshalVerboseSuccessJSONPageResult | 1 + json_page_result_test.go | 13 ++++++ xml_page_result_test.go | 40 +++++++++++++++++++ 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 .snapshots/TestMarshalErrorXMLPageResult create mode 100644 .snapshots/TestMarshalSuccessXMLPageResult create mode 100644 .snapshots/TestMarshalVerboseSuccessJSONPageResult create mode 100644 xml_page_result_test.go diff --git a/.snapshots/TestMarshalErrorXMLPageResult b/.snapshots/TestMarshalErrorXMLPageResult new file mode 100644 index 00000000..3c072c1c --- /dev/null +++ b/.snapshots/TestMarshalErrorXMLPageResult @@ -0,0 +1,5 @@ + + + + + diff --git a/.snapshots/TestMarshalSuccessJSONPageResult b/.snapshots/TestMarshalSuccessJSONPageResult index 0156d382..ad8501e0 100644 --- a/.snapshots/TestMarshalSuccessJSONPageResult +++ b/.snapshots/TestMarshalSuccessJSONPageResult @@ -1 +1 @@ -{"url":"http://foo.com","links":[{"url":"http://foo.com/foo","status":200}]} +{"url":"http://foo.com","links":[]} diff --git a/.snapshots/TestMarshalSuccessXMLPageResult b/.snapshots/TestMarshalSuccessXMLPageResult new file mode 100644 index 00000000..21d805bc --- /dev/null +++ b/.snapshots/TestMarshalSuccessXMLPageResult @@ -0,0 +1,3 @@ + + + diff --git a/.snapshots/TestMarshalVerboseSuccessJSONPageResult b/.snapshots/TestMarshalVerboseSuccessJSONPageResult new file mode 100644 index 00000000..0156d382 --- /dev/null +++ b/.snapshots/TestMarshalVerboseSuccessJSONPageResult @@ -0,0 +1 @@ +{"url":"http://foo.com","links":[{"url":"http://foo.com/foo","status":200}]} diff --git a/json_page_result_test.go b/json_page_result_test.go index 2d144244..839f858c 100644 --- a/json_page_result_test.go +++ b/json_page_result_test.go @@ -23,6 +23,19 @@ func TestMarshalErrorJSONPageResult(t *testing.T) { } func TestMarshalSuccessJSONPageResult(t *testing.T) { + bs, err := json.Marshal(newJSONPageResult( + &pageResult{ + "http://foo.com", + []*successLinkResult{ + {"http://foo.com/foo", 200}, + }, + []*errorLinkResult{}, + }, false)) + assert.Nil(t, err) + cupaloy.SnapshotT(t, bs) +} + +func TestMarshalVerboseSuccessJSONPageResult(t *testing.T) { bs, err := json.Marshal(newJSONPageResult( &pageResult{ "http://foo.com", diff --git a/xml_page_result_test.go b/xml_page_result_test.go new file mode 100644 index 00000000..e1389433 --- /dev/null +++ b/xml_page_result_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "encoding/xml" + "errors" + "testing" + + "github.com/bradleyjkemp/cupaloy" + "github.com/stretchr/testify/assert" +) + +func TestMarshalErrorXMLPageResult(t *testing.T) { + bs, err := marshalXML(newXMLPageResult( + &pageResult{ + "http://foo.com", + []*successLinkResult{}, + []*errorLinkResult{ + {"http://foo.com/bar", errors.New("baz")}, + }, + })) + assert.Nil(t, err) + cupaloy.SnapshotT(t, bs) +} + +func TestMarshalSuccessXMLPageResult(t *testing.T) { + bs, err := marshalXML(newXMLPageResult( + &pageResult{ + "http://foo.com", + []*successLinkResult{ + {"http://foo.com/bar", 200}, + }, + []*errorLinkResult{}, + })) + assert.Nil(t, err) + cupaloy.SnapshotT(t, bs) +} + +func marshalXML(x any) ([]byte, error) { + return xml.MarshalIndent(x, "", " ") +}