Permalink
Browse files

Support for hashes of specific size

  • Loading branch information...
1 parent 1377a1b commit 345f62863e1d5e34305282221e8f69355945ea7b @porras committed Nov 9, 2011
Showing with 30 additions and 4 deletions.
  1. +5 −0 README.md
  2. +14 −4 lib/mrproper/dsl.rb
  3. +11 −0 test/mrproper/dsl_test.rb
View
@@ -85,6 +85,11 @@ In addition to plain class names, we can feed `data` with more or less complex e
# parser thinks we're defining a block `;)`)
data({String => [Integer]}) # generates hashes whose keys are strings
# and whose values are arrays of integers
+ data({:first => String,
+ :second => Integer}) # generates a hash with two keys, :first and
+ # :second, each one with an String or Integer
+ # value (this is specially useful for testing
+ # methods with more than one parameter)
In case this is not enough, you can just use a block and do whatever you want to generate the data:
View
@@ -32,10 +32,20 @@ def data_block(spec = nil, &block)
Proc.new { spec.map { |s| data_block(s).call }}
end
when Hash
- Proc.new do
- {}.tap do |h|
- rand(20).times.each do
- h[data_block(spec.keys.first).call] = data_block(spec.values.first).call
+ if spec.size == 1
+ Proc.new do
+ {}.tap do |h|
+ rand(20).times.each do
+ h[data_block(spec.keys.first).call] = data_block(spec.values.first).call
+ end
+ end
+ end
+ else
+ Proc.new do
+ {}.tap do |h|
+ spec.each do |k, v|
+ h[data_block(k).call] = data_block(v).call
+ end
end
end
end
View
@@ -131,4 +131,15 @@ def test_data_block_with_hash_of_symbol_to_array_of_strings
assert_changes { b.call.size }
end
+ def test_data_block_with_hash_of_two_elements
+ b = @pb.data_block({:first => Integer, :second => Float})
+ assert_kind_of Proc, b
+ assert_kind_of Hash, b.call
+ assert_equal 2, b.call.size
+ assert_kind_of Integer, b.call[:first]
+ assert_kind_of Float, b.call[:second]
+ assert_changes { b.call[:first] }
+ assert_changes { b.call[:second] }
+ end
+
end

0 comments on commit 345f628

Please sign in to comment.