summaryrefslogtreecommitdiff
path: root/test/json/test_json_encoding.rb
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-05 11:49:39 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-05 11:49:39 +0000
commit11306587383f345d54752f262bee5f67adaae2f0 (patch)
treed63e457298a3c27b345fd674c1557e1cb390248c /test/json/test_json_encoding.rb
parent5eff15d1bdda4efcdd8aa59c91c804e345394a1c (diff)
* ext/json/*, test/json/*: Update json-2.0.1.
Changes of 2.0.0: https://github.com/flori/json/blob/f679ebd0c69a94e3e70a897ac9a229f5779c2ee1/CHANGES.md#2015-09-11-200 Changes of 2.0.1: https://github.com/flori/json/blob/f679ebd0c69a94e3e70a897ac9a229f5779c2ee1/CHANGES.md#2016-07-01-201 [Feature #12542][ruby-dev:49706][fix GH-1395] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55576 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/json/test_json_encoding.rb')
-rw-r--r--test/json/test_json_encoding.rb103
1 files changed, 71 insertions, 32 deletions
diff --git a/test/json/test_json_encoding.rb b/test/json/test_json_encoding.rb
index 50fb4ea2793..ede6402842c 100644
--- a/test/json/test_json_encoding.rb
+++ b/test/json/test_json_encoding.rb
@@ -1,44 +1,33 @@
-#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: false
-
-require 'test/unit'
-require File.join(File.dirname(__FILE__), 'setup_variant')
+require 'test_helper'
class TestJSONEncoding < Test::Unit::TestCase
include JSON
def setup
- @utf_8 = '["© ≠ €!"]'
- @parsed = [ "© ≠ €!" ]
- @generated = '["\u00a9 \u2260 \u20ac!"]'
+ @utf_8 = '"© ≠ €!"'
+ @ascii_8bit = @utf_8.dup.force_encoding('ascii-8bit')
+ @parsed = "© ≠ €!"
+ @generated = '"\u00a9 \u2260 \u20ac!"'
if String.method_defined?(:encode)
- @utf_16_data = [@parsed.first.encode('utf-16be', 'utf-8')]
- @utf_8_ascii_8bit = @utf_8.dup.force_encoding(Encoding::ASCII_8BIT)
+ @utf_16_data = @parsed.encode('utf-16be', 'utf-8')
@utf_16be = @utf_8.encode('utf-16be', 'utf-8')
- @utf_16be_ascii_8bit = @utf_16be.dup.force_encoding(Encoding::ASCII_8BIT)
@utf_16le = @utf_8.encode('utf-16le', 'utf-8')
- @utf_16le_ascii_8bit = @utf_16le.dup.force_encoding(Encoding::ASCII_8BIT)
@utf_32be = @utf_8.encode('utf-32be', 'utf-8')
- @utf_32be_ascii_8bit = @utf_32be.dup.force_encoding(Encoding::ASCII_8BIT)
@utf_32le = @utf_8.encode('utf-32le', 'utf-8')
- @utf_32le_ascii_8bit = @utf_32le.dup.force_encoding(Encoding::ASCII_8BIT)
else
require 'iconv'
- @utf_16_data = Iconv.iconv('utf-16be', 'utf-8', @parsed.first)
- @utf_8_ascii_8bit = @utf_8.dup
+ @utf_16_data, = Iconv.iconv('utf-16be', 'utf-8', @parsed)
@utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
- @utf_16be_ascii_8bit = @utf_16be.dup
@utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
- @utf_16le_ascii_8bit = @utf_16le.dup
@utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
- @utf_32be_ascii_8bit = @utf_32be.dup
@utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
- @utf_32le_ascii_8bit = @utf_32le.dup
end
end
def test_parse
+ assert_equal @parsed, JSON.parse(@ascii_8bit)
assert_equal @parsed, JSON.parse(@utf_8)
assert_equal @parsed, JSON.parse(@utf_16be)
assert_equal @parsed, JSON.parse(@utf_16le)
@@ -46,21 +35,71 @@ class TestJSONEncoding < Test::Unit::TestCase
assert_equal @parsed, JSON.parse(@utf_32le)
end
- def test_parse_ascii_8bit
- assert_equal @parsed, JSON.parse(@utf_8_ascii_8bit)
- assert_equal @parsed, JSON.parse(@utf_16be_ascii_8bit)
- assert_equal @parsed, JSON.parse(@utf_16le_ascii_8bit)
- assert_equal @parsed, JSON.parse(@utf_32be_ascii_8bit)
- assert_equal @parsed, JSON.parse(@utf_32le_ascii_8bit)
- end
-
def test_generate
assert_equal @generated, JSON.generate(@parsed, :ascii_only => true)
- if defined?(::Encoding)
- assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
- else
- # XXX checking of correct utf8 data is not as strict (yet?) without :ascii_only
- assert_raise(JSON::GeneratorError) { JSON.generate(@utf_16_data, :ascii_only => true) }
+ assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
+ 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 = '["© ≠ €! \u0001"]'
+ assert_equal json, utf8.to_json(:ascii_only => false)
+ assert_equal utf8, parse(json)
+ json = '["\u00a9 \u2260 \u20ac! \u0001"]'
+ 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\"]"
+ assert_equal utf8, parse(json)
+ assert_equal json, utf8.to_json(:ascii_only => false)
+ utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
+ assert_equal utf8, parse(json)
+ json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]"
+ assert_equal json, utf8.to_json(:ascii_only => true)
+ assert_equal utf8, parse(json)
+ utf8 = ['საქართველო']
+ json = '["საქართველო"]'
+ assert_equal json, utf8.to_json(:ascii_only => false)
+ json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]"
+ assert_equal json, utf8.to_json(:ascii_only => true)
+ assert_equal utf8, parse(json)
+ assert_equal '["Ã"]', generate(["Ã"], :ascii_only => false)
+ assert_equal '["\\u00c3"]', generate(["Ã"], :ascii_only => true)
+ assert_equal ["€"], parse('["\u20ac"]')
+ utf8 = ["\xf0\xa0\x80\x81"]
+ json = "[\"\xf0\xa0\x80\x81\"]"
+ assert_equal json, generate(utf8, :ascii_only => false)
+ assert_equal utf8, parse(json)
+ json = '["\ud840\udc01"]'
+ assert_equal json, generate(utf8, :ascii_only => true)
+ assert_equal utf8, parse(json)
+ end
+
+ def test_chars
+ (0..0x7f).each do |i|
+ json = '["\u%04x"]' % i
+ if RUBY_VERSION >= "1.9."
+ i = i.chr
+ end
+ assert_equal i, parse(json).first[0]
+ if i == ?\b
+ generated = generate(["" << i])
+ assert '["\b"]' == generated || '["\10"]' == generated
+ elsif [?\n, ?\r, ?\t, ?\f].include?(i)
+ assert_equal '[' << ('' << i).dump << ']', generate(["" << i])
+ elsif i.chr < 0x20.chr
+ assert_equal json, generate(["" << i])
+ end
+ end
+ assert_raise(JSON::GeneratorError) do
+ generate(["\x80"], :ascii_only => true)
end
+ assert_equal "\302\200", parse('["\u0080"]').first
end
end