oj 3.13.23 → 3.16.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +86 -0
  3. data/README.md +2 -2
  4. data/ext/oj/buf.h +7 -6
  5. data/ext/oj/cache.c +29 -26
  6. data/ext/oj/cache.h +3 -2
  7. data/ext/oj/cache8.c +10 -9
  8. data/ext/oj/circarray.c +7 -5
  9. data/ext/oj/circarray.h +2 -2
  10. data/ext/oj/code.c +5 -12
  11. data/ext/oj/code.h +2 -2
  12. data/ext/oj/compat.c +20 -60
  13. data/ext/oj/custom.c +26 -59
  14. data/ext/oj/debug.c +3 -9
  15. data/ext/oj/dump.c +103 -53
  16. data/ext/oj/dump.h +1 -4
  17. data/ext/oj/dump_compat.c +557 -592
  18. data/ext/oj/dump_leaf.c +3 -5
  19. data/ext/oj/dump_object.c +42 -48
  20. data/ext/oj/dump_strict.c +10 -22
  21. data/ext/oj/encoder.c +1 -1
  22. data/ext/oj/err.c +2 -13
  23. data/ext/oj/err.h +9 -12
  24. data/ext/oj/extconf.rb +16 -7
  25. data/ext/oj/fast.c +63 -98
  26. data/ext/oj/intern.c +62 -47
  27. data/ext/oj/intern.h +3 -7
  28. data/ext/oj/mem.c +318 -0
  29. data/ext/oj/mem.h +53 -0
  30. data/ext/oj/mimic_json.c +54 -38
  31. data/ext/oj/object.c +33 -43
  32. data/ext/oj/odd.c +8 -6
  33. data/ext/oj/odd.h +4 -4
  34. data/ext/oj/oj.c +245 -216
  35. data/ext/oj/oj.h +83 -81
  36. data/ext/oj/parse.c +109 -153
  37. data/ext/oj/parse.h +21 -24
  38. data/ext/oj/parser.c +80 -67
  39. data/ext/oj/parser.h +9 -8
  40. data/ext/oj/rails.c +71 -94
  41. data/ext/oj/reader.c +9 -14
  42. data/ext/oj/reader.h +4 -2
  43. data/ext/oj/resolve.c +3 -4
  44. data/ext/oj/rxclass.c +6 -5
  45. data/ext/oj/rxclass.h +1 -1
  46. data/ext/oj/saj.c +13 -15
  47. data/ext/oj/saj2.c +37 -49
  48. data/ext/oj/saj2.h +1 -1
  49. data/ext/oj/scp.c +6 -20
  50. data/ext/oj/sparse.c +22 -70
  51. data/ext/oj/stream_writer.c +46 -48
  52. data/ext/oj/strict.c +22 -56
  53. data/ext/oj/string_writer.c +64 -40
  54. data/ext/oj/trace.h +31 -4
  55. data/ext/oj/usual.c +125 -114
  56. data/ext/oj/usual.h +7 -6
  57. data/ext/oj/util.h +1 -1
  58. data/ext/oj/val_stack.c +13 -2
  59. data/ext/oj/val_stack.h +8 -7
  60. data/ext/oj/wab.c +25 -57
  61. data/lib/oj/active_support_helper.rb +1 -3
  62. data/lib/oj/bag.rb +7 -1
  63. data/lib/oj/easy_hash.rb +4 -5
  64. data/lib/oj/error.rb +0 -1
  65. data/lib/oj/json.rb +162 -150
  66. data/lib/oj/mimic.rb +7 -7
  67. data/lib/oj/schandler.rb +5 -4
  68. data/lib/oj/state.rb +8 -5
  69. data/lib/oj/version.rb +1 -2
  70. data/lib/oj.rb +2 -0
  71. data/pages/InstallOptions.md +20 -0
  72. data/pages/Options.md +4 -0
  73. metadata +46 -121
  74. data/test/_test_active.rb +0 -76
  75. data/test/_test_active_mimic.rb +0 -96
  76. data/test/_test_mimic_rails.rb +0 -126
  77. data/test/activerecord/result_test.rb +0 -32
  78. data/test/activesupport4/decoding_test.rb +0 -108
  79. data/test/activesupport4/encoding_test.rb +0 -531
  80. data/test/activesupport4/test_helper.rb +0 -41
  81. data/test/activesupport5/abstract_unit.rb +0 -45
  82. data/test/activesupport5/decoding_test.rb +0 -133
  83. data/test/activesupport5/encoding_test.rb +0 -500
  84. data/test/activesupport5/encoding_test_cases.rb +0 -98
  85. data/test/activesupport5/test_helper.rb +0 -72
  86. data/test/activesupport5/time_zone_test_helpers.rb +0 -39
  87. data/test/activesupport6/abstract_unit.rb +0 -44
  88. data/test/activesupport6/decoding_test.rb +0 -133
  89. data/test/activesupport6/encoding_test.rb +0 -507
  90. data/test/activesupport6/encoding_test_cases.rb +0 -98
  91. data/test/activesupport6/test_common.rb +0 -17
  92. data/test/activesupport6/test_helper.rb +0 -163
  93. data/test/activesupport6/time_zone_test_helpers.rb +0 -39
  94. data/test/activesupport7/abstract_unit.rb +0 -49
  95. data/test/activesupport7/decoding_test.rb +0 -125
  96. data/test/activesupport7/encoding_test.rb +0 -486
  97. data/test/activesupport7/encoding_test_cases.rb +0 -104
  98. data/test/activesupport7/time_zone_test_helpers.rb +0 -47
  99. data/test/bar.rb +0 -11
  100. data/test/baz.rb +0 -16
  101. data/test/bug.rb +0 -16
  102. data/test/files.rb +0 -29
  103. data/test/foo.rb +0 -77
  104. data/test/helper.rb +0 -42
  105. data/test/isolated/shared.rb +0 -308
  106. data/test/isolated/test_mimic_after.rb +0 -13
  107. data/test/isolated/test_mimic_alone.rb +0 -12
  108. data/test/isolated/test_mimic_as_json.rb +0 -45
  109. data/test/isolated/test_mimic_before.rb +0 -13
  110. data/test/isolated/test_mimic_define.rb +0 -28
  111. data/test/isolated/test_mimic_rails_after.rb +0 -22
  112. data/test/isolated/test_mimic_rails_before.rb +0 -21
  113. data/test/isolated/test_mimic_redefine.rb +0 -15
  114. data/test/json_gem/json_addition_test.rb +0 -216
  115. data/test/json_gem/json_common_interface_test.rb +0 -153
  116. data/test/json_gem/json_encoding_test.rb +0 -107
  117. data/test/json_gem/json_ext_parser_test.rb +0 -20
  118. data/test/json_gem/json_fixtures_test.rb +0 -35
  119. data/test/json_gem/json_generator_test.rb +0 -396
  120. data/test/json_gem/json_generic_object_test.rb +0 -90
  121. data/test/json_gem/json_parser_test.rb +0 -477
  122. data/test/json_gem/json_string_matching_test.rb +0 -42
  123. data/test/json_gem/test_helper.rb +0 -30
  124. data/test/mem.rb +0 -33
  125. data/test/perf.rb +0 -107
  126. data/test/perf_compat.rb +0 -130
  127. data/test/perf_dump.rb +0 -50
  128. data/test/perf_fast.rb +0 -164
  129. data/test/perf_file.rb +0 -64
  130. data/test/perf_object.rb +0 -138
  131. data/test/perf_once.rb +0 -58
  132. data/test/perf_parser.rb +0 -189
  133. data/test/perf_saj.rb +0 -109
  134. data/test/perf_scp.rb +0 -152
  135. data/test/perf_simple.rb +0 -287
  136. data/test/perf_strict.rb +0 -139
  137. data/test/perf_wab.rb +0 -131
  138. data/test/prec.rb +0 -23
  139. data/test/sample/change.rb +0 -14
  140. data/test/sample/dir.rb +0 -19
  141. data/test/sample/doc.rb +0 -36
  142. data/test/sample/file.rb +0 -48
  143. data/test/sample/group.rb +0 -16
  144. data/test/sample/hasprops.rb +0 -16
  145. data/test/sample/layer.rb +0 -12
  146. data/test/sample/line.rb +0 -20
  147. data/test/sample/oval.rb +0 -10
  148. data/test/sample/rect.rb +0 -10
  149. data/test/sample/shape.rb +0 -35
  150. data/test/sample/text.rb +0 -20
  151. data/test/sample.rb +0 -54
  152. data/test/sample_json.rb +0 -37
  153. data/test/test_compat.rb +0 -540
  154. data/test/test_custom.rb +0 -544
  155. data/test/test_debian.rb +0 -53
  156. data/test/test_fast.rb +0 -530
  157. data/test/test_file.rb +0 -255
  158. data/test/test_gc.rb +0 -60
  159. data/test/test_generate.rb +0 -21
  160. data/test/test_hash.rb +0 -39
  161. data/test/test_integer_range.rb +0 -72
  162. data/test/test_null.rb +0 -376
  163. data/test/test_object.rb +0 -1025
  164. data/test/test_parser.rb +0 -11
  165. data/test/test_parser_debug.rb +0 -27
  166. data/test/test_parser_saj.rb +0 -335
  167. data/test/test_parser_usual.rb +0 -217
  168. data/test/test_rails.rb +0 -35
  169. data/test/test_saj.rb +0 -186
  170. data/test/test_scp.rb +0 -431
  171. data/test/test_strict.rb +0 -435
  172. data/test/test_various.rb +0 -752
  173. data/test/test_wab.rb +0 -309
  174. data/test/test_writer.rb +0 -380
  175. data/test/tests.rb +0 -33
  176. data/test/tests_mimic.rb +0 -23
  177. data/test/tests_mimic_addition.rb +0 -16
  178. data/test/zoo.rb +0 -13
data/test/test_wab.rb DELETED
@@ -1,309 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
- $oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
6
- %w(lib ext).each do |dir|
7
- $: << File.join($oj_dir, dir)
8
- end
9
-
10
- require 'minitest'
11
- require 'minitest/autorun'
12
- require 'stringio'
13
- require 'date'
14
- require 'bigdecimal'
15
- require 'uri'
16
- require 'oj'
17
-
18
- # Simple version of WAB::UUID for testing.
19
- module WAB
20
- class UUID
21
- attr_reader :id
22
- def initialize(id)
23
- @id = id.downcase
24
- raise Exception.new("Invalid UUID format.") if /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.match(@id).nil?
25
- end
26
- def to_s
27
- @id
28
- end
29
- def ==(other)
30
- other.is_a?(self.class) && @id == other.id
31
- end
32
- end # UUID
33
- end # WAB
34
-
35
-
36
- class WabJuice < Minitest::Test
37
-
38
- module TestModule
39
- end
40
-
41
- def test_nil
42
- dump_and_load(nil, false)
43
- end
44
-
45
- def test_true
46
- dump_and_load(true, false)
47
- end
48
-
49
- def test_false
50
- dump_and_load(false, false)
51
- end
52
-
53
- def test_fixnum
54
- dump_and_load(0, false)
55
- dump_and_load(12345, false)
56
- dump_and_load(-54321, false)
57
- dump_and_load(1, false)
58
- end
59
-
60
- def test_float
61
- dump_and_load(0.0, false)
62
- dump_and_load(12345.6789, false)
63
- dump_and_load(70.35, false)
64
- dump_and_load(-54321.012, false)
65
- dump_and_load(1.7775, false)
66
- dump_and_load(2.5024, false)
67
- dump_and_load(2.48e16, false)
68
- dump_and_load(2.48e100 * 1.0e10, false)
69
- dump_and_load(-2.48e100 * 1.0e10, false)
70
- end
71
-
72
- def test_nan_dump
73
- assert_raises() { Oj.dump(0/0.0, mode: :wab) }
74
- end
75
-
76
- def test_infinity_dump
77
- assert_raises() { Oj.dump(1/0.0, mode: :wab) }
78
- end
79
-
80
- def test_neg_infinity_dump
81
- assert_raises() { Oj.dump(-1/0.0, mode: :wab) }
82
- end
83
-
84
- def test_string
85
- dump_and_load('', false)
86
- dump_and_load('abc', false)
87
- dump_and_load("abc\ndef", false)
88
- dump_and_load("a\u0041", false)
89
- end
90
-
91
- def test_encode
92
- dump_and_load("ぴーたー", false)
93
- end
94
-
95
- def test_array
96
- dump_and_load([], false)
97
- dump_and_load([true, false], false)
98
- dump_and_load(['a', 1, nil], false)
99
- dump_and_load([[nil]], false)
100
- dump_and_load([[nil], 58], false)
101
- end
102
-
103
- def test_array_deep
104
- dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
105
- end
106
-
107
- def test_deep_nest
108
- skip 'TruffleRuby causes SEGV' if RUBY_ENGINE == 'truffleruby'
109
-
110
- begin
111
- n = 10000
112
- Oj.wab_load('[' * n + ']' * n)
113
- rescue Exception => e
114
- assert(false, e.message)
115
- end
116
- end
117
-
118
- # Hash
119
- def test_hash
120
- dump_and_load({}, false)
121
- dump_and_load({ true: true, false: false}, false)
122
- dump_and_load({ true: true, array: [], hash: { }}, false)
123
- end
124
-
125
- def test_hash_non_sym_keys
126
- assert_raises() { Oj.dump({ 'true' => true}, mode: :wab) }
127
- end
128
-
129
- def test_hash_deep
130
- dump_and_load({x1: {
131
- x2: {
132
- x3: {
133
- x4: {
134
- x5: {
135
- x6: {
136
- x7: {
137
- x8: {
138
- x9: {
139
- x10: {
140
- x11: {
141
- x12: {
142
- x13: {
143
- x14: {
144
- x15: {
145
- x16: {
146
- x17: {
147
- x18: {
148
- x19: {
149
- x20: {}}}}}}}}}}}}}}}}}}}}}, false)
150
- end
151
-
152
- def test_non_str_hash
153
- assert_raises() { Oj.dump({ 1 => true, 0 => false }, mode: :wab) }
154
- end
155
-
156
- def test_bignum_object
157
- dump_and_load(7 ** 55, false)
158
- end
159
-
160
- # BigDecimal
161
- def test_bigdecimal_wab
162
- dump_and_load(BigDecimal('3.14159265358979323846'), false)
163
- end
164
-
165
- def test_bigdecimal_load
166
- orig = BigDecimal('80.51')
167
- json = Oj.dump(orig, mode: :wab)
168
- bg = Oj.load(json, :mode => :wab, :bigdecimal_load => true)
169
- assert_equal(BigDecimal, bg.class)
170
- assert_equal(orig, bg)
171
- end
172
-
173
- def test_range
174
- assert_raises() { Oj.dump(1..7, mode: :wab) }
175
- end
176
-
177
- # Stream IO
178
- def test_io_string
179
- json = %{{
180
- "x":true,
181
- "y":58,
182
- "z": [1,2,3]
183
- }
184
- }
185
- input = StringIO.new(json)
186
- obj = Oj.wab_load(input)
187
- assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
188
- end
189
-
190
- def test_io_file
191
- filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
192
- File.open(filename, 'w') { |f| f.write(%{{
193
- "x":true,
194
- "y":58,
195
- "z": [1,2,3]
196
- }
197
- }) }
198
- f = File.new(filename)
199
- obj = Oj.wab_load(f)
200
- f.close()
201
- assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
202
- end
203
-
204
- def test_symbol
205
- json = Oj.dump(:abc, mode: :wab)
206
- assert_equal('"abc"', json)
207
- end
208
-
209
- def test_time
210
- t = Time.gm(2017, 1, 5, 23, 58, 7, 123456.789)
211
- json = Oj.dump(t, mode: :wab)
212
- assert_equal('"2017-01-05T23:58:07.123456789Z"', json)
213
- # must load and convert to json as the Time#== does not match identical
214
- # times due to the way it tracks fractional seconds.
215
- loaded = Oj.wab_load(json);
216
- assert_equal(json, Oj.dump(loaded, mode: :wab), "json mismatch after load")
217
- end
218
-
219
- def test_uuid
220
- u = ::WAB::UUID.new('123e4567-e89b-12d3-a456-426655440000')
221
- json = Oj.dump(u, mode: :wab)
222
- assert_equal('"123e4567-e89b-12d3-a456-426655440000"', json)
223
- dump_and_load(u, false)
224
- end
225
-
226
- def test_uri
227
- u = URI('http://opo.technology/sample')
228
- json = Oj.dump(u, mode: :wab)
229
- assert_equal('"http://opo.technology/sample"', json)
230
- dump_and_load(u, false)
231
- end
232
-
233
- def test_class
234
- assert_raises() { Oj.dump(WabJuice, mode: :wab) }
235
- end
236
-
237
- def test_module
238
- assert_raises() { Oj.dump(TestModule, mode: :wab) }
239
- end
240
-
241
- # symbol_keys option
242
- def test_symbol_keys
243
- json = %{{
244
- "x":true,
245
- "y":58,
246
- "z": [1,2,3]
247
- }
248
- }
249
- obj = Oj.wab_load(json, :symbol_keys => true)
250
- assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
251
- end
252
-
253
- # comments
254
- def test_comment_slash
255
- json = %{{
256
- "x":true,//three
257
- "y":58,
258
- "z": [1,2,
259
- 3 // six
260
- ]}
261
- }
262
- obj = Oj.wab_load(json)
263
- assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
264
- end
265
-
266
- def test_comment_c
267
- json = %{{
268
- "x"/*one*/:/*two*/true,
269
- "y":58,
270
- "z": [1,2,3]}
271
- }
272
- obj = Oj.wab_load(json)
273
- assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
274
- end
275
-
276
- def test_comment
277
- json = %{{
278
- "x"/*one*/:/*two*/true,//three
279
- "y":58/*four*/,
280
- "z": [1,2/*five*/,
281
- 3 // six
282
- ]
283
- }
284
- }
285
- obj = Oj.wab_load(json)
286
- assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
287
- end
288
-
289
- def test_double
290
- json = %{{ "x": 1}{ "y": 2}}
291
- results = []
292
- Oj.load(json, :mode => :wab) { |x| results << x }
293
-
294
- assert_equal([{ x: 1 }, { y: 2 }], results)
295
- end
296
-
297
- def dump_and_load(obj, trace=false)
298
- json = Oj.dump(obj, mode: :wab, indent: 2)
299
- puts json if trace
300
- loaded = Oj.wab_load(json);
301
- if obj.nil?
302
- assert_nil(loaded)
303
- else
304
- assert_equal(obj, loaded)
305
- end
306
- loaded
307
- end
308
-
309
- end
data/test/test_writer.rb DELETED
@@ -1,380 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: utf-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- require 'helper'
7
-
8
- class OjWriter < Minitest::Test
9
-
10
- def setup
11
- @default_options = Oj.default_options
12
- end
13
-
14
- def teardown
15
- Oj.default_options = @default_options
16
- end
17
-
18
- def test_string_writer_empty_array
19
- w = Oj::StringWriter.new(:indent => 0)
20
- w.push_array()
21
- w.pop()
22
- assert_equal("[]\n", w.to_s)
23
- end
24
-
25
- def test_string_writer_nested_array
26
- w = Oj::StringWriter.new(:indent => 0)
27
- w.push_array()
28
- w.push_array()
29
- w.pop()
30
- w.push_array()
31
- w.push_array()
32
- w.pop()
33
- w.pop()
34
- w.push_array()
35
- w.pop()
36
- w.pop()
37
- assert_equal("[[],[[]],[]]\n", w.to_s)
38
- end
39
-
40
- def test_string_writer_empty_object
41
- w = Oj::StringWriter.new(:indent => 0)
42
- w.push_object()
43
- w.pop()
44
- assert_equal("{}\n", w.to_s)
45
- end
46
-
47
- def test_string_writer_push_null_value_with_key
48
- w = Oj::StringWriter.new(:indent => 0, :mode => :compat)
49
- w.push_object()
50
- w.push_value(nil, 'nothing')
51
- w.pop()
52
- assert_equal(%|{"nothing":null}\n|, w.to_s)
53
- end
54
-
55
- def test_string_writer_nested_object
56
- w = Oj::StringWriter.new(:indent => 0)
57
- w.push_object()
58
- w.push_object("a1")
59
- w.pop()
60
- w.push_object("a2")
61
- w.push_object("b")
62
- w.pop()
63
- w.pop()
64
- w.push_object("a3")
65
- w.pop()
66
- w.pop()
67
- assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{}}\n|, w.to_s)
68
- end
69
-
70
- def test_string_writer_nested_key
71
- w = Oj::StringWriter.new(:indent => 0)
72
- w.push_object()
73
- w.push_key('a1')
74
- w.push_object()
75
- w.pop()
76
- w.push_key('a2')
77
- w.push_object('x')
78
- w.push_object('b')
79
- w.pop()
80
- w.pop()
81
- w.push_key('a3')
82
- w.push_object()
83
- w.push_key('a4')
84
- w.push_value(37)
85
- w.pop()
86
- w.pop()
87
- assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{"a4":37}}\n|, w.to_s)
88
- end
89
-
90
- def test_string_writer_value_array
91
- w = Oj::StringWriter.new(:indent => 2)
92
- w.push_array()
93
- w.push_value(7)
94
- w.push_value(7.3)
95
- w.push_value(true)
96
- w.push_value(nil)
97
- w.push_value("a string")
98
- w.push_value({'a' => 65})
99
- w.push_value([1,2])
100
- w.pop()
101
- assert_equal(%|[
102
- 7,
103
- 7.3,
104
- true,
105
- null,
106
- "a string",
107
- {
108
- "a":65
109
- },
110
- [
111
- 1,
112
- 2
113
- ]
114
- ]
115
- |, w.to_s)
116
- end
117
-
118
- def test_string_writer_block
119
- w = Oj::StringWriter.new(:indent => 0)
120
- w.push_object() {
121
- w.push_object("a1") {
122
- w.push_value(7, 'a7')
123
- }
124
- w.push_array("a2") {
125
- w.push_value('x')
126
- w.push_value(3)
127
- }
128
- }
129
- assert_equal(%|{"a1":{"a7":7},"a2":["x",3]}\n|, w.to_s)
130
- end
131
-
132
- def test_string_writer_json
133
- w = Oj::StringWriter.new(:indent => 0)
134
- w.push_array()
135
- w.push_json('7')
136
- w.push_json('true')
137
- w.push_json(%|"a string"|)
138
- w.push_object()
139
- w.push_json('{"a":65}', 'x')
140
- w.pop()
141
- w.pop()
142
- assert_equal(%|[7,true,"a string",{"x":{"a":65}}]\n|, w.to_s)
143
- end
144
-
145
- def test_string_writer_pop_excess
146
- w = Oj::StringWriter.new(:indent => 0)
147
- begin
148
- w.push_object() {
149
- w.push_key('key')
150
- }
151
- rescue Exception
152
- assert(true)
153
- return
154
- end
155
- assert(false, "*** expected an exception")
156
- end
157
-
158
- def test_string_writer_array_key
159
- w = Oj::StringWriter.new(:indent => 0)
160
- begin
161
- w.push_array() {
162
- w.push_key('key')
163
- w.push_value(7)
164
- }
165
- rescue Exception
166
- assert(true)
167
- return
168
- end
169
- assert(false, "*** expected an exception")
170
- end
171
-
172
- def test_string_writer_pop_with_key
173
- w = Oj::StringWriter.new(:indent => 0)
174
- begin
175
- w.pop()
176
- rescue Exception
177
- assert(true)
178
- return
179
- end
180
- assert(false, "*** expected an exception")
181
- end
182
-
183
- def test_string_writer_obj_no_key
184
- w = Oj::StringWriter.new(:indent => 0)
185
- w.push_object()
186
- begin
187
- w.push_value(59)
188
- rescue Exception
189
- assert(true)
190
- return
191
- end
192
- assert(false, "*** expected an exception")
193
- end
194
-
195
- def test_string_writer_deep
196
- cnt = 1000
197
- w = Oj::StringWriter.new(:indent => 0)
198
- cnt.times do
199
- w.push_array()
200
- end
201
- cnt.times do
202
- w.pop()
203
- end
204
- # if no exception then passed
205
- assert(true)
206
- end
207
-
208
- def test_string_writer_pop_all
209
- w = Oj::StringWriter.new(:indent => 0)
210
- w.push_object()
211
- w.push_object("a1")
212
- w.pop()
213
- w.push_array("a2")
214
- w.push_value(3)
215
- w.push_array()
216
- w.pop_all()
217
- assert_equal(%|{"a1":{},"a2":[3,[]]}\n|, w.to_s)
218
- end
219
-
220
- def test_string_writer_reset
221
- w = Oj::StringWriter.new(:indent => 0)
222
- w.push_array()
223
- w.pop()
224
- w.reset()
225
- assert_equal('', w.to_s)
226
- end
227
-
228
- # Stream Writer
229
-
230
- class SString < ::String
231
- alias :write :concat
232
- end
233
-
234
- def test_stream_writer_encoding
235
- output = SString.new.force_encoding(::Encoding::UTF_8)
236
- w = Oj::StreamWriter.new(output, :indent => 0)
237
- # Oddly enough, when pushing ASCII characters with UTF-8 encoding or even
238
- # ASCII-8BIT does not change the output encoding. Pushing any non-ASCII no
239
- # matter what the encoding changes the output encoding to ASCII-8BIT.
240
- x = "香港" # Hong Kong
241
- x = x.force_encoding('ASCII-8BIT')
242
- w.push_value(x)
243
- assert_equal(::Encoding::UTF_8, output.encoding)
244
- end
245
-
246
- def test_stream_writer_empty_array
247
- output = StringIO.open("", "w+")
248
- w = Oj::StreamWriter.new(output, :indent => 0)
249
- w.push_array()
250
- w.pop()
251
- assert_equal("[]\n", output.string())
252
- end
253
-
254
- def test_stream_writer_mixed_stringio
255
- output = StringIO.open("", "w+")
256
- w = Oj::StreamWriter.new(output, :indent => 0)
257
- w.push_object()
258
- w.push_object("a1")
259
- w.pop()
260
- w.push_object("a2")
261
- w.push_array("b")
262
- w.push_value(7)
263
- w.push_value(true)
264
- w.push_value("string")
265
- w.pop()
266
- w.pop()
267
- w.push_object("a3")
268
- w.pop()
269
- w.pop()
270
- result = output.string()
271
- assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, result)
272
- end
273
-
274
- def test_stream_writer_mixed_file
275
- filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
276
- File.open(filename, "w") do |f|
277
- w = Oj::StreamWriter.new(f, :indent => 0)
278
- w.push_object()
279
- w.push_object("a1")
280
- w.pop()
281
- w.push_object("a2")
282
- w.push_array("b")
283
- w.push_value(7)
284
- w.push_value(true)
285
- w.push_value("string")
286
- w.pop()
287
- w.pop()
288
- w.push_object("a3")
289
- w.pop()
290
- w.pop()
291
- end
292
- content = File.read(filename)
293
- assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, content)
294
- end
295
-
296
- def test_stream_writer_nested_key_object
297
- output = StringIO.open("", "w+")
298
- w = Oj::StreamWriter.new(output, :indent => 0)
299
- w.push_object()
300
- w.push_key('a1')
301
- w.push_object()
302
- w.pop()
303
- w.push_key('a2')
304
- w.push_object('x')
305
- w.push_object('b')
306
- w.pop()
307
- w.pop()
308
- w.push_key('a3')
309
- w.push_object()
310
- w.push_key('a4')
311
- w.push_value(37)
312
- w.pop()
313
- w.pop()
314
- assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{"a4":37}}\n|, output.string())
315
- end
316
-
317
- def push_stuff(w, pop_all=true)
318
- w.push_object()
319
- w.push_object("a1")
320
- w.pop()
321
- w.push_object("a2")
322
- w.push_array("b")
323
- w.push_value(7)
324
- w.push_value(true)
325
- w.push_value("string")
326
- w.pop()
327
- w.pop()
328
- w.push_object("a3")
329
- if pop_all
330
- w.pop_all()
331
- else
332
- w.pop()
333
- w.pop()
334
- end
335
- end
336
-
337
- def test_stream_writer_buf_small
338
- output = StringIO.open("", "w+")
339
- w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 20)
340
- push_stuff(w)
341
- assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
342
- end
343
-
344
- def test_stream_writer_buf_large
345
- output = StringIO.open("", "w+")
346
- w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 16000)
347
- push_stuff(w)
348
- assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
349
- end
350
-
351
- def test_stream_writer_buf_flush
352
- output = StringIO.open("", "w+")
353
- w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 4096)
354
- push_stuff(w, false)
355
- # no flush so nothing should be in the output yet
356
- assert_equal("", output.string())
357
- w.flush()
358
- assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
359
- end
360
-
361
- def test_stream_writer_buf_flush_small
362
- output = StringIO.open("", "w+")
363
- w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 30)
364
- push_stuff(w, false)
365
- # flush should be called at 30 bytes but since the writes are chunky flush
366
- # is called after adding "string".
367
- assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"|, output.string())
368
- w.flush()
369
- assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
370
- end
371
-
372
- def test_stream_writer_push_null_value_with_key
373
- output = StringIO.open("", "w+")
374
- w = Oj::StreamWriter.new(output, :indent => 0)
375
- w.push_object()
376
- w.push_value(nil, 'nothing')
377
- w.pop()
378
- assert_equal(%|{"nothing":null}\n|, output.string())
379
- end
380
- end # OjWriter
data/test/tests.rb DELETED
@@ -1,33 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
- $oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
6
- %w(lib ext).each do |dir|
7
- $: << File.join($oj_dir, dir)
8
- end
9
-
10
- require 'test_compat'
11
- require 'test_custom'
12
- require 'test_fast'
13
- require 'test_file'
14
- require 'test_gc'
15
- require 'test_hash'
16
- require 'test_null'
17
- require 'test_object'
18
- require 'test_saj'
19
- require 'test_scp'
20
- require 'test_strict'
21
- require 'test_rails'
22
- require 'test_wab'
23
- require 'test_writer'
24
- require 'test_integer_range'
25
-
26
- at_exit do
27
- require 'helper'
28
- if '3.1.0' <= RUBY_VERSION && !(RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/)
29
- #Oj::debug_odd("teardown before GC.verify_compaction_references")
30
- verify_gc_compaction
31
- #Oj::debug_odd("teardown after GC.verify_compaction_references")
32
- end
33
- end