jsoniter_find_end_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package jsoniter
  2. import (
  3. "github.com/json-iterator/go/require"
  4. "io"
  5. "testing"
  6. )
  7. func Test_string_end(t *testing.T) {
  8. end, escaped := ParseString(`abc"`).findStringEnd()
  9. if end != 4 {
  10. t.Fatal(end)
  11. }
  12. if escaped != false {
  13. t.Fatal(escaped)
  14. }
  15. end, escaped = ParseString(`abc\\"`).findStringEnd()
  16. if end != 6 {
  17. t.Fatal(end)
  18. }
  19. if escaped != true {
  20. t.Fatal(escaped)
  21. }
  22. end, escaped = ParseString(`abc\\\\"`).findStringEnd()
  23. if end != 8 {
  24. t.Fatal(end)
  25. }
  26. if escaped != true {
  27. t.Fatal(escaped)
  28. }
  29. end, escaped = ParseString(`abc\"`).findStringEnd()
  30. if end != -1 {
  31. t.Fatal(end)
  32. }
  33. if escaped != false {
  34. t.Fatal(escaped)
  35. }
  36. end, escaped = ParseString(`abc\`).findStringEnd()
  37. if end != -1 {
  38. t.Fatal(end)
  39. }
  40. if escaped != true {
  41. t.Fatal(escaped)
  42. }
  43. end, escaped = ParseString(`abc\\`).findStringEnd()
  44. if end != -1 {
  45. t.Fatal(end)
  46. }
  47. if escaped != false {
  48. t.Fatal(escaped)
  49. }
  50. end, escaped = ParseString(`\\`).findStringEnd()
  51. if end != -1 {
  52. t.Fatal(end)
  53. }
  54. if escaped != false {
  55. t.Fatal(escaped)
  56. }
  57. end, escaped = ParseString(`\`).findStringEnd()
  58. if end != -1 {
  59. t.Fatal(end)
  60. }
  61. if escaped != true {
  62. t.Fatal(escaped)
  63. }
  64. }
  65. type StagedReader struct {
  66. r1 string
  67. r2 string
  68. r3 string
  69. r int
  70. }
  71. func (reader *StagedReader) Read(p []byte) (n int, err error) {
  72. reader.r++
  73. switch reader.r {
  74. case 1:
  75. copy(p, []byte(reader.r1))
  76. return len(reader.r1), nil
  77. case 2:
  78. copy(p, []byte(reader.r2))
  79. return len(reader.r2), nil
  80. case 3:
  81. copy(p, []byte(reader.r3))
  82. return len(reader.r3), nil
  83. default:
  84. return 0, io.EOF
  85. }
  86. }
  87. func Test_skip_string(t *testing.T) {
  88. should := require.New(t)
  89. iter := ParseString(`"abc`)
  90. iter.skipString()
  91. should.Equal(1, iter.head)
  92. iter = ParseString(`\""abc`)
  93. iter.skipString()
  94. should.Equal(3, iter.head)
  95. reader := &StagedReader{
  96. r1: `abc`,
  97. r2: `"`,
  98. }
  99. iter = Parse(reader, 4096)
  100. iter.skipString()
  101. should.Equal(1, iter.head)
  102. reader = &StagedReader{
  103. r1: `abc`,
  104. r2: `1"`,
  105. }
  106. iter = Parse(reader, 4096)
  107. iter.skipString()
  108. should.Equal(2, iter.head)
  109. reader = &StagedReader{
  110. r1: `abc\`,
  111. r2: `"`,
  112. }
  113. iter = Parse(reader, 4096)
  114. iter.skipString()
  115. should.NotNil(iter.Error)
  116. reader = &StagedReader{
  117. r1: `abc\`,
  118. r2: `""`,
  119. }
  120. iter = Parse(reader, 4096)
  121. iter.skipString()
  122. should.Equal(2, iter.head)
  123. }
  124. func Test_skip_object(t *testing.T) {
  125. iter := ParseString(`}`)
  126. iter.skipObject()
  127. if iter.head != 1 {
  128. t.Fatal(iter.head)
  129. }
  130. iter = ParseString(`a}`)
  131. iter.skipObject()
  132. if iter.head != 2 {
  133. t.Fatal(iter.head)
  134. }
  135. iter = ParseString(`{}}a`)
  136. iter.skipObject()
  137. if iter.head != 3 {
  138. t.Fatal(iter.head)
  139. }
  140. reader := &StagedReader{
  141. r1: `{`,
  142. r2: `}}a`,
  143. }
  144. iter = Parse(reader, 4096)
  145. iter.skipObject()
  146. if iter.head != 2 {
  147. t.Fatal(iter.head)
  148. }
  149. iter = ParseString(`"}"}a`)
  150. iter.skipObject()
  151. if iter.head != 4 {
  152. t.Fatal(iter.head)
  153. }
  154. }