PageRenderTime 75ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/activesupport/test/json/encoding_test.rb

https://bitbucket.org/nicksieger/rails
Ruby | 139 lines | 114 code | 24 blank | 1 comment | 0 complexity | b773e491a11ca748b65280f388c87f3a MD5 | raw file
  1. # encoding: utf-8
  2. require 'abstract_unit'
  3. require 'active_support/json'
  4. class TestJSONEncoding < Test::Unit::TestCase
  5. class Foo
  6. def initialize(a, b)
  7. @a, @b = a, b
  8. end
  9. end
  10. TrueTests = [[ true, %(true) ]]
  11. FalseTests = [[ false, %(false) ]]
  12. NilTests = [[ nil, %(null) ]]
  13. NumericTests = [[ 1, %(1) ],
  14. [ 2.5, %(2.5) ]]
  15. StringTests = [[ 'this is the <string>', %("this is the \\u003Cstring\\u003E")],
  16. [ 'a "string" with quotes & an ampersand', %("a \\"string\\" with quotes \\u0026 an ampersand") ],
  17. [ 'http://test.host/posts/1', %("http://test.host/posts/1")]]
  18. ArrayTests = [[ ['a', 'b', 'c'], %([\"a\",\"b\",\"c\"]) ],
  19. [ [1, 'a', :b, nil, false], %([1,\"a\",\"b\",null,false]) ]]
  20. SymbolTests = [[ :a, %("a") ],
  21. [ :this, %("this") ],
  22. [ :"a b", %("a b") ]]
  23. ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
  24. VariableTests = [[ ActiveSupport::JSON::Variable.new('foo'), 'foo'],
  25. [ ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")']]
  26. RegexpTests = [[ /^a/, '/^a/' ], [/^\w{1,2}[a-z]+/ix, '/^\\w{1,2}[a-z]+/ix']]
  27. DateTests = [[ Date.new(2005,2,1), %("2005/02/01") ]]
  28. TimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]]
  29. DateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]]
  30. StandardDateTests = [[ Date.new(2005,2,1), %("2005-02-01") ]]
  31. StandardTimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005-02-01T15:15:10Z") ]]
  32. StandardDateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10+00:00") ]]
  33. StandardStringTests = [[ 'this is the <string>', %("this is the <string>")]]
  34. constants.grep(/Tests$/).each do |class_tests|
  35. define_method("test_#{class_tests[0..-6].underscore}") do
  36. begin
  37. ActiveSupport.escape_html_entities_in_json = class_tests !~ /^Standard/
  38. ActiveSupport.use_standard_json_time_format = class_tests =~ /^Standard/
  39. self.class.const_get(class_tests).each do |pair|
  40. assert_equal pair.last, ActiveSupport::JSON.encode(pair.first)
  41. end
  42. ensure
  43. ActiveSupport.escape_html_entities_in_json = false
  44. ActiveSupport.use_standard_json_time_format = false
  45. end
  46. end
  47. end
  48. def test_hash_encoding
  49. assert_equal %({\"a\":\"b\"}), ActiveSupport::JSON.encode(:a => :b)
  50. assert_equal %({\"a\":1}), ActiveSupport::JSON.encode('a' => 1)
  51. assert_equal %({\"a\":[1,2]}), ActiveSupport::JSON.encode('a' => [1,2])
  52. assert_equal %({"1":2}), ActiveSupport::JSON.encode(1 => 2)
  53. sorted_json = '{' + ActiveSupport::JSON.encode(:a => :b, :c => :d)[1..-2].split(',').sort.join(',') + '}'
  54. assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json
  55. end
  56. def test_utf8_string_encoded_properly_when_kcode_is_utf8
  57. with_kcode 'UTF8' do
  58. assert_equal '"\\u20ac2.99"', ActiveSupport::JSON.encode('€2.99')
  59. assert_equal '"\\u270e\\u263a"', ActiveSupport::JSON.encode('✎☺')
  60. end
  61. end
  62. def test_exception_raised_when_encoding_circular_reference
  63. a = [1]
  64. a << a
  65. assert_raise(ActiveSupport::JSON::CircularReferenceError) { ActiveSupport::JSON.encode(a) }
  66. end
  67. def test_hash_key_identifiers_are_always_quoted
  68. values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"}
  69. assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values))
  70. end
  71. def test_hash_should_allow_key_filtering_with_only
  72. assert_equal %({"a":1}), ActiveSupport::JSON.encode({'a' => 1, :b => 2, :c => 3}, :only => 'a')
  73. end
  74. def test_hash_should_allow_key_filtering_with_except
  75. assert_equal %({"b":2}), ActiveSupport::JSON.encode({'foo' => 'bar', :b => 2, :c => 3}, :except => ['foo', :c])
  76. end
  77. def test_time_to_json_includes_local_offset
  78. ActiveSupport.use_standard_json_time_format = true
  79. with_env_tz 'US/Eastern' do
  80. assert_equal %("2005-02-01T15:15:10-05:00"), ActiveSupport::JSON.encode(Time.local(2005,2,1,15,15,10))
  81. end
  82. ensure
  83. ActiveSupport.use_standard_json_time_format = false
  84. end
  85. def test_nested_hash_with_float
  86. assert_nothing_raised do
  87. hash = {
  88. "CHI" => {
  89. :dislay_name => "chicago",
  90. :latitude => 123.234
  91. }
  92. }
  93. result = ActiveSupport::JSON.encode(hash)
  94. end
  95. end
  96. protected
  97. def object_keys(json_object)
  98. json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
  99. end
  100. def with_env_tz(new_tz = 'US/Eastern')
  101. old_tz, ENV['TZ'] = ENV['TZ'], new_tz
  102. yield
  103. ensure
  104. old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
  105. end
  106. end
  107. class JsonOptionsTests < Test::Unit::TestCase
  108. def test_enumerable_should_passthrough_options_to_elements
  109. json_options = { :include => :posts }
  110. ActiveSupport::JSON.expects(:encode).with(1, json_options)
  111. ActiveSupport::JSON.expects(:encode).with(2, json_options)
  112. ActiveSupport::JSON.expects(:encode).with('foo', json_options)
  113. [1, 2, 'foo'].send(:rails_to_json, json_options)
  114. end
  115. end