jsoniter_null_test.go 1.4 KB

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