1require 'psych/helper' 2 3require 'stringio' 4require 'tempfile' 5 6class TestPsych < Psych::TestCase 7 def teardown 8 Psych.domain_types.clear 9 end 10 11 def test_line_width 12 yml = Psych.dump('123456 7', { :line_width => 5 }) 13 assert_match(/^\s*7/, yml) 14 end 15 16 def test_indent 17 yml = Psych.dump({:a => {'b' => 'c'}}, {:indentation => 5}) 18 assert_match(/^[ ]{5}b/, yml) 19 end 20 21 def test_canonical 22 yml = Psych.dump({:a => {'b' => 'c'}}, {:canonical => true}) 23 assert_match(/\? "b/, yml) 24 end 25 26 def test_header 27 yml = Psych.dump({:a => {'b' => 'c'}}, {:header => true}) 28 assert_match(/YAML/, yml) 29 end 30 31 def test_version_array 32 yml = Psych.dump({:a => {'b' => 'c'}}, {:version => [1,1]}) 33 assert_match(/1.1/, yml) 34 end 35 36 def test_version_string 37 yml = Psych.dump({:a => {'b' => 'c'}}, {:version => '1.1'}) 38 assert_match(/1.1/, yml) 39 end 40 41 def test_version_bool 42 yml = Psych.dump({:a => {'b' => 'c'}}, {:version => true}) 43 assert_match(/1.1/, yml) 44 end 45 46 def test_load_argument_error 47 assert_raises(TypeError) do 48 Psych.load nil 49 end 50 end 51 52 def test_non_existing_class_on_deserialize 53 e = assert_raises(ArgumentError) do 54 Psych.load("--- !ruby/object:NonExistent\nfoo: 1") 55 end 56 assert_equal 'undefined class/module NonExistent', e.message 57 end 58 59 def test_dump_stream 60 things = [22, "foo \n", {}] 61 stream = Psych.dump_stream(*things) 62 assert_equal things, Psych.load_stream(stream) 63 end 64 65 def test_dump_file 66 hash = {'hello' => 'TGIF!'} 67 Tempfile.open('fun.yml') do |io| 68 assert_equal io, Psych.dump(hash, io) 69 io.rewind 70 assert_equal Psych.dump(hash), io.read 71 io.close(true) 72 end 73 end 74 75 def test_dump_io 76 hash = {'hello' => 'TGIF!'} 77 stringio = StringIO.new '' 78 assert_equal stringio, Psych.dump(hash, stringio) 79 assert_equal Psych.dump(hash), stringio.string 80 end 81 82 def test_simple 83 assert_equal 'foo', Psych.load("--- foo\n") 84 end 85 86 def test_libyaml_version 87 assert Psych.libyaml_version 88 assert_equal Psych.libyaml_version.join('.'), Psych::LIBYAML_VERSION 89 end 90 91 def test_load_documents 92 docs = Psych.load_documents("--- foo\n...\n--- bar\n...") 93 assert_equal %w{ foo bar }, docs 94 end 95 96 def test_parse_stream 97 docs = Psych.parse_stream("--- foo\n...\n--- bar\n...") 98 assert_equal %w{ foo bar }, docs.children.map { |x| x.transform } 99 end 100 101 def test_add_builtin_type 102 got = nil 103 Psych.add_builtin_type 'omap' do |type, val| 104 got = val 105 end 106 Psych.load('--- !!omap hello') 107 assert_equal 'hello', got 108 ensure 109 Psych.remove_type 'omap' 110 end 111 112 def test_domain_types 113 got = nil 114 Psych.add_domain_type 'foo.bar,2002', 'foo' do |type, val| 115 got = val 116 end 117 118 Psych.load('--- !foo.bar,2002/foo hello') 119 assert_equal 'hello', got 120 121 Psych.load("--- !foo.bar,2002/foo\n- hello\n- world") 122 assert_equal %w{ hello world }, got 123 124 Psych.load("--- !foo.bar,2002/foo\nhello: world") 125 assert_equal({ 'hello' => 'world' }, got) 126 end 127 128 def test_load_file 129 t = Tempfile.new(['yikes', 'yml']) 130 t.binmode 131 t.write('--- hello world') 132 t.close 133 assert_equal 'hello world', Psych.load_file(t.path) 134 t.close(true) 135 end 136 137 def test_parse_file 138 t = Tempfile.new(['yikes', 'yml']) 139 t.binmode 140 t.write('--- hello world') 141 t.close 142 assert_equal 'hello world', Psych.parse_file(t.path).transform 143 t.close(true) 144 end 145 146 def test_degenerate_strings 147 assert_equal false, Psych.load(' ') 148 assert_equal false, Psych.parse(' ') 149 assert_equal false, Psych.load('') 150 assert_equal false, Psych.parse('') 151 end 152 153 def test_callbacks 154 types = [] 155 appender = lambda { |*args| types << args } 156 157 Psych.add_builtin_type('foo', &appender) 158 Psych.add_domain_type('example.com,2002', 'foo', &appender) 159 Psych.load <<-eoyml 160- !tag:yaml.org,2002:foo bar 161- !tag:example.com,2002:foo bar 162 eoyml 163 164 assert_equal [ 165 ["tag:yaml.org,2002:foo", "bar"], 166 ["tag:example.com,2002:foo", "bar"] 167 ], types 168 end 169end 170