jsoniter_bool_test.go 1.1 KB

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