From e567a5eb1afe1ac38f1da37f1c1e3922bbf79d2a Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Tue, 27 Dec 2005 03:11:03 +0000 Subject: Add ActiveSupport::JSON and Object#to_json for converting Ruby objects to JSON strings git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3356 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../test/core_ext/object_and_class_ext_test.rb | 7 +++ activesupport/test/core_ext/string_ext_test.rb | 10 ++++ activesupport/test/json.rb | 53 ++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 activesupport/test/json.rb (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index 25420b7f6f..22e4c6b8b4 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -104,4 +104,11 @@ class ObjectInstanceVariableTest < Test::Unit::TestCase assert !@dest.instance_variables.include?('@quux') assert_equal 'baz', @dest.instance_variable_get('@baz') end + + def test_instance_values + object = Object.new + object.instance_variable_set :@a, 1 + object.instance_variable_set :@b, 2 + assert_equal({'a' => 1, 'b' => 2}, object.instance_values) + end end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 0e82badad7..92f57beb5f 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -89,4 +89,14 @@ class StringInflectionsTest < Test::Unit::TestCase assert s.ends_with?('lo') assert !s.ends_with?('el') end + + def test_each_char_with_utf8_string_when_kcode_is_utf8 + old_kcode, $KCODE = $KCODE, 'UTF8' + '€2.99'.each_char do |char| + assert_not_equal 1, char.length + break + end + ensure + $KCODE = old_kcode + end end diff --git a/activesupport/test/json.rb b/activesupport/test/json.rb new file mode 100644 index 0000000000..c5b597abd7 --- /dev/null +++ b/activesupport/test/json.rb @@ -0,0 +1,53 @@ +$:.unshift File.dirname(__FILE__) + '/../lib' +require 'active_support' +require 'test/unit' + +class Foo + def initialize(a, b) + @a, @b = a, b + end +end + +class TestJSONEmitters < Test::Unit::TestCase + TrueTests = [[ true, %(true) ]] + FalseTests = [[ false, %(false) ]] + NilTests = [[ nil, %(null) ]] + NumericTests = [[ 1, %(1) ], + [ 2.5, %(2.5) ]] + + StringTests = [[ 'this is the string', %("this is the string") ], + [ 'a "string" with quotes', %("a \\"string\\" with quotes") ]] + + ArrayTests = [[ ['a', 'b', 'c'], %([\"a\", \"b\", \"c\"]) ], + [ [1, 'a', :b, nil, false], %([1, \"a\", \"b\", null, false]) ]] + + HashTests = [[ {:a => :b, :c => :d}, %({\"c\": \"d\", \"a\": \"b\"}) ]] + + SymbolTests = [[ :a, %("a") ], + [ :this, %("this") ], + [ :"a b", %("a b") ]] + + ObjectTests = [[ Foo.new(1, 2), %({\"a\": 1, \"b\": 2}) ]] + + constants.grep(/Tests$/).each do |class_tests| + define_method("test_#{class_tests[0..-6].downcase}") do + self.class.const_get(class_tests).each do |pair| + assert_equal pair.last, pair.first.to_json + end + end + end + + def test_utf8_string_encoded_properly_when_kcode_is_utf8 + old_kcode, $KCODE = $KCODE, 'UTF8' + assert_equal '"\\u20ac2.99"', '€2.99'.to_json + assert_equal '"\\u270e\\u263a"', '✎☺'.to_json + ensure + $KCODE = old_kcode + end + + def test_exception_raised_when_encoding_circular_reference + a = [1] + a << a + assert_raises(ActiveSupport::JSON::CircularReferenceError) { a.to_json } + end +end -- cgit v1.2.3