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
|
#!/usr/bin/env ruby
# frozen_string_literal: true
$LOAD_PATH << __dir__
@oj_dir = File.dirname(File.expand_path(__dir__))
%w(lib ext).each do |dir|
$LOAD_PATH << File.join(@oj_dir, dir)
end
require 'minitest'
require 'minitest/autorun'
require 'stringio'
require 'date'
require 'bigdecimal'
require 'uri'
require 'oj'
# Simple version of WAB::UUID for testing.
module WAB
class UUID
attr_reader :id
def initialize(id)
@id = id.downcase
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?
end
def to_s
@id
end
def ==(other)
other.is_a?(self.class) && @id == other.id
end
end # UUID
end # WAB
class WabJuice < Minitest::Test
module TestModule
end
def test_nil
dump_and_load(nil, false)
end
def test_true
dump_and_load(true, false)
end
def test_false
dump_and_load(false, false)
end
def test_fixnum
dump_and_load(0, false)
dump_and_load(12_345, false)
dump_and_load(-54_321, false)
dump_and_load(1, false)
end
def test_float
dump_and_load(0.0, false)
dump_and_load(12_345.6789, false)
dump_and_load(70.35, false)
dump_and_load(-54_321.012, false)
dump_and_load(1.7775, false)
dump_and_load(2.5024, false)
dump_and_load(2.48e16, false)
dump_and_load(2.48e100 * 1.0e10, false)
dump_and_load(-2.48e100 * 1.0e10, false)
end
def test_nan_dump
assert_raises() { Oj.dump(0/0.0, mode: :wab) }
end
def test_infinity_dump
assert_raises() { Oj.dump(1/0.0, mode: :wab) }
end
def test_neg_infinity_dump
assert_raises() { Oj.dump(-1/0.0, mode: :wab) }
end
def test_string
dump_and_load('', false)
dump_and_load('abc', false)
dump_and_load("abc\ndef", false)
dump_and_load("a\u0041", false)
end
def test_encode
dump_and_load('ぴーたー', false)
end
def test_array
dump_and_load([], false)
dump_and_load([true, false], false)
dump_and_load(['a', 1, nil], false)
dump_and_load([[nil]], false)
dump_and_load([[nil], 58], false)
end
def test_array_deep
dump_and_load([1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20]]]]]]]]]]]]]]]]]]]], false)
end
def test_deep_nest
skip 'TruffleRuby causes SEGV' if RUBY_ENGINE == 'truffleruby'
begin
n = 10_000
Oj.wab_load(('[' * n) + (']' * n))
rescue Exception => e
refute(e.message)
end
end
# Hash
def test_hash
dump_and_load({}, false)
dump_and_load({ tru: true, fals: false}, false)
dump_and_load({ tru: true, array: [], hash: {}}, false)
end
def test_hash_non_sym_keys
assert_raises() { Oj.dump({ 'true' => true}, mode: :wab) }
end
def test_hash_deep
dump_and_load({x1: {
x2: {
x3: {
x4: {
x5: {
x6: {
x7: {
x8: {
x9: {
x10: {
x11: {
x12: {
x13: {
x14: {
x15: {
x16: {
x17: {
x18: {
x19: {
x20: {}}}}}}}}}}}}}}}}}}}}}, false)
end
def test_non_str_hash
assert_raises() { Oj.dump({ 1 => true, 0 => false }, mode: :wab) }
end
def test_bignum_object
dump_and_load(7**55, false)
end
# BigDecimal
def test_bigdecimal_wab
dump_and_load(BigDecimal('3.14159265358979323846'), false)
end
def test_bigdecimal_load
orig = BigDecimal('80.51')
json = Oj.dump(orig, mode: :wab)
bg = Oj.load(json, :mode => :wab, :bigdecimal_load => true)
assert_equal(BigDecimal, bg.class)
assert_equal(orig, bg)
end
def test_range
assert_raises() { Oj.dump(1..7, mode: :wab) }
end
# Stream IO
def test_io_string
json = %{{
"x":true,
"y":58,
"z": [1,2,3]
}
}
input = StringIO.new(json)
obj = Oj.wab_load(input)
assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
end
def test_io_file
filename = File.join(__dir__, 'open_file_test.json')
File.write(filename, %{{
"x":true,
"y":58,
"z": [1,2,3]
}
})
f = File.new(filename)
obj = Oj.wab_load(f)
f.close()
assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
end
def test_symbol
json = Oj.dump(:abc, mode: :wab)
assert_equal('"abc"', json)
end
def test_time
t = Time.gm(2017, 1, 5, 23, 58, 7, 123_456.789)
json = Oj.dump(t, mode: :wab)
assert_equal('"2017-01-05T23:58:07.123456789Z"', json)
# must load and convert to json as the Time#== does not match identical
# times due to the way it tracks fractional seconds.
loaded = Oj.wab_load(json)
assert_equal(json, Oj.dump(loaded, mode: :wab), 'json mismatch after load')
end
def test_uuid
u = ::WAB::UUID.new('123e4567-e89b-12d3-a456-426655440000')
json = Oj.dump(u, mode: :wab)
assert_equal('"123e4567-e89b-12d3-a456-426655440000"', json)
dump_and_load(u, false)
end
def test_uri
u = URI('http://opo.technology/sample')
json = Oj.dump(u, mode: :wab)
assert_equal('"http://opo.technology/sample"', json)
dump_and_load(u, false)
end
def test_class
assert_raises() { Oj.dump(WabJuice, mode: :wab) }
end
def test_module
assert_raises() { Oj.dump(TestModule, mode: :wab) }
end
# symbol_keys option
def test_symbol_keys
json = %{{
"x":true,
"y":58,
"z": [1,2,3]
}
}
obj = Oj.wab_load(json, :symbol_keys => true)
assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
end
# comments
def test_comment_slash
json = %{{
"x":true,//three
"y":58,
"z": [1,2,
3 // six
]}
}
obj = Oj.wab_load(json)
assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
end
def test_comment_c
json = %{{
"x"/*one*/:/*two*/true,
"y":58,
"z": [1,2,3]}
}
obj = Oj.wab_load(json)
assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
end
def test_comment
json = %{{
"x"/*one*/:/*two*/true,//three
"y":58/*four*/,
"z": [1,2/*five*/,
3 // six
]
}
}
obj = Oj.wab_load(json)
assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
end
def test_double
json = %{{ "x": 1}{ "y": 2}}
results = []
Oj.load(json, :mode => :wab) { |x| results << x }
assert_equal([{ x: 1 }, { y: 2 }], results)
end
def dump_and_load(obj, trace=false)
json = Oj.dump(obj, mode: :wab, indent: 2)
puts json if trace
loaded = Oj.wab_load(json)
if obj.nil?
assert_nil(loaded)
else
assert_equal(obj, loaded)
end
loaded
end
end
|