summaryrefslogtreecommitdiff
path: root/encode_test.go
blob: bb09c4d3717eabf6cf044ea019b5db365f13a128 (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-2023  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, NewEncoder(&out).Encode(1))
	assert.NoError(t, NewEncoder(&out).Encode(1))
	assert.NoError(t, NewEncoder(&out).Encode(struct{}{}))
	assert.NoError(t, NewEncoder(&out).Encode(nil))
	assert.NoError(t, NewEncoder(&out).Encode(1))
	assert.Equal(t, "11{}null1", out.String())
}