1# -*- coding: UTF-8 -*- 2 3require 'psych/helper' 4require 'tmpdir' 5 6begin 7 require 'yaml/dbm' 8rescue LoadError 9end 10 11module Psych 12 ::Psych::DBM = ::YAML::DBM unless defined?(::Psych::DBM) 13 14 class YAMLDBMTest < TestCase 15 def setup 16 @engine, YAML::ENGINE.yamler = YAML::ENGINE.yamler, 'psych' 17 @dir = Dir.mktmpdir("rubytest-file") 18 File.chown(-1, Process.gid, @dir) 19 @yamldbm_file = make_tmp_filename("yamldbm") 20 @yamldbm = YAML::DBM.new(@yamldbm_file) 21 end 22 23 def teardown 24 YAML::ENGINE.yamler = @engine 25 @yamldbm.clear 26 @yamldbm.close 27 FileUtils.remove_entry_secure @dir 28 end 29 30 def make_tmp_filename(prefix) 31 @dir + "/" + prefix + File.basename(__FILE__) + ".#{$$}.test" 32 end 33 34 def test_store 35 @yamldbm.store('a','b') 36 @yamldbm.store('c','d') 37 assert_equal 'b', @yamldbm['a'] 38 assert_equal 'd', @yamldbm['c'] 39 assert_nil @yamldbm['e'] 40 end 41 42 def test_store_using_carret 43 @yamldbm['a'] = 'b' 44 @yamldbm['c'] = 'd' 45 assert_equal 'b', @yamldbm['a'] 46 assert_equal 'd', @yamldbm['c'] 47 assert_nil @yamldbm['e'] 48 end 49 50 def test_to_a 51 @yamldbm['a'] = 'b' 52 @yamldbm['c'] = 'd' 53 assert_equal([['a','b'],['c','d']], @yamldbm.to_a.sort) 54 end 55 56 def test_to_hash 57 @yamldbm['a'] = 'b' 58 @yamldbm['c'] = 'd' 59 assert_equal({'a'=>'b','c'=>'d'}, @yamldbm.to_hash) 60 end 61 62 def test_has_value? 63 @yamldbm['a'] = 'b' 64 @yamldbm['c'] = 'd' 65 assert_equal true, @yamldbm.has_value?('b') 66 assert_equal true, @yamldbm.has_value?('d') 67 assert_equal false, @yamldbm.has_value?('f') 68 end 69 70 # Note: 71 # YAML::DBM#index makes warning from internal of ::DBM#index. 72 # It says 'DBM#index is deprecated; use DBM#key', but DBM#key 73 # behaves not same as DBM#index. 74 # 75 # def test_index 76 # @yamldbm['a'] = 'b' 77 # @yamldbm['c'] = 'd' 78 # assert_equal 'a', @yamldbm.index('b') 79 # assert_equal 'c', @yamldbm.index('d') 80 # assert_nil @yamldbm.index('f') 81 # end 82 83 def test_key 84 skip 'only on ruby 2.0.0' if RUBY_VERSION < '2.0.0' 85 @yamldbm['a'] = 'b' 86 @yamldbm['c'] = 'd' 87 assert_equal 'a', @yamldbm.key('b') 88 assert_equal 'c', @yamldbm.key('d') 89 assert_nil @yamldbm.key('f') 90 end 91 92 def test_fetch 93 assert_equal('bar', @yamldbm['foo']='bar') 94 assert_equal('bar', @yamldbm.fetch('foo')) 95 assert_nil @yamldbm.fetch('bar') 96 assert_equal('baz', @yamldbm.fetch('bar', 'baz')) 97 assert_equal('foobar', @yamldbm.fetch('bar') {|key| 'foo' + key }) 98 end 99 100 def test_shift 101 @yamldbm['a'] = 'b' 102 @yamldbm['c'] = 'd' 103 assert_equal([['a','b'], ['c','d']], 104 [@yamldbm.shift, @yamldbm.shift].sort) 105 assert_nil @yamldbm.shift 106 end 107 108 def test_invert 109 @yamldbm['a'] = 'b' 110 @yamldbm['c'] = 'd' 111 assert_equal({'b'=>'a','d'=>'c'}, @yamldbm.invert) 112 end 113 114 def test_update 115 @yamldbm['a'] = 'b' 116 @yamldbm['c'] = 'd' 117 @yamldbm.update({'c'=>'d','e'=>'f'}) 118 assert_equal 'b', @yamldbm['a'] 119 assert_equal 'd', @yamldbm['c'] 120 assert_equal 'f', @yamldbm['e'] 121 end 122 123 def test_replace 124 @yamldbm['a'] = 'b' 125 @yamldbm['c'] = 'd' 126 @yamldbm.replace({'c'=>'d','e'=>'f'}) 127 assert_nil @yamldbm['a'] 128 assert_equal 'd', @yamldbm['c'] 129 assert_equal 'f', @yamldbm['e'] 130 end 131 132 def test_delete 133 @yamldbm['a'] = 'b' 134 @yamldbm['c'] = 'd' 135 assert_equal 'b', @yamldbm.delete('a') 136 assert_nil @yamldbm['a'] 137 assert_equal 'd', @yamldbm['c'] 138 assert_nil @yamldbm.delete('e') 139 end 140 141 def test_delete_if 142 @yamldbm['a'] = 'b' 143 @yamldbm['c'] = 'd' 144 @yamldbm['e'] = 'f' 145 146 @yamldbm.delete_if {|k,v| k == 'a'} 147 assert_nil @yamldbm['a'] 148 assert_equal 'd', @yamldbm['c'] 149 assert_equal 'f', @yamldbm['e'] 150 151 @yamldbm.delete_if {|k,v| v == 'd'} 152 assert_nil @yamldbm['c'] 153 assert_equal 'f', @yamldbm['e'] 154 155 @yamldbm.delete_if {|k,v| false } 156 assert_equal 'f', @yamldbm['e'] 157 end 158 159 def test_reject 160 @yamldbm['a'] = 'b' 161 @yamldbm['c'] = 'd' 162 @yamldbm['e'] = 'f' 163 assert_equal({'c'=>'d','e'=>'f'}, @yamldbm.reject {|k,v| k == 'a'}) 164 assert_equal({'a'=>'b','e'=>'f'}, @yamldbm.reject {|k,v| v == 'd'}) 165 assert_equal({'a'=>'b','c'=>'d','e'=>'f'}, @yamldbm.reject {false}) 166 end 167 168 def test_values 169 assert_equal [], @yamldbm.values 170 @yamldbm['a'] = 'b' 171 @yamldbm['c'] = 'd' 172 assert_equal ['b','d'], @yamldbm.values.sort 173 end 174 175 def test_values_at 176 @yamldbm['a'] = 'b' 177 @yamldbm['c'] = 'd' 178 assert_equal ['b','d'], @yamldbm.values_at('a','c') 179 end 180 181 def test_selsct 182 @yamldbm['a'] = 'b' 183 @yamldbm['c'] = 'd' 184 @yamldbm['e'] = 'f' 185 assert_equal(['b','d'], @yamldbm.select('a','c')) 186 end 187 188 def test_selsct_with_block 189 @yamldbm['a'] = 'b' 190 @yamldbm['c'] = 'd' 191 @yamldbm['e'] = 'f' 192 assert_equal([['a','b']], @yamldbm.select {|k,v| k == 'a'}) 193 assert_equal([['c','d']], @yamldbm.select {|k,v| v == 'd'}) 194 assert_equal([], @yamldbm.select {false}) 195 end 196 end 197end if defined?(YAML::DBM) && defined?(Psych) 198