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