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
|
#!/usr/bin/env ruby
# frozen_string_literal: true
$LOAD_PATH << __dir__
require 'helper'
$json = %{{
"array": [
{
"num" : 3,
"string": "message",
"hash" : {
"h2" : {
"a" : [ 1, 2, 3 ]
}
}
}
],
"boolean" : true
}}
class AllSaj < Oj::Saj
attr_accessor :calls
def initialize
@calls = []
super
end
def hash_start(key)
@calls << [:hash_start, key]
end
def hash_end(key)
@calls << [:hash_end, key]
end
def array_start(key)
@calls << [:array_start, key]
end
def array_end(key)
@calls << [:array_end, key]
end
def add_value(value, key)
@calls << [:add_value, value, key]
end
def error(message, line, column)
@calls << [:error, message, line, column]
end
end # AllSaj
class SajTest < Minitest::Test
def setup
@default_options = Oj.default_options
end
def teardown
Oj.default_options = @default_options
end
def test_nil
handler = AllSaj.new()
json = %{null}
Oj.saj_parse(handler, json)
assert_equal([[:add_value, nil, nil]], handler.calls)
end
def test_true
handler = AllSaj.new()
json = %{true}
Oj.saj_parse(handler, json)
assert_equal([[:add_value, true, nil]], handler.calls)
end
def test_false
handler = AllSaj.new()
json = %{false}
Oj.saj_parse(handler, json)
assert_equal([[:add_value, false, nil]], handler.calls)
end
def test_string
handler = AllSaj.new()
json = %{"a string"}
Oj.saj_parse(handler, json)
assert_equal([[:add_value, 'a string', nil]], handler.calls)
end
def test_fixnum
handler = AllSaj.new()
json = %{12345}
Oj.saj_parse(handler, json)
assert_equal([[:add_value, 12_345, nil]], handler.calls)
end
def test_float
handler = AllSaj.new()
json = %{12345.6789}
Oj.saj_parse(handler, json)
assert_equal([[:add_value, 12_345.6789, nil]], handler.calls)
end
def test_float_exp
handler = AllSaj.new()
json = %{12345.6789e7}
Oj.saj_parse(handler, json)
assert_equal(1, handler.calls.size)
assert_equal(:add_value, handler.calls[0][0])
assert_equal((12_345.6789e7 * 10_000).to_i, (handler.calls[0][1] * 10_000).to_i)
end
def test_array_empty
handler = AllSaj.new()
json = %{[]}
Oj.saj_parse(handler, json)
assert_equal([[:array_start, nil],
[:array_end, nil]], handler.calls)
end
def test_array
handler = AllSaj.new()
json = %{[true,false]}
Oj.saj_parse(handler, json)
assert_equal([[:array_start, nil],
[:add_value, true, nil],
[:add_value, false, nil],
[:array_end, nil]], handler.calls)
end
def test_hash_empty
handler = AllSaj.new()
json = %{{}}
Oj.saj_parse(handler, json)
assert_equal([[:hash_start, nil],
[:hash_end, nil]], handler.calls)
end
def test_hash
handler = AllSaj.new()
json = %{{"one":true,"two":false}}
Oj.saj_parse(handler, json)
assert_equal([[:hash_start, nil],
[:add_value, true, 'one'],
[:add_value, false, 'two'],
[:hash_end, nil]], handler.calls)
end
def test_full
handler = AllSaj.new()
Oj.saj_parse(handler, $json)
assert_equal([[:hash_start, nil],
[:array_start, 'array'],
[:hash_start, nil],
[:add_value, 3, 'num'],
[:add_value, 'message', 'string'],
[:hash_start, 'hash'],
[:hash_start, 'h2'],
[:array_start, 'a'],
[:add_value, 1, nil],
[:add_value, 2, nil],
[:add_value, 3, nil],
[:array_end, 'a'],
[:hash_end, 'h2'],
[:hash_end, 'hash'],
[:hash_end, nil],
[:array_end, 'array'],
[:add_value, true, 'boolean'],
[:hash_end, nil]], handler.calls)
end
def test_fixnum_bad
handler = AllSaj.new()
json = %{12345xyz}
Oj.saj_parse(handler, json)
assert_equal([:add_value, 12_345, nil], handler.calls.first)
type, message, line, column = handler.calls.last
assert_equal([:error, 1, 6], [type, line, column])
assert_match(%r{invalid format, extra characters at line 1, column 6 \[(?:[A-Za-z]:/)?(?:[a-z.]+/)*saj\.c:\d+\]}, message)
end
end
|