2007-06-04 08:31:26 -04:00
|
|
|
#!/usr/bin/env ruby
|
2013-02-11 22:05:45 -05:00
|
|
|
# encoding: utf-8
|
2007-06-04 08:31:26 -04:00
|
|
|
|
|
|
|
require 'test/unit'
|
2011-07-10 04:01:04 -04:00
|
|
|
require File.join(File.dirname(__FILE__), 'setup_variant')
|
2007-06-04 08:31:26 -04:00
|
|
|
|
2012-05-07 12:00:49 -04:00
|
|
|
class TestJSONUnicode < Test::Unit::TestCase
|
2007-06-04 08:31:26 -04:00
|
|
|
include JSON
|
|
|
|
|
|
|
|
def test_unicode
|
|
|
|
assert_equal '""', ''.to_json
|
|
|
|
assert_equal '"\\b"', "\b".to_json
|
|
|
|
assert_equal '"\u0001"', 0x1.chr.to_json
|
|
|
|
assert_equal '"\u001f"', 0x1f.chr.to_json
|
|
|
|
assert_equal '" "', ' '.to_json
|
|
|
|
assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json
|
|
|
|
utf8 = [ "© ≠ €! \01" ]
|
2010-04-25 20:06:35 -04:00
|
|
|
json = '["© ≠ €! \u0001"]'
|
|
|
|
assert_equal json, utf8.to_json(:ascii_only => false)
|
|
|
|
assert_equal utf8, parse(json)
|
2007-06-04 08:31:26 -04:00
|
|
|
json = '["\u00a9 \u2260 \u20ac! \u0001"]'
|
2010-04-25 20:06:35 -04:00
|
|
|
assert_equal json, utf8.to_json(:ascii_only => true)
|
|
|
|
assert_equal utf8, parse(json)
|
|
|
|
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
|
|
|
|
json = "[\"\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212\"]"
|
2007-06-04 08:31:26 -04:00
|
|
|
assert_equal utf8, parse(json)
|
2010-04-25 20:06:35 -04:00
|
|
|
assert_equal json, utf8.to_json(:ascii_only => false)
|
2007-06-04 08:31:26 -04:00
|
|
|
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
|
2010-04-25 20:06:35 -04:00
|
|
|
assert_equal utf8, parse(json)
|
2007-06-04 08:31:26 -04:00
|
|
|
json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]"
|
2010-04-25 20:06:35 -04:00
|
|
|
assert_equal json, utf8.to_json(:ascii_only => true)
|
2007-06-04 08:31:26 -04:00
|
|
|
assert_equal utf8, parse(json)
|
|
|
|
utf8 = ['საქართველო']
|
2010-04-25 20:06:35 -04:00
|
|
|
json = '["საქართველო"]'
|
|
|
|
assert_equal json, utf8.to_json(:ascii_only => false)
|
2007-06-04 08:31:26 -04:00
|
|
|
json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]"
|
2010-04-25 20:06:35 -04:00
|
|
|
assert_equal json, utf8.to_json(:ascii_only => true)
|
2007-06-04 08:31:26 -04:00
|
|
|
assert_equal utf8, parse(json)
|
2010-04-25 20:06:35 -04:00
|
|
|
assert_equal '["Ã"]', JSON.generate(["Ã"], :ascii_only => false)
|
|
|
|
assert_equal '["\\u00c3"]', JSON.generate(["Ã"], :ascii_only => true)
|
2007-06-04 08:31:26 -04:00
|
|
|
assert_equal ["€"], JSON.parse('["\u20ac"]')
|
|
|
|
utf8 = ["\xf0\xa0\x80\x81"]
|
2010-04-25 20:06:35 -04:00
|
|
|
json = "[\"\xf0\xa0\x80\x81\"]"
|
|
|
|
assert_equal json, JSON.generate(utf8, :ascii_only => false)
|
|
|
|
assert_equal utf8, JSON.parse(json)
|
2007-06-04 08:31:26 -04:00
|
|
|
json = '["\ud840\udc01"]'
|
2010-04-25 20:06:35 -04:00
|
|
|
assert_equal json, JSON.generate(utf8, :ascii_only => true)
|
2007-06-04 08:31:26 -04:00
|
|
|
assert_equal utf8, JSON.parse(json)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chars
|
|
|
|
(0..0x7f).each do |i|
|
|
|
|
json = '["\u%04x"]' % i
|
2007-06-06 18:38:42 -04:00
|
|
|
if RUBY_VERSION >= "1.9."
|
|
|
|
i = i.chr
|
|
|
|
end
|
|
|
|
assert_equal i, JSON.parse(json).first[0]
|
|
|
|
if i == ?\b
|
2007-06-04 08:31:26 -04:00
|
|
|
generated = JSON.generate(["" << i])
|
|
|
|
assert '["\b"]' == generated || '["\10"]' == generated
|
2007-06-06 18:38:42 -04:00
|
|
|
elsif [?\n, ?\r, ?\t, ?\f].include?(i)
|
2007-06-04 08:31:26 -04:00
|
|
|
assert_equal '[' << ('' << i).dump << ']', JSON.generate(["" << i])
|
2007-06-06 18:38:42 -04:00
|
|
|
elsif i.chr < 0x20.chr
|
2007-06-04 08:31:26 -04:00
|
|
|
assert_equal json, JSON.generate(["" << i])
|
|
|
|
end
|
|
|
|
end
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(JSON::GeneratorError) do
|
2010-04-25 20:06:35 -04:00
|
|
|
JSON.generate(["\x80"], :ascii_only => true)
|
2007-06-04 08:31:26 -04:00
|
|
|
end
|
|
|
|
assert_equal "\302\200", JSON.parse('["\u0080"]').first
|
|
|
|
end
|
|
|
|
end
|