-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Faker | ||
class LoremPixel < Base | ||
class << self | ||
SUPPORTED_CATEGORIES = %w(abstract animals business cats city food nightlife fashion people nature sports technics transport) | ||
|
||
def image(size = '300x300', is_gray = false, category = nil, number = nil, text = nil) | ||
raise ArgumentError, 'Size should be specified in format 300x300' unless size.match(/^[0-9]+x[0-9]+$/) | ||
raise ArgumentError, "Supported categories are #{SUPPORTED_CATEGORIES.join(', ')}" unless category.nil? || SUPPORTED_CATEGORIES.include?(category) | ||
raise ArgumentError, 'Category required when number is passed' if !number.nil? && category.nil? | ||
raise ArgumentError, 'Number must be between 1 and 10' unless number.nil? || (1..10).include?(number) | ||
raise ArgumentError, 'Category and number must be passed when text is passed' if !text.nil? && number.nil? && category.nil? | ||
|
||
url_parts = ['http://lorempixel.com'] | ||
url_parts << 'g' if is_gray | ||
url_parts += size.split('x') | ||
url_parts += [category, number, text].compact | ||
url_parts.join('/') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb') | ||
|
||
class TestLoremPixel < Test::Unit::TestCase | ||
def setup | ||
@tester = Faker::LoremPixel | ||
end | ||
|
||
def test_placeholdit | ||
assert @tester.image.match(/http:\/\/lorempixel\.com\/(\d+\/\d+)/)[1] != nil | ||
end | ||
|
||
def test_image_with_custom_size | ||
assert @tester.image('3x3').match(/http:\/\/lorempixel\.com\/(\d+\/\d+)/)[1] == '3/3' | ||
end | ||
|
||
def test_image_with_incorrect_size | ||
assert_raise ArgumentError do | ||
@tester.image('300x300s') | ||
end | ||
end | ||
|
||
def test_image_gray | ||
assert @tester.image('300x300', true).match(/http:\/\/lorempixel\.com\/g\/\d+\/\d+/) | ||
end | ||
|
||
def test_image_with_supported_category | ||
assert @tester.image('300x300', false, 'animals').match(/http:\/\/lorempixel\.com\/\d+\/\d+\/(.*)/)[1] == 'animals' | ||
end | ||
|
||
def test_image_with_incorrect_category | ||
assert_raise ArgumentError do | ||
@tester.image('300x300', false, 'wrong_category') | ||
end | ||
end | ||
|
||
def test_image_with_supported_category_and_correct_number | ||
assert @tester.image('300x300', false, 'animals', 3).match(/http:\/\/lorempixel\.com\/\d+\/\d+\/.+\/(\d+)/)[1] == '3' | ||
end | ||
|
||
def test_image_with_supported_category_and_incorrect_number | ||
assert_raise ArgumentError do | ||
@tester.image('300x300', false, 'animals', 11) | ||
end | ||
end | ||
|
||
def test_image_with_correct_number_and_without_category | ||
assert_raise ArgumentError do | ||
@tester.image('300x300', false, 'wrong_category', 3) | ||
end | ||
end | ||
|
||
def test_image_with_text_correct_number_and_supported_category | ||
assert @tester.image('300x300', false, 'animals', 3, 'Dummy-text').match(/http:\/\/lorempixel\.com\/\d+\/\d+\/.+\/3\/(.+)/)[1] == 'Dummy-text' | ||
end | ||
|
||
def test_image_with_text_supported_category_and_text_without_number | ||
assert @tester.image('300x300', false, 'animals', nil, 'Dummy-text').match(/http:\/\/lorempixel\.com\/\d+\/\d+\/.+\/(.+)/)[1] == 'Dummy-text' | ||
end | ||
|
||
def test_image_with_text_without_number_and_category | ||
assert_raise ArgumentError do | ||
@tester.image('300x300', false, nil, nil, 'Dummy-text') | ||
end | ||
end | ||
|
||
end |