From af1c4167287b9353fec766f932fe4afe97116ad4 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: svn+ssh://ci.ruby-lang.org/ruby/trunk@12428 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/json/test_json_unicode.rb | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 test/json/test_json_unicode.rb (limited to 'test/json/test_json_unicode.rb') diff --git a/test/json/test_json_unicode.rb b/test/json/test_json_unicode.rb new file mode 100755 index 0000000000..a23e50edf9 --- /dev/null +++ b/test/json/test_json_unicode.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby + +require 'test/unit' +require 'json' + +class TC_JSONUnicode < Test::Unit::TestCase + include JSON + + def setup + $KCODE = 'UTF8' + end + + 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" ] + json = '["\u00a9 \u2260 \u20ac! \u0001"]' + assert_equal json, utf8.to_json + assert_equal utf8, parse(json) + utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"] + json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]" + assert_equal json, utf8.to_json + assert_equal utf8, parse(json) + utf8 = ['საქართველო'] + json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]" + assert_equal json, utf8.to_json + assert_equal utf8, parse(json) + assert_equal '["\\u00c3"]', JSON.generate(["Ã"]) + assert_equal ["€"], JSON.parse('["\u20ac"]') + utf8 = ["\xf0\xa0\x80\x81"] + json = '["\ud840\udc01"]' + assert_equal json, JSON.generate(utf8) + assert_equal utf8, JSON.parse(json) + end + + def test_chars + (0..0x7f).each do |i| + c = ('%c' % i)[0] # c is a character object + json = '["\u%04x"]' % i + assert_equal c, JSON.parse(json).first + if c == ?\b + generated = JSON.generate(["" << i]) + assert '["\b"]' == generated || '["\10"]' == generated + elsif [?\n, ?\r, ?\t, ?\f].include?(c) + assert_equal '[' << ('' << i).dump << ']', JSON.generate(["" << i]) + elsif i < 0x20 + assert_equal json, JSON.generate(["" << i]) + end + end + assert_raises(JSON::GeneratorError) do + JSON.generate(["" << 0x80]) + end + assert_equal "\302\200", JSON.parse('["\u0080"]').first + end +end -- cgit v1.2.3