Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support for ranges of integers or floats

  • Loading branch information...
commit 8eac608e9620c178fa083725242401cc7e572fd5 1 parent 2b971a4
@porras authored
View
4 README.md
@@ -77,6 +77,8 @@ In addition to plain class names, we can feed `data` with more or less complex e
# or [-44, "eB"]
data [[Float]] # generates arrays of arrays of floats
# 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
# and whose values are strings such as
# {:tR=>"m", :aSKnsndwWK=>"QUrGwAAh"}
@@ -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
# 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
rand > 0.5 ? Wadus.new(rand(9)) : FooBar.new(rand(9))
View
13 lib/mrproper/data_block.rb
@@ -45,11 +45,20 @@ def to_proc
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
if @spec == Integer
- Proc.new { rand(1000) - 500 }
+ DataBlock.new(-500..500).to_proc
elsif @spec == Float
- Proc.new { rand * 10 - 10 }
+ DataBlock.new(-5.0..5.0).to_proc
elsif @spec == String
Proc.new { String.random(rand(10)) }
elsif @spec == Symbol
View
22 test/mrproper/data_block_test.rb
@@ -137,4 +137,26 @@ def test_data_block_with_hash_of_two_elements
assert_changes { b.call[:second] }
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
View
4 test/test_helper.rb
@@ -2,4 +2,8 @@ module TestHelper
def assert_changes(&block)
assert 10.times.map(&block).uniq.size > 1
end
+
+ def assert_in_range(number, range)
+ assert range.include?(number), "Expected #{number} to be within #{range.inspect}"
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.