forked from faker-ruby/faker
/
test_faker_commerce.rb
96 lines (77 loc) · 2.55 KB
/
test_faker_commerce.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')
class TestFakerCommerce < Test::Unit::TestCase
def setup
@tester = Faker::Commerce
end
def test_color
assert @tester.color.match(/[a-z]+\.?/)
end
def test_promotion_code
assert @tester.promotion_code.match(/[A-Z][a-z]+[A-Z][a-z]+\d{6}/)
end
def test_promotion_code_should_have_specified_number_of_digits
assert @tester.promotion_code(3).match(/[A-Z][a-z]+[A-Z][a-z]+\d{3}/)
end
def test_department
assert @tester.department.match(/[A-Z][a-z]+\.?/)
end
def test_single_department_should_not_contain_separators
assert_match(/\A[A-Za-z]+\z/, @tester.department(1))
end
def test_department_should_have_ampersand_as_default_separator
assert_match ' & ', @tester.department(2, true)
end
def test_department_should_accept_localized_separator
@old_locales = I18n.config.available_locales
data = {
faker: {
separator: ' + ',
commerce: {
department: %w[Books Movies]
}
}
}
I18n.config.available_locales += [:xy]
I18n.backend.store_translations(:xy, data)
I18n.with_locale(:xy) do
assert_match ' + ', @tester.department(2, true)
end
I18n.config.available_locales = @old_locales
end
def test_department_should_have_exact_number_of_categories_when_fixed_amount
assert_match(/\A([A-Za-z]+, ){8}[A-Za-z]+ & [A-Za-z]+\z/, @tester.department(10, true))
end
def test_department_should_never_exceed_the_max_number_of_categories_when_random_amount
100.times do
assert_match(/\A([A-Za-z]+(, | & )){0,5}[A-Za-z]+\z/, @tester.department(6))
end
end
def test_department_should_have_no_duplicate_categories
department = @tester.department(10, true)
departments = department.split(/[,& ]+/)
assert_equal departments, departments.uniq
end
def test_product_name
assert @tester.product_name.match(/[A-Z][a-z]+\.?/)
end
def test_material
assert @tester.material.match(/[A-Z][a-z]+\.?/)
end
def test_price
assert_includes 0..100, @tester.price
assert_instance_of Float, @tester.price(5..6)
assert_includes 5..6, @tester.price(5..6)
assert_includes 990...1000, @tester.price(990...1000)
end
def test_price_with_srand
Faker::Config.random = Random.new(12_345)
assert_equal 92.96, @tester.price
end
def test_price_is_float
assert @tester.price.is_a? Float
end
def test_when_as_string_is_true
assert @tester.price(0..100.0, true).is_a?(String)
assert @tester.price(100..500.0, true).include?('.')
end
end