From ebb0b1206925fb2796498a927e7c4e0fdf4d2df3 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 13 Feb 2026 16:25:37 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20bep.JSON=20helper=20for=20JSO?= =?UTF-8?q?N=20assertion=20in=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Create internal/testutil/bep package with JSON helper - Use bep.JSON in formatters/json_test.go, models/entry_test.go, and models/meta_test.go --- formatters/json_test.go | 17 ++++------------- internal/testutil/bep/json.go | 21 +++++++++++++++++++++ models/entry_test.go | 6 ++---- models/meta_test.go | 6 ++---- 4 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 internal/testutil/bep/json.go diff --git a/formatters/json_test.go b/formatters/json_test.go index 26f5848..8139369 100644 --- a/formatters/json_test.go +++ b/formatters/json_test.go @@ -1,11 +1,11 @@ package formatters import ( - "encoding/json" "fmt" "testing" "time" + "codeberg.org/danjones000/my-log/internal/testutil/bep" "codeberg.org/danjones000/my-log/models" "github.com/nalgeon/be" ) @@ -21,10 +21,7 @@ func TestJsonMeta(t *testing.T) { exp := `{"foo":42}` o, err := f.Meta(m) be.Err(t, err, nil) - var got, want any - json.Unmarshal([]byte(exp), &want) - json.Unmarshal(o, &got) - be.Equal(t, got, want) + bep.JSON(t, o, []byte(exp)) } func TestJsonEntry(t *testing.T) { @@ -39,10 +36,7 @@ func TestJsonEntry(t *testing.T) { exp := fmt.Sprintf(`{"title":"%s","date":"%s","foo":42}`, e.Title, when.Format(time.RFC3339)) o, err := f.Entry(e) be.Err(t, err, nil) - var got, want any - json.Unmarshal([]byte(exp), &want) - json.Unmarshal(o, &got) - be.Equal(t, got, want) + bep.JSON(t, o, []byte(exp)) } func TestJsonLog(t *testing.T) { @@ -58,10 +52,7 @@ func TestJsonLog(t *testing.T) { exp := fmt.Sprintf(`{"%s":[{"title":"%s","date":"%s","foo":42}]}`, l.Name, e.Title, when.Format(time.RFC3339)) o, err := f.Log(l) be.Err(t, err, nil) - var got, want any - json.Unmarshal([]byte(exp), &want) - json.Unmarshal(o, &got) - be.Equal(t, got, want) + bep.JSON(t, o, []byte(exp)) } func TestJsonNoLogs(t *testing.T) { diff --git a/internal/testutil/bep/json.go b/internal/testutil/bep/json.go new file mode 100644 index 0000000..f0fb46b --- /dev/null +++ b/internal/testutil/bep/json.go @@ -0,0 +1,21 @@ +package bep + +import ( + "encoding/json" + "testing" + + "github.com/nalgeon/be" +) + +func JSON(t *testing.T, got, want []byte) { + t.Helper() + + var gotAny, wantAny any + err := json.Unmarshal(want, &wantAny) + be.Err(t, err, nil) + + err = json.Unmarshal(got, &gotAny) + be.Err(t, err, nil) + + be.Equal(t, gotAny, wantAny) +} diff --git a/models/entry_test.go b/models/entry_test.go index 4a13902..6372897 100644 --- a/models/entry_test.go +++ b/models/entry_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + "codeberg.org/danjones000/my-log/internal/testutil/bep" "github.com/nalgeon/be" ) @@ -298,10 +299,7 @@ func getEntryJsonMarshalTestRunner(title string, date time.Time, fields []Meta, e := Entry{title, date, fields} o, er := json.Marshal(e) if err == nil { - var got, want any - json.Unmarshal([]byte(out), &want) - json.Unmarshal(o, &got) - be.Equal(t, got, want) + bep.JSON(t, o, []byte(out)) } else { be.Err(t, er, err) } diff --git a/models/meta_test.go b/models/meta_test.go index 7715065..8827444 100644 --- a/models/meta_test.go +++ b/models/meta_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "codeberg.org/danjones000/my-log/internal/testutil/bep" "github.com/nalgeon/be" ) @@ -103,10 +104,7 @@ func TestMetasJson(t *testing.T) { exp := `{"me":41,"you":false}` o, err := json.Marshal(ms) be.Err(t, err, nil) - var got, want any - json.Unmarshal([]byte(exp), &want) - json.Unmarshal(o, &got) - be.Equal(t, got, want) + bep.JSON(t, o, []byte(exp)) } func TestMetasJsonUnmarshal(t *testing.T) {