summaryrefslogtreecommitdiff
path: root/test/psych/test_yamldbm.rb
blob: 62c88b753eb0b7561935b5695e601cbc5cdf7ed0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
require_relative 'helper'
require 'tmpdir'

begin
  require 'yaml/dbm'
rescue LoadError
end

module Psych
  ::Psych::DBM = ::YAML::DBM unless defined?(::Psych::DBM)

  class YAMLDBMTest < TestCase
    def setup
      @dir = Dir.mktmpdir("rubytest-file")
      File.chown(-1, Process.gid, @dir)
      @yamldbm_file = make_tmp_filename("yamldbm")
      @yamldbm = YAML::DBM.new(@yamldbm_file)
    end

    def teardown
      @yamldbm.clear
      @yamldbm.close
      FileUtils.remove_entry_secure @dir
    end

    def make_tmp_filename(prefix)
      @dir + "/" + prefix + File.basename(__FILE__) + ".#{$$}.test"
    end

    def test_store
      @yamldbm.store('a','b')
      @yamldbm.store('c','d')
      assert_equal 'b', @yamldbm['a']
      assert_equal 'd', @yamldbm['c']
      assert_nil @yamldbm['e']
    end

    def test_store_using_carret
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      assert_equal 'b', @yamldbm['a']
      assert_equal 'd', @yamldbm['c']
      assert_nil @yamldbm['e']
    end

    def test_to_a
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      assert_equal([['a','b'],['c','d']], @yamldbm.to_a.sort)
    end

    def test_to_hash
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      assert_equal({'a'=>'b','c'=>'d'}, @yamldbm.to_hash)
    end

    def test_has_value?
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      assert_equal true, @yamldbm.has_value?('b')
      assert_equal true, @yamldbm.has_value?('d')
      assert_equal false, @yamldbm.has_value?('f')
    end

    # Note:
    # YAML::DBM#index makes warning from internal of ::DBM#index.
    # It says 'DBM#index is deprecated; use DBM#key', but DBM#key
    # behaves not same as DBM#index.
    #
    # def test_index
    #  @yamldbm['a'] = 'b'
    #  @yamldbm['c'] = 'd'
    #  assert_equal 'a', @yamldbm.index('b')
    #  assert_equal 'c', @yamldbm.index('d')
    #  assert_nil @yamldbm.index('f')
    # end

    def test_key
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      assert_equal 'a', @yamldbm.key('b')
      assert_equal 'c', @yamldbm.key('d')
      assert_nil @yamldbm.key('f')
    end

    def test_fetch
      assert_equal('bar', @yamldbm['foo']='bar')
      assert_equal('bar', @yamldbm.fetch('foo'))
      assert_nil @yamldbm.fetch('bar')
      assert_equal('baz', @yamldbm.fetch('bar', 'baz'))
      assert_equal('foobar', @yamldbm.fetch('bar') {|key| 'foo' + key })
    end

    def test_shift
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      assert_equal([['a','b'], ['c','d']],
                   [@yamldbm.shift, @yamldbm.shift].sort)
      assert_nil @yamldbm.shift
    end

    def test_invert
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      assert_equal({'b'=>'a','d'=>'c'}, @yamldbm.invert)
    end

    def test_update
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      @yamldbm.update({'c'=>'d','e'=>'f'})
      assert_equal 'b', @yamldbm['a']
      assert_equal 'd', @yamldbm['c']
      assert_equal 'f', @yamldbm['e']
    end

    def test_replace
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      @yamldbm.replace({'c'=>'d','e'=>'f'})
      assert_nil @yamldbm['a']
      assert_equal 'd', @yamldbm['c']
      assert_equal 'f', @yamldbm['e']
    end

    def test_delete
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      assert_equal 'b', @yamldbm.delete('a')
      assert_nil @yamldbm['a']
      assert_equal 'd', @yamldbm['c']
      assert_nil @yamldbm.delete('e')
    end

    def test_delete_if
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      @yamldbm['e'] = 'f'

      @yamldbm.delete_if {|k,v| k == 'a'}
      assert_nil @yamldbm['a']
      assert_equal 'd', @yamldbm['c']
      assert_equal 'f', @yamldbm['e']

      @yamldbm.delete_if {|k,v| v == 'd'}
      assert_nil @yamldbm['c']
      assert_equal 'f', @yamldbm['e']

      @yamldbm.delete_if {|k,v| false }
      assert_equal 'f', @yamldbm['e']
    end

    def test_reject
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      @yamldbm['e'] = 'f'
      assert_equal({'c'=>'d','e'=>'f'}, @yamldbm.reject {|k,v| k == 'a'})
      assert_equal({'a'=>'b','e'=>'f'}, @yamldbm.reject {|k,v| v == 'd'})
      assert_equal({'a'=>'b','c'=>'d','e'=>'f'}, @yamldbm.reject {false})
    end

    def test_values
      assert_equal [], @yamldbm.values
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      assert_equal ['b','d'], @yamldbm.values.sort
    end

    def test_values_at
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      assert_equal ['b','d'], @yamldbm.values_at('a','c')
    end

    def test_selsct
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      @yamldbm['e'] = 'f'
      assert_equal(['b','d'], @yamldbm.select('a','c'))
    end

    def test_selsct_with_block
      @yamldbm['a'] = 'b'
      @yamldbm['c'] = 'd'
      @yamldbm['e'] = 'f'
      assert_equal([['a','b']], @yamldbm.select {|k,v| k == 'a'})
      assert_equal([['c','d']], @yamldbm.select {|k,v| v == 'd'})
      assert_equal([], @yamldbm.select {false})
    end
  end
end if defined?(YAML::DBM) && defined?(Psych)