summaryrefslogtreecommitdiff
path: root/encode_test.go
blob: 05770c7ce2757290b7a1c8d6964c4196128dfd52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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())
}