summary refs log tree commit
path: root/tests
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2009-10-26 12:48:08 +0100
committerFlorian Frank <flori@ping.de>2009-10-26 22:58:08 +0100
commitdd06e48aa414674f52e81f9cdc7836b6456c04f8 (patch)
tree531d1f78eed1bbab7fae33d8b77f1544a2b11832 /tests
parent4852738aad89a27fbb48673eb2604f381bc8b811 (diff)
downloadruby-json-dd06e48aa414674f52e81f9cdc7836b6456c04f8.tar.gz
optimised speed a lot, added only_ascii mode
Diffstat (limited to 'tests')
-rw-r--r--tests/test_json_encoding.rb7
-rwxr-xr-xtests/test_json_unicode.rb26
2 files changed, 24 insertions, 9 deletions
diff --git a/tests/test_json_encoding.rb b/tests/test_json_encoding.rb
index bfb3e60..fdea329 100644
--- a/tests/test_json_encoding.rb
+++ b/tests/test_json_encoding.rb
@@ -57,11 +57,12 @@ class TC_JSONEncoding < Test::Unit::TestCase
   end
 
   def test_generate
-    assert_equal @generated, JSON.generate(@parsed)
+    assert_equal @generated, JSON.generate(@parsed, :ascii_only => true)
     if defined?(::Encoding)
-      assert_equal @generated, JSON.generate(@utf_16_data)
+      assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
     else
-       assert_raises(JSON::GeneratorError) { JSON.generate(@utf_16_data) }
+      # XXX checking of correct utf8 data is not as strict (yet?) without :ascii_only
+      assert_raises(JSON::GeneratorError) { JSON.generate(@utf_16_data, :ascii_only => true) }
     end
   end
 end
diff --git a/tests/test_json_unicode.rb b/tests/test_json_unicode.rb
index 1454fe1..505f5d5 100755
--- a/tests/test_json_unicode.rb
+++ b/tests/test_json_unicode.rb
@@ -19,22 +19,36 @@ class TC_JSONUnicode < Test::Unit::TestCase
     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
+    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
+    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
+    assert_equal json, utf8.to_json(:ascii_only => true)
     assert_equal utf8, parse(json)
-    assert_equal '["\\u00c3"]', JSON.generate(["Ã"])
+    assert_equal '["Ã"]', JSON.generate(["Ã"], :ascii_only => false)
+    assert_equal '["\\u00c3"]', JSON.generate(["Ã"], :ascii_only => true)
     assert_equal ["€"], JSON.parse('["\u20ac"]')
     utf8 = ["\xf0\xa0\x80\x81"]
+    json = "[\"\xf0\xa0\x80\x81\"]"
+    assert_equal json, JSON.generate(utf8, :ascii_only => false)
+    assert_equal utf8, JSON.parse(json)
     json = '["\ud840\udc01"]'
-    assert_equal json, JSON.generate(utf8)
+    assert_equal json, JSON.generate(utf8, :ascii_only => true)
     assert_equal utf8, JSON.parse(json)
   end
 
@@ -55,7 +69,7 @@ class TC_JSONUnicode < Test::Unit::TestCase
       end
     end
     assert_raise(JSON::GeneratorError) do
-      JSON.generate(["\x80"])
+      JSON.generate(["\x80"], :ascii_only => true)
     end
     assert_equal "\302\200", JSON.parse('["\u0080"]').first
   end