~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to test/json/test_json_unicode.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-06-06 11:58:24 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20070606115824-qzldkdwq3dvfpf84
Tags: 1.9.0+20070606-1
* new upstream snapshot. (2006-06-06)
* updated debian/generated-incs/* files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require 'test/unit'
 
4
require 'json'
 
5
 
 
6
class TC_JSONUnicode < Test::Unit::TestCase
 
7
  include JSON
 
8
 
 
9
  def setup
 
10
    $KCODE = 'UTF8'
 
11
  end
 
12
 
 
13
  def test_unicode
 
14
    assert_equal '""', ''.to_json
 
15
    assert_equal '"\\b"', "\b".to_json
 
16
    assert_equal '"\u0001"', 0x1.chr.to_json
 
17
    assert_equal '"\u001f"', 0x1f.chr.to_json
 
18
    assert_equal '" "', ' '.to_json
 
19
    assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json
 
20
    utf8 = [ "© ≠ €! \01" ]
 
21
    json = '["\u00a9 \u2260 \u20ac! \u0001"]'
 
22
    assert_equal json, utf8.to_json
 
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 = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]"
 
26
    assert_equal json, utf8.to_json
 
27
    assert_equal utf8, parse(json)
 
28
    utf8 = ['საქართველო']
 
29
    json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]"
 
30
    assert_equal json, utf8.to_json
 
31
    assert_equal utf8, parse(json)
 
32
    assert_equal '["\\u00c3"]', JSON.generate(["Ã"])
 
33
    assert_equal ["€"], JSON.parse('["\u20ac"]')
 
34
    utf8 = ["\xf0\xa0\x80\x81"]
 
35
    json = '["\ud840\udc01"]'
 
36
    assert_equal json, JSON.generate(utf8)
 
37
    assert_equal utf8, JSON.parse(json)
 
38
  end
 
39
 
 
40
  def test_chars
 
41
    (0..0x7f).each do |i|
 
42
      c = ('%c' % i)[0] # c is a character object
 
43
      json = '["\u%04x"]' % i
 
44
      assert_equal c, JSON.parse(json).first
 
45
      if c == ?\b
 
46
        generated = JSON.generate(["" << i])
 
47
        assert '["\b"]' == generated || '["\10"]' == generated
 
48
      elsif [?\n, ?\r, ?\t, ?\f].include?(c)
 
49
        assert_equal '[' << ('' << i).dump << ']', JSON.generate(["" << i])
 
50
      elsif i < 0x20
 
51
        assert_equal json, JSON.generate(["" << i])
 
52
      end
 
53
    end
 
54
    assert_raises(JSON::GeneratorError) do
 
55
      JSON.generate(["" << 0x80])
 
56
    end
 
57
    assert_equal "\302\200", JSON.parse('["\u0080"]').first
 
58
  end
 
59
end