git.lukeshu.com/go/lowmemjson@v0.3.9-0.20230723050957-72f6d13f6fb2/encode_test.go (about)

     1  // Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
     2  //
     3  // SPDX-License-Identifier: GPL-2.0-or-later
     4  
     5  package lowmemjson
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestEncoder(t *testing.T) {
    15  	t.Parallel()
    16  	var out strings.Builder
    17  	enc := NewEncoder(&out)
    18  
    19  	assert.NoError(t, enc.Encode(1))
    20  	assert.NoError(t, enc.Encode(1))
    21  	assert.NoError(t, enc.Encode(struct{}{}))
    22  	assert.NoError(t, enc.Encode(nil))
    23  	assert.NoError(t, enc.Encode(1))
    24  	assert.Equal(t, "1\n1{}null1", out.String())
    25  }
    26  
    27  func TestEncoderIndent(t *testing.T) {
    28  	t.Parallel()
    29  	var out strings.Builder
    30  	enc := NewEncoder(NewReEncoder(&out, ReEncoderConfig{
    31  		AllowMultipleValues: true,
    32  		Indent:              "\t",
    33  	}))
    34  
    35  	assert.NoError(t, enc.Encode(1))
    36  	assert.NoError(t, enc.Encode(2))
    37  	assert.NoError(t, enc.Encode(struct{}{}))
    38  	assert.NoError(t, enc.Encode([]int{9}))
    39  	assert.NoError(t, enc.Encode(nil))
    40  	assert.NoError(t, enc.Encode(1))
    41  	assert.Equal(t, "1\n2\n{}\n[\n\t9\n]\nnull\n1", out.String())
    42  }
    43  
    44  func TestEncode(t *testing.T) {
    45  	t.Parallel()
    46  	var out strings.Builder
    47  
    48  	assert.NoError(t, NewEncoder(&out).Encode(1))
    49  	assert.NoError(t, NewEncoder(&out).Encode(1))
    50  	assert.NoError(t, NewEncoder(&out).Encode(struct{}{}))
    51  	assert.NoError(t, NewEncoder(&out).Encode(nil))
    52  	assert.NoError(t, NewEncoder(&out).Encode(1))
    53  	assert.Equal(t, "11{}null1", out.String())
    54  }