File: test_parser_saj.rb

package info (click to toggle)
ruby-oj 3.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,852 kB
  • sloc: ansic: 19,402; ruby: 11,451; makefile: 17
file content (337 lines) | stat: -rwxr-xr-x 8,443 bytes parent folder | download
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
#!/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 LocSaj
  attr_accessor :calls

  def initialize
    @calls = []
  end

  def hash_start(key, line, column)
    @calls << [:hash_start, key, line, column]
  end

  def hash_end(key, line, column)
    @calls << [:hash_end, key, line, column]
  end

  def array_start(key, line, column)
    @calls << [:array_start, key, line, column]
  end

  def array_end(key, line, column)
    @calls << [:array_end, key, line, column]
  end

  def add_value(value, key, line, column)
    @calls << [:add_value, value, key, line, column]
  end

end # LocSaj

class SajTest < Minitest::Test

  def test_nil
    handler = AllSaj.new()
    json = %{null}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal([[:add_value, nil, nil]], handler.calls)
  end

  def test_true
    handler = AllSaj.new()
    json = %{true}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal([[:add_value, true, nil]], handler.calls)
  end

  def test_false
    handler = AllSaj.new()
    json = %{false}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal([[:add_value, false, nil]], handler.calls)
  end

  def test_string
    handler = AllSaj.new()
    json = %{"a string"}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal([[:add_value, 'a string', nil]], handler.calls)
  end

  def test_fixnum
    handler = AllSaj.new()
    json = %{12345}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal([[:add_value, 12_345, nil]], handler.calls)
  end

  def test_float
    handler = AllSaj.new()
    json = %{12345.6789}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal([[:add_value, 12_345.6789, nil]], handler.calls)
  end

  def test_float_exp
    handler = AllSaj.new()
    json = %{12345.6789e7}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(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_bignum
    handler = AllSaj.new()
    json = %{-11.899999999999999}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal(1, handler.calls.size)
    assert_equal(:add_value, handler.calls[0][0])
    assert_equal(-118_999, (handler.calls[0][1] * 10_000).to_i)
  end

  def test_bignum_loc
    handler = LocSaj.new()
    json = <<~JSON
      {
        "width": 192.33800000000002,
        "xaxis": {
          "anchor": "y"
        }
      }
    JSON

    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal(6, handler.calls.size)
    assert_equal(1_923_380, (handler.calls[1][1] * 10_000).to_i)
    handler.calls[1][1] = 1_923_380
    assert_equal([[:hash_start, nil, 1, 1],
                  [:add_value, 1_923_380, 'width', 2, 30],
                  [:hash_start, 'xaxis', 3, 12],
                  [:add_value, 'y', 'anchor', 4, 17],
                  [:hash_end, 'xaxis', 5, 3],
                  [:hash_end, nil, 6, 1]],
                 handler.calls)
  end

  def test_array_empty
    handler = AllSaj.new()
    json = %{[]}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal([[:array_start, nil],
                  [:array_end, nil]], handler.calls)
  end

  def test_array
    handler = AllSaj.new()
    json = %{[true,false]}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(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 = %{{}}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal([[:hash_start, nil],
                  [:hash_end, nil]], handler.calls)
  end

  def test_hash
    handler = AllSaj.new()
    json = %{{"one":true,"two":false}}
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(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_multiple
    handler = AllSaj.new()
    json = %|[true][false]|
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.parse(json)
    assert_equal([
                   [:array_start, nil],
                   [:add_value, true, nil],
                   [:array_end, nil],
                   [:array_start, nil],
                   [:add_value, false, nil],
                   [:array_end, nil],
                 ], handler.calls)
  end

  def test_io
    handler = AllSaj.new()
    json = %| [true,false]  |
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.load(StringIO.new(json))
    assert_equal([
                   [:array_start, nil],
                   [:add_value, true, nil],
                   [:add_value, false, nil],
                   [:array_end, nil],
                 ], handler.calls)
  end

  def test_file
    handler = AllSaj.new()
    p = Oj::Parser.new(:saj)
    p.handler = handler
    p.file('saj_test.json')
    assert_equal([
                   [:array_start, nil],
                   [:add_value, true, nil],
                   [:add_value, false, nil],
                   [:array_end, nil],
                 ], handler.calls)
  end

  def test_default
    handler = AllSaj.new()
    json = %|[true]|
    Oj::Parser.saj.handler = handler
    Oj::Parser.saj.parse(json)
    assert_equal([
                   [:array_start, nil],
                   [:add_value, true, nil],
                   [:array_end, nil],
                 ], handler.calls)
  end

  def test_loc
    handler = LocSaj.new()
    Oj::Parser.saj.handler = handler
    Oj::Parser.saj.parse($json)
    assert_equal([[:hash_start, nil, 1, 1],
                  [:array_start, 'array', 2, 12],
                  [:hash_start, nil, 3, 5],
                  [:add_value, 3, 'num', 4, 18],
                  [:add_value, 'message', 'string', 5, 25],
                  [:hash_start, 'hash', 6, 17],
                  [:hash_start, 'h2', 7, 17],
                  [:array_start, 'a', 8, 17],
                  [:add_value, 1, nil, 8, 20],
                  [:add_value, 2, nil, 8, 23],
                  [:add_value, 3, nil, 8, 26],
                  [:array_end, 'a', 8, 27],
                  [:hash_end, 'h2', 9, 9],
                  [:hash_end, 'hash', 10, 7],
                  [:hash_end, nil, 11, 5],
                  [:array_end, 'array', 12, 3],
                  [:add_value, true, 'boolean', 13, 18],
                  [:hash_end, nil, 14, 1]], handler.calls)
  end

end