Sha256: ee3421a62d1cc83f2e2665d8438b44e53f6f2063074c939cff44654e275d0027

Contents?: true

Size: 1.74 KB

Versions: 30

Compression:

Stored size: 1.74 KB

Contents

#!/usr/bin/env ruby
# encoding: 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

  def test_from_hash
    result  = GenericObject.from_hash(
      :foo => { :bar => { :baz => true }, :quux => [ { :foobar => true } ] })
    assert_kind_of GenericObject, result.foo
    assert_kind_of GenericObject, result.foo.bar
    assert_equal   true, result.foo.bar.baz
    assert_kind_of GenericObject, result.foo.quux.first
    assert_equal   true, result.foo.quux.first.foobar
    assert_equal   true, GenericObject.from_hash(true)
  end

  private

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

Version data entries

30 entries across 26 versions & 10 rubygems

Version Path
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/json-1.7.7/tests/test_json_generic_object.rb
vagrant-shell-0.2.9 demo/templates/vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
vagrant-shell-0.2.8 demo/templates/vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
swipe-rails-0.0.5 vendor/bundle/gems/json-1.7.7/tests/test_json_generic_object.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
vagrant-shell-0.2.6 vendor/bundle/gems/json-1.7.7/tests/test_json_generic_object.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
vagrant-shell-0.2.5 vendor/bundle/gems/json-1.7.7/tests/test_json_generic_object.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/json_pure-1.7.7/tests/test_json_generic_object.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
fc-webicons-0.0.1 vendor/bundle/ruby/1.9.1/gems/json-1.7.7/tests/test_json_generic_object.rb
vagrant-actionio-0.0.9 vendor/bundle/gems/json-1.7.7/tests/test_json_generic_object.rb