diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-06-04 12:31:26 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-06-04 12:31:26 +0000 |
commit | af1c4167287b9353fec766f932fe4afe97116ad4 (patch) | |
tree | fe952114fed9f49b11fa58533478ef130eddeba7 /test/json/test_json_generate.rb | |
parent | 6b3ef2249cc6d03b9e153a1bbfb24eb12d4032a5 (diff) |
* lib/json.rb, lib/json, ext/json, test/json:
import JSON library.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12428 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/json/test_json_generate.rb')
-rwxr-xr-x | test/json/test_json_generate.rb | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/test/json/test_json_generate.rb b/test/json/test_json_generate.rb new file mode 100755 index 0000000000..519d56ac5d --- /dev/null +++ b/test/json/test_json_generate.rb @@ -0,0 +1,81 @@ +require 'test/unit' +require 'json' + +class TC_JSONGenerate < Test::Unit::TestCase + include JSON + + def setup + $KCODE = 'UTF8' + @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 + + def test_unparse + json = unparse(@hash) + assert_equal(@json2, 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 + + def test_unparse_pretty + json = pretty_unparse(@hash) + assert_equal(@json3, 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 + + 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, 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 +end |