Add bep.JSON helper for JSON assertion in tests

- 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
This commit is contained in:
Dan Jones 2026-02-13 16:25:37 -06:00
commit ebb0b12069
4 changed files with 29 additions and 21 deletions

View file

@ -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) {

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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) {