From d785886d7a025cc5d49cc7549220d2f92d61832f Mon Sep 17 00:00:00 2001 From: Bradly Feeley <4227+bradly@users.noreply.github.com> Date: Fri, 11 Aug 2023 12:30:11 -0700 Subject: [PATCH] Adding Faker::Commerce.rating --- doc/default/commerce.md | 5 +++++ lib/faker/default/commerce.rb | 24 +++++++++++++++++++++++ test/faker/default/test_faker_commerce.rb | 18 ++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/doc/default/commerce.md b/doc/default/commerce.md index b8d751ab00..37e1fabcec 100644 --- a/doc/default/commerce.md +++ b/doc/default/commerce.md @@ -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" diff --git a/lib/faker/default/commerce.rb b/lib/faker/default/commerce.rb index 91ea13421a..dcf9262b21 100644 --- a/lib/faker/default/commerce.rb +++ b/lib/faker/default/commerce.rb @@ -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) diff --git a/test/faker/default/test_faker_commerce.rb b/test/faker/default/test_faker_commerce.rb index 0c0916400f..fa2fcbddb1 100644 --- a/test/faker/default/test_faker_commerce.rb +++ b/test/faker/default/test_faker_commerce.rb @@ -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 @@ -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