1require 'psych/helper' 2 3module Psych 4 class TestJSONTree < TestCase 5 def test_string 6 assert_match(/"foo"/, Psych.to_json("foo")) 7 end 8 9 def test_symbol 10 assert_match(/"foo"/, Psych.to_json(:foo)) 11 end 12 13 def test_nil 14 assert_match(/^null/, Psych.to_json(nil)) 15 end 16 17 def test_int 18 assert_match(/^10/, Psych.to_json(10)) 19 end 20 21 def test_float 22 assert_match(/^1.2/, Psych.to_json(1.2)) 23 end 24 25 def test_hash 26 hash = { 'one' => 'two' } 27 json = Psych.to_json(hash) 28 assert_match(/}$/, json) 29 assert_match(/^\{/, json) 30 assert_match(/['"]one['"]/, json) 31 assert_match(/['"]two['"]/, json) 32 end 33 34 class Bar 35 def encode_with coder 36 coder.represent_seq 'omg', %w{ a b c } 37 end 38 end 39 40 def test_json_list_dump_exclude_tag 41 json = Psych.to_json Bar.new 42 refute_match('omg', json) 43 end 44 45 def test_list_to_json 46 list = %w{ one two } 47 json = Psych.to_json(list) 48 assert_match(/]$/, json) 49 assert_match(/^\[/, json) 50 assert_match(/"one"/, json) 51 assert_match(/"two"/, json) 52 end 53 54 def test_time 55 time = Time.utc(2010, 10, 10) 56 assert_equal "{\"a\": \"2010-10-10 00:00:00.000000000 Z\"}\n", 57Psych.to_json({'a' => time }) 58 end 59 60 def test_datetime 61 time = Time.new(2010, 10, 10).to_datetime 62 assert_equal "{\"a\": \"#{time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")}\"}\n", Psych.to_json({'a' => time }) 63 end 64 end 65end 66