File: case_core_ext.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 (79 lines) | stat: -rw-r--r-- 1,695 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
require 'helper'

test_case Hash do

  method :rekey do

    test "default" do
      foo = { "a"=>1, "b"=>2 }
      foo.rekey.assert == { :a=>1, :b=>2 }
      foo.assert == { "a"=>1, "b"=>2 }
    end

    test "specific key" do
      bar = { :a=>1, :b=>2 }
      foo = bar.rekey(:a=>:c)
      foo[:c].assert == 1
      foo[:b].assert == 2
      foo[:a].assert == nil
    end

    test "with block" do
      bar = { :a=>1, :b=>2 }
      foo = bar.rekey{ |k| k.to_s }
      foo['a'].assert == 1
      foo['b'].assert == 2
      foo[:a].assert  == nil
      foo[:b].assert  == nil
      foo.assert == { 'a'=>1, 'b'=>2 }
    end

    test "symbol proc" do
      foo = { :a=>1, :b=>2 }
      foo.rekey(&:to_s).assert == { "a"=>1, "b"=>2 }
      foo.assert == { :a =>1, :b=>2 }
    end

  end

  method :rekey! do

    test "default" do
      foo = { "a"=>1, "b"=>2 }
      foo.rekey!.assert == { :a=>1, :b=>2 }
      foo.assert == { :a=>1, :b=>2 }
    end

    test "specific key" do
      foo = { :a=>1, :b=>2 }
      foo.rekey!(:a=>:c)
      foo[:c].assert == 1
      foo[:b].assert == 2
      foo[:a].assert == nil
    end

    test "with block" do
      foo = { :a=>1, :b=>2 }
      foo.rekey!{ |k| k.to_s }
      foo['a'].assert == 1
      foo['b'].assert == 2
      foo[:a].assert == nil
      foo[:b].assert == nil
      foo.assert == { 'a'=>1, 'b'=>2 }
    end

    test "symbol proc" do
      foo = { :a=>1, :b=>2 }
      foo.rekey!(&:to_s).assert == { "a"=>1, "b"=>2 }
      foo.assert == { "a"=>1, "b"=>2 }
    end

    test "no conflict between keys" do
      r = {1 => :a, 2 => :b}.rekey!{ |k| k + 1 }
      r.refute = {3 => :a}
      r.assert = {2 => :a, 3 => :b}  
    end

  end

end