Class: TC_JSONEncoding

Inherits:
Test::Unit::TestCase
  • Object
show all
Includes:
JSON
Defined in:
lib/vendor/json_pure/tests/test_json_encoding.rb

Constant Summary

Constants included from JSON

JSON::Infinity, JSON::JSON_LOADED, JSON::MAP, JSON::MinusInfinity, JSON::NaN, JSON::UnparserError, JSON::VERSION, JSON::VERSION_ARRAY, JSON::VERSION_BUILD, JSON::VERSION_MAJOR, JSON::VERSION_MINOR

Instance Method Summary collapse

Methods included from JSON

[], deep_const_get, dump, fast_generate, generate, iconv, load, parse, parse!, pretty_generate, recurse_proc, swap!, utf8_to_json, utf8_to_json_ascii

Instance Method Details

#setupObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vendor/json_pure/tests/test_json_encoding.rb', line 15

def setup
  @utf_8 = '["© ≠ €!"]'
  @parsed = [ "© ≠ €!" ]
  @utf_16_data = Iconv.iconv('utf-16be', 'utf-8', @parsed.first)
  @generated = '["\u00a9 \u2260 \u20ac!"]'
  if defined?(::Encoding)
    @utf_8_ascii_8bit = @utf_8.dup.force_encoding(Encoding::ASCII_8BIT)
    @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
    @utf_16be_ascii_8bit = @utf_16be.dup.force_encoding(Encoding::ASCII_8BIT)
    @utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
    @utf_16le_ascii_8bit = @utf_16le.dup.force_encoding(Encoding::ASCII_8BIT)
    @utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
    @utf_32be_ascii_8bit = @utf_32be.dup.force_encoding(Encoding::ASCII_8BIT)
    @utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
    @utf_32le_ascii_8bit = @utf_32le.dup.force_encoding(Encoding::ASCII_8BIT)
  else
    @utf_8_ascii_8bit = @utf_8.dup
    @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

#test_generateObject



59
60
61
62
63
64
65
66
67
# File 'lib/vendor/json_pure/tests/test_json_encoding.rb', line 59

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_raises(JSON::GeneratorError) { JSON.generate(@utf_16_data, :ascii_only => true) }
  end
end

#test_parseObject



43
44
45
46
47
48
49
# File 'lib/vendor/json_pure/tests/test_json_encoding.rb', line 43

def test_parse
  assert_equal @parsed, JSON.parse(@utf_8)
  assert_equal @parsed, JSON.parse(@utf_16be)
  assert_equal @parsed, JSON.parse(@utf_16le)
  assert_equal @parsed, JSON.parse(@utf_32be)
  assert_equal @parsed, JSON.parse(@utf_32le)
end

#test_parse_ascii_8bitObject



51
52
53
54
55
56
57
# File 'lib/vendor/json_pure/tests/test_json_encoding.rb', line 51

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