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

Add Faker::Commerce.rating #2808

Closed
wants to merge 2 commits 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
5 changes: 5 additions & 0 deletions doc/default/commerce.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Faker::Commerce.product_name #=> "Practical Granite Shirt"
Faker::Commerce.price #=> 44.6
Faker::Commerce.price(range: 0..10.0, as_string: true) #=> "2.18"

# Produces a number between between 0 and 5
# Keyword arguments: range, as_string, decimals
Faker::Commerce.rating #=> 4
Faker::Commerce.rating(range: 0..10, decimals: 2, as_string: true) #=> "8.92"

# Generate a random promotion code.
# Keyword arguments: digits
Faker::Commerce.promotion_code #=> "AmazingDeal829102"
Expand Down
24 changes: 24 additions & 0 deletions lib/faker/default/commerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ def vendor
fetch('commerce.vendor')
end

##
# Produces a random product rating.
#
# @param range [Range] A range to generate the rating number within. Defaults to 0..5.
# @param decimals [Integer] Max number of decimals to generate. Defaults to 0
#
# @return [Integer]
#
# @example
# Faker::Commerce.rating #=> 3
# Faker::Commerce.rating(range: 0..10.0, decimals: 2, as_string: true) #=> "8.92"
#
# @faker.version next
def rating(range: 0..5, decimals: 0, as_string: false)
random_value = rand(range)

if decimals.positive?
format_string = "%.#{decimals}f"
random_value = format(format_string, random_value)
end

as_string ? random_value.to_s : random_value
end

private

def categories(num)
Expand Down
18 changes: 17 additions & 1 deletion test/faker/default/test_faker_commerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_price_is_float
assert @tester.price.is_a? Float
end

def test_when_as_string_is_true
def test_price_when_as_string_is_true
assert @tester.price(range: 0..100.0, as_string: true).is_a?(String)
assert_includes @tester.price(range: 100..500.0, as_string: true), '.'
end
Expand All @@ -106,4 +106,20 @@ def test_brand
def test_vendor
assert_match(/[A-Z][a-z]+\.?/, @tester.vendor)
end

def test_rating
assert_includes 0..5, @tester.rating
end

def test_rating_with_range
assert_includes 1..2, @tester.rating(range: 1..2)
end

def test_rating_with_as_string
assert_instance_of String, @tester.rating(as_string: true)
end

def test_rating_with_decimals
assert_match(/\A\d\.\d{2}\z/, @tester.rating(decimals: 2, as_string: true))
end
end