rhash 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/rhash.rb +10 -6
  2. data/test/test.rb +254 -34
  3. metadata +4 -4
@@ -184,7 +184,7 @@ class RHash
184
184
  selected_entries = []
185
185
  @hash.each do |info|
186
186
  if block_given?
187
- if yield info
187
+ if yield info.key, info.value
188
188
  selected_entries << [info.key, info.value]
189
189
  end
190
190
  end
@@ -334,13 +334,17 @@ class RHash
334
334
  # See Hash::fetch
335
335
  #
336
336
  def fetch(key, msg='', &block)
337
- value = select{|i| i.key == key}[0]
338
-
339
- if block_given? and value.nil?
337
+ value = select{|k, v| k == key}[0]
338
+
339
+ if block_given?
340
340
  yield
341
- else
342
- raise IndexError, "Key (#{key.inspect}): not found" if msg.empty?
341
+ else
342
+ if value.nil?
343
+ raise IndexError, "Key (#{key.inspect}): not found" if msg.empty?
344
+ end
343
345
  end
346
+
347
+ value[1]
344
348
  end
345
349
 
346
350
  #
@@ -1,37 +1,257 @@
1
1
  require 'lib/rhash'
2
2
  require 'rubygems'
3
- #require 'test/unit'
4
- n = RHash.new
5
- n["r"] = 5
6
- n["s"] = 6
7
- n["d"] = 5
8
- n[:h] = "ola"
9
- n[:m] = 5
3
+ require 'test/unit'
10
4
 
11
- #x= {}
12
- #x["s"] = 5
13
- #x["r"] = 6
14
-
15
- x= RHash.new
16
- x["body"] = RHash.new
17
- x["xx"] = {:x => {:y => {:z => 666, :g => "galinha"} }}
18
- x["body"]["color"] = "0x0454"
19
- x["body"]["size"] = 24
20
- x["body"]["letter"] = "Arial"
21
- x["body"]["mode"] = RHash.new
22
- x["body"]["mode"]["present"] = "Love"
23
- x["body"]["mode"]["to"] = "Rose Mary"
24
- x["body"]["hash"] = {:ola => 45}
25
- x["body"]["mode"]["vaca"] = RHash.new
26
- x["body"]["mode"]["vaca"]["mooo"] = RHash.new
27
- x["body"]["mode"]["vaca"]["mooo"]["milk"] = true
28
- x["body"]["mode"]["vaca"]["mooo"]["feed"] = "grass"
29
-
30
- #puts n == x
31
- #puts n == 5
32
- #a,b = n.diff x
33
- #puts a.inspect
34
- #puts b.inspect
35
- #puts x.to_html
36
-
37
- puts x.to_yaml
5
+ class BigBadWolfUnitTest < Test::Unit::TestCase
6
+ def setup
7
+ @rhash = RHash.new
8
+ @hash = {}
9
+ end
10
+
11
+ def test_add_value
12
+ assert_nothing_raised do
13
+ @rhash[:key] = "value"
14
+ end
15
+
16
+ assert_nothing_raised do
17
+ @rhash[5] = "value"
18
+ end
19
+
20
+ assert_nothing_raised do
21
+ @rhash["555"] = "value"
22
+ end
23
+ end
24
+
25
+ def test_read_value
26
+ [:a, "5", 5].each do |key|
27
+ @rhash[key] = "value"
28
+ @hash[key] = "value"
29
+ assert_equal @rhash[key], @hash[key]
30
+ end
31
+ end
32
+
33
+ def test_sort
34
+ @rhash[:key] = "5"
35
+ @rhash["cow"] = "moooo..."
36
+ @rhash['4'] = "2*2"
37
+
38
+ assert_equal ['4', "2*2"],@rhash.sort.first
39
+ assert_equal [:key, "5"],@rhash.sort.last
40
+
41
+ @rhash.sort!
42
+
43
+ assert_equal ['4', "2*2"],@rhash.first
44
+ assert_equal [:key, "5"],@rhash.last
45
+ end
46
+
47
+ def test_sort_each_sort_each_pair_and_each
48
+ @rhash[:key] = "5"
49
+ @rhash["cow"] = "moooo..."
50
+ @rhash['4'] = "2*2"
51
+
52
+ count = 0
53
+ @rhash.sort_each do |info|
54
+ case count
55
+ when 0
56
+ assert_equal ['4', "2*2"], info
57
+ when 1
58
+ assert_equal ['cow', "moooo..."], info
59
+ when 2
60
+ assert_equal [:key, "5"], info
61
+ end
62
+ count += 1
63
+ end
64
+
65
+ count = 0
66
+ @rhash.sort_each_pair do |key, value|
67
+ case count
68
+ when 0
69
+ assert_equal ['4', "2*2"], [key, value]
70
+ when 1
71
+ assert_equal ['cow', "moooo..."], [key, value]
72
+ when 2
73
+ assert_equal [:key, "5"], [key, value]
74
+ end
75
+ count += 1
76
+ end
77
+
78
+ count = 0
79
+ @rhash.each do |info|
80
+ case count
81
+ when 2
82
+ assert_equal ['4', "2*2"], info
83
+ when 1
84
+ assert_equal ['cow', "moooo..."], info
85
+ when 0
86
+ assert_equal [:key, "5"], info
87
+ end
88
+ count += 1
89
+ end
90
+ end
91
+
92
+ def test_select
93
+ @rhash[:key] = "5"
94
+ @rhash["cow"] = "moooo..."
95
+ @rhash['4'] = "2*2"
96
+
97
+ correct = @rhash.select{|key, value| key == "4"}
98
+ wrong = @rhash.select{|key| key == "4"}
99
+
100
+ assert_raise NoMethodError do
101
+ wrong = @rhash.select{|key| key.key == "4"}
102
+ end
103
+
104
+ assert wrong.empty?
105
+ assert !correct.empty?
106
+ end
107
+
108
+ def test_keys_and_values
109
+ @rhash[:key] = 5
110
+ @rhash["cow"] = "moooo..."
111
+ @rhash['4'] = "2*2"
112
+
113
+ keys = @rhash.keys
114
+
115
+ assert keys.include?(:key)
116
+ assert keys.include?("cow")
117
+ assert keys.include?("4")
118
+ assert !keys.include?(666)
119
+
120
+ values = @rhash.values
121
+
122
+ assert values.include?(5)
123
+ assert values.include?("moooo...")
124
+ assert values.include?("2*2")
125
+ assert !values.include?(55555)
126
+ end
127
+
128
+ def test_has_key_has_value
129
+ @rhash[:key] = 5
130
+ @rhash[:key2] = 666
131
+
132
+ assert @rhash.has_key?(:key)
133
+ assert !@rhash.has_key?("bfasa")
134
+ assert !@rhash.has_key?(1654654)
135
+ assert !@rhash.has_key?(:dhsafduafdas)
136
+ assert @rhash.has_key?(:key2)
137
+
138
+ assert @rhash.has_value?(5)
139
+ assert !@rhash.has_value?("bfasa")
140
+ assert !@rhash.has_value?(1654654)
141
+ assert !@rhash.has_value?(:dhsafduafdas)
142
+ assert @rhash.has_value?(666)
143
+ end
144
+
145
+ def test_delete_if
146
+ @rhash[:key] = 5
147
+
148
+ @rhash.delete_if{|k| k == :key}
149
+ assert !@rhash.empty?
150
+
151
+ assert_raise NoMethodError do
152
+ @rhash.delete_if{|k| k.key == :key}
153
+ end
154
+
155
+ @rhash.delete_if{|k,v| k == :key}
156
+ assert @rhash.empty?
157
+
158
+ @rhash[:key] = 5
159
+ @rhash[:key2] = 5
160
+ @rhash.delete_if
161
+ assert !@rhash.empty?
162
+
163
+ @rhash.delete_if{|k,v| k == :key}
164
+ assert !@rhash[:key]
165
+ assert @rhash[:key2]
166
+ end
167
+
168
+ def test_delete
169
+ @rhash[:key] = 5
170
+
171
+ @rhash.delete(:key)
172
+ assert @rhash.empty?
173
+
174
+ assert !@rhash.delete(:key){"Boooom!"}
175
+ end
176
+
177
+ def test_size
178
+ @rhash[:key] = 5
179
+ @rhash[:key1] = 55
180
+ @rhash[:key2] = 555
181
+ @rhash[:key3] = 5555
182
+ @rhash[:key4] = 55555
183
+
184
+ assert_equal 5, @rhash.size
185
+ end
186
+
187
+ def test_invert
188
+ # Easy invert
189
+ @rhash[:key] = 5
190
+ inverted_rhash = @rhash.invert
191
+
192
+ assert_equal :key, inverted_rhash[5]
193
+
194
+ @rhash[:key1] = 6
195
+ @rhash[:key2] = 7
196
+ @rhash[:key3] = 8
197
+
198
+ inverted_rhash = @rhash.invert
199
+
200
+ assert_equal :key1, inverted_rhash[6]
201
+ assert_equal :key2, inverted_rhash[7]
202
+ assert_equal :key3, inverted_rhash[8]
203
+ end
204
+
205
+ def test_merge
206
+ @rhash[:key] = 5
207
+ @rhash[:key1] = 55
208
+
209
+ @hash[:key2] = 6
210
+ @hash[:key3] = 66
211
+
212
+ assert_nothing_raised do
213
+ @rhash.merge @hash
214
+ end
215
+
216
+ merged_rhash = @rhash.merge @hash
217
+
218
+ assert_equal 5, merged_rhash[:key]
219
+ assert_equal 55, merged_rhash[:key1]
220
+ assert_equal 6, merged_rhash[:key2]
221
+ assert_equal 66, merged_rhash[:key3]
222
+
223
+ @rhash.merge! @hash
224
+
225
+ assert_equal 5, @rhash[:key]
226
+ assert_equal 55, @rhash[:key1]
227
+ assert_equal 6, @rhash[:key2]
228
+ assert_equal 66, @rhash[:key3]
229
+
230
+ @hash[:key] = 6
231
+
232
+ @rhash.merge! @hash
233
+
234
+ # HAHAHAHA
235
+ # Merge with same key.
236
+ assert_equal 6, @rhash[:key]
237
+ assert_equal 55, @rhash[:key1]
238
+ assert_equal 6, @rhash[:key2]
239
+ assert_equal 66, @rhash[:key3]
240
+ end
241
+
242
+ def test_fat_fetch
243
+ @rhash[:key] = 5
244
+ @rhash[:key1] = 55
245
+
246
+ assert_equal 5, @rhash.fetch(:key)
247
+
248
+ assert_raise IndexError do
249
+ @rhash.fetch(:key666)
250
+ end
251
+ end
252
+
253
+ def teardown
254
+ @rhash.clear
255
+ end
256
+
257
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhash
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rodrigo Mello Nardi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-19 00:00:00 -03:00
18
+ date: 2010-07-20 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies: []
21
21