jsoniter_null_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.WriteNull()
  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. iter := ParseString(`[null,"a"]`)
  52. iter.ReadArray()
  53. if iter.ReadString() != "" {
  54. t.FailNow()
  55. }
  56. iter.ReadArray()
  57. if iter.ReadString() != "a" {
  58. t.FailNow()
  59. }
  60. }
  61. func Test_decode_null_skip(t *testing.T) {
  62. iter := ParseString(`[null,"a"]`)
  63. iter.ReadArray()
  64. iter.Skip()
  65. iter.ReadArray()
  66. if iter.ReadString() != "a" {
  67. t.FailNow()
  68. }
  69. }