summaryrefslogtreecommitdiff
path: root/encode_test.go
blob: edd8af1adf5422170c08b40542ceefd1d221ba50 (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
53
54
// 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) {
	t.Parallel()
	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) {
	t.Parallel()
	var out strings.Builder
	enc := NewEncoder(NewReEncoder(&out, ReEncoderConfig{
		AllowMultipleValues: true,
		Indent:              "\t",
	}))

	assert.NoError(t, enc.Encode(1))
	assert.NoError(t, enc.Encode(2))
	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\n2\n{}\n[\n\t9\n]\nnull\n1", out.String())
}

func TestEncode(t *testing.T) {
	t.Parallel()
	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())
}