Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support range argument for Faker::Lorem #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/faker/lorem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,42 @@ class Lorem
Words = %w(alias consequatur aut perferendis sit voluptatem accusantium doloremque aperiam eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo aspernatur aut odit aut fugit sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt neque dolorem ipsum quia dolor sit amet consectetur adipisci velit sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem ut enim ad minima veniam quis nostrum exercitationem ullam corporis nemo enim ipsam voluptatem quia voluptas sit suscipit laboriosam nisi ut aliquid ex ea commodi consequatur quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae et iusto odio dignissimos ducimus qui blanditiis praesentium laudantium totam rem voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident sed ut perspiciatis unde omnis iste natus error similique sunt in culpa qui officia deserunt mollitia animi id est laborum et dolorum fuga et harum quidem rerum facilis est et expedita distinctio nam libero tempore cum soluta nobis est eligendi optio cumque nihil impedit quo porro quisquam est qui minus id quod maxime placeat facere possimus omnis voluptas assumenda est omnis dolor repellendus temporibus autem quibusdam et aut consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur at vero eos et accusamus officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae itaque earum rerum hic tenetur a sapiente delectus ut aut reiciendis voluptatibus maiores doloribus asperiores repellat)

def self.words(num = 3)
Words.shuffle[0, num]
Words.shuffle[0, randomize(num)]
end

def self.sentence(word_count = 4)
words(word_count + rand(6)).join(' ').capitalize + '.'
words(randomize(word_count) + rand(6)).join(' ').capitalize + '.'
end

def self.sentences(sentence_count = 3)
returning([]) do |sentences|
1.upto(sentence_count) do
1.upto(randomize(sentence_count)) do
sentences << sentence
end
end
end

def self.paragraph(sentence_count = 3)
sentences(sentence_count + rand(3)).join(' ')
sentences(randomize(sentence_count) + rand(3)).join(' ')
end

def self.paragraphs(paragraph_count = 3)
returning([]) do |paragraphs|
1.upto(paragraph_count) do
1.upto(randomize(paragraph_count)) do
paragraphs << paragraph
end
end
end

protected
def self.randomize(value)
case value
when Range
rand(value.max - value.min) + value.min
else
value
end
end

end
end
end
38 changes: 38 additions & 0 deletions test/test_lorem.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

require File.dirname(__FILE__) + '/test_helper.rb'

class TestFakerLorem < Test::Unit::TestCase

def setup
@tester = Faker::Lorem
end

def test_words
assert @tester.words(3).size == 3
end

def test_sentence
assert @tester.sentence(3).split(' ').size > 3
end

def test_sentences
assert @tester.sentences(3).size == 3
end


def test_paragraph
assert @tester.paragraph(3).is_a?(String)
end


def test_paragraphs
assert @tester.paragraphs(3).size == 3
end

def test_argument_as_range
1.upto(4) do
assert @tester.words(3..5).size >= 3
assert @tester.words(3..5).size <= 5
end
end
end