Skip to content

Commit

Permalink
fix issue: faker-ruby#2813 change to use deterministically_verify rat…
Browse files Browse the repository at this point in the history
…her than times
  • Loading branch information
ak2-lucky committed Aug 25, 2023
1 parent 552b91d commit 84d1676
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 170 deletions.
16 changes: 8 additions & 8 deletions test/faker/books/test_lovecraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ def test_words_without_spaces

# Faker::Lovecraft.word generates random word from wordlist
def test_word
1000.times { assert_includes @wordlist, @tester.word }
deterministically_verify(-> { @tester.word }, depth: 1000) do |word|
assert_includes @wordlist, word
end
end

# Word should not return any word with spaces
def test_word_without_spaces
1000.times { refute_match(/\s/, @tester.word) }
deterministically_verify(-> { @tester.word }, depth: 1000) do |word|
refute_match(/\s/, word)
end
end

def test_exact_count_param
Expand Down Expand Up @@ -69,18 +73,14 @@ def test_words_with_large_count_params
end

def test_sentence_with_open_compounds_allowed
1000.times do
sentence = @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: true)

deterministically_verify(-> { @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: true) }, depth: 1000) do |sentence|
assert(sentence.split.length >= 5)
end
end

# Sentence should not contain any open compounds
def test_sentence_without_open_compounds_allowed
1000.times do
sentence = @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: false)

deterministically_verify(-> { @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: false) }, depth: 1000) do |sentence|
assert_equal(5, sentence.split.length)
end
end
Expand Down
20 changes: 5 additions & 15 deletions test/faker/default/test_faker_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,39 @@ def test_name
end

def test_basic_semantic_version
10.times do |_digits|
test_sem_vers = @tester.semantic_version.split('.')

deterministically_verify(-> { @tester.semantic_version.split('.') }, depth: 10) do |test_sem_vers|
assert_match(/[0-9]{1}/, test_sem_vers[0])
assert_match(/[0-9]{1}/, test_sem_vers[1])
assert_match(/[1-9]{1}/, test_sem_vers[2])
end
end

def test_major_semantic_version
10.times do |_digits|
test_sem_vers = @tester.semantic_version(major: (1000..9999)).split('.')

deterministically_verify(-> { @tester.semantic_version(major: (1000..9999)).split('.') }, depth: 10) do |test_sem_vers|
assert_match(/[0-9]{4}/, test_sem_vers[0])
assert_match(/[0-9]{1}/, test_sem_vers[1])
assert_match(/[1-9]{1}/, test_sem_vers[2])
end
end

def test_minor_semantic_version
10.times do |_digits|
test_sem_vers = @tester.semantic_version(minor: (1000..9999)).split('.')

deterministically_verify(-> { @tester.semantic_version(minor: (1000..9999)).split('.') }, depth: 10) do |test_sem_vers|
assert_match(/[0-9]{1}/, test_sem_vers[0])
assert_match(/[0-9]{4}/, test_sem_vers[1])
assert_match(/[1-9]{1}/, test_sem_vers[2])
end
end

def test_patch_semantic_version
10.times do |_digits|
test_sem_vers = @tester.semantic_version(patch: (1000..9999)).split('.')

deterministically_verify(-> { @tester.semantic_version(patch: (1000..9999)).split('.') }, depth: 10) do |test_sem_vers|
assert_match(/[0-9]{1}/, test_sem_vers[0])
assert_match(/[0-9]{1}/, test_sem_vers[1])
assert_match(/[0-9]{4}/, test_sem_vers[2])
end
end

def test_all_semantic_version
10.times do |_digits|
test_sem_vers = @tester.semantic_version(major: (1000..9999), minor: (1000..9999), patch: (1000..9999)).split('.')

deterministically_verify(-> { @tester.semantic_version(major: (1000..9999), minor: (1000..9999), patch: (1000..9999)).split('.') }, depth: 10) do |test_sem_vers|
assert_match(/[0-9]{4}/, test_sem_vers[0])
assert_match(/[0-9]{4}/, test_sem_vers[1])
assert_match(/[0-9]{4}/, test_sem_vers[2])
Expand Down
18 changes: 7 additions & 11 deletions test/faker/default/test_faker_bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,7 @@ def test_iban_fo

# France
def test_iban_fr
100.times do
account = @tester.iban(country_code: 'fr')

deterministically_verify(-> { @tester.iban(country_code: 'fr') }, depth: 100) do |account|
assert_equal(27, account.length)
assert_match(/^#{IBAN_HEADER}\d{10}[A-Z0-9]{11}\d{2}$/, account)
assert valid_iban_checksum?(account), 'IBAN checksum is invalid'
Expand All @@ -302,9 +300,7 @@ def test_iban_fr

# United Kingdom
def test_iban_gb
100.times do
account = @tester.iban(country_code: 'gb')

deterministically_verify(-> { @tester.iban(country_code: 'gb') }, depth: 100) do |account|
assert_equal(22, account.length)
assert_match(/^#{IBAN_HEADER}[A-Z]{4}\d{14}$/, account)
assert valid_iban_checksum?(account), 'IBAN checksum is invalid'
Expand All @@ -313,11 +309,11 @@ def test_iban_gb

# Georgia
def test_iban_ge
account = @tester.iban(country_code: 'ge')

assert_equal(22, account.length)
assert_match(/^#{IBAN_HEADER}[A-Z]{2}\d{16}$/, account)
assert valid_iban_checksum?(account), 'IBAN checksum is invalid'
deterministically_verify(-> { @tester.iban(country_code: 'ge') }, depth: 100) do |account|
assert_equal(22, account.length)
assert_match(/^#{IBAN_HEADER}[A-Z]{2}\d{16}$/, account)
assert valid_iban_checksum?(account), 'IBAN checksum is invalid'
end
end

# Gibraltar
Expand Down
20 changes: 10 additions & 10 deletions test/faker/default/test_faker_cannabis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@ def setup
end

def test_strain
10.times { assert_match(/\w+/, Faker::Cannabis.strain) }
deterministically_verify(-> { Faker::Cannabis.strain }, depth: 10) { |result| assert_match(/\w+/, result) }
end

def test_cannabinoid_abbreviation
10.times { assert_match(/\w+/, Faker::Cannabis.cannabinoid_abbreviation) }
deterministically_verify(-> { Faker::Cannabis.cannabinoid_abbreviation }, depth: 10) { |result| assert_match(/\w+/, result) }
end

def test_cannabinoid
10.times { assert_match(/\w+/, Faker::Cannabis.cannabinoid) }
deterministically_verify(-> { Faker::Cannabis.cannabinoid }, depth: 10) { |result| assert_match(/\w+/, result) }
end

def test_terpene
10.times { assert_match(/\w+/, Faker::Cannabis.terpene) }
deterministically_verify(-> { Faker::Cannabis.terpene }, depth: 10) { |result| assert_match(/\w+/, result) }
end

def test_medical_use
10.times { assert_match(/\w+/, Faker::Cannabis.medical_use) }
deterministically_verify(-> { Faker::Cannabis.medical_use }, depth: 10) { |result| assert_match(/\w+/, result) }
end

def test_health_benefit
10.times { assert_match(/\w+/, Faker::Cannabis.health_benefit) }
deterministically_verify(-> { Faker::Cannabis.health_benefit }, depth: 10) { |result| assert_match(/\w+/, result) }
end

def test_category
10.times { assert_match(/\w+/, Faker::Cannabis.category) }
deterministically_verify(-> { Faker::Cannabis.category }, depth: 10) { |result| assert_match(/\w+/, result) }
end

def test_type
10.times { assert_match(/\w+/, Faker::Cannabis.type) }
deterministically_verify(-> { Faker::Cannabis.type }, depth: 10) { |result| assert_match(/\w+/, result) }
end

def test_buzzword
10.times { assert_match(/\w+/, Faker::Cannabis.buzzword) }
deterministically_verify(-> { Faker::Cannabis.buzzword }, depth: 10) { |result| assert_match(/\w+/, result) }
end

def test_brand
10.times { assert_match(/\w+/, Faker::Cannabis.brand) }
deterministically_verify(-> { Faker::Cannabis.brand }, depth: 10) { |result| assert_match(/\w+/, result) }
end

def test_locales
Expand Down
5 changes: 2 additions & 3 deletions test/faker/default/test_faker_city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ def teardown

def test_default_city_formats
I18n.with_locale(:xx) do
100.times do
cities = ['west alice', 'west smith', 'west aliceburg', 'west smithburg', 'aliceburg', 'smithburg']
city = Faker::Address.city
cities = ['west alice', 'west smith', 'west aliceburg', 'west smithburg', 'aliceburg', 'smithburg']

deterministically_verify(-> { Faker::Address.city }, depth: 100) do |city|
assert_includes cities, city, "Expected <#{cities.join(' / ')}>, but got #{city}"
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/faker/default/test_faker_commerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def test_department_should_have_exact_number_of_categories_when_fixed_amount
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(max: 6))
deterministically_verify(-> { @tester.department(max: 6) }, depth: 100) do |result|
assert_match(/\A([A-Za-z]+(, | & )){0,5}[A-Za-z]+\z/, result)
end
end

Expand Down
44 changes: 12 additions & 32 deletions test/faker/default/test_faker_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ def test_between
from = Date.parse('2012-01-01')
to = Date.parse('2013-01-01')

100.times do
random_date = @tester.between(from: from, to: to)

deterministically_verify(-> { @tester.between(from: from, to: to) }, depth: 100) do |random_date|
assert random_date >= from, "Expected >= \"#{from}\", but got #{random_date}"
assert random_date <= to, "Expected <= \"#{to}\", but got #{random_date}"
end
Expand All @@ -24,9 +22,7 @@ def test_between_except
to = Date.parse('2012-01-05')
excepted = Date.parse('2012-01-03')

100.times do
random_date = @tester.between_except(from: from, to: to, excepted: excepted)

deterministically_verify(-> { @tester.between_except(from: from, to: to, excepted: excepted) }, depth: 100) do |random_date|
assert_not_nil random_date
refute_equal random_date, excepted, "Expected != \"#{excepted}\", but got #{random_date}"
end
Expand All @@ -39,9 +35,7 @@ def test_between_except_with_strings

excepted_date = Date.parse(excepted)

100.times do
random_date = @tester.between_except(from: from, to: to, excepted: excepted)

deterministically_verify(-> { @tester.between_except(from: from, to: to, excepted: excepted) }, depth: 100) do |random_date|
assert_not_nil random_date
refute_equal random_date, excepted_date, "Expected != \"#{excepted}\", but got #{random_date}"
end
Expand All @@ -56,9 +50,7 @@ def test_between_except_with_same_from_to_and_except
def test_forward
today = Date.today

100.times do
random_date = @tester.forward(days: 5)

deterministically_verify(-> { @tester.forward(days: 5) }, depth: 100) do |random_date|
assert random_date > today, "Expected > \"#{today}\", but got #{random_date}"
end
end
Expand All @@ -77,19 +69,15 @@ def test_forward_with_string_parameter

from_date = Date.parse(from)

100.times do
random_date = @tester.forward(from: from, days: 5)

deterministically_verify(-> { @tester.forward(from: from, days: 5) }, depth: 100) do |random_date|
assert random_date > from_date, "Expected > \"#{from}\", but got #{random_date}"
end
end

def test_backward
today = Date.today

100.times do
random_date = @tester.backward(days: 5)

deterministically_verify(-> { @tester.backward(days: 5) }, depth: 100) do |random_date|
assert random_date < today, "Expected < \"#{today}\", but got #{random_date}"
end
end
Expand Down Expand Up @@ -118,9 +106,7 @@ def test_birthday
birthdate_min = Date.new(t.year - max, t.month, t.day)
birthdate_max = Date.new(t.year - min, t.month, t.day)

100.times do
birthday = @tester.birthday(min_age: min, max_age: max)

deterministically_verify(-> { @tester.birthday(min_age: min, max_age: max) }, depth: 100) do |birthday|
assert birthday >= birthdate_min, "Expect >= \"#{birthdate_min}\", but got #{birthday}"
assert birthday <= birthdate_max, "Expect <= \"#{birthdate_max}\", but got #{birthday}"
end
Expand Down Expand Up @@ -165,38 +151,33 @@ def test_default_birthday
birthdate_min = Date.new(t.year - max, t.month, t.day)
birthdate_max = Date.new(t.year - min, t.month, t.day)

100.times do
birthday = @tester.birthday

deterministically_verify(-> { @tester.birthday }, depth: 100) do |birthday|
assert birthday >= birthdate_min, "Expect >= \"#{birthdate_min}\", but got #{birthday}"
assert birthday < birthdate_max, "Expect < \"#{birthdate_max}\", but got #{birthday}"
end
end

def test_default_in_date_period
current_year = Date.today.year
10.times do
date = @tester.in_date_period

deterministically_verify(-> { @tester.in_date_period }, depth: 10) do |date|
assert_equal date.year, current_year
end
end

def test_in_date_period_with_year
year = 2015
10.times do
date = @tester.in_date_period(year: year)

deterministically_verify(-> { @tester.in_date_period(year: year) }, depth: 10) do |date|
assert_equal date.year, year
end
end

def test_in_date_period_with_month
month = 2
current_year = Date.today.year
10.times do
date = @tester.in_date_period(month: month)

deterministically_verify(-> { @tester.in_date_period(month: month) }, depth: 10) do |date|
assert_equal date.month, month
assert_equal date.year, current_year
end
Expand All @@ -205,9 +186,8 @@ def test_in_date_period_with_month
def test_in_date_period_date
year = 2008
month = 3
10.times do
date = @tester.in_date_period(year: year, month: month)

deterministically_verify(-> { @tester.in_date_period(year: year, month: month) }, depth: 10) do |date|
assert_equal date.month, month
assert_equal date.year, year
end
Expand Down
16 changes: 8 additions & 8 deletions test/faker/default/test_faker_hipster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ def test_word
@tester = Faker::Hipster
@standard_wordlist = I18n.translate('faker.hipster.words')

1000.times { assert_includes @standard_wordlist, @tester.word }
deterministically_verify(-> { @tester.word }, depth: 1000) do |word|
assert_includes @standard_wordlist, word
end
end

# Word should not return any word with spaces
def test_word_without_spaces
@tester = Faker::Hipster

1000.times { refute_match(/\s/, @tester.word) }
deterministically_verify(-> { @tester.word }, depth: 1000) do |word|
refute_match(/\s/, word)
end
end

def test_exact_count_param
Expand Down Expand Up @@ -83,18 +87,14 @@ def test_words_with_large_count_params
end

def test_sentence_with_open_compounds_allowed
1000.times do
sentence = @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: true)

deterministically_verify(-> { @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: true) }, depth: 1000) do |sentence|
assert(sentence.split.length >= 5)
end
end

# Sentence should not contain any open compounds
def test_sentence_without_open_compounds_allowed
1000.times do
sentence = @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: false)

deterministically_verify(-> { @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: false) }, depth: 1000) do |sentence|
assert_equal(5, sentence.split.length)
end
end
Expand Down

0 comments on commit 84d1676

Please sign in to comment.