diff --git a/README.md b/README.md index 312f1f6..029d9d8 100644 --- a/README.md +++ b/README.md @@ -79,11 +79,16 @@ Faker::Business.credit_card_type #=> "visa" Faker::Commerce.color #=> "lavender" +# Optional arguments max=3, fixed_amount=false Faker::Commerce.department #=> "Grocery, Health & Beauty" +Faker::Commerce.department(5) #=> "Grocery, Books, Health & Beauty" +Faker::Commerce.department(2, true) #=> "Books & Tools" + Faker::Commerce.product_name #=> "Practical Granite Shirt" Faker::Commerce.price #=> "44.6" +Faker::Commerce.material #=> "Plastic" ``` ### Faker::Company diff --git a/spec/commerce_spec.cr b/spec/commerce_spec.cr new file mode 100644 index 0000000..3d85416 --- /dev/null +++ b/spec/commerce_spec.cr @@ -0,0 +1,55 @@ +require "./spec_helper" + +describe Faker::Commerce do + it "color" do + Faker::Commerce.color.match(/[a-z]+\.?/).should_not eq nil + end + + it "department" do + Faker::Commerce.department.match(/[A-Z][a-z]+\.?/).should_not eq nil + end + + it "single_department_should_not_contain_separators" do + Faker::Commerce.department(1).match(/\A[A-Za-z]+\z/).should_not eq nil + end + + it "department_should_have_ampersand_as_default_separator" do + Faker::Commerce.department(2, true).scan(" & ").should_not eq nil + end + + it "department_should_have_exact_number_of_categories_when_fixed_amount" do + Faker::Commerce.department(10, true).match(/\A([A-Za-z]+, ){8}[A-Za-z]+ & [A-Za-z]+\z/).should_not eq nil + end + + it "department_should_never_exceed_the_max_number_of_categories_when_random_amount" do + 100.times do + Faker::Commerce.department(6).match(/\A([A-Za-z]+(, | & )){0,5}[A-Za-z]+\z/).should_not eq nil + end + end + + it "department_should_have_no_duplicate_categories" do + department = Faker::Commerce.department(10, true) + + departments = department.split(/[,& ]+/) + departments.should eq departments.uniq + end + + it "product_name" do + Faker::Commerce.product_name.match(/[A-Z][a-z]+\.?/).should_not eq nil + end + + it "material" do + Faker::Commerce.material.match(/[A-Z][a-z]+\.?/).should_not eq nil + end + + it "price" do + (0 <= Faker::Commerce.price <= 100).should be_true + (5 <= Faker::Commerce.price(5..6) <= 6).should be_true + (990 <= Faker::Commerce.price(990...1000) <= 1000).should be_true + Faker::Commerce.price(5..6).is_a?(Float).should be_true + end + + it "price_is_float" do + Faker::Commerce.price.is_a?(Float).should be_true + end +end diff --git a/src/data.cr b/src/data.cr index 22565f5..b20f073 100644 --- a/src/data.cr +++ b/src/data.cr @@ -1,7 +1,6 @@ module Faker Data = { - "separator": " & ", - "address": { + "address": { "building_number": ["#####", "####", "###"], "city": ["%{Address.city_prefix} %{Name.first_name}%{Address.city_suffix}", "%{Address.city_prefix} %{Name.first_name}", "%{Name.first_name}%{Address.city_suffix}", "%{Name.last_name}%{Address.city_suffix}"], "city_prefix": ["North", "East", "West", "South", "New", "Lake", "Port"], diff --git a/src/faker/commerce.cr b/src/faker/commerce.cr index 3f67d93..0f88e88 100644 --- a/src/faker/commerce.cr +++ b/src/faker/commerce.cr @@ -4,8 +4,22 @@ module Faker Faker.fetch(Data["color"]["name"]) end - def self.department - Faker.fetch(Data["commerce"]["department"]) + def self.department(max = 3, fixed_amount = false) + num = max if fixed_amount + num ||= 1 + rand(max) + + categories = categories(num) + + if num > 1 + merge_categories(categories) + else + categories[0] + end + end + + def self.material + product_name = Data["commerce"]["product_name"] as Hash + Faker.fetch(product_name["material"]) end def self.product_name @@ -13,8 +27,23 @@ module Faker Faker.fetch(product_name["adjective"]) + " " + Faker.fetch(product_name["material"]) + " " + Faker.fetch(product_name["product"]) end - def self.price - (rand(100) + rand).round(2) + def self.price(range = 0.0..100.0) + (rand(range) * 100).floor/100.0 + end + + private def self.categories(num) + categories = [] of String + while categories.size < num + category = Faker.fetch(Data["commerce"]["department"]) + categories << category unless categories.includes?(category) + end + categories + end + + private def self.merge_categories(categories) + separator = " & " + comma_separated = categories[0...-1].join(", ") + [comma_separated, categories[-1]].join(separator) end end end