From 4bfa9492a221592dece12de8784ec9e34e2b3c13 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Mon, 18 Mar 2024 17:17:32 +0500 Subject: [PATCH] :memo: Added example GoldenEqual test and golden file --- testing/testdata/TestGoldenEqual.golden | 12 +++++++++ testing/testing.go | 3 +++ testing/testing_test.go | 36 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100755 testing/testdata/TestGoldenEqual.golden create mode 100644 testing/testing_test.go diff --git a/testing/testdata/TestGoldenEqual.golden b/testing/testdata/TestGoldenEqual.golden new file mode 100755 index 0000000..cf6b273 --- /dev/null +++ b/testing/testdata/TestGoldenEqual.golden @@ -0,0 +1,12 @@ + + + + + + Testing + + +

Hello, World!

+

This is a testing HTML page of example.com website.

+ + \ No newline at end of file diff --git a/testing/testing.go b/testing/testing.go index c23fa12..b6c6dcd 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -22,6 +22,9 @@ var update = flag.Bool("update", false, "save current tests results as golden fi // be overwritten with the provided contents of output, creating the testdata/ // directory if it does not exist. // +// Check TestGoldenEqual in testing_test.go and testdata/TestGoldenEqual.golden +// for example usage. +// // See: https://youtu.be/8hQG7QlcLBk?t=749 // //nolint:cyclop // no need for splitting diff --git a/testing/testing_test.go b/testing/testing_test.go new file mode 100644 index 0000000..5868568 --- /dev/null +++ b/testing/testing_test.go @@ -0,0 +1,36 @@ +package testing_test + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + + hacktesting "source.toby3d.me/toby3d/hacks/testing" +) + +func TestGoldenEqual(t *testing.T) { + t.Parallel() + + req := httptest.NewRequest(http.MethodGet, "https://example.com/", nil) + w := httptest.NewRecorder() + testHandler := func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, ` + + + + + Testing + + +

Hello, World!

+

This is a testing HTML page of %s website.

+ +`, r.Host) // NOTE(toby3d): Host must be 'example.com' + } + + testHandler(w, req) + + // NOTE(toby3d): compare recorded response body against saved golden file + hacktesting.GoldenEqual(t, w.Result().Body) +}