1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
#!/usr/bin/env ruby
# frozen_string_literal: false
$LOAD_PATH << __dir__
require 'helper'
class OjWriter < Minitest::Test
def setup
@default_options = Oj.default_options
end
def teardown
Oj.default_options = @default_options
end
def test_string_writer_empty_array
w = Oj::StringWriter.new(:indent => 0)
w.push_array()
w.pop()
assert_equal("[]\n", w.to_s)
end
def test_string_writer_nested_array
w = Oj::StringWriter.new(:indent => 0)
w.push_array()
w.push_array()
w.pop()
w.push_array()
w.push_array()
w.pop()
w.pop()
w.push_array()
w.pop()
w.pop()
assert_equal("[[],[[]],[]]\n", w.to_s)
end
def test_string_writer_empty_object
w = Oj::StringWriter.new(:indent => 0)
w.push_object()
w.pop()
assert_equal("{}\n", w.to_s)
end
def test_string_writer_push_null_value_with_key
w = Oj::StringWriter.new(:indent => 0, :mode => :compat)
w.push_object()
w.push_value(nil, 'nothing')
w.pop()
assert_equal(%|{"nothing":null}\n|, w.to_s)
end
def test_string_writer_nested_object
w = Oj::StringWriter.new(:indent => 0)
w.push_object()
w.push_object('a1')
w.pop()
w.push_object('a2')
w.push_object('b')
w.pop()
w.pop()
w.push_object('a3')
w.pop()
w.pop()
assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{}}\n|, w.to_s)
end
def test_string_writer_nested_key
w = Oj::StringWriter.new(:indent => 0)
w.push_object()
w.push_key('a1')
w.push_object()
w.pop()
w.push_key('a2')
w.push_object('x')
w.push_object('b')
w.pop()
w.pop()
w.push_key('a3')
w.push_object()
w.push_key('a4')
w.push_value(37)
w.pop()
w.pop()
assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{"a4":37}}\n|, w.to_s)
end
def test_string_writer_value_array
w = Oj::StringWriter.new(:indent => 2)
w.push_array()
w.push_value(7)
w.push_value(7.3)
w.push_value(true)
w.push_value(nil)
w.push_value('a string')
w.push_value({'a' => 65})
w.push_value([1, 2])
w.pop()
assert_equal(%|[
7,
7.3,
true,
null,
"a string",
{
"a":65
},
[
1,
2
]
]
|, w.to_s)
end
def test_string_writer_block
w = Oj::StringWriter.new(:indent => 0)
w.push_object() {
w.push_object('a1') {
w.push_value(7, 'a7')
}
w.push_array('a2') {
w.push_value('x')
w.push_value(3)
}
}
assert_equal(%|{"a1":{"a7":7},"a2":["x",3]}\n|, w.to_s)
end
def test_string_writer_json
w = Oj::StringWriter.new(:indent => 0)
w.push_array()
w.push_json('7')
w.push_json('true')
w.push_json(%|"a string"|)
w.push_object()
w.push_json('{"a":65}', 'x')
w.pop()
w.pop()
assert_equal(%|[7,true,"a string",{"x":{"a":65}}]\n|, w.to_s)
end
def test_string_writer_pop_excess
w = Oj::StringWriter.new(:indent => 0)
begin
w.push_object() {
w.push_key('key')
}
rescue Exception
assert(true)
return
end
assert(false, '*** expected an exception')
end
def test_string_writer_array_key
w = Oj::StringWriter.new(:indent => 0)
begin
w.push_array() {
w.push_key('key')
w.push_value(7)
}
rescue Exception
assert(true)
return
end
assert(false, '*** expected an exception')
end
def test_string_writer_pop_with_key
w = Oj::StringWriter.new(:indent => 0)
begin
w.pop()
rescue Exception
assert(true)
return
end
assert(false, '*** expected an exception')
end
def test_string_writer_obj_no_key
w = Oj::StringWriter.new(:indent => 0)
w.push_object()
begin
w.push_value(59)
rescue Exception
assert(true)
return
end
assert(false, '*** expected an exception')
end
def test_string_writer_deep
cnt = 1000
w = Oj::StringWriter.new(:indent => 0)
cnt.times do
w.push_array()
end
cnt.times do
w.pop()
end
# if no exception then passed
assert(true)
end
def test_string_writer_pop_all
w = Oj::StringWriter.new(:indent => 0)
w.push_object()
w.push_object('a1')
w.pop()
w.push_array('a2')
w.push_value(3)
w.push_array()
w.pop_all()
assert_equal(%|{"a1":{},"a2":[3,[]]}\n|, w.to_s)
end
def test_string_writer_reset
w = Oj::StringWriter.new(:indent => 0)
w.push_array()
w.pop()
w.reset()
assert_equal('', w.to_s)
end
# Stream Writer
class SString < ::String
alias write concat
end
def test_stream_writer_encoding
output = SString.new.force_encoding(::Encoding::UTF_8)
w = Oj::StreamWriter.new(output, :indent => 0)
# Oddly enough, when pushing ASCII characters with UTF-8 encoding or even
# ASCII-8BIT does not change the output encoding. Pushing any non-ASCII no
# matter what the encoding changes the output encoding to ASCII-8BIT.
x = '香港' # Hong Kong
x = x.force_encoding('ASCII-8BIT')
w.push_value(x)
assert_equal(::Encoding::UTF_8, output.encoding)
end
def test_stream_writer_empty_array
output = StringIO.open('', 'w+')
w = Oj::StreamWriter.new(output, :indent => 0)
w.push_array()
w.pop()
assert_equal("[]\n", output.string())
end
def test_stream_writer_mixed_stringio
output = StringIO.open('', 'w+')
w = Oj::StreamWriter.new(output, :indent => 0)
w.push_object()
w.push_object('a1')
w.pop()
w.push_object('a2')
w.push_array('b')
w.push_value(7)
w.push_value(true)
w.push_value('string')
w.pop()
w.pop()
w.push_object('a3')
w.pop()
w.pop()
result = output.string()
assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, result)
end
def test_stream_writer_mixed_file
filename = File.join(__dir__, 'open_file_test.json')
File.open(filename, 'w') do |f|
w = Oj::StreamWriter.new(f, :indent => 0)
w.push_object()
w.push_object('a1')
w.pop()
w.push_object('a2')
w.push_array('b')
w.push_value(7)
w.push_value(true)
w.push_value('string')
w.pop()
w.pop()
w.push_object('a3')
w.pop()
w.pop()
end
content = File.read(filename)
assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, content)
end
def test_stream_writer_nested_key_object
output = StringIO.open('', 'w+')
w = Oj::StreamWriter.new(output, :indent => 0)
w.push_object()
w.push_key('a1')
w.push_object()
w.pop()
w.push_key('a2')
w.push_object('x')
w.push_object('b')
w.pop()
w.pop()
w.push_key('a3')
w.push_object()
w.push_key('a4')
w.push_value(37)
w.pop()
w.pop()
assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{"a4":37}}\n|, output.string())
end
def push_stuff(w, pop_all=true)
w.push_object()
w.push_object('a1')
w.pop()
w.push_object('a2')
w.push_array('b')
w.push_value(7)
w.push_value(true)
w.push_value('string')
w.pop()
w.pop()
w.push_object('a3')
if pop_all
w.pop_all()
else
w.pop()
w.pop()
end
end
def test_stream_writer_buf_small
output = StringIO.open('', 'w+')
w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 20)
push_stuff(w)
assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
end
def test_stream_writer_buf_large
output = StringIO.open('', 'w+')
w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 16_000)
push_stuff(w)
assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
end
def test_stream_writer_buf_flush
output = StringIO.open('', 'w+')
w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 4096)
push_stuff(w, false)
# no flush so nothing should be in the output yet
assert_equal('', output.string())
w.flush()
assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
end
def test_stream_writer_buf_flush_small
output = StringIO.open('', 'w+')
w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 30)
push_stuff(w, false)
# flush should be called at 30 bytes but since the writes are chunky flush
# is called after adding "string".
assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"|, output.string())
w.flush()
assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
end
def test_stream_writer_push_null_value_with_key
output = StringIO.open('', 'w+')
w = Oj::StreamWriter.new(output, :indent => 0)
w.push_object()
w.push_value(nil, 'nothing')
w.pop()
assert_equal(%|{"nothing":null}\n|, output.string())
end
end # OjWriter
|