jsoniter_null_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package jsoniter
  2. import (
  3. "testing"
  4. "github.com/json-iterator/go/require"
  5. "bytes"
  6. )
  7. func Test_decode_null(t *testing.T) {
  8. iter := ParseString(`null`)
  9. if iter.ReadNil() != true {
  10. t.FailNow()
  11. }
  12. }
  13. func Test_write_null(t *testing.T) {
  14. should := require.New(t)
  15. buf := &bytes.Buffer{}
  16. stream := NewStream(buf, 4096)
  17. stream.WriteNil()
  18. stream.Flush()
  19. should.Nil(stream.Error)
  20. should.Equal("null", buf.String())
  21. }
  22. func Test_encode_null(t *testing.T) {
  23. should := require.New(t)
  24. str, err := MarshalToString(nil)
  25. should.Nil(err)
  26. should.Equal("null", str)
  27. }
  28. func Test_decode_null_object(t *testing.T) {
  29. iter := ParseString(`[null,"a"]`)
  30. iter.ReadArray()
  31. if iter.ReadObject() != "" {
  32. t.FailNow()
  33. }
  34. iter.ReadArray()
  35. if iter.ReadString() != "a" {
  36. t.FailNow()
  37. }
  38. }
  39. func Test_decode_null_array(t *testing.T) {
  40. iter := ParseString(`[null,"a"]`)
  41. iter.ReadArray()
  42. if iter.ReadArray() != false {
  43. t.FailNow()
  44. }
  45. iter.ReadArray()
  46. if iter.ReadString() != "a" {
  47. t.FailNow()
  48. }
  49. }
  50. func Test_decode_null_string(t *testing.T) {
  51. should := require.New(t)
  52. iter := ParseString(`[null,"a"]`)
  53. should.True(iter.ReadArray())
  54. should.True(iter.ReadNil())
  55. should.True(iter.ReadArray())
  56. should.Equal("a", iter.ReadString())
  57. }
  58. func Test_decode_null_skip(t *testing.T) {
  59. iter := ParseString(`[null,"a"]`)
  60. iter.ReadArray()
  61. iter.Skip()
  62. iter.ReadArray()
  63. if iter.ReadString() != "a" {
  64. t.FailNow()
  65. }
  66. }