From 4fac3a042baf75b31b7cc7d06ce9052d48ed9d92 Mon Sep 17 00:00:00 2001 From: naruse Date: Mon, 4 Jun 2007 12:31:26 +0000 Subject: * lib/json.rb, lib/json, ext/json, test/json: import JSON library. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@12428 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/json/test_json_addition.rb | 94 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 test/json/test_json_addition.rb (limited to 'test/json/test_json_addition.rb') diff --git a/test/json/test_json_addition.rb b/test/json/test_json_addition.rb new file mode 100755 index 000000000..e527f70a1 --- /dev/null +++ b/test/json/test_json_addition.rb @@ -0,0 +1,94 @@ +#!/usr/bin/env ruby + +require 'test/unit' +require 'json' + +class TC_JSONAddition < Test::Unit::TestCase + include JSON + + class A + def initialize(a) + @a = a + end + + attr_reader :a + + def ==(other) + a == other.a + end + + def self.json_create(object) + new(*object['args']) + end + + def to_json(*args) + { + 'json_class' => self.class, + 'args' => [ @a ], + }.to_json(*args) + end + end + + class B + def to_json(*args) + { + 'json_class' => self.class, + }.to_json(*args) + end + end + + class C + def to_json(*args) + { + 'json_class' => 'TC_JSONAddition::Nix', + }.to_json(*args) + end + end + + def setup + $KCODE = 'UTF8' + end + + def test_extended_json + a = A.new(666) + assert A.json_creatable? + json = generate(a) + a_again = JSON.parse(json) + assert_kind_of a.class, a_again + assert_equal a, a_again + end + + def test_extended_json_fail + b = B.new + assert !B.json_creatable? + json = generate(b) + assert_equal({ 'json_class' => B.name }, JSON.parse(json)) + end + + def test_extended_json_fail + c = C.new + assert !C.json_creatable? + json = generate(c) + assert_raises(ArgumentError) { JSON.parse(json) } + end + + def test_raw_strings + raw = '' + raw_array = [] + for i in 0..255 + raw << i + raw_array << i + end + json = raw.to_json_raw + json_raw_object = raw.to_json_raw_object + hash = { 'json_class' => 'String', 'raw'=> raw_array } + assert_equal hash, json_raw_object + json_raw = <