Class: TC_JSON
- Includes:
- JSON
- Defined in:
- lib/vendor/json_pure/tests/test_json.rb
Defined Under Namespace
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
- #assert_equal_float(expected, is) ⇒ Object
- #setup ⇒ Object
- #test_backslash ⇒ Object
- #test_big_integers ⇒ Object
- #test_comments ⇒ Object
- #test_construction ⇒ Object
- #test_generation_of_core_subclasses ⇒ Object
- #test_load_dump ⇒ Object
- #test_nesting ⇒ Object
- #test_parse_array ⇒ Object
- #test_parse_array_custom_class ⇒ Object
- #test_parse_arrays ⇒ Object
- #test_parse_complex_objects ⇒ Object
- #test_parse_more_complex_arrays ⇒ Object
- #test_parse_object ⇒ Object
- #test_parse_object_custom_class ⇒ Object
- #test_parse_simple_arrays ⇒ Object
- #test_parse_simple_objects ⇒ Object
- #test_parse_values ⇒ Object
- #test_parser_reset ⇒ Object
- #test_symbolize_names ⇒ Object
- #test_wrong_inputs ⇒ Object
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
#assert_equal_float(expected, is) ⇒ Object
56 57 58 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 56 def assert_equal_float(expected, is) assert_in_delta(expected.first, is.first, 1e-2) end |
#setup ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 29 def setup @ary = [1, "foo", 3.14, 4711.0, 2.718, nil, [1,-2,3], false, true].map do |x| [x] end @ary_to_parse = ["1", '"foo"', "3.14", "4711.0", "2.718", "null", "[1,-2,3]", "false", "true"].map do |x| "[#{x}]" end @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 } @json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\ '"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}' end |
#test_backslash ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 251 def test_backslash data = [ '\\.(?i:gif|jpe?g|png)$' ] json = '["\\\\.(?i:gif|jpe?g|png)$"]' assert_equal json, JSON.generate(data) assert_equal data, JSON.parse(json) # data = [ '\\"' ] json = '["\\\\\""]' assert_equal json, JSON.generate(data) assert_equal data, JSON.parse(json) # json = '["/"]' data = JSON.parse(json) assert_equal ['/'], data assert_equal json, JSON.generate(data) # json = '["\""]' data = JSON.parse(json) assert_equal ['"'], data assert_equal json, JSON.generate(data) json = '["\\\'"]' data = JSON.parse(json) assert_equal ["'"], data assert_equal '["\'"]', JSON.generate(data) end |
#test_big_integers ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 349 def test_big_integers json1 = JSON([orig = (1 << 31) - 1]) assert_equal orig, JSON[json1][0] json2 = JSON([orig = 1 << 31]) assert_equal orig, JSON[json2][0] json3 = JSON([orig = (1 << 62) - 1]) assert_equal orig, JSON[json3][0] json4 = JSON([orig = 1 << 62]) assert_equal orig, JSON[json4][0] json5 = JSON([orig = 1 << 64]) assert_equal orig, JSON[json5][0] end |
#test_comments ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 211 def test_comments json = <<EOT { "key1":"value1", // eol comment "key2":"value2" /* multi line * comment */, "key3":"value3" /* multi line // nested eol comment * comment */ } EOT assert_equal( { "key1" => "value1", "key2" => "value2", "key3" => "value3" }, parse(json)) json = <<EOT { "key1":"value1" /* multi line // nested eol comment /* illegal nested multi line comment */ * comment */ } EOT assert_raises(ParserError) { parse(json) } json = <<EOT { "key1":"value1" /* multi line // nested eol comment closed multi comment */ and again, throw an Error */ } EOT assert_raises(ParserError) { parse(json) } json = <<EOT { "key1":"value1" /*/*/ } EOT assert_equal({ "key1" => "value1" }, parse(json)) end |
#test_construction ⇒ Object
51 52 53 54 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 51 def test_construction parser = JSON::Parser.new('test') assert_equal 'test', parser.source end |
#test_generation_of_core_subclasses ⇒ Object
195 196 197 198 199 200 201 202 203 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 195 def test_generation_of_core_subclasses obj = SubHash.new.merge( "foo" => SubHash.new.merge("bar" => true)) obj_json = JSON(obj) obj_again = JSON(obj_json) assert_kind_of SubHash, obj_again assert_kind_of SubHash, obj_again['foo'] assert obj_again['foo']['bar'] assert_equal obj, obj_again end |
#test_load_dump ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 333 def test_load_dump too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]' assert_equal too_deep, JSON.dump(eval(too_deep)) assert_kind_of String, Marshal.dump(eval(too_deep)) assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 19) } assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 19) } assert_equal too_deep, JSON.dump(eval(too_deep), 20) assert_kind_of String, Marshal.dump(eval(too_deep), 20) output = StringIO.new JSON.dump(eval(too_deep), output) assert_equal too_deep, output.string output = StringIO.new JSON.dump(eval(too_deep), output, 20) assert_equal too_deep, output.string end |
#test_nesting ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 295 def test_nesting assert_raises(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 } assert_raises(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse } assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2) too_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]' too_deep_ary = eval too_deep assert_raises(JSON::NestingError) { JSON.parse too_deep } assert_raises(JSON::NestingError) { JSON.parser.new(too_deep).parse } assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 19 } ok = JSON.parse too_deep, :max_nesting => 20 assert_equal too_deep_ary, ok ok = JSON.parse too_deep, :max_nesting => nil assert_equal too_deep_ary, ok ok = JSON.parse too_deep, :max_nesting => false assert_equal too_deep_ary, ok ok = JSON.parse too_deep, :max_nesting => 0 assert_equal too_deep_ary, ok assert_raises(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 } assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2) assert_raises(JSON::NestingError) { JSON.generate too_deep_ary } assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 19 } ok = JSON.generate too_deep_ary, :max_nesting => 20 assert_equal too_deep, ok ok = JSON.generate too_deep_ary, :max_nesting => nil assert_equal too_deep, ok ok = JSON.generate too_deep_ary, :max_nesting => false assert_equal too_deep, ok ok = JSON.generate too_deep_ary, :max_nesting => 0 assert_equal too_deep, ok end |
#test_parse_array ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 149 def test_parse_array assert_equal([], parse('[]')) assert_equal([], parse(' [ ] ')) assert_equal([1], parse('[1]')) assert_equal([1], parse(' [ 1 ] ')) assert_equal(@ary, parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\ ',[false],[true]]')) assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2] , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] })) end |
#test_parse_array_custom_class ⇒ Object
163 164 165 166 167 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 163 def test_parse_array_custom_class res = parse('[]', :array_class => SubArray) assert_equal([], res) assert_equal(SubArray, res.class) end |
#test_parse_arrays ⇒ Object
131 132 133 134 135 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 131 def test_parse_arrays assert_equal([1,2,3], parse('[1,2,3]')) assert_equal([1.2,2,3], parse('[1.2,2,3]')) assert_equal([[],[[],[]]], parse('[[],[[],[]]]')) end |
#test_parse_complex_objects ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 120 def test_parse_complex_objects a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] a.permutation.each do |perm| s = "a" orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h } json = pretty_generate(orig_obj) assert_equal orig_obj, parse(json) end end |
#test_parse_more_complex_arrays ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 112 def test_parse_more_complex_arrays a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }] a.permutation.each do |perm| json = pretty_generate(perm) assert_equal perm, parse(json) end end |
#test_parse_object ⇒ Object
169 170 171 172 173 174 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 169 def test_parse_object assert_equal({}, parse('{}')) assert_equal({}, parse(' { } ')) assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}')) assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } ')) end |
#test_parse_object_custom_class ⇒ Object
189 190 191 192 193 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 189 def test_parse_object_custom_class res = parse('{}', :object_class => SubHash) assert_equal({}, res) assert_equal(SubHash, res.class) end |
#test_parse_simple_arrays ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 60 def test_parse_simple_arrays assert_equal([], parse('[]')) assert_equal([], parse(' [ ] ')) assert_equal([nil], parse('[null]')) assert_equal([false], parse('[false]')) assert_equal([true], parse('[true]')) assert_equal([-23], parse('[-23]')) assert_equal([23], parse('[23]')) assert_equal([0.23], parse('[0.23]')) assert_equal([0.0], parse('[0e0]')) assert_raises(JSON::ParserError) { parse('[+23.2]') } assert_raises(JSON::ParserError) { parse('[+23]') } assert_raises(JSON::ParserError) { parse('[.23]') } assert_raises(JSON::ParserError) { parse('[023]') } assert_equal_float [3.141], parse('[3.141]') assert_equal_float [-3.141], parse('[-3.141]') assert_equal_float [3.141], parse('[3141e-3]') assert_equal_float [3.141], parse('[3141.1e-3]') assert_equal_float [3.141], parse('[3141E-3]') assert_equal_float [3.141], parse('[3141.0E-3]') assert_equal_float [-3.141], parse('[-3141.0e-3]') assert_equal_float [-3.141], parse('[-3141e-3]') assert_raises(ParserError) { parse('[NaN]') } assert parse('[NaN]', :allow_nan => true).first.nan? assert_raises(ParserError) { parse('[Infinity]') } assert_equal [1.0/0], parse('[Infinity]', :allow_nan => true) assert_raises(ParserError) { parse('[-Infinity]') } assert_equal [-1.0/0], parse('[-Infinity]', :allow_nan => true) assert_equal([""], parse('[""]')) assert_equal(["foobar"], parse('["foobar"]')) assert_equal([{}], parse('[{}]')) end |
#test_parse_simple_objects ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 93 def test_parse_simple_objects assert_equal({}, parse('{}')) assert_equal({}, parse(' { } ')) assert_equal({ "a" => nil }, parse('{ "a" : null}')) assert_equal({ "a" => nil }, parse('{"a":null}')) assert_equal({ "a" => false }, parse('{ "a" : false } ')) assert_equal({ "a" => false }, parse('{"a":false}')) assert_raises(JSON::ParserError) { parse('{false}') } assert_equal({ "a" => true }, parse('{"a":true}')) assert_equal({ "a" => true }, parse(' { "a" : true } ')) assert_equal({ "a" => -23 }, parse(' { "a" : -23 } ')) assert_equal({ "a" => -23 }, parse(' { "a" : -23 } ')) assert_equal({ "a" => 23 }, parse('{"a":23 } ')) assert_equal({ "a" => 23 }, parse(' { "a" : 23 } ')) assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } ')) assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } ')) end |
#test_parse_values ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 137 def test_parse_values assert_equal([""], parse('[""]')) assert_equal(["\\"], parse('["\\\\"]')) assert_equal(['"'], parse('["\""]')) assert_equal(['\\"\\'], parse('["\\\\\\"\\\\"]')) assert_equal(["\"\b\n\r\t\0\037"], parse('["\"\b\n\r\t\u0000\u001f"]')) for i in 0 ... @ary.size assert_equal(@ary[i], parse(@ary_to_parse[i])) end end |
#test_parser_reset ⇒ Object
205 206 207 208 209 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 205 def test_parser_reset parser = Parser.new(@json) assert_equal(@hash, parser.parse) assert_equal(@hash, parser.parse) end |
#test_symbolize_names ⇒ Object
326 327 328 329 330 331 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 326 def test_symbolize_names assert_equal({ "foo" => "bar", "baz" => "quux" }, JSON.parse('{"foo":"bar", "baz":"quux"}')) assert_equal({ :foo => "bar", :baz => "quux" }, JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true)) end |
#test_wrong_inputs ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/vendor/json_pure/tests/test_json.rb', line 277 def test_wrong_inputs assert_raises(ParserError) { JSON.parse('"foo"') } assert_raises(ParserError) { JSON.parse('123') } assert_raises(ParserError) { JSON.parse('[] bla') } assert_raises(ParserError) { JSON.parse('[] 1') } assert_raises(ParserError) { JSON.parse('[] []') } assert_raises(ParserError) { JSON.parse('[] {}') } assert_raises(ParserError) { JSON.parse('{} []') } assert_raises(ParserError) { JSON.parse('{} {}') } assert_raises(ParserError) { JSON.parse('[NULL]') } assert_raises(ParserError) { JSON.parse('[FALSE]') } assert_raises(ParserError) { JSON.parse('[TRUE]') } assert_raises(ParserError) { JSON.parse('[07] ') } assert_raises(ParserError) { JSON.parse('[0a]') } assert_raises(ParserError) { JSON.parse('[1.]') } assert_raises(ParserError) { JSON.parse(' ') } end |