refactor: focus around Render/Compact/Pretty/NewWith functions

This is yet another drastic refactor of public API and concepts.
Hopefully the last one, as I'm now fairly happy with things.
This commit is contained in:
2024-03-25 01:40:31 +00:00
parent de3a9e55a8
commit e2e2754970
18 changed files with 1462 additions and 387 deletions

View File

@@ -23,38 +23,22 @@ func (mjm *mockJSONMarshaler) MarshalJSON() ([]byte, error) {
func TestJSON_Render(t *testing.T) {
tests := []struct {
name string
pretty bool
prefix string
indent string
value interface{}
want string
wantErr string
wantErrIs []error
name string
prefix string
indent string
value any
want string
wantPretty string
wantErr string
wantErrIs []error
}{
{
name: "simple object without pretty",
pretty: false,
value: map[string]int{"age": 30},
want: "{\"age\":30}\n",
name: "simple object",
value: map[string]int{"age": 30},
want: "{\"age\":30}\n",
},
{
name: "simple object with pretty",
pretty: true,
value: map[string]int{"age": 30},
want: "{\n \"age\": 30\n}\n",
},
{
name: "pretty with prefix and indent",
pretty: true,
prefix: "// ",
indent: "\t",
value: map[string]int{"age": 30},
want: "{\n// \t\"age\": 30\n// }\n",
},
{
name: "prefix and indent without pretty",
pretty: false,
name: "ignores prefix and indent",
prefix: "// ",
indent: "\t",
value: map[string]int{"age": 30},
@@ -72,7 +56,6 @@ func TestJSON_Render(t *testing.T) {
},
{
name: "invalid value",
pretty: false,
value: make(chan int),
wantErr: "render: failed: json: unsupported type: chan int",
wantErrIs: []error{Err, ErrFailed},
@@ -81,7 +64,6 @@ func TestJSON_Render(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := &JSON{
Pretty: tt.pretty,
Prefix: tt.prefix,
Indent: tt.indent,
}
@@ -105,3 +87,76 @@ func TestJSON_Render(t *testing.T) {
})
}
}
func TestJSON_RenderPretty(t *testing.T) {
tests := []struct {
name string
prefix string
indent string
value any
want string
wantPretty string
wantErr string
wantErrIs []error
}{
{
name: "simple object",
value: map[string]int{"age": 30},
want: "{\n \"age\": 30\n}\n",
},
{
name: "uses prefix and indent",
prefix: "// ",
indent: "\t",
value: map[string]int{"age": 30},
want: "{\n// \t\"age\": 30\n// }\n",
},
{
name: "implements json.Marshaler",
value: &mockJSONMarshaler{data: []byte(`{"age":30}`)},
want: "{\n \"age\": 30\n}\n",
},
{
name: "error from json.Marshaler",
value: &mockJSONMarshaler{err: errors.New("marshal error!!1")},
wantErrIs: []error{Err, ErrFailed},
},
{
name: "invalid value",
value: make(chan int),
wantErr: "render: failed: json: unsupported type: chan int",
wantErrIs: []error{Err, ErrFailed},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := &JSON{
Prefix: tt.prefix,
Indent: tt.indent,
}
var buf bytes.Buffer
err := j.RenderPretty(&buf, tt.value)
got := buf.String()
if tt.wantErr != "" {
require.Error(t, err)
assert.EqualError(t, err, tt.wantErr)
}
for _, e := range tt.wantErrIs {
assert.ErrorIs(t, err, e)
}
if tt.wantErr == "" && len(tt.wantErrIs) == 0 {
require.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}
func TestJSON_Formats(t *testing.T) {
h := &JSON{}
assert.Equal(t, []string{"json"}, h.Formats())
}