diff options
author | Luke Shumaker <lukeshu@datawire.io> | 2022-08-18 01:07:39 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@datawire.io> | 2022-08-18 01:07:39 -0600 |
commit | fb063e2955debb9d02debf3f59de291d99017058 (patch) | |
tree | fdd0e8a1ed8b5ee63f7ea2aacb6657249fb63dbb /reencode_test.go | |
parent | 3bd2e0e93647b806a68f06f319ea23511ec24a37 (diff) |
Add more reencode tests
Diffstat (limited to 'reencode_test.go')
-rw-r--r-- | reencode_test.go | 131 |
1 files changed, 114 insertions, 17 deletions
diff --git a/reencode_test.go b/reencode_test.go index 8886432..7f1634f 100644 --- a/reencode_test.go +++ b/reencode_test.go @@ -11,26 +11,123 @@ import ( "github.com/stretchr/testify/assert" ) -func TestReEncodeCompactIfUnder(t *testing.T) { - var out strings.Builder - enc := NewEncoder(&ReEncoder{ - Out: &out, - AllowMultipleValues: true, - Indent: "\t", - CompactIfUnder: 10, - }) - - obj := map[string][]string{ - "a": {"b", "c"}, - "d": {"eeeeeeeeeeeeeee"}, +func TestReEncode(t *testing.T) { + type testcase struct { + enc ReEncoder + in any + exp string } - - assert.NoError(t, enc.Encode(obj)) - exp := `{ + testcases := map[string]testcase{ + "basic": { + enc: ReEncoder{ + Indent: "\t", + CompactIfUnder: 10, + }, + in: map[string][]string{ + "a": {"b", "c"}, + "d": {"eeeeeeeeeeeeeee"}, + }, + exp: `{ "a": ["b","c"], "d": [ "eeeeeeeeeeeeeee" ] -}` - assert.Equal(t, exp, out.String()) +}`, + }, + "arrays1": { + enc: ReEncoder{ + Indent: "\t", + CompactIfUnder: 10, + ForceTrailingNewlines: true, + }, + in: []any{ + map[string]any{ + "generation": 123456, + }, + map[string]any{ + "a": 1, + }, + map[string]any{ + "generation": 7891011213, + }, + }, + exp: `[ + { + "generation": 123456 + }, + {"a":1}, + { + "generation": 7891011213 + } +] +`, + }, + "arrays2": { + enc: ReEncoder{ + Indent: "\t", + CompactIfUnder: 10, + ForceTrailingNewlines: true, + }, + in: []any{ + map[string]any{ + "a": 1, + }, + map[string]any{ + "generation": 123456, + }, + map[string]any{ + "generation": 7891011213, + }, + }, + exp: `[ + {"a":1}, + { + "generation": 123456 + }, + { + "generation": 7891011213 + } +] +`, + }, + "arrays3": { + enc: ReEncoder{ + Indent: "\t", + ForceTrailingNewlines: true, + }, + in: []any{ + map[string]any{ + "a": 1, + }, + map[string]any{ + "generation": 123456, + }, + map[string]any{ + "generation": 7891011213, + }, + }, + exp: `[ + { + "a": 1 + }, + { + "generation": 123456 + }, + { + "generation": 7891011213 + } +] +`, + }, + } + for tcName, tc := range testcases { + t.Run(tcName, func(t *testing.T) { + var out strings.Builder + fmter := tc.enc + fmter.Out = &out + enc := NewEncoder(&fmter) + assert.NoError(t, enc.Encode(tc.in)) + assert.Equal(t, tc.exp, out.String()) + }) + } } |