File: test_json_generic_object.rb

package info (click to toggle)
ruby-json 1.7.3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,296 kB
  • sloc: java: 3,380; ansic: 3,297; ruby: 2,632; makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,336 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
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'test/unit'
require File.join(File.dirname(__FILE__), 'setup_variant')
class TestJSONGenericObject < Test::Unit::TestCase
  include JSON

  def setup
    @go = GenericObject[ :a => 1, :b => 2 ]
  end

  def test_attributes
    assert_equal 1, @go.a
    assert_equal 1, @go[:a]
    assert_equal 2, @go.b
    assert_equal 2, @go[:b]
    assert_nil @go.c
    assert_nil @go[:c]
  end

  def test_generate_json
    switch_json_creatable do
      assert_equal @go, JSON(JSON(@go), :create_additions => true)
    end
  end

  def test_parse_json
    assert_kind_of Hash, JSON('{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }', :create_additions => true)
    switch_json_creatable do
      assert_equal @go, l = JSON('{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }', :create_additions => true)
      assert_equal 1, l.a
      assert_equal @go, l = JSON('{ "a": 1, "b": 2 }', :object_class => GenericObject)
      assert_equal 1, l.a
      assert_equal GenericObject[:a => GenericObject[:b => 2]],
        l = JSON('{ "a": { "b": 2 } }', :object_class => GenericObject)
      assert_equal 2, l.a.b
    end
  end

  private

  def switch_json_creatable
    JSON::GenericObject.json_creatable = true
    yield
  ensure
    JSON::GenericObject.json_creatable = false
  end
end