File: case_open_hash.rb

package info (click to toggle)
ruby-hashery 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 404 kB
  • sloc: ruby: 2,997; makefile: 7
file content (87 lines) | stat: -rw-r--r-- 1,389 bytes parent folder | download | duplicates (4)
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
require 'helper'

testcase OpenHash do

  class_method :[] do
    test do
      o = OpenHash[:a=>1, :b=>2]
      o.a.assert == 1
      o.b.assert == 2
    end

    test do
      o = OpenHash[:a=>1, :b=>2]
      o.a.assert == 1
      o.b.assert == 2
    end
  end

  method :open? do
    test do
      o = OpenHash[:a=>1, :b=>2]
      o.assert.open?(:foo)
      o.refute.open?(:each)
    end
  end

  method :open! do
    test do
      o = OpenHash[:a=>1, :b=>2]
      o.open!(:each)
      o.assert.open?(:each)
      o.each = 10
      o.each.assert == 10
    end
  end

  method :close! do
    test do
      o = OpenHash[:a=>1, :b=>2]
      o.open!(:each)
      o.assert.open?(:each)
      o.each = 10
      o.each.assert == 10
      o.close!(:each)
      o.each.refute == 10
    end
  end

  method :method_missing do
    test 'bang method' do
      o = OpenHash[]
      o.open!(:each)
      o.each = 10
      o.each.assert == 10

      a = []
      o.each! do |k,v|
        a << [k,v]
      end
      a.assert == [[:each,10]]
    end

    test 'query method' do
      o = OpenHash[]
      o.a = 1
      o.assert.a?
      o.refute.b?
    end
  end

  method :send do
    test do
      o = OpenHash[]
      o.open!(:each)
      o.each = 10
      o.each.assert == 10

      a = []
      o.send(:each) do |k,v|
        a << [k,v]
      end
      a.assert == [[:each,10]]
    end
  end

end