File: shared.rb

package info (click to toggle)
ruby-oj 3.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,852 kB
  • sloc: ansic: 19,402; ruby: 11,451; makefile: 17
file content (309 lines) | stat: -rw-r--r-- 7,863 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
# encoding: UTF-8

# The rails tests set this to true. Both Rails and the JSON gem monkey patch the
# as_json methods on several base classes. Depending on which one replaces the
# method last the behavior will be different. Oj.mimic_JSON abides by the same
# conflicting behavior and the tests reflect that.
$rails_monkey = false unless defined?($rails_monkey)

class SharedMimicTest < Minitest::Test
  class Jam
    attr_accessor :x, :y

    def initialize(x, y)
      @x = x
      @y = y
    end

    def eql?(o)
      self.class == o.class && @x == o.x && @y == o.y
    end
    alias == eql?

    def as_json()
      {"json_class" => self.class.to_s, "x" => @x, "y" => @y}
    end

    def self.json_create(h)
      self.new(h['x'], h['y'])
    end

  end # Jam

  def setup
    @default_options = Oj.default_options
    @time = Time.at(1400000000).utc
    @expected_time_string =
      if defined?(Rails)
        %{"2014-05-13T16:53:20.000Z"}
      else
        %{"2014-05-13 16:53:20 UTC"}
      end
  end

  def teardown
    Oj.default_options = @default_options
  end

# exception
  def test_exception
    begin
      JSON.parse("{")
      puts "Failed"
    rescue JSON::ParserError
      assert(true)
    rescue Exception
      assert(false, 'Expected a JSON::ParserError')
    end
  end

# dump
  def test_dump_string
    json = JSON.dump([1, true, nil, @time])
    if $rails_monkey
      assert_equal(%{[1,true,null,#{@expected_time_string}]}, json)
    else
      assert_equal(%{[1,true,null,{"json_class":"Time","s":1400000000,"n":0}]}, json)
    end
  end

  def test_dump_with_options
    Oj.default_options= {:indent => 2} # JSON this will not change anything
    json = JSON.dump([1, true, nil, @time])
    if $rails_monkey
      assert_equal(%{[
  1,
  true,
  null,
  #{@expected_time_string}
]
}, json)
    else
      assert_equal(%{[
  1,
  true,
  null,
  {
    "json_class":"Time",
    "s":1400000000,
    "n\":0
  }
]
}, json)
    end
  end

  def test_dump_io
    s = StringIO.new()
    json = JSON.dump([1, true, nil, @time], s)
    assert_equal(s, json)
    if $rails_monkey
      assert_equal(%{[1,true,null,#{@expected_time_string}]}, s.string)
    else
      assert_equal(%{[1,true,null,{"json_class":"Time","s":1400000000,"n":0}]}, s.string)
    end
  end
  # TBD options

  def test_dump_struct
    # anonymous Struct not supported by json so name it
    if Object.const_defined?("Struct::Abc")
      s = Struct::Abc
    else
      s = Struct.new("Abc", :a, :b, :c)
    end
    o = s.new(1, 'two', [true, false])
    json = JSON.dump(o)
    if o.respond_to?(:as_json)
      if $rails_monkey
        assert_equal(%|{"a":1,"b":"two","c":[true,false]}|, json)
      else
        assert_equal(%|{"json_class":"Struct::Abc","v":[1,"two",[true,false]]}|, json)
      end
    else
      j = '"' + o.to_s.gsub('"', '\\"') + '"'
      assert_equal(j, json)
    end
  end

# load
  def test_load_string
    json = %{{"a":1,"b":[true,false]}}
    obj = JSON.load(json)
    assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
  end

  def test_load_io
    json = %{{"a":1,"b":[true,false]}}
    obj = JSON.load(StringIO.new(json))
    assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
  end

  def test_load_proc
    Oj.mimic_JSON
    children = []
    json = %{{"a":1,"b":[true,false]}}
    if 'rubinius' == $ruby
      obj = JSON.load(json) {|x| children << x }
    else
      p = Proc.new {|x| children << x }
      obj = JSON.load(json, p)
    end
    assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
    assert([1, true, false, [true, false], { 'a' => 1, 'b' => [true, false]}] == children ||
           [true, false, [true, false], 1, { 'a' => 1, 'b' => [true, false]}] == children,
           "children don't match")
  end

  def test_parse_with_quirks_mode
    json = %{null}
    assert_equal(nil, JSON.parse(json, :quirks_mode => true))
    assert_raises(JSON::ParserError) { JSON.parse(json, :quirks_mode => false) }
    assert_raises(JSON::ParserError) { JSON.parse(json) }
  end

  def test_parse_with_empty_string
    Oj.mimic_JSON
    assert_raises(JSON::ParserError) { JSON.parse(' ') }
    assert_raises(JSON::ParserError) { JSON.parse("\t\t\n   ") }
  end

# []
  def test_bracket_load
    json = %{{"a":1,"b":[true,false]}}
    obj = JSON[json]
    assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
  end

  def test_bracket_dump
    json = JSON[[1, true, nil]]
    assert_equal(%{[1,true,null]}, json)
  end

# generate
  def test_generate
    json = JSON.generate({ 'a' => 1, 'b' => [true, false]})
    assert(%{{"a":1,"b":[true,false]}} == json ||
           %{{"b":[true,false],"a":1}} == json)
  end
  def test_generate_options
    json = JSON.generate({ 'a' => 1, 'b' => [true, false]},
                         :indent => "--",
                         :array_nl => "\n",
                         :object_nl => "#\n",
                         :space => "*",
                         :space_before => "~")
    assert(%{{#
--"a"~:*1,#
--"b"~:*[
----true,
----false
--]#
}} == json ||
%{{#
--"b"~:*[
----true,
----false
--],#
--"a"~:*1#
}} == json)
  end

# fast_generate
  def test_fast_generate
    json = JSON.generate({ 'a' => 1, 'b' => [true, false]})
    assert(%{{"a":1,"b":[true,false]}} == json ||
           %{{"b":[true,false],"a":1}} == json)
  end

# pretty_generate
  def test_pretty_generate
    json = JSON.pretty_generate({ 'a' => 1, 'b' => [true, false]})
    assert(%{{
  "a": 1,
  "b": [
    true,
    false
  ]
}} == json ||
%{{
  "b": [
    true,
    false
  ],
  "a": 1
}} == json)
  end

# parse
  def test_parse
    json = %{{"a":1,"b":[true,false]}}
    obj = JSON.parse(json)
    assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
  end
  def test_parse_sym_names
    json = %{{"a":1,"b":[true,false]}}
    obj = JSON.parse(json, :symbolize_names => true)
    assert_equal({ :a => 1, :b => [true, false]}, obj)
  end
  def test_parse_additions
    jam = Jam.new(true, 58)
    json = Oj.dump(jam, :mode => :compat, :use_to_json => true)
    obj = JSON.parse(json)
    assert_equal(jam, obj)
    obj = JSON.parse(json, :create_additions => true)
    assert_equal(jam, obj)
    obj = JSON.parse(json, :create_additions => false)
    assert_equal({'json_class' => 'SharedMimicTest::Jam', 'x' => true, 'y' => 58}, obj)
    json.gsub!('json_class', 'kson_class')
    JSON.create_id = 'kson_class'
    obj = JSON.parse(json, :create_additions => true)
    JSON.create_id = 'json_class'
    assert_equal(jam, obj)

    assert_nothing_raised { JSON.create_id = nil }
  end
  def test_parse_bang
    json = %{{"a":1,"b":[true,false]}}
    obj = JSON.parse!(json)
    assert_equal({ 'a' => 1, 'b' => [true, false]}, obj)
  end

# recurse_proc
  def test_recurse_proc
    children = []
    JSON.recurse_proc({ 'a' => 1, 'b' => [true, false]}) { |x| children << x }
    # JRuby 1.7.0 rb_yield() is broken and converts the [true, false] array into true
    unless 'jruby' == $ruby
      assert([1, true, false, [true, false], { 'a' => 1, 'b' => [true, false]}] == children ||
             [true, false, [true, false], 1, { 'b' => [true, false], 'a' => 1}] == children)
    end
  end

# make sure to_json is defined for object.
  def test_mimic_to_json
    {'a' => 1}.to_json()
    Object.new().to_json()
  end
end # SharedMimicTest

if defined?(ActiveSupport)
  class SharedMimicRailsTest < SharedMimicTest
    def test_activesupport_exception
      begin
        ActiveSupport::JSON.decode("{")
        puts "Failed"
      rescue ActiveSupport::JSON.parse_error
        assert(true)
      rescue Exception
        assert(false, 'Expected a JSON::ParserError')
      end
    end

    def test_activesupport_encode
      Oj.default_options= {:indent => 0}
      json = ActiveSupport::JSON.encode([1, true, nil])
      assert_equal(%{[1,true,null]}, json)
    end
  end # SharedMimicRailsTest
end