summaryrefslogtreecommitdiff
path: root/encode_test.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@datawire.io>2022-08-17 15:19:12 -0600
committerLuke Shumaker <lukeshu@datawire.io>2022-08-17 15:43:38 -0600
commit494ad195bc31ce6a65f759544355801fe357c56d (patch)
treed7c8b5d59626651a446614666ee02f6a3044e439 /encode_test.go
parent25f5b3a2aabe11a7f8dad3d001ac30b65c1e6c06 (diff)
Add more tests around trailing-newlines from the encoder
Diffstat (limited to 'encode_test.go')
-rw-r--r--encode_test.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/encode_test.go b/encode_test.go
new file mode 100644
index 0000000..06eadf7
--- /dev/null
+++ b/encode_test.go
@@ -0,0 +1,52 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package lowmemjson
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestEncoder(t *testing.T) {
+ var out strings.Builder
+ enc := NewEncoder(&out)
+
+ assert.NoError(t, enc.Encode(1))
+ assert.NoError(t, enc.Encode(1))
+ assert.NoError(t, enc.Encode(struct{}{}))
+ assert.NoError(t, enc.Encode(nil))
+ assert.NoError(t, enc.Encode(1))
+ assert.Equal(t, "1\n1{}null1", out.String())
+}
+
+func TestEncoderIndent(t *testing.T) {
+ var out strings.Builder
+ enc := NewEncoder(&ReEncoder{
+ Out: &out,
+ AllowMultipleValues: true,
+ Indent: "\t",
+ })
+
+ assert.NoError(t, enc.Encode(1))
+ assert.NoError(t, enc.Encode(1))
+ assert.NoError(t, enc.Encode(struct{}{}))
+ assert.NoError(t, enc.Encode([]int{9}))
+ assert.NoError(t, enc.Encode(nil))
+ assert.NoError(t, enc.Encode(1))
+ assert.Equal(t, "1\n1\n{}\n[\n\t9\n]\nnull\n1", out.String())
+}
+
+func TestEncode(t *testing.T) {
+ var out strings.Builder
+
+ assert.NoError(t, Encode(&out, 1))
+ assert.NoError(t, Encode(&out, 1))
+ assert.NoError(t, Encode(&out, struct{}{}))
+ assert.NoError(t, Encode(&out, nil))
+ assert.NoError(t, Encode(&out, 1))
+ assert.Equal(t, "11{}null1", out.String())
+}