File: case_casting_hash.rb

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

test_case CastingHash do

  class_method :[] do
    test do
      h = CastingHash[:a=>1, :b=>2]
    end
  end

  class_method :new do
    test do
      h = CastingHash.new
    end

    test 'with default' do
      h = CastingHash.new(0)
      h['a'].assert == 0
    end

    test 'with casting procedure' do
      h = CastingHash.new{ |k,v| [k.to_sym, v] }
      h['a'] = 1
      h.assert == {:a=>1}
    end

    test 'with default and casting procedure' do
      h = CastingHash.new(0){ |k,v| [k.to_sym, v] }
      h['a'].assert == 0
      h['b'] = 2
      h.assert == {:b=>2}
    end
  end

  method :recast! do
    test do
      h = CastingHash[:a=>1, :b=>2]
      h.cast_proc{ |k,v| [k.to_s, v] }
      h.recast!
      h.assert == {'a'=>1, 'b'=>2}
    end
  end

  method :cast_proc= do
    test do
      h = CastingHash[:a=>1, :b=>2]
      h.cast_proc = Proc.new{ |k,v| [k.to_s, v] }
      h.recast!
      h.assert == {'a'=>1, 'b'=>2}
    end
  end

  method :to_hash do
    test do
      h = CastingHash[:a=>1, :b=>2]
      h.to_hash
      ::Hash.assert === h
      h.assert == {:a=>1, :b=>2}
    end
  end

  method :to_h do
    test do
      h = CastingHash[:a=>1, :b=>2]
      h.to_h
      ::Hash.assert === h
      h.assert == {:a=>1, :b=>2}
    end
  end

end