oj 3.10.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +104 -0
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +107 -0
  6. data/ext/oj/cache8.h +48 -0
  7. data/ext/oj/circarray.c +68 -0
  8. data/ext/oj/circarray.h +23 -0
  9. data/ext/oj/code.c +235 -0
  10. data/ext/oj/code.h +42 -0
  11. data/ext/oj/compat.c +299 -0
  12. data/ext/oj/custom.c +1218 -0
  13. data/ext/oj/dump.c +1249 -0
  14. data/ext/oj/dump.h +96 -0
  15. data/ext/oj/dump_compat.c +975 -0
  16. data/ext/oj/dump_leaf.c +252 -0
  17. data/ext/oj/dump_object.c +844 -0
  18. data/ext/oj/dump_strict.c +434 -0
  19. data/ext/oj/encode.h +45 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +53 -0
  23. data/ext/oj/fast.c +1771 -0
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +890 -0
  28. data/ext/oj/object.c +775 -0
  29. data/ext/oj/odd.c +231 -0
  30. data/ext/oj/odd.h +44 -0
  31. data/ext/oj/oj.c +1723 -0
  32. data/ext/oj/oj.h +387 -0
  33. data/ext/oj/parse.c +1134 -0
  34. data/ext/oj/parse.h +112 -0
  35. data/ext/oj/rails.c +1528 -0
  36. data/ext/oj/rails.h +21 -0
  37. data/ext/oj/reader.c +231 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +102 -0
  40. data/ext/oj/resolve.h +14 -0
  41. data/ext/oj/rxclass.c +147 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +714 -0
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +924 -0
  46. data/ext/oj/stream_writer.c +363 -0
  47. data/ext/oj/strict.c +212 -0
  48. data/ext/oj/string_writer.c +534 -0
  49. data/ext/oj/trace.c +79 -0
  50. data/ext/oj/trace.h +28 -0
  51. data/ext/oj/util.c +136 -0
  52. data/ext/oj/util.h +19 -0
  53. data/ext/oj/val_stack.c +118 -0
  54. data/ext/oj/val_stack.h +185 -0
  55. data/ext/oj/wab.c +631 -0
  56. data/lib/oj.rb +21 -0
  57. data/lib/oj/active_support_helper.rb +41 -0
  58. data/lib/oj/bag.rb +88 -0
  59. data/lib/oj/easy_hash.rb +52 -0
  60. data/lib/oj/error.rb +22 -0
  61. data/lib/oj/json.rb +176 -0
  62. data/lib/oj/mimic.rb +267 -0
  63. data/lib/oj/saj.rb +66 -0
  64. data/lib/oj/schandler.rb +142 -0
  65. data/lib/oj/state.rb +131 -0
  66. data/lib/oj/version.rb +5 -0
  67. data/pages/Advanced.md +22 -0
  68. data/pages/Compatibility.md +25 -0
  69. data/pages/Custom.md +23 -0
  70. data/pages/Encoding.md +65 -0
  71. data/pages/JsonGem.md +79 -0
  72. data/pages/Modes.md +155 -0
  73. data/pages/Options.md +287 -0
  74. data/pages/Rails.md +155 -0
  75. data/pages/Security.md +20 -0
  76. data/pages/WAB.md +13 -0
  77. data/test/_test_active.rb +76 -0
  78. data/test/_test_active_mimic.rb +96 -0
  79. data/test/_test_mimic_rails.rb +126 -0
  80. data/test/activerecord/result_test.rb +27 -0
  81. data/test/activesupport4/decoding_test.rb +108 -0
  82. data/test/activesupport4/encoding_test.rb +531 -0
  83. data/test/activesupport4/test_helper.rb +41 -0
  84. data/test/activesupport5/abstract_unit.rb +45 -0
  85. data/test/activesupport5/decoding_test.rb +133 -0
  86. data/test/activesupport5/encoding_test.rb +500 -0
  87. data/test/activesupport5/encoding_test_cases.rb +98 -0
  88. data/test/activesupport5/test_helper.rb +72 -0
  89. data/test/activesupport5/time_zone_test_helpers.rb +39 -0
  90. data/test/activesupport6/abstract_unit.rb +44 -0
  91. data/test/activesupport6/decoding_test.rb +133 -0
  92. data/test/activesupport6/encoding_test.rb +507 -0
  93. data/test/activesupport6/encoding_test_cases.rb +98 -0
  94. data/test/activesupport6/test_common.rb +17 -0
  95. data/test/activesupport6/test_helper.rb +163 -0
  96. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  97. data/test/bar.rb +35 -0
  98. data/test/baz.rb +16 -0
  99. data/test/files.rb +29 -0
  100. data/test/foo.rb +52 -0
  101. data/test/helper.rb +26 -0
  102. data/test/isolated/shared.rb +308 -0
  103. data/test/isolated/test_mimic_after.rb +13 -0
  104. data/test/isolated/test_mimic_alone.rb +12 -0
  105. data/test/isolated/test_mimic_as_json.rb +45 -0
  106. data/test/isolated/test_mimic_before.rb +13 -0
  107. data/test/isolated/test_mimic_define.rb +28 -0
  108. data/test/isolated/test_mimic_rails_after.rb +22 -0
  109. data/test/isolated/test_mimic_rails_before.rb +21 -0
  110. data/test/isolated/test_mimic_redefine.rb +15 -0
  111. data/test/json_gem/json_addition_test.rb +216 -0
  112. data/test/json_gem/json_common_interface_test.rb +148 -0
  113. data/test/json_gem/json_encoding_test.rb +107 -0
  114. data/test/json_gem/json_ext_parser_test.rb +20 -0
  115. data/test/json_gem/json_fixtures_test.rb +35 -0
  116. data/test/json_gem/json_generator_test.rb +383 -0
  117. data/test/json_gem/json_generic_object_test.rb +90 -0
  118. data/test/json_gem/json_parser_test.rb +470 -0
  119. data/test/json_gem/json_string_matching_test.rb +42 -0
  120. data/test/json_gem/test_helper.rb +18 -0
  121. data/test/perf.rb +107 -0
  122. data/test/perf_compat.rb +130 -0
  123. data/test/perf_fast.rb +164 -0
  124. data/test/perf_file.rb +64 -0
  125. data/test/perf_object.rb +138 -0
  126. data/test/perf_saj.rb +109 -0
  127. data/test/perf_scp.rb +151 -0
  128. data/test/perf_simple.rb +287 -0
  129. data/test/perf_strict.rb +145 -0
  130. data/test/perf_wab.rb +131 -0
  131. data/test/prec.rb +23 -0
  132. data/test/sample.rb +54 -0
  133. data/test/sample/change.rb +14 -0
  134. data/test/sample/dir.rb +19 -0
  135. data/test/sample/doc.rb +36 -0
  136. data/test/sample/file.rb +48 -0
  137. data/test/sample/group.rb +16 -0
  138. data/test/sample/hasprops.rb +16 -0
  139. data/test/sample/layer.rb +12 -0
  140. data/test/sample/line.rb +20 -0
  141. data/test/sample/oval.rb +10 -0
  142. data/test/sample/rect.rb +10 -0
  143. data/test/sample/shape.rb +35 -0
  144. data/test/sample/text.rb +20 -0
  145. data/test/sample_json.rb +37 -0
  146. data/test/test_compat.rb +502 -0
  147. data/test/test_custom.rb +527 -0
  148. data/test/test_debian.rb +53 -0
  149. data/test/test_fast.rb +470 -0
  150. data/test/test_file.rb +239 -0
  151. data/test/test_gc.rb +49 -0
  152. data/test/test_hash.rb +29 -0
  153. data/test/test_integer_range.rb +72 -0
  154. data/test/test_null.rb +376 -0
  155. data/test/test_object.rb +1027 -0
  156. data/test/test_rails.rb +26 -0
  157. data/test/test_saj.rb +186 -0
  158. data/test/test_scp.rb +433 -0
  159. data/test/test_strict.rb +433 -0
  160. data/test/test_various.rb +719 -0
  161. data/test/test_wab.rb +307 -0
  162. data/test/test_writer.rb +380 -0
  163. data/test/tests.rb +25 -0
  164. data/test/tests_mimic.rb +14 -0
  165. data/test/tests_mimic_addition.rb +7 -0
  166. data/test/zoo.rb +13 -0
  167. metadata +381 -0
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ require 'helper'
7
+
8
+ class RailsJuice < Minitest::Test
9
+
10
+ def test_bigdecimal_dump
11
+ orig = Oj.default_options
12
+ Oj.default_options = { mode: :rails, bigdecimal_as_decimal: true }
13
+ bd = BigDecimal('123')
14
+ json = Oj.dump(bd)
15
+ Oj.default_options = orig
16
+
17
+ assert_equal('0.123e3', json.downcase)
18
+
19
+ json = Oj.dump(bd, mode: :rails, bigdecimal_as_decimal: false)
20
+ assert_equal('"0.123e3"', json.downcase)
21
+
22
+ json = Oj.dump(bd, mode: :rails, bigdecimal_as_decimal: true)
23
+ assert_equal('0.123e3', json.downcase)
24
+ end
25
+
26
+ end
@@ -0,0 +1,186 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ require 'helper'
7
+
8
+ $json = %{{
9
+ "array": [
10
+ {
11
+ "num" : 3,
12
+ "string": "message",
13
+ "hash" : {
14
+ "h2" : {
15
+ "a" : [ 1, 2, 3 ]
16
+ }
17
+ }
18
+ }
19
+ ],
20
+ "boolean" : true
21
+ }}
22
+
23
+ class AllSaj < Oj::Saj
24
+ attr_accessor :calls
25
+
26
+ def initialize()
27
+ @calls = []
28
+ end
29
+
30
+ def hash_start(key)
31
+ @calls << [:hash_start, key]
32
+ end
33
+
34
+ def hash_end(key)
35
+ @calls << [:hash_end, key]
36
+ end
37
+
38
+ def array_start(key)
39
+ @calls << [:array_start, key]
40
+ end
41
+
42
+ def array_end(key)
43
+ @calls << [:array_end, key]
44
+ end
45
+
46
+ def add_value(value, key)
47
+ @calls << [:add_value, value, key]
48
+ end
49
+
50
+ def error(message, line, column)
51
+ @calls << [:error, message, line, column]
52
+ end
53
+
54
+ end # AllSaj
55
+
56
+ class SajTest < Minitest::Test
57
+
58
+ def setup
59
+ @default_options = Oj.default_options
60
+ end
61
+
62
+ def teardown
63
+ Oj.default_options = @default_options
64
+ end
65
+
66
+ def test_nil
67
+ handler = AllSaj.new()
68
+ json = %{null}
69
+ Oj.saj_parse(handler, json)
70
+ assert_equal([[:add_value, nil, nil]], handler.calls)
71
+ end
72
+
73
+ def test_true
74
+ handler = AllSaj.new()
75
+ json = %{true}
76
+ Oj.saj_parse(handler, json)
77
+ assert_equal([[:add_value, true, nil]], handler.calls)
78
+ end
79
+
80
+ def test_false
81
+ handler = AllSaj.new()
82
+ json = %{false}
83
+ Oj.saj_parse(handler, json)
84
+ assert_equal([[:add_value, false, nil]], handler.calls)
85
+ end
86
+
87
+ def test_string
88
+ handler = AllSaj.new()
89
+ json = %{"a string"}
90
+ Oj.saj_parse(handler, json)
91
+ assert_equal([[:add_value, 'a string', nil]], handler.calls)
92
+ end
93
+
94
+ def test_fixnum
95
+ handler = AllSaj.new()
96
+ json = %{12345}
97
+ Oj.saj_parse(handler, json)
98
+ assert_equal([[:add_value, 12345, nil]], handler.calls)
99
+ end
100
+
101
+ def test_float
102
+ handler = AllSaj.new()
103
+ json = %{12345.6789}
104
+ Oj.saj_parse(handler, json)
105
+ assert_equal([[:add_value, 12345.6789, nil]], handler.calls)
106
+ end
107
+
108
+ def test_float_exp
109
+ handler = AllSaj.new()
110
+ json = %{12345.6789e7}
111
+ Oj.saj_parse(handler, json)
112
+ assert_equal(1, handler.calls.size)
113
+ assert_equal(:add_value, handler.calls[0][0])
114
+ assert_equal((12345.6789e7 * 10000).to_i, (handler.calls[0][1] * 10000).to_i)
115
+ end
116
+
117
+ def test_array_empty
118
+ handler = AllSaj.new()
119
+ json = %{[]}
120
+ Oj.saj_parse(handler, json)
121
+ assert_equal([[:array_start, nil],
122
+ [:array_end, nil]], handler.calls)
123
+ end
124
+
125
+ def test_array
126
+ handler = AllSaj.new()
127
+ json = %{[true,false]}
128
+ Oj.saj_parse(handler, json)
129
+ assert_equal([[:array_start, nil],
130
+ [:add_value, true, nil],
131
+ [:add_value, false, nil],
132
+ [:array_end, nil]], handler.calls)
133
+ end
134
+
135
+ def test_hash_empty
136
+ handler = AllSaj.new()
137
+ json = %{{}}
138
+ Oj.saj_parse(handler, json)
139
+ assert_equal([[:hash_start, nil],
140
+ [:hash_end, nil]], handler.calls)
141
+ end
142
+
143
+ def test_hash
144
+ handler = AllSaj.new()
145
+ json = %{{"one":true,"two":false}}
146
+ Oj.saj_parse(handler, json)
147
+ assert_equal([[:hash_start, nil],
148
+ [:add_value, true, 'one'],
149
+ [:add_value, false, 'two'],
150
+ [:hash_end, nil]], handler.calls)
151
+ end
152
+
153
+ def test_full
154
+ handler = AllSaj.new()
155
+ Oj.saj_parse(handler, $json)
156
+ assert_equal([[:hash_start, nil],
157
+ [:array_start, 'array'],
158
+ [:hash_start, nil],
159
+ [:add_value, 3, 'num'],
160
+ [:add_value, 'message', 'string'],
161
+ [:hash_start, 'hash'],
162
+ [:hash_start, 'h2'],
163
+ [:array_start, 'a'],
164
+ [:add_value, 1, nil],
165
+ [:add_value, 2, nil],
166
+ [:add_value, 3, nil],
167
+ [:array_end, 'a'],
168
+ [:hash_end, 'h2'],
169
+ [:hash_end, 'hash'],
170
+ [:hash_end, nil],
171
+ [:array_end, 'array'],
172
+ [:add_value, true, 'boolean'],
173
+ [:hash_end, nil]], handler.calls)
174
+ end
175
+
176
+ def test_fixnum_bad
177
+ handler = AllSaj.new()
178
+ json = %{12345xyz}
179
+ Oj.saj_parse(handler, json)
180
+ assert_equal([:add_value, 12345, nil], handler.calls.first)
181
+ type, message, line, column = handler.calls.last
182
+ assert_equal([:error, 1, 6], [type, line, column])
183
+ assert_match(%r{invalid format, extra characters at line 1, column 6 \[(?:[a-z\.]+/)*saj\.c:\d+\]}, message)
184
+ end
185
+
186
+ end
@@ -0,0 +1,433 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ require 'helper'
7
+ require 'socket'
8
+ require 'stringio'
9
+
10
+ $json = %{{
11
+ "array": [
12
+ {
13
+ "num" : 3,
14
+ "string": "message",
15
+ "hash" : {
16
+ "h2" : {
17
+ "a" : [ 1, 2, 3 ]
18
+ }
19
+ }
20
+ }
21
+ ],
22
+ "boolean" : true
23
+ }}
24
+
25
+ class NoHandler < Oj::ScHandler
26
+ def initialize()
27
+ end
28
+ end
29
+
30
+ class AllHandler < Oj::ScHandler
31
+ attr_accessor :calls
32
+
33
+ def initialize()
34
+ @calls = []
35
+ end
36
+
37
+ def hash_start()
38
+ @calls << [:hash_start]
39
+ {}
40
+ end
41
+
42
+ def hash_end()
43
+ @calls << [:hash_end]
44
+ end
45
+
46
+ def hash_key(key)
47
+ @calls << [:hash_key, key]
48
+ return 'too' if 'two' == key
49
+ return :symbol if 'symbol' == key
50
+ key
51
+ end
52
+
53
+ def array_start()
54
+ @calls << [:array_start]
55
+ []
56
+ end
57
+
58
+ def array_end()
59
+ @calls << [:array_end]
60
+ end
61
+
62
+ def add_value(value)
63
+ @calls << [:add_value, value]
64
+ end
65
+
66
+ def hash_set(h, key, value)
67
+ @calls << [:hash_set, key, value]
68
+ end
69
+
70
+ def array_append(a, value)
71
+ @calls << [:array_append, value]
72
+ end
73
+
74
+ end # AllHandler
75
+
76
+ class Closer < AllHandler
77
+ attr_accessor :io
78
+ def initialize(io)
79
+ super()
80
+ @io = io
81
+ end
82
+
83
+ def hash_start()
84
+ @calls << [:hash_start]
85
+ @io.close
86
+ {}
87
+ end
88
+
89
+ def hash_set(h, key, value)
90
+ @calls << [:hash_set, key, value]
91
+ @io.close
92
+ end
93
+
94
+ end # Closer
95
+
96
+ class ScpTest < Minitest::Test
97
+
98
+ def setup
99
+ @default_options = Oj.default_options
100
+ end
101
+
102
+ def teardown
103
+ Oj.default_options = @default_options
104
+ end
105
+
106
+ def test_nil
107
+ handler = AllHandler.new()
108
+ json = %{null}
109
+ Oj.sc_parse(handler, json)
110
+ assert_equal([[:add_value, nil]], handler.calls)
111
+ end
112
+
113
+ def test_true
114
+ handler = AllHandler.new()
115
+ json = %{true}
116
+ Oj.sc_parse(handler, json)
117
+ assert_equal([[:add_value, true]], handler.calls)
118
+ end
119
+
120
+ def test_false
121
+ handler = AllHandler.new()
122
+ json = %{false}
123
+ Oj.sc_parse(handler, json)
124
+ assert_equal([[:add_value, false]], handler.calls)
125
+ end
126
+
127
+ def test_string
128
+ handler = AllHandler.new()
129
+ json = %{"a string"}
130
+ Oj.sc_parse(handler, json)
131
+ assert_equal([[:add_value, 'a string']], handler.calls)
132
+ end
133
+
134
+ def test_fixnum
135
+ handler = AllHandler.new()
136
+ json = %{12345}
137
+ Oj.sc_parse(handler, json)
138
+ assert_equal([[:add_value, 12345]], handler.calls)
139
+ end
140
+
141
+ def test_float
142
+ handler = AllHandler.new()
143
+ json = %{12345.6789}
144
+ Oj.sc_parse(handler, json)
145
+ assert_equal([[:add_value, 12345.6789]], handler.calls)
146
+ end
147
+
148
+ def test_float_exp
149
+ handler = AllHandler.new()
150
+ json = %{12345.6789e7}
151
+ Oj.sc_parse(handler, json)
152
+ assert_equal(1, handler.calls.size)
153
+ assert_equal(:add_value, handler.calls[0][0])
154
+ assert_equal((12345.6789e7 * 10000).to_i, (handler.calls[0][1] * 10000).to_i)
155
+ end
156
+
157
+ def test_array_empty
158
+ handler = AllHandler.new()
159
+ json = %{[]}
160
+ Oj.sc_parse(handler, json)
161
+ assert_equal([[:array_start],
162
+ [:array_end],
163
+ [:add_value, []]], handler.calls)
164
+ end
165
+
166
+ def test_array
167
+ handler = AllHandler.new()
168
+ json = %{[true,false]}
169
+ Oj.sc_parse(handler, json)
170
+ assert_equal([[:array_start],
171
+ [:array_append, true],
172
+ [:array_append, false],
173
+ [:array_end],
174
+ [:add_value, []]], handler.calls)
175
+ end
176
+
177
+ def test_hash_empty
178
+ handler = AllHandler.new()
179
+ json = %{{}}
180
+ Oj.sc_parse(handler, json)
181
+ assert_equal([[:hash_start],
182
+ [:hash_end],
183
+ [:add_value, {}]], handler.calls)
184
+ end
185
+
186
+ def test_hash
187
+ handler = AllHandler.new()
188
+ json = %{{"one":true,"two":false}}
189
+ Oj.sc_parse(handler, json)
190
+ assert_equal([[:hash_start],
191
+ [:hash_key, 'one'],
192
+ [:hash_set, 'one', true],
193
+ [:hash_key, 'two'],
194
+ [:hash_set, 'too', false],
195
+ [:hash_end],
196
+ [:add_value, {}]], handler.calls)
197
+ end
198
+
199
+ def test_hash_sym
200
+ handler = AllHandler.new()
201
+ json = %{{"one":true,"two":false}}
202
+ Oj.sc_parse(handler, json, :symbol_keys => true)
203
+ assert_equal([[:hash_start],
204
+ [:hash_key, 'one'],
205
+ [:hash_set, 'one', true],
206
+ [:hash_key, 'two'],
207
+ [:hash_set, 'too', false],
208
+ [:hash_end],
209
+ [:add_value, {}]], handler.calls)
210
+ end
211
+
212
+ def test_symbol_hash_key_without_symbol_keys
213
+ handler = AllHandler.new()
214
+ json = %{{"one":true,"symbol":false}}
215
+ Oj.sc_parse(handler, json)
216
+ assert_equal([[:hash_start],
217
+ [:hash_key, 'one'],
218
+ [:hash_set, 'one', true],
219
+ [:hash_key, 'symbol'],
220
+ [:hash_set, :symbol, false],
221
+ [:hash_end],
222
+ [:add_value, {}]], handler.calls)
223
+ end
224
+
225
+ def test_full
226
+ handler = AllHandler.new()
227
+ Oj.sc_parse(handler, $json)
228
+ assert_equal([[:hash_start],
229
+ [:hash_key, 'array'],
230
+ [:array_start],
231
+ [:hash_start],
232
+ [:hash_key, 'num'],
233
+ [:hash_set, "num", 3],
234
+ [:hash_key, 'string'],
235
+ [:hash_set, "string", "message"],
236
+ [:hash_key, 'hash'],
237
+ [:hash_start],
238
+ [:hash_key, 'h2'],
239
+ [:hash_start],
240
+ [:hash_key, 'a'],
241
+ [:array_start],
242
+ [:array_append, 1],
243
+ [:array_append, 2],
244
+ [:array_append, 3],
245
+ [:array_end],
246
+ [:hash_set, "a", []],
247
+ [:hash_end],
248
+ [:hash_set, "h2", {}],
249
+ [:hash_end],
250
+ [:hash_set, "hash", {}],
251
+ [:hash_end],
252
+ [:array_append, {}],
253
+ [:array_end],
254
+ [:hash_set, "array", []],
255
+ [:hash_key, 'boolean'],
256
+ [:hash_set, "boolean", true],
257
+ [:hash_end],
258
+ [:add_value, {}]], handler.calls)
259
+ end
260
+
261
+ def test_double
262
+ handler = AllHandler.new()
263
+ json = %{{"one":true,"two":false}{"three":true,"four":false}}
264
+ Oj.sc_parse(handler, json)
265
+ assert_equal([[:hash_start],
266
+ [:hash_key, 'one'],
267
+ [:hash_set, 'one', true],
268
+ [:hash_key, 'two'],
269
+ [:hash_set, 'too', false],
270
+ [:hash_end],
271
+ [:add_value, {}],
272
+ [:hash_start],
273
+ [:hash_key, 'three'],
274
+ [:hash_set, 'three', true],
275
+ [:hash_key, 'four'],
276
+ [:hash_set, 'four', false],
277
+ [:hash_end],
278
+ [:add_value, {}]], handler.calls)
279
+ end
280
+
281
+ def test_double_io
282
+ handler = AllHandler.new()
283
+ json = %{{"one":true,"two":false}{"three":true,"four":false}}
284
+ Oj.sc_parse(handler, StringIO.new(json))
285
+ assert_equal([[:hash_start],
286
+ [:hash_key, 'one'],
287
+ [:hash_set, 'one', true],
288
+ [:hash_key, 'two'],
289
+ [:hash_set, 'too', false],
290
+ [:hash_end],
291
+ [:add_value, {}],
292
+ [:hash_start],
293
+ [:hash_key, 'three'],
294
+ [:hash_set, 'three', true],
295
+ [:hash_key, 'four'],
296
+ [:hash_set, 'four', false],
297
+ [:hash_end],
298
+ [:add_value, {}]], handler.calls)
299
+ end
300
+
301
+ def test_none
302
+ handler = NoHandler.new()
303
+ Oj.sc_parse(handler, $json)
304
+ end
305
+
306
+ def test_fixnum_bad
307
+ handler = AllHandler.new()
308
+ json = %{12345xyz}
309
+ assert_raises Oj::ParseError do
310
+ Oj.sc_parse(handler, json)
311
+ end
312
+ end
313
+
314
+ def test_null_string
315
+ handler = AllHandler.new()
316
+ json = %{"\0"}
317
+ assert_raises Oj::ParseError do
318
+ Oj.sc_parse(handler, json)
319
+ end
320
+ end
321
+
322
+ def test_pipe
323
+ # Windows does not support fork
324
+ return if RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
325
+
326
+ handler = AllHandler.new()
327
+ json = %{{"one":true,"two":false}}
328
+ IO.pipe do |read_io, write_io|
329
+ if fork
330
+ write_io.close
331
+ Oj.sc_parse(handler, read_io) {|v| p v}
332
+ read_io.close
333
+ assert_equal([[:hash_start],
334
+ [:hash_key, 'one'],
335
+ [:hash_set, 'one', true],
336
+ [:hash_key, 'two'],
337
+ [:hash_set, 'too', false],
338
+ [:hash_end],
339
+ [:add_value, {}]], handler.calls)
340
+ else
341
+ read_io.close
342
+ write_io.write json
343
+ write_io.close
344
+ Process.exit(0)
345
+ end
346
+ end
347
+ end
348
+
349
+ def test_bad_bignum
350
+ if '2.4.0' < RUBY_VERSION
351
+ handler = AllHandler.new()
352
+ json = %|{"big":-e123456789}|
353
+ assert_raises Exception do # Can be either Oj::ParseError or ArgumentError depending on Ruby version
354
+ Oj.sc_parse(handler, json)
355
+ end
356
+ end
357
+ end
358
+
359
+ def test_pipe_close
360
+ # Windows does not support fork
361
+ return if RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
362
+
363
+ json = %{{"one":true,"two":false}}
364
+ IO.pipe do |read_io, write_io|
365
+ if fork
366
+ write_io.close
367
+ handler = Closer.new(read_io)
368
+ err = nil
369
+ begin
370
+ Oj.sc_parse(handler, read_io)
371
+ read_io.close
372
+ rescue Exception => e
373
+ err = e.class.to_s
374
+ end
375
+ assert_equal("IOError", err)
376
+ assert_equal([[:hash_start],
377
+ [:hash_key, 'one'],
378
+ [:hash_set, 'one', true]], handler.calls)
379
+ else
380
+ read_io.close
381
+ write_io.write json[0..11]
382
+ sleep(0.1)
383
+ begin
384
+ write_io.write json[12..-1]
385
+ rescue Exception => e
386
+ # ignore, should fail to write
387
+ end
388
+ write_io.close
389
+ Process.exit(0)
390
+ end
391
+ end
392
+ end
393
+
394
+ def test_socket_close
395
+ json = %{{"one":true,"two":false}}
396
+ begin
397
+ server = TCPServer.new(8080)
398
+ rescue
399
+ # Not able to open a socket to run the test. Might be Travis.
400
+ return
401
+ end
402
+ Thread.start(json) do |j|
403
+ c = server.accept()
404
+ c.puts json[0..11]
405
+ 10.times {
406
+ break if c.closed?
407
+ sleep(0.1)
408
+ }
409
+ unless c.closed?
410
+ c.puts json[12..-1]
411
+ c.close
412
+ end
413
+ end
414
+ begin
415
+ sock = TCPSocket.new('localhost', 8080)
416
+ rescue
417
+ # Not able to open a socket to run the test. Might be Travis.
418
+ return
419
+ end
420
+ handler = Closer.new(sock)
421
+ err = nil
422
+ begin
423
+ Oj.sc_parse(handler, sock)
424
+ rescue Exception => e
425
+ err = e.class.to_s
426
+ end
427
+ assert_equal("IOError", err)
428
+ assert_equal([[:hash_start],
429
+ [:hash_key, 'one'],
430
+ [:hash_set, 'one', true]], handler.calls)
431
+ end
432
+
433
+ end