Skip to content

Commit

Permalink
Use match? method
Browse files Browse the repository at this point in the history
Follow up faker-ruby#2038.

This PR uses `match?`. Since Ruby 2.4, `match?` method has been introduced.
If `MatchData` is not captured, it is faster to use `match?`.
  • Loading branch information
koic committed Jun 19, 2020
1 parent 24e937e commit 181a390
Show file tree
Hide file tree
Showing 188 changed files with 879 additions and 879 deletions.
4 changes: 2 additions & 2 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def regexify(reg)
# with an array of values and selecting one of them.
def fetch(key)
fetched = sample(translate("faker.#{key}"))
if fetched&.match(%r{^\/}) && fetched&.match(%r{\/$}) # A regex
if fetched&.match?(%r{^\/}) && fetched&.match?(%r{\/$}) # A regex
regexify(fetched)
else
fetched
Expand All @@ -109,7 +109,7 @@ def fetch(key)
def fetch_all(key)
fetched = translate("faker.#{key}")
fetched = fetched.last if fetched.size <= 1
if !fetched.respond_to?(:sample) && fetched.match(%r{^\/}) && fetched.match(%r{\/$}) # A regex
if !fetched.respond_to?(:sample) && fetched.match?(%r{^\/}) && fetched.match?(%r{\/$}) # A regex
regexify(fetched)
else
fetched
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/books/lovecraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def sentence(legacy_word_count = NOT_GIVEN, legacy_random_words_to_add = NOT_GIV
# @faker.version 1.9.3
def word
random_word = sample(translate('faker.lovecraft.words'))
random_word =~ /\s/ ? word : random_word
/\s/.match?(random_word) ? word : random_word
end

##
Expand Down Expand Up @@ -147,7 +147,7 @@ def words(legacy_number = NOT_GIVEN, legacy_spaces_allowed = NOT_GIVEN, number:
return shuffle(word_list)[0, resolved_num] if spaces_allowed

words = shuffle(word_list)[0, resolved_num]
words.each_with_index { |w, i| words[i] = word if w =~ /\s/ }
words.each_with_index { |w, i| words[i] = word if /\s/.match?(w) }
end

##
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/avatar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def image(legacy_slug = NOT_GIVEN, legacy_size = NOT_GIVEN, legacy_format = NOT_
keywords << :bgset if legacy_bgset != NOT_GIVEN
end

raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /^[0-9]+x[0-9]+$/
raise ArgumentError, 'Size should be specified in format 300x300' unless /^[0-9]+x[0-9]+$/.match?(size)
raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}" unless SUPPORTED_FORMATS.include?(format)

slug ||= Faker::Lorem.words.join
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def compile_bsb_number
def iban_checksum(country_code, account)
# Converts letters to numbers according the iban rules, A=10..Z=35
account_to_number = "#{account}#{country_code}00".upcase.chars.map do |d|
d =~ /[A-Z]/ ? (d.ord - 55).to_s : d
/[A-Z]/.match?(d) ? (d.ord - 55).to_s : d
end.join.to_i

# This is answer to (iban_to_num + checksum) % 97 == 1
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/default/fillmurray.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def image(legacy_grayscale = NOT_GIVEN, legacy_width = NOT_GIVEN, legacy_height
keywords << :height if legacy_height != NOT_GIVEN
end

raise ArgumentError, 'Width should be a number' unless width.to_s =~ /^\d+$/
raise ArgumentError, 'Height should be a number' unless height.to_s =~ /^\d+$/
raise ArgumentError, 'Width should be a number' unless /^\d+$/.match?(width.to_s)
raise ArgumentError, 'Height should be a number' unless /^\d+$/.match?(height.to_s)
raise ArgumentError, 'Grayscale should be a boolean' unless [true, false].include?(grayscale)

"https://www.fillmurray.com#{'/g' if grayscale == true}/#{width}/#{height}"
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/default/hipster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class << self
# @faker.version 1.6.0
def word
random_word = sample(translate('faker.hipster.words'))
random_word =~ /\s/ ? word : random_word
/\s/.match?(random_word) ? word : random_word
end

##
Expand Down Expand Up @@ -51,7 +51,7 @@ def words(legacy_number = NOT_GIVEN, legacy_supplemental = NOT_GIVEN, legacy_spa
return shuffle(word_list)[0, resolved_num] if spaces_allowed

words = shuffle(word_list)[0, resolved_num]
words.each_with_index { |w, i| words[i] = word if w =~ /\s/ }
words.each_with_index { |w, i| words[i] = word if /\s/.match?(w) }
end

##
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/default/id_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def invalid
def ssn_valid
ssn = regexify(/[0-8]\d{2}-\d{2}-\d{4}/)
# We could still have all 0s in one segment or another
INVALID_SSN.any? { |regex| regex =~ ssn } ? ssn_valid : ssn
INVALID_SSN.any? { |regex| regex.match?(ssn) } ? ssn_valid : ssn
end

##
Expand Down Expand Up @@ -151,7 +151,7 @@ def brazilian_citizen_number(legacy_formatted = NOT_GIVEN, formatted: false)
keywords << :formatted if legacy_formatted != NOT_GIVEN
end

digits = Faker::Number.leading_zero_number(digits: 9) until digits&.match(/(\d)((?!\1)\d)+/)
digits = Faker::Number.leading_zero_number(digits: 9) until digits&.match?(/(\d)((?!\1)\d)+/)
first_digit = brazilian_citizen_number_checksum_digit(digits)
second_digit = brazilian_citizen_number_checksum_digit(digits + first_digit)
number = [digits, first_digit, second_digit].join
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def password(legacy_min_length = NOT_GIVEN, legacy_max_length = NOT_GIVEN, legac
if mix_case
alpha_count = 0
temp.chars.each_with_index do |char, index|
if char =~ /[[:alpha:]]/
if /[[:alpha:]]/.match?(char)
temp[index] = char.upcase if alpha_count.even?
alpha_count += 1
end
Expand Down Expand Up @@ -229,7 +229,7 @@ def private_nets_regex
end

def private_net_checker
->(addr) { private_nets_regex.any? { |net| net =~ addr } }
->(addr) { private_nets_regex.any? { |net| net.match?(addr) } }
end

def reserved_nets_regex
Expand All @@ -245,7 +245,7 @@ def reserved_nets_regex
end

def reserved_net_checker
->(addr) { (private_nets_regex + reserved_nets_regex).any? { |net| net =~ addr } }
->(addr) { (private_nets_regex + reserved_nets_regex).any? { |net| net.match?(addr) } }
end

def ip_v4_cidr
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def reference(legacy_ref = NOT_GIVEN, ref: '')
def iban_checksum(country_code, account)
# Converts letters to numbers according the iban rules, A=10..Z=35
account_to_number = "#{account}#{country_code}00".upcase.chars.map do |d|
d =~ /[A-Z]/ ? (d.ord - 55).to_s : d
/[A-Z]/.match?(d) ? (d.ord - 55).to_s : d
end.join.to_i

# This is answer to (iban_to_num + checksum) % 97 == 1
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/lorem_flickr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def colorized_image(legacy_size = NOT_GIVEN, legacy_color = NOT_GIVEN, legacy_se
private

def build_url(size, format, search_terms, match_all)
raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /^[0-9]+x[0-9]+$/
raise ArgumentError, 'Size should be specified in format 300x300' unless /^[0-9]+x[0-9]+$/.match?(size)

url_parts = ['https://loremflickr.com']
url_parts << format
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/lorem_pixel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def image(legacy_size = NOT_GIVEN, legacy_is_gray = NOT_GIVEN, legacy_category =
keywords << :secure if legacy_secure != NOT_GIVEN
end

raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /^[0-9]+x[0-9]+$/
raise ArgumentError, 'Size should be specified in format 300x300' unless /^[0-9]+x[0-9]+$/.match?(size)
raise ArgumentError, "Supported categories are #{SUPPORTED_CATEGORIES.join(', ')}" unless category.nil? || SUPPORTED_CATEGORIES.include?(category)
raise ArgumentError, 'Category required when number is passed' if !number.nil? && category.nil?
raise ArgumentError, 'Number must be between 1 and 10' unless number.nil? || (1..10).cover?(number)
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/default/placeholdit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def image(legacy_size = NOT_GIVEN, legacy_format = NOT_GIVEN, legacy_background_
background_color = generate_color if background_color == :random
text_color = generate_color if text_color == :random

raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /^[0-9]+x[0-9]+$/
raise ArgumentError, 'Size should be specified in format 300x300' unless /^[0-9]+x[0-9]+$/.match?(size)
raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}" unless SUPPORTED_FORMATS.include?(format)
raise ArgumentError, "background_color must be a hex value without '#'" unless background_color.nil? || background_color =~ /((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/
raise ArgumentError, "text_color must be a hex value without '#'" unless text_color.nil? || text_color =~ /((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/
raise ArgumentError, "background_color must be a hex value without '#'" unless background_color.nil? || /((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/.match?(background_color)
raise ArgumentError, "text_color must be a hex value without '#'" unless text_color.nil? || /((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/.match?(text_color)

image_url = "https://placehold.it/#{size}.#{format}"
image_url += "/#{background_color}" if background_color
Expand Down
2 changes: 1 addition & 1 deletion test/faker/blockchain/bitcoin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class TestFakerBitcoin < Test::Unit::TestCase
def test_address
assert Faker::Blockchain::Bitcoin.address.match(/^[13][1-9A-Za-z][^OIl]{20,40}/)
assert Faker::Blockchain::Bitcoin.address.match?(/^[13][1-9A-Za-z][^OIl]{20,40}/)
end

def test_deterministic_address
Expand Down
2 changes: 1 addition & 1 deletion test/faker/blockchain/ethereum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def setup
end

def test_address
assert @tester.address.match(/0x([a-fA-F0-9]{40})/)
assert @tester.address.match?(/0x([a-fA-F0-9]{40})/)
end
end
10 changes: 5 additions & 5 deletions test/faker/blockchain/tezos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

class TestFakerTezos < Test::Unit::TestCase
def test_contract
assert Faker::Blockchain::Tezos.contract.match(/^KT1[1-9A-Za-z][^OIl]{20,40}/)
assert Faker::Blockchain::Tezos.contract.match?(/^KT1[1-9A-Za-z][^OIl]{20,40}/)
end

def test_account
assert Faker::Blockchain::Tezos.account.match(/^tz1[1-9A-Za-z][^OIl]{20,40}/)
assert Faker::Blockchain::Tezos.account.match?(/^tz1[1-9A-Za-z][^OIl]{20,40}/)
end

def test_operation
assert Faker::Blockchain::Tezos.operation.match(/^o[1-9A-Za-z][^OIl]{20,40}/)
assert Faker::Blockchain::Tezos.operation.match?(/^o[1-9A-Za-z][^OIl]{20,40}/)
end

def test_block
assert Faker::Tezos.block.match(/^B[1-9A-Za-z][^OIl]{20,40}/)
assert Faker::Tezos.block.match?(/^B[1-9A-Za-z][^OIl]{20,40}/)
end

def test_signature
assert Faker::Blockchain::Tezos.signature.match(/^edsig[1-9A-Za-z][^OIl]{20,40}/)
assert Faker::Blockchain::Tezos.signature.match?(/^edsig[1-9A-Za-z][^OIl]{20,40}/)
end

def test_deterministic_contract
Expand Down
8 changes: 4 additions & 4 deletions test/faker/books/test_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ def setup
end

def test_title
assert @tester.title.match(/(\w+\.? ?){2,3}/)
assert @tester.title.match?(/(\w+\.? ?){2,3}/)
end

def test_author
assert @tester.author.match(/(\w+\.? ?){2,3}/)
assert @tester.author.match?(/(\w+\.? ?){2,3}/)
end

def test_publisher
assert @tester.publisher.match(/(\w+\.? ?){2,3}/)
assert @tester.publisher.match?(/(\w+\.? ?){2,3}/)
end

def test_genre
assert @tester.genre.match(/(\w+\.? ?){2,3}/)
assert @tester.genre.match?(/(\w+\.? ?){2,3}/)
end
end
12 changes: 6 additions & 6 deletions test/faker/books/test_faker_culture_series.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ def setup
end

def test_book
assert @tester.book.match(/\w+/)
assert @tester.book.match?(/\w+/)
end

def test_culture_ship
assert @tester.culture_ship.match(/\w+/)
assert @tester.culture_ship.match?(/\w+/)
end

def test_culture_ship_class
assert @tester.culture_ship_class.match(/\w+/)
assert @tester.culture_ship_class.match?(/\w+/)
end

def test_culture_ship_abv
assert @tester.culture_ship_class_abv.match(/\w+/)
assert @tester.culture_ship_class_abv.match?(/\w+/)
end

def test_civ
assert @tester.civ.match(/\w+/)
assert @tester.civ.match?(/\w+/)
end

def test_planet
assert @tester.planet.match(/\w+/)
assert @tester.planet.match?(/\w+/)
end
end
14 changes: 7 additions & 7 deletions test/faker/books/test_faker_dune.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ def setup
end

def test_character
assert @tester.character.match(/\w+/)
assert @tester.character.match?(/\w+/)
end

def test_title
assert @tester.title.match(/\w+/)
assert @tester.title.match?(/\w+/)
end

def test_planet
assert @tester.planet.match(/\w+/)
assert @tester.planet.match?(/\w+/)
end

def test_random_quote
assert @tester.quote.match(/\w+/)
assert @tester.quote.match?(/\w+/)
end

# test good match
def test_random_character_quote
assert @tester.quote(character: 'paul').match(/\w+/)
assert @tester.quote(character: 'paul').match?(/\w+/)
end

# test error on no match
Expand All @@ -36,12 +36,12 @@ def test_invalid_quote
end

def test_random_saying
assert @tester.saying.match(/\w+/)
assert @tester.saying.match?(/\w+/)
end

# test good match
def test_random_source_saying
assert @tester.saying(source: 'fremen').match(/\w+/)
assert @tester.saying(source: 'fremen').match?(/\w+/)
end

# test error on no match
Expand Down
12 changes: 6 additions & 6 deletions test/faker/books/test_lovecraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_words
# Words should not return any word with spaces
def test_words_without_spaces
@words = @tester.words(number: 1000)
@words.each { |w| assert !w.match(/\s/) }
@words.each { |w| assert !w.match?(/\s/) }
end

# Faker::Lovecraft.word generates random word from wordlist
Expand All @@ -27,7 +27,7 @@ def test_word

# Word should not return any word with spaces
def test_word_without_spaces
1000.times { assert !@tester.word.match(/\s/) }
1000.times { assert !@tester.word.match?(/\s/) }
end

def test_exact_count_param
Expand Down Expand Up @@ -72,18 +72,18 @@ def test_paragraph_char_count
end

def test_tome
assert @tester.tome.match(/\w/)
assert @tester.tome.match?(/\w/)
end

def test_location
assert @tester.location.match(/\w/)
assert @tester.location.match?(/\w/)
end

def test_deity
assert @tester.deity.match(/\w/)
assert @tester.deity.match?(/\w/)
end

def test_fhtagn
assert @tester.fhtagn.match(/\w/)
assert @tester.fhtagn.match?(/\w/)
end
end
2 changes: 1 addition & 1 deletion test/faker/creature/test_faker_animal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def setup
end

def test_name
assert @tester.name.match(/\w+/)
assert @tester.name.match?(/\w+/)
end
end
6 changes: 3 additions & 3 deletions test/faker/creature/test_faker_cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def setup
end

def test_name
assert @tester.name.match(/\w+\.?/)
assert @tester.name.match?(/\w+\.?/)
end

def test_breed
assert @tester.breed.match(/\w+\.?/)
assert @tester.breed.match?(/\w+\.?/)
end

def test_registry
assert @tester.registry.match(/\w+\.?/)
assert @tester.registry.match?(/\w+\.?/)
end
end
Loading

0 comments on commit 181a390

Please sign in to comment.