File: test_mimic_as_json.rb

package info (click to toggle)
ruby-oj 3.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,508 kB
  • sloc: ansic: 16,262; ruby: 10,577; makefile: 2
file content (45 lines) | stat: -rwxr-xr-x 960 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env ruby
# encoding: UTF-8

$: << File.dirname(__FILE__)

require './test/helper'
require 'oj'

class ObjectFolder < Minitest::Test
  class Raccoon
    attr_accessor :name

    def initialize(name)
      @name = name
    end

    def as_json(options={})
      {:name => @name}.merge(options)
    end
  end

  def setup
    @default_options = Oj.default_options
  end

  def teardown
    Oj.default_options = @default_options
  end

  def test_as_json_options
    Oj.mimic_JSON()
    raccoon = Raccoon.new('Rocket')
    json = raccoon.to_json()
    assert_equal(json, '{"name":"Rocket"}')

    json = raccoon.to_json(:occupation => 'bounty hunter')
    # depending on the ruby version the order of the hash members maybe different.
    if (json.start_with?('{"name'))
        assert_equal(json, '{"name":"Rocket","occupation":"bounty hunter"}')
    else
        assert_equal(json, '{"occupation":"bounty hunter","name":"Rocket"}')
    end
  end

end