ace56cae361685f9b1b3f2161922b6e2a97981a7
[packages/precise/mcollective.git] / lib / mcollective / vendor / json / tests / test_json_unicode.rb
1 #!/usr/bin/env ruby
2 # -*- coding: utf-8 -*-
3
4 require 'test/unit'
5 require File.join(File.dirname(__FILE__), 'setup_variant')
6
7 class TC_JSONUnicode < Test::Unit::TestCase
8   include JSON
9
10   def test_unicode
11     assert_equal '""', ''.to_json
12     assert_equal '"\\b"', "\b".to_json
13     assert_equal '"\u0001"', 0x1.chr.to_json
14     assert_equal '"\u001f"', 0x1f.chr.to_json
15     assert_equal '" "', ' '.to_json
16     assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json
17     utf8 = [ "© ≠ €! \01" ]
18     json = '["© ≠ €! \u0001"]'
19     assert_equal json, utf8.to_json(:ascii_only => false)
20     assert_equal utf8, parse(json)
21     json = '["\u00a9 \u2260 \u20ac! \u0001"]'
22     assert_equal json, utf8.to_json(:ascii_only => true)
23     assert_equal utf8, parse(json)
24     utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
25     json = "[\"\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212\"]"
26     assert_equal utf8, parse(json)
27     assert_equal json, utf8.to_json(:ascii_only => false)
28     utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
29     assert_equal utf8, parse(json)
30     json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]"
31     assert_equal json, utf8.to_json(:ascii_only => true)
32     assert_equal utf8, parse(json)
33     utf8 = ['საქართველო']
34     json = '["საქართველო"]'
35     assert_equal json, utf8.to_json(:ascii_only => false)
36     json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]"
37     assert_equal json, utf8.to_json(:ascii_only => true)
38     assert_equal utf8, parse(json)
39     assert_equal '["Ã"]', JSON.generate(["Ã"], :ascii_only => false)
40     assert_equal '["\\u00c3"]', JSON.generate(["Ã"], :ascii_only => true)
41     assert_equal ["€"], JSON.parse('["\u20ac"]')
42     utf8 = ["\xf0\xa0\x80\x81"]
43     json = "[\"\xf0\xa0\x80\x81\"]"
44     assert_equal json, JSON.generate(utf8, :ascii_only => false)
45     assert_equal utf8, JSON.parse(json)
46     json = '["\ud840\udc01"]'
47     assert_equal json, JSON.generate(utf8, :ascii_only => true)
48     assert_equal utf8, JSON.parse(json)
49   end
50
51   def test_chars
52     (0..0x7f).each do |i|
53       json = '["\u%04x"]' % i
54       if RUBY_VERSION >= "1.9."
55         i = i.chr
56       end
57       assert_equal i, JSON.parse(json).first[0]
58       if i == ?\b
59         generated = JSON.generate(["" << i])
60         assert '["\b"]' == generated || '["\10"]' == generated
61       elsif [?\n, ?\r, ?\t, ?\f].include?(i)
62         assert_equal '[' << ('' << i).dump << ']', JSON.generate(["" << i])
63       elsif i.chr < 0x20.chr
64         assert_equal json, JSON.generate(["" << i])
65       end
66     end
67     assert_raise(JSON::GeneratorError) do
68       JSON.generate(["\x80"], :ascii_only => true)
69     end
70     assert_equal "\302\200", JSON.parse('["\u0080"]').first
71   end
72 end