Skip to content

Commit

Permalink
Support for ranges of integers or floats
Browse files Browse the repository at this point in the history
  • Loading branch information
porras committed Nov 25, 2011
1 parent 2b971a4 commit 8eac608
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -77,6 +77,8 @@ In addition to plain class names, we can feed `data` with more or less complex e
# or [-44, "eB"] # or [-44, "eB"]
data [[Float]] # generates arrays of arrays of floats data [[Float]] # generates arrays of arrays of floats
# such as [[0.12, 3.41], [-2.31]] # such as [[0.12, 3.41], [-2.31]]
data (1..10) # generates integers between 1 and 10
data (0.0..10.0) # generates floats between 1 and 10
data({Symbol => String}) # generates hashes whose keys are symbols data({Symbol => String}) # generates hashes whose keys are symbols
# and whose values are strings such as # and whose values are strings such as
# {:tR=>"m", :aSKnsndwWK=>"QUrGwAAh"} # {:tR=>"m", :aSKnsndwWK=>"QUrGwAAh"}
Expand All @@ -91,7 +93,7 @@ In addition to plain class names, we can feed `data` with more or less complex e
# value (this is specially useful for testing # value (this is specially useful for testing
# methods with more than one parameter) # 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: You can combine this cases ad infinitum, but in case this is not enough, you can just use a block and do whatever you want to generate the data:


data do data do
rand > 0.5 ? Wadus.new(rand(9)) : FooBar.new(rand(9)) rand > 0.5 ? Wadus.new(rand(9)) : FooBar.new(rand(9))
Expand Down
13 changes: 11 additions & 2 deletions lib/mrproper/data_block.rb
Expand Up @@ -45,11 +45,20 @@ def to_proc
end end
end end
end end
when Range
case @spec.begin
when Integer
Proc.new { @spec.begin + rand(@spec.end - @spec.begin) }
when Float
Proc.new { @spec.begin + rand * (@spec.end - @spec.begin) }
else
Proc.new { @spec }
end
when Class when Class
if @spec == Integer if @spec == Integer
Proc.new { rand(1000) - 500 } DataBlock.new(-500..500).to_proc
elsif @spec == Float elsif @spec == Float
Proc.new { rand * 10 - 10 } DataBlock.new(-5.0..5.0).to_proc
elsif @spec == String elsif @spec == String
Proc.new { String.random(rand(10)) } Proc.new { String.random(rand(10)) }
elsif @spec == Symbol elsif @spec == Symbol
Expand Down
22 changes: 22 additions & 0 deletions test/mrproper/data_block_test.rb
Expand Up @@ -137,4 +137,26 @@ def test_data_block_with_hash_of_two_elements
assert_changes { b.call[:second] } assert_changes { b.call[:second] }
end end


def test_data_block_with_range_of_integers
b = MrProper::DataBlock.new(-10..-5)

assert_kind_of Integer, b.call
assert_changes { b.call }
10.times do
assert_in_range b.call, -10..-5
end
end

def test_data_block_with_range_of_floats
b = MrProper::DataBlock.new(-30.0..-27.0)

assert_kind_of Float, b.call
assert_changes { b.call }
10.times do
assert_in_range b.call, -30..-27
end
assert b.data.size > 10 # arbitrary number to check it's not converting to
# integer and generating just -30, -29, -28, -27
end

end end
4 changes: 4 additions & 0 deletions test/test_helper.rb
Expand Up @@ -2,4 +2,8 @@ module TestHelper
def assert_changes(&block) def assert_changes(&block)
assert 10.times.map(&block).uniq.size > 1 assert 10.times.map(&block).uniq.size > 1
end end

def assert_in_range(number, range)
assert range.include?(number), "Expected #{number} to be within #{range.inspect}"
end
end end

0 comments on commit 8eac608

Please sign in to comment.