Skip to content
This repository
Browse code

Fix randomness in lorem ipsum words method and friends

  • Loading branch information...
commit 8b392bfed259d55a63c4d43549b8b3731dabcefb 1 parent 427ce83
Nathan Sutton nate authored
4 lib/forgery/forgery/basic.rb
@@ -82,7 +82,7 @@ def self.number(options={})
82 82 options = {:at_least => 1,
83 83 :at_most => 10}.merge(options)
84 84
85   - Forgery::Extend((options[:at_least]..options[:at_most])).random
  85 + Forgery::Extend(options[:at_least]..options[:at_most]).random
86 86 end
87 87
88 88 def self.text(options={})
@@ -100,7 +100,7 @@ def self.text(options={})
100 100 allowed_characters += NUMERIC if options[:allow_numeric]
101 101 allowed_characters += SPECIAL_CHARACTERS if options[:allow_special]
102 102
103   - length = options[:exactly] || Forgery::Extend((options[:at_least]..options[:at_most])).random
  103 + length = options[:exactly] || Forgery::Extend(options[:at_least]..options[:at_most]).random
104 104
105 105 Forgery::Extend(allowed_characters).random_subset(length).join
106 106 end
2  lib/forgery/forgery/lorem_ipsum.rb
@@ -99,7 +99,7 @@ def self.range_from_quantity(quantity, options={})
99 99 return quantity if quantity.is_a?(Range)
100 100
101 101 if options[:random]
102   - start = (0..options[:random_limit]).random
  102 + start = Forgery::Extend(0..options[:random_limit]).random
103 103 start..(start+quantity-1)
104 104 else
105 105 0..(quantity-1)
12 spec/extensions/range_spec.rb
@@ -3,29 +3,29 @@
3 3
4 4 describe Range do
5 5 it "should get a random number out of the range" do
6   - range = Forgery::Extend((0..9))
  6 + range = Forgery::Extend(0..9)
7 7 10.times { range.should include(range.random) }
8 8 end
9 9
10 10 it "should not take a long time when the range is huge" do
11   - Timeout.timeout(1){Forgery::Extend((1234567890..12345678901234567890)).random}.should_not raise_error(Timeout::Error)
  11 + Timeout.timeout(1){Forgery::Extend(1234567890..12345678901234567890).random}.should_not raise_error(Timeout::Error)
12 12 end
13 13
14 14 it "should return nil for a random number from a reverse range" do
15   - 10.times { Forgery::Extend((9..0)).random.should be_nil }
  15 + 10.times { Forgery::Extend(9..0).random.should be_nil }
16 16 end
17 17
18 18 it "should get a random string our of the range" do
19   - range = Forgery::Extend(("a".."z"))
  19 + range = Forgery::Extend("a".."z")
20 20 10.times { range.should include(range.random) }
21 21 end
22 22
23 23 it "should return nil if the maximum is less than the minimum" do
24   - Forgery::Extend(("z".."a")).random.should be_nil
  24 + Forgery::Extend("z".."a").random.should be_nil
25 25 end
26 26
27 27 it "should not return the maximum at excluded range" do
28 28 Kernel.should_receive(:rand).with(9).and_return(8)
29   - Forgery::Extend((0...9)).random.should_not be_equal 9
  29 + Forgery::Extend(0...9).random.should_not be_equal 9
30 30 end
31 31 end
4 spec/forgery/lorem_ipsum_spec.rb
@@ -94,6 +94,10 @@
94 94 it "should return text with 1 or more character" do
95 95 Forgery::LoremIpsum.words.size.should >= 1
96 96 end
  97 +
  98 + it "should return a random set of words" do
  99 + Forgery::LoremIpsum.words(2, :random => true)
  100 + end
97 101 end
98 102
99 103 describe ".word" do

0 comments on commit 8b392bf

Please sign in to comment.
Something went wrong with that request. Please try again.