Class: TC_JSONGenerate

Inherits:
Test::Unit::TestCase
  • Object
show all
Includes:
JSON
Defined in:
lib/crazy_ivan/vendor/json-1.1.7/tests/test_json_generate.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, load, parse, parse!, pretty_generate, recurse_proc, swap!, utf8_to_json

Instance Method Details

#setupObject



14
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
42
43
44
45
# File 'lib/crazy_ivan/vendor/json-1.1.7/tests/test_json_generate.rb', line 14

def setup
  @hash = {
    'a' => 2,
    'b' => 3.141,
    'c' => 'c',
    'd' => [ 1, "b", 3.14 ],
    'e' => { 'foo' => 'bar' },
    'g' => "\"\0\037",
    'h' => 1000.0,
    'i' => 0.001
  }
  @json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
    '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
  @json3 = <<'EOT'.chomp
{
"a": 2,
"b": 3.141,
"c": "c",
"d": [
  1,
  "b",
  3.14
],
"e": {
  "foo": "bar"
},
"g": "\"\u0000\u001f",
"h": 1000.0,
"i": 0.001
}
EOT
end

#test_allow_nanObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/crazy_ivan/vendor/json-1.1.7/tests/test_json_generate.rb', line 89

def test_allow_nan
  assert_raises(GeneratorError) { generate([JSON::NaN]) }
  assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
  assert_equal '[NaN]', fast_generate([JSON::NaN])
  assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) }
  assert_equal "[\n  NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true)
  assert_raises(GeneratorError) { generate([JSON::Infinity]) }
  assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true)
  assert_equal '[Infinity]', fast_generate([JSON::Infinity])
  assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) }
  assert_equal "[\n  Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true)
  assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) }
  assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true)
  assert_equal '[-Infinity]', fast_generate([JSON::MinusInfinity])
  assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
  assert_equal "[\n  -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
end

#test_statesObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/crazy_ivan/vendor/json-1.1.7/tests/test_json_generate.rb', line 73

def test_states
  json = generate({1=>2}, nil)
  assert_equal('{"1":2}', json)
  s = JSON.state.new(:check_circular => true)
  #assert s.check_circular
  h = { 1=>2 }
  h[3] = h
  assert_raises(JSON::CircularDatastructure) {  generate(h) }
  assert_raises(JSON::CircularDatastructure) {  generate(h, s) }
  s = JSON.state.new(:check_circular => true)
  #assert s.check_circular
  a = [ 1, 2 ]
  a << a
  assert_raises(JSON::CircularDatastructure) {  generate(a, s) }
end

#test_unparseObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/crazy_ivan/vendor/json-1.1.7/tests/test_json_generate.rb', line 47

def test_unparse
  json = unparse(@hash)
  assert_equal(JSON.parse(@json2), JSON.parse(json))
  parsed_json = parse(json)
  assert_equal(@hash, parsed_json)
  json = generate({1=>2})
  assert_equal('{"1":2}', json)
  parsed_json = parse(json)
  assert_equal({"1"=>2}, parsed_json)
end

#test_unparse_prettyObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/crazy_ivan/vendor/json-1.1.7/tests/test_json_generate.rb', line 58

def test_unparse_pretty
  json = pretty_unparse(@hash)
  assert_equal(JSON.parse(@json3), JSON.parse(json))
  parsed_json = parse(json)
  assert_equal(@hash, parsed_json)
  json = pretty_generate({1=>2})
  assert_equal(<<'EOT'.chomp, json)
{
"1": 2
}
EOT
  parsed_json = parse(json)
  assert_equal({"1"=>2}, parsed_json)
end