github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/misc_tests/jsoniter_bool_test.go (about)

     1  package misc_tests
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"testing"
     7  
     8  	"github.com/bingoohuang/gg/pkg/jsoni"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func Test_true(t *testing.T) {
    13  	should := require.New(t)
    14  	iter := jsoni.ParseString(jsoni.ConfigDefault, `true`)
    15  	should.True(iter.ReadBool())
    16  	iter = jsoni.ParseString(jsoni.ConfigDefault, `true`)
    17  	should.Equal(true, iter.Read(context.Background()))
    18  }
    19  
    20  func Test_false(t *testing.T) {
    21  	should := require.New(t)
    22  	iter := jsoni.ParseString(jsoni.ConfigDefault, `false`)
    23  	should.False(iter.ReadBool())
    24  }
    25  
    26  func Test_write_true_false(t *testing.T) {
    27  	should := require.New(t)
    28  	buf := &bytes.Buffer{}
    29  	stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
    30  	stream.WriteTrue()
    31  	stream.WriteFalse()
    32  	stream.WriteBool(false)
    33  	stream.Flush()
    34  	should.Nil(stream.Error)
    35  	should.Equal("truefalsefalse", buf.String())
    36  }
    37  
    38  func Test_write_val_bool(t *testing.T) {
    39  	should := require.New(t)
    40  	buf := &bytes.Buffer{}
    41  	stream := jsoni.NewStream(jsoni.ConfigDefault, buf, 4096)
    42  	stream.WriteVal(context.Background(), true)
    43  	should.Equal(stream.Buffered(), 4)
    44  	stream.Flush()
    45  	should.Equal(stream.Buffered(), 0)
    46  	should.Nil(stream.Error)
    47  	should.Equal("true", buf.String())
    48  }