diff --git a/.editorconfig b/.editorconfig index c6c8b362..b372c226 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,3 +7,4 @@ end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true +max_line_length = 120 diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml new file mode 100644 index 00000000..375bf1ed --- /dev/null +++ b/.github/workflows/rubocop.yml @@ -0,0 +1,17 @@ +name: RuboCop +on: + pull_request: + push: + branches: + - "main" +jobs: + test: + runs-on: ubuntu-latest + name: RuboCop lint + steps: + - uses: actions/checkout@v3 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: 'ruby' + bundler-cache: true # 'bundle install' and cache + - run: bundle exec rake rubocop diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 21acb700..61eeae59 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,4 +20,4 @@ jobs: with: ruby-version: ${{ matrix.ruby }} bundler-cache: true # 'bundle install' and cache - - run: rake test + - run: bundle exec rake test diff --git a/.gitignore b/.gitignore index 32541c97..d7395d88 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,3 @@ Gemfile.lock .ruby-gemset .ruby-version .rvmrc -.rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml index 88b12c4e..24f072da 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,15 @@ +require: + - rubocop-performance + - rubocop-rake + +inherit_mode: + merge: + - Include + - Exclude + +inherit_from: + - ./.rubocop_todo.yml + AllCops: TargetRubyVersion: 3.0 NewCops: enable @@ -24,13 +36,23 @@ Style/Documentation: Style/AsciiComments: Enabled: true Exclude: - - 'lib/ffaker/address_ja.rb' - - 'lib/ffaker/address_se.rb' + - '**/*_cs.rb' + - '**/*_ja.rb' + - '**/*_mx.rb' + - '**/*_se.rb' Style/OptionalBooleanParameter: Enabled: false Metrics/ModuleLength: Enabled: true + Max: 200 +Metrics/ClassLength: Exclude: - - 'lib/ffaker/bank.rb' + - 'test/**/*.rb' +Metrics/MethodLength: + AllowedPatterns: + - 'test_' +Metrics/AbcSize: + AllowedPatterns: + - 'test_' Style/RedundantArgument: Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 00000000..26edf105 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,6 @@ +Metrics/MethodLength: + Enabled: false +Metrics/AbcSize: + Enabled: false +Metrics/CyclomaticComplexity: + Enabled: false diff --git a/Changelog.md b/Changelog.md index 8f031926..3138bf0f 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,8 @@ ## development - Add your change HERE + - Resolve a lot of RuboCop offenses [@AlexWayfer] + - Change `Image.url`, `Image.file`, `Book.cover` and `Avatar.image` arguments to keywords [@AlexWayfer] - Adds FFaker::Date.birthday [@professor] - Adds FFaker::Crypto.sha256 [@professor] - Update README [@professor] diff --git a/Gemfile b/Gemfile index 5f10ba8c..9e598278 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,19 @@ # frozen_string_literal: true source 'https://rubygems.org' + gemspec + +group :development do + gem 'rake', '~> 13.0' +end + +group :development, :lint do + gem 'rubocop' + gem 'rubocop-performance' + gem 'rubocop-rake' +end + +group :development, :test do + gem 'test-unit' +end diff --git a/REFERENCE.md b/REFERENCE.md index d8ae463e..330878dc 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -88,7 +88,7 @@ * [FFaker::IdentificationESCO](#ffakeridentificationesco) * [FFaker::IdentificationIN](#ffakeridentificationin) * [FFaker::IdentificationIT](#ffakeridentificationit) - * [FFaker::IdentificationKr](#ffakeridentificationkr) + * [FFaker::IdentificationKR](#ffakeridentificationkr) * [FFaker::IdentificationMX](#ffakeridentificationmx) * [FFaker::IdentificationPL](#ffakeridentificationpl) * [FFaker::IdentificationTW](#ffakeridentificationtw) @@ -1385,7 +1385,7 @@ | `gender` | ‼️ FFaker::UniqueUtils::RetryLimitExceeded: Retry limit exceeded for gender | | `ssn` | 11830877014613103430, 45436375172610549922, 29278264986997805738 | -## FFaker::IdentificationKr +## FFaker::IdentificationKR | Method | Example | | ------ | ------- | diff --git a/Rakefile b/Rakefile index f24a0534..0e427c37 100644 --- a/Rakefile +++ b/Rakefile @@ -23,6 +23,10 @@ module Test end end +require 'rubocop/rake_task' + +RuboCop::RakeTask.new + ############################################################################# # # Helper functions @@ -80,14 +84,9 @@ rescue LoadError end end -############################################################################# -# -# Packaging tasks -# -############################################################################# - +desc 'Packaging tasks' task release: :build do - unless `git branch` =~ /^\* main$/ + unless `git branch`.match?(/^\* main$/) puts 'You must be on the main branch to release!' exit! end @@ -103,6 +102,7 @@ task build: %i[validate reference] do sh "gem build #{gemspec_file} --output=pkg/#{gem_file}" end +desc 'Validate library files' task :validate do libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"] unless libfiles.empty? diff --git a/ffaker.gemspec b/ffaker.gemspec index a2b0cb3b..9fe4f1c5 100644 --- a/ffaker.gemspec +++ b/ffaker.gemspec @@ -29,16 +29,12 @@ Gem::Specification.new do |s| s.files = `git ls-files` .split("\n") .sort - .reject { |file| file =~ /^\./ } - .reject { |file| file =~ /^(rdoc|pkg)/ } + .grep_v(/^\./) + .grep_v(/^(rdoc|pkg)/) s.metadata = { 'changelog_uri' => 'https://github.com/ffaker/ffaker/blob/main/Changelog.md', 'documentation_uri' => 'https://github.com/ffaker/ffaker/blob/main/REFERENCE.md', 'rubygems_mfa_required' => 'true' } - - s.add_development_dependency 'rake', '~> 13.0' - s.add_development_dependency 'rubocop' - s.add_development_dependency 'test-unit' end diff --git a/lib/ffaker.rb b/lib/ffaker.rb index 6937c5a0..35b59b34 100644 --- a/lib/ffaker.rb +++ b/lib/ffaker.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative 'ffaker/version' + module FFaker require_relative 'ffaker/utils/array_utils' require_relative 'ffaker/utils/module_utils' @@ -28,198 +30,63 @@ def self.bothify(masks) letterify(numerify(masks)) end - autoload :Address, 'ffaker/address' - autoload :AddressAU, 'ffaker/address_au' - autoload :AddressBR, 'ffaker/address_br' - autoload :AddressCA, 'ffaker/address_ca' - autoload :AddressCH, 'ffaker/address_ch' - autoload :AddressCHDE, 'ffaker/address_ch_de' - autoload :AddressCHFR, 'ffaker/address_ch_fr' - autoload :AddressCHIT, 'ffaker/address_ch_it' - autoload :AddressDA, 'ffaker/address_da' - autoload :AddressDE, 'ffaker/address_de' - autoload :AddressFI, 'ffaker/address_fi' - autoload :AddressFR, 'ffaker/address_fr' - autoload :AddressGR, 'ffaker/address_gr' - autoload :AddressID, 'ffaker/address_id' - autoload :AddressIN, 'ffaker/address_in' - autoload :AddressIT, 'ffaker/address_it' - autoload :AddressJA, 'ffaker/address_ja' - autoload :AddressKR, 'ffaker/address_kr' - autoload :AddressMX, 'ffaker/address_mx' - autoload :AddressNL, 'ffaker/address_nl' - autoload :AddressPL, 'ffaker/address_pl' - autoload :AddressRU, 'ffaker/address_ru' - autoload :AddressSE, 'ffaker/address_se' - autoload :AddressSN, 'ffaker/address_sn' - autoload :AddressUA, 'ffaker/address_ua' - autoload :AddressUK, 'ffaker/address_uk' - autoload :AddressUS, 'ffaker/address_us' - autoload :Airline, 'ffaker/airline' - autoload :Animal, 'ffaker/animal' - autoload :AnimalBR, 'ffaker/animal_br' - autoload :AnimalCN, 'ffaker/animal_cn' - autoload :AnimalES, 'ffaker/animal_es' - autoload :AnimalPL, 'ffaker/animal_pl' - autoload :AnimalUS, 'ffaker/animal_us' - autoload :Avatar, 'ffaker/avatar' - autoload :AWS, 'ffaker/aws' - autoload :BaconIpsum, 'ffaker/bacon_ipsum' - autoload :Bank, 'ffaker/bank' - autoload :Book, 'ffaker/book' - autoload :Boolean, 'ffaker/boolean' - autoload :CheesyLingo, 'ffaker/cheesy_lingo' - autoload :Code, 'ffaker/code' - autoload :Color, 'ffaker/color' - autoload :ColorPL, 'ffaker/color_pl' - autoload :ColorUA, 'ffaker/color_ua' - autoload :Company, 'ffaker/company' - autoload :CompanyCN, 'ffaker/company_cn' - autoload :CompanyFR, 'ffaker/company_fr' - autoload :CompanyIT, 'ffaker/company_it' - autoload :CompanyJA, 'ffaker/company_ja' - autoload :CompanySE, 'ffaker/company_se' - autoload :Conference, 'ffaker/conference' - autoload :CoursesFR, 'ffaker/courses_fr' - autoload :Crypto, 'ffaker/crypto' - autoload :Currency, 'ffaker/currency' - autoload :Date, 'ffaker/date' - autoload :DizzleIpsum, 'ffaker/dizzle_ipsum' - autoload :Education, 'ffaker/education' - autoload :EducationCN, 'ffaker/education_cn' - autoload :Filesystem, 'ffaker/filesystem' - autoload :Food, 'ffaker/food' - autoload :FoodPL, 'ffaker/food_pl' - autoload :FreedomIpsum, 'ffaker/freedom_ipsum' - autoload :Game, 'ffaker/game' - autoload :Gender, 'ffaker/gender' - autoload :GenderBR, 'ffaker/gender_br' - autoload :GenderCN, 'ffaker/gender_cn' - autoload :GenderID, 'ffaker/gender_id' - autoload :GenderIT, 'ffaker/gender_it' - autoload :GenderJA, 'ffaker/gender_ja' - autoload :GenderJP, 'ffaker/gender_jp' - autoload :GenderKR, 'ffaker/gender_kr' - autoload :GenderPL, 'ffaker/gender_pl' - autoload :GenderRU, 'ffaker/gender_ru' - autoload :Geolocation, 'ffaker/geolocation' - autoload :Guid, 'ffaker/guid' - autoload :HealthcareIpsum, 'ffaker/healthcare_ipsum' - autoload :HealthcareRU, 'ffaker/healthcare_ru' - autoload :HipsterIpsum, 'ffaker/hipster_ipsum' - autoload :HTMLIpsum, 'ffaker/html_ipsum' - autoload :Identification, 'ffaker/identification' - autoload :IdentificationBR, 'ffaker/identification_br' - autoload :IdentificationES, 'ffaker/identification_es' - autoload :IdentificationESCL, 'ffaker/identification_es_cl' - autoload :IdentificationESCO, 'ffaker/identification_es_co' - autoload :IdentificationIN, 'ffaker/identification_in' - autoload :IdentificationIT, 'ffaker/identification_it' - autoload :IdentificationKr, 'ffaker/identification_kr' - autoload :IdentificationMX, 'ffaker/identification_mx' - autoload :IdentificationPL, 'ffaker/identification_pl' - autoload :IdentificationTW, 'ffaker/identification_tw' - autoload :IdentificationEC, 'ffaker/identification_ec' - autoload :Image, 'ffaker/image' - autoload :Internet, 'ffaker/internet' - autoload :InternetSE, 'ffaker/internet_se' - autoload :Job, 'ffaker/job' - autoload :JobBR, 'ffaker/job_br' - autoload :JobCN, 'ffaker/job_cn' - autoload :JobFR, 'ffaker/job_fr' - autoload :JobIT, 'ffaker/job_it' - autoload :JobJA, 'ffaker/job_ja' - autoload :JobKR, 'ffaker/job_kr' - autoload :JobVN, 'ffaker/job_vn' - autoload :JoJo, 'ffaker/jo_jo' - autoload :Locale, 'ffaker/locale' - autoload :Lorem, 'ffaker/lorem' - autoload :LoremAR, 'ffaker/lorem_ar' - autoload :LoremBR, 'ffaker/lorem_br' - autoload :LoremCN, 'ffaker/lorem_cn' - autoload :LoremFR, 'ffaker/lorem_fr' - autoload :LoremIE, 'ffaker/lorem_ie' - autoload :LoremIT, 'ffaker/lorem_it' - autoload :LoremJA, 'ffaker/lorem_ja' - autoload :LoremKR, 'ffaker/lorem_kr' - autoload :LoremPL, 'ffaker/lorem_pl' - autoload :LoremRU, 'ffaker/lorem_ru' - autoload :LoremUA, 'ffaker/lorem_ua' - autoload :Movie, 'ffaker/movie' - autoload :Music, 'ffaker/music' - autoload :Name, 'ffaker/name' - autoload :NameAR, 'ffaker/name_ar' - autoload :NameBR, 'ffaker/name_br' - autoload :NameCN, 'ffaker/name_cn' - autoload :NameCS, 'ffaker/name_cs' - autoload :NameDA, 'ffaker/name_da' - autoload :NameDE, 'ffaker/name_de' - autoload :NameES, 'ffaker/name_es' - autoload :NameFR, 'ffaker/name_fr' - autoload :NameGA, 'ffaker/name_ga' - autoload :NameGR, 'ffaker/name_gr' - autoload :NameID, 'ffaker/name_id' - autoload :NameIN, 'ffaker/name_in' - autoload :NameIT, 'ffaker/name_it' - autoload :NameJA, 'ffaker/name_ja' - autoload :NameKH, 'ffaker/name_kh' - autoload :NameKR, 'ffaker/name_kr' - autoload :NameMX, 'ffaker/name_mx' - autoload :NameNB, 'ffaker/name_nb' - autoload :NameNL, 'ffaker/name_nl' - autoload :NamePH, 'ffaker/name_ph' - autoload :NamePL, 'ffaker/name_pl' - autoload :NameRU, 'ffaker/name_ru' - autoload :NameSE, 'ffaker/name_se' - autoload :NameSN, 'ffaker/name_sn' - autoload :NameTH, 'ffaker/name_th' - autoload :NameTW, 'ffaker/name_tw' - autoload :NameTHEN, 'ffaker/name_th_en' - autoload :NameUA, 'ffaker/name_ua' - autoload :NameVN, 'ffaker/name_vn' - autoload :NatoAlphabet, 'ffaker/nato_alphabet' - autoload :Number, 'ffaker/number' - autoload :PhoneNumber, 'ffaker/phone_number' - autoload :PhoneNumberAU, 'ffaker/phone_number_au' - autoload :PhoneNumberBR, 'ffaker/phone_number_br' - autoload :PhoneNumberCH, 'ffaker/phone_number_ch' - autoload :PhoneNumberCU, 'ffaker/phone_number_cu' - autoload :PhoneNumberDA, 'ffaker/phone_number_da' - autoload :PhoneNumberDE, 'ffaker/phone_number_de' - autoload :PhoneNumberFR, 'ffaker/phone_number_fr' - autoload :PhoneNumberID, 'ffaker/phone_number_id' - autoload :PhoneNumberIT, 'ffaker/phone_number_it' - autoload :PhoneNumberJA, 'ffaker/phone_number_ja' - autoload :PhoneNumberKR, 'ffaker/phone_number_kr' - autoload :PhoneNumberMX, 'ffaker/phone_number_mx' - autoload :PhoneNumberNL, 'ffaker/phone_number_nl' - autoload :PhoneNumberPL, 'ffaker/phone_number_pl' - autoload :PhoneNumberSE, 'ffaker/phone_number_se' - autoload :PhoneNumberSG, 'ffaker/phone_number_sg' - autoload :PhoneNumberSN, 'ffaker/phone_number_sn' - autoload :PhoneNumberTW, 'ffaker/phone_number_tw' - autoload :PhoneNumberUA, 'ffaker/phone_number_ua' - autoload :PhoneNumberRU, 'ffaker/phone_number_ru' - autoload :Product, 'ffaker/product' - autoload :SemVer, 'ffaker/sem_ver' - autoload :Skill, 'ffaker/skill' - autoload :Sport, 'ffaker/sport' - autoload :SportPL, 'ffaker/sport_pl' - autoload :SportUS, 'ffaker/sport_us' - autoload :SportRU, 'ffaker/sport_ru' - autoload :SSN, 'ffaker/ssn' - autoload :SSNMX, 'ffaker/ssn_mx' - autoload :SSNSE, 'ffaker/ssn_se' - autoload :String, 'ffaker/string' - autoload :Time, 'ffaker/time' - autoload :Tweet, 'ffaker/tweet' - autoload :Unit, 'ffaker/unit' - autoload :UnitEnglish, 'ffaker/unit_english' - autoload :UnitMetric, 'ffaker/unit_metric' - autoload :VERSION, 'ffaker/version' - autoload :Vehicle, 'ffaker/vehicle' - autoload :Venue, 'ffaker/venue' - autoload :Youtube, 'ffaker/youtube' + autoload_acronyms = { + 'ar' => 'AR', + 'au' => 'AU', + 'aws' => 'AWS', + 'br' => 'BR', + 'ca' => 'CA', + 'ch' => 'CH', + 'cl' => 'CL', + 'cn' => 'CN', + 'co' => 'CO', + 'cs' => 'CS', + 'cu' => 'CU', + 'da' => 'DA', + 'de' => 'DE', + 'ec' => 'EC', + 'en' => 'EN', + 'es' => 'ES', + 'fi' => 'FI', + 'fr' => 'FR', + 'ga' => 'GA', + 'gr' => 'GR', + 'html' => 'HTML', + 'id' => 'ID', + 'ie' => 'IE', + 'in' => 'IN', + 'it' => 'IT', + 'ja' => 'JA', + 'jp' => 'JP', + 'kh' => 'KH', + 'kr' => 'KR', + 'mx' => 'MX', + 'nb' => 'NB', + 'nl' => 'NL', + 'ph' => 'PH', + 'pl' => 'PL', + 'ru' => 'RU', + 'se' => 'SE', + 'sg' => 'SG', + 'sn' => 'SN', + 'ssn' => 'SSN', + 'th' => 'TH', + 'tw' => 'TW', + 'ua' => 'UA', + 'uk' => 'UK', + 'us' => 'US', + 'vn' => 'VN' + } + + directory = "#{__dir__}/ffaker" + Dir["#{directory}/*.rb"].each do |file_name| + relative_file_path = Pathname.new(file_name).relative_path_from(directory).to_s.chomp('.rb') + ## Don't consider files in sub-directories + constant_name = relative_file_path.split('_').map do |part| + autoload_acronyms.fetch(part) { part.capitalize } + end.join + autoload constant_name, file_name + end # Random Number Generator (RNG) used with ModuleUtils#fetch, #shuffle, #rand # in order to provide deterministic repeatability. diff --git a/lib/ffaker/address.rb b/lib/ffaker/address.rb index 9cb935bf..3aaa1af5 100644 --- a/lib/ffaker/address.rb +++ b/lib/ffaker/address.rb @@ -36,10 +36,10 @@ def city_suffix def city case rand(0..3) - when 0 then format('%s %s%s', city_prefix, Name.first_name, city_suffix) - when 1 then format('%s %s', city_prefix, Name.first_name) - when 2 then format('%s%s', Name.first_name, city_suffix) - when 3 then format('%s%s', Name.last_name, city_suffix) + when 0 then "#{city_prefix} #{Name.first_name}#{city_suffix}" + when 1 then "#{city_prefix} #{Name.first_name}" + when 2 then "#{Name.first_name}#{city_suffix}" + when 3 then "#{Name.last_name}#{city_suffix}" end end diff --git a/lib/ffaker/address_fr.rb b/lib/ffaker/address_fr.rb index bb250797..94a873bf 100644 --- a/lib/ffaker/address_fr.rb +++ b/lib/ffaker/address_fr.rb @@ -14,11 +14,9 @@ module AddressFR POSTAL_CODE_FORMATS = ['#####', '97###', '2A###', '2B###'].freeze def street_address - FFaker.numerify(fetch_sample(NUM)) + - fetch_sample(MOD) + - fetch_sample(SEP) + - fetch_sample(TYPE) + ' ' + - FFaker::NameFR.name + <<~TEXT.chomp + #{FFaker.numerify(fetch_sample(NUM))}#{fetch_sample(MOD)}#{fetch_sample(SEP)}#{fetch_sample(TYPE)} #{FFaker::NameFR.name} + TEXT end def postal_code diff --git a/lib/ffaker/address_it.rb b/lib/ffaker/address_it.rb index 6c92b653..26d2e75d 100644 --- a/lib/ffaker/address_it.rb +++ b/lib/ffaker/address_it.rb @@ -11,10 +11,7 @@ module AddressIT POSTAL_CODE_FORMATS = ['#####'].freeze def street_address - FFaker.numerify(fetch_sample(NUM)) + - fetch_sample(SEP) + - fetch_sample(TYPE) + ' ' + - FFaker::NameIT.name + "#{FFaker.numerify(fetch_sample(NUM))}#{fetch_sample(SEP)}#{fetch_sample(TYPE)} #{FFaker::NameIT.name}" end def postal_code diff --git a/lib/ffaker/avatar.rb b/lib/ffaker/avatar.rb index 96023072..c91a2975 100644 --- a/lib/ffaker/avatar.rb +++ b/lib/ffaker/avatar.rb @@ -7,16 +7,24 @@ module Avatar SUPPORTED_FORMATS = %w[png jpg bmp].freeze - def image(slug = nil, size = '300x300', format = 'png', bgset = nil) + def image(*args, slug: nil, size: '300x300', format: 'png', bgset: nil) + if args.any? + warn "Positional arguments for Avatar##{__method__} are deprecated. Please use keyword arguments." + slug = args[0] + size = args[1] if args.size > 1 + format = args[2] if args.size > 2 + bgset = args[3] if args.size > 3 + end + check_passed_params(size, format, bgset) slug ||= FFaker::Lorem.words.join - "https://robohash.org/#{slug}.#{format}?size=#{size}#{'&bgset=bg' + bgset.to_s if bgset}" + "https://robohash.org/#{slug}.#{format}?size=#{size}#{"&bgset=bg#{bgset}" if bgset}" end private def check_passed_params(size, format, bgset) - invalid_size_error unless size =~ /^[0-9]+x[0-9]+$/ + invalid_size_error unless size.match?(/^[0-9]+x[0-9]+$/) invalid_format_error unless SUPPORTED_FORMATS.include?(format) invalid_background_number unless bgset.nil? || (1..2).cover?(bgset.to_i) end diff --git a/lib/ffaker/bank.rb b/lib/ffaker/bank.rb index 3f80ab06..2b6753ce 100644 --- a/lib/ffaker/bank.rb +++ b/lib/ffaker/bank.rb @@ -110,7 +110,7 @@ def formatify_iban(code) end def check_country_existence(country_code) - return if COUNTRIES.keys.include?(country_code.upcase) + return if COUNTRIES.key?(country_code.upcase) raise ArgumentError, "Unexpected country code: '#{country_code}'" end diff --git a/lib/ffaker/book.rb b/lib/ffaker/book.rb index 9059edf1..68f0427c 100644 --- a/lib/ffaker/book.rb +++ b/lib/ffaker/book.rb @@ -30,8 +30,16 @@ def description(sentence_count = 3) FFaker::Lorem.paragraph(sentence_count) end - def cover(slug = nil, size = '300x300', format = 'png', bgset = nil) - FFaker::Avatar.image(slug, size, format, bgset) + def cover(*args, slug: nil, size: '300x300', format: 'png', bgset: nil) + if args.any? + warn "Positional arguments for Book##{__method__} are deprecated. Please use keyword arguments." + slug = args[0] + size = args[1] if args.size > 1 + format = args[2] if args.size > 2 + bgset = args[3] if args.size > 3 + end + + FFaker::Avatar.image(slug: slug, size: size, format: format, bgset: bgset) end def orly_cover(name = title, book_author = author, top_text = genre) diff --git a/lib/ffaker/cheesy_lingo.rb b/lib/ffaker/cheesy_lingo.rb index e09dc2c0..cf11024a 100644 --- a/lib/ffaker/cheesy_lingo.rb +++ b/lib/ffaker/cheesy_lingo.rb @@ -23,11 +23,11 @@ def words(count = 5) end def sentence - [ + "#{[ fetch_sample(CHEESY_PHRASES).capitalize, fetch_sample(CHEESY_PHRASES), fetch_sample(CHEESY_PHRASES) - ].join + '.' + ].join}." end def paragraph(number_of_phrases = 10) diff --git a/lib/ffaker/code.rb b/lib/ffaker/code.rb index 2adfd0e4..b469a30e 100644 --- a/lib/ffaker/code.rb +++ b/lib/ffaker/code.rb @@ -17,7 +17,7 @@ def npi(legacy: false) .chars .inject(0) { |sum, digit| sum + digit.to_i } - npi_checksum = (10 - ((24 + summed_digits) % 10)).to_s.chars.last + npi_checksum = (10 - ((24 + summed_digits) % 10)).to_s[-1] base_npi + npi_checksum end diff --git a/lib/ffaker/company.rb b/lib/ffaker/company.rb index 153191a8..ae378fb8 100644 --- a/lib/ffaker/company.rb +++ b/lib/ffaker/company.rb @@ -13,7 +13,7 @@ def name case rand(0..2) when 0 then "#{Name.last_name} #{suffix}" when 1 then "#{Name.last_name}-#{Name.last_name}" - when 2 then format('%s, %s and %s', Name.last_name, Name.last_name, Name.last_name) + when 2 then "#{Name.last_name}, #{Name.last_name} and #{Name.last_name}" end end diff --git a/lib/ffaker/company_fr.rb b/lib/ffaker/company_fr.rb index 78db2ded..9ac94e7f 100644 --- a/lib/ffaker/company_fr.rb +++ b/lib/ffaker/company_fr.rb @@ -11,7 +11,7 @@ def name case rand(0..2) when 0 then "#{NameFR.last_name} #{suffix}" when 1 then "#{NameFR.last_name}-#{NameFR.last_name}" - when 2 then format('%s, %s and %s', NameFR.last_name, NameFR.last_name, NameFR.last_name) + when 2 then "#{NameFR.last_name}, #{NameFR.last_name} and #{NameFR.last_name}" end end diff --git a/lib/ffaker/crypto.rb b/lib/ffaker/crypto.rb index ab20093e..5a853029 100644 --- a/lib/ffaker/crypto.rb +++ b/lib/ffaker/crypto.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'openssl' module FFaker diff --git a/lib/ffaker/filesystem.rb b/lib/ffaker/filesystem.rb index f2a9b980..f1567249 100644 --- a/lib/ffaker/filesystem.rb +++ b/lib/ffaker/filesystem.rb @@ -17,8 +17,18 @@ def directory(directory_separator = File::SEPARATOR) Lorem.words.join(directory_separator) end - def file_name(dir = directory, name = Lorem.word.downcase, - ext = extension, directory_separator = File::SEPARATOR) + def file_name( + *args, + dir: directory, name: Lorem.word.downcase, ext: extension, directory_separator: File::SEPARATOR + ) + if args.any? + warn "Positional arguments for Filesystem##{__method__} are deprecated. Please use keyword arguments." + dir = args[0] + name = args[1] if args.size > 1 + ext = args[2] if args.size > 2 + directory_separator = args[3] if args.size > 3 + end + "#{dir}#{directory_separator}#{name}.#{ext}" end end diff --git a/lib/ffaker/identification_br.rb b/lib/ffaker/identification_br.rb index 9c76ee7a..af770743 100644 --- a/lib/ffaker/identification_br.rb +++ b/lib/ffaker/identification_br.rb @@ -17,11 +17,7 @@ def cpf def pretty_cpf cpf_numbers = cpf - format('%s.%s.%s-%s', - cpf_numbers[0..2], - cpf_numbers[3..5], - cpf_numbers[6..8], - cpf_numbers[9..10]) + "#{cpf_numbers[0..2]}.#{cpf_numbers[3..5]}.#{cpf_numbers[6..8]}-#{cpf_numbers[9..10]}" end def cnpj @@ -32,12 +28,7 @@ def cnpj def pretty_cnpj cnpj_numbers = cnpj.to_s.rjust(14, '0') - format('%s.%s.%s/%s-%s', - cnpj_numbers[0..1], - cnpj_numbers[2..4], - cnpj_numbers[5..7], - cnpj_numbers[8..11], - cnpj_numbers[12..14]) + "#{cnpj_numbers[0..1]}.#{cnpj_numbers[2..4]}.#{cnpj_numbers[5..7]}/#{cnpj_numbers[8..11]}-#{cnpj_numbers[12..14]}" end def rg diff --git a/lib/ffaker/identification_ec.rb b/lib/ffaker/identification_ec.rb index e45491f0..96b413fd 100644 --- a/lib/ffaker/identification_ec.rb +++ b/lib/ffaker/identification_ec.rb @@ -20,7 +20,7 @@ def ci_digits def last_digit(digits) mod = digits_sum(digits) % 10 - 10 - mod if mod > 0 + 10 - mod if mod.positive? end def digits_sum(digits) diff --git a/lib/ffaker/identification_es_cl.rb b/lib/ffaker/identification_es_cl.rb index de1e6489..b5481198 100644 --- a/lib/ffaker/identification_es_cl.rb +++ b/lib/ffaker/identification_es_cl.rb @@ -20,7 +20,7 @@ def rut private def dv(rut) - total = rut.to_s.rjust(8, '0').split('').zip(%w[3 2 7 6 5 4 3 2]).sum do |a, b| + total = rut.to_s.rjust(8, '0').chars.zip(%w[3 2 7 6 5 4 3 2]).sum do |a, b| a.to_i * b.to_i end (11 - (total % 11)).to_s.gsub('10', 'k').gsub('11', '0') diff --git a/lib/ffaker/identification_kr.rb b/lib/ffaker/identification_kr.rb index c6f2e868..af6ce951 100644 --- a/lib/ffaker/identification_kr.rb +++ b/lib/ffaker/identification_kr.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module FFaker - module IdentificationKr + module IdentificationKR extend ModuleUtils extend self @@ -11,8 +11,15 @@ def rrn birth = fetch_sample(::Date.new(1970, 1, 1)..::Date.new(1999, 12, 31)).strftime('%y%d%m') sex = fetch_sample([1, 2]) loc = FFaker.numerify("#{fetch_sample(Array('00'..'95'))}###") - a, b, c, d, e, f, g, h, i, j, k, l = "#{birth}#{sex}#{loc}".split('').map(&:to_i) - checksum = (11 - (((2 * a) + (3 * b) + (4 * c) + (5 * d) + (6 * e) + (7 * f) + (8 * g) + (9 * h) + (2 * i) + (3 * j) + (4 * k) + (5 * l)) % 11)) % 10 + a, b, c, d, e, f, g, h, i, j, k, l = "#{birth}#{sex}#{loc}".chars.map(&:to_i) + checksum = ( + 11 - ( + ( + (2 * a) + (3 * b) + (4 * c) + (5 * d) + (6 * e) + (7 * f) + (8 * g) + (9 * h) + + (2 * i) + (3 * j) + (4 * k) + (5 * l) + ) % 11 + ) + ) % 10 "#{birth}-#{sex}#{loc}#{checksum}" end end diff --git a/lib/ffaker/identification_pl.rb b/lib/ffaker/identification_pl.rb index 61b5e54c..975ff8f3 100644 --- a/lib/ffaker/identification_pl.rb +++ b/lib/ffaker/identification_pl.rb @@ -63,14 +63,14 @@ def pesel_century_differentiator(year) end def pesel_checksum(date, serial_number) - pesel_digits = "#{date}#{serial_number}".split('').map(&:to_i) + pesel_digits = "#{date}#{serial_number}".chars.map(&:to_i) a, b, c, d, e, f, g, h, i, j = pesel_digits ((a * 9) + (b * 7) + (c * 3) + d + (e * 9) + (f * 7) + (g * 3) + h + (i * 9) + (j * 7)) % 10 end def identity_card_checksum(letter_part, number_part) a, b, c = letter_part.codepoints.map { |codepoints| codepoints - 55 } - d, e, f, g, h = number_part.split('').map(&:to_i) + d, e, f, g, h = number_part.chars.map(&:to_i) ((a * 7) + (b * 3) + c + (7 * d) + (3 * e) + f + (7 * g) + (3 * h)) % 10 end end diff --git a/lib/ffaker/image.rb b/lib/ffaker/image.rb index 4a49dcfb..bcf44f89 100644 --- a/lib/ffaker/image.rb +++ b/lib/ffaker/image.rb @@ -11,7 +11,18 @@ module Image SUPPORTED_FORMATS = %w[png jpg jpeg gif].freeze - def url(size = '300x300', format = 'png', bg_color = :random, text_color = :random, text = nil) + ## `*args` for old format support, it will be removed with deprecation + # rubocop:disable Metrics/ParameterLists + def url(*args, size: '300x300', format: 'png', bg_color: :random, text_color: :random, text: nil) + if args.any? + warn "Positional arguments for Image##{__method__} are deprecated. Please use keyword arguments." + size = args[0] + format = args[1] if args.size > 1 + bg_color = args[2] if args.size > 2 + text_color = args[3] if args.size > 3 + text = args[4] if args.size > 4 + end + check_size!(size) check_format!(format) @@ -21,20 +32,33 @@ def url(size = '300x300', format = 'png', bg_color = :random, text_color = :rand "https://dummyimage.com/#{size}/#{bg_color}/#{text_color}.#{format}?text=#{text}" end + # rubocop:enable Metrics/ParameterLists + + ## `*args` for old format support, it will be removed with deprecation + # rubocop:disable Metrics/ParameterLists + def file(*args, size: '300x300', format: 'png', bg_color: :random, text_color: :random, text: nil) + if args.any? + warn "Positional arguments for Image##{__method__} are deprecated. Please use keyword arguments." + size = args[0] + format = args[1] if args.size > 1 + bg_color = args[2] if args.size > 2 + text_color = args[3] if args.size > 3 + text = args[4] if args.size > 4 + end - def file(size = '300x300', format = 'png', bg_color = :random, text_color = :random, text = nil) - uri = URI.parse(url(size, format, bg_color, text_color, text)) + uri = URI.parse(url(size: size, format: format, bg_color: bg_color, text_color: text_color, text: text)) file = Tempfile.new('ffaker_image') file.binmode file << uri.open.read file.close File.new(file.path) end + # rubocop:enable Metrics/ParameterLists private def check_size!(size) - return true if size =~ /\A\d+x\d+\z/ + return true if size.match?(/\A\d+x\d+\z/) raise ArgumentError, 'Size should be specified in format 300x300' end diff --git a/lib/ffaker/lorem_ja.rb b/lib/ffaker/lorem_ja.rb index acd74e71..0bd4d8dd 100644 --- a/lib/ffaker/lorem_ja.rb +++ b/lib/ffaker/lorem_ja.rb @@ -30,7 +30,7 @@ def sentences(count = 3) end def paragraph(sentence_count = 3) - sentences(sentence_count).join('、') + '、' + fetch_sample(SENTENCES_END) + '。' + "#{sentences(sentence_count).join('、')}、#{fetch_sample(SENTENCES_END)}。" end def paragraphs(count = 3) diff --git a/lib/ffaker/lorem_ru.rb b/lib/ffaker/lorem_ru.rb index 339344e5..195691b2 100644 --- a/lib/ffaker/lorem_ru.rb +++ b/lib/ffaker/lorem_ru.rb @@ -50,7 +50,7 @@ def capitalize_russian(string) if CAPITAL_CHARS.include?(string[0]) string else - CAPITAL_CHARS[CHARS.index(string[0])] + string[1..-1] + CAPITAL_CHARS[CHARS.index(string[0])] + string[1..] end end end diff --git a/lib/ffaker/movie.rb b/lib/ffaker/movie.rb index 43018cd1..b6436d9b 100644 --- a/lib/ffaker/movie.rb +++ b/lib/ffaker/movie.rb @@ -37,7 +37,7 @@ def title_with_suffix end def maybe_adj_or_adv - fetch_sample(ADJ_AND_ADV) + ' ' if rand(0..1) == 1 + "#{fetch_sample(ADJ_AND_ADV)} " if rand(0..1) == 1 end def simple_title diff --git a/lib/ffaker/name_fr.rb b/lib/ffaker/name_fr.rb index cbef6ce1..3bec47de 100644 --- a/lib/ffaker/name_fr.rb +++ b/lib/ffaker/name_fr.rb @@ -25,9 +25,10 @@ def first_name def name case rand(0..9) - when 7 then "#{first_name} #{prefix} #{last_name}" - when 5 then "#{first_name} #{prefix} #{last_name}" - else "#{first_name} #{last_name}" + when 5, 7 + "#{first_name} #{prefix} #{last_name}" + else + "#{first_name} #{last_name}" end end end diff --git a/lib/ffaker/phone_number_de.rb b/lib/ffaker/phone_number_de.rb index d09dd5ed..868e0f25 100644 --- a/lib/ffaker/phone_number_de.rb +++ b/lib/ffaker/phone_number_de.rb @@ -7,15 +7,15 @@ module PhoneNumberDE # Mobile prefixes are in the 015x, 016x, 017x ranges def mobile_prefix(leading_zero = true) - mobile_prefix = '1' + rand(5..7).to_s + rand(0..9).to_s - mobile_prefix = '0' + mobile_prefix if leading_zero + mobile_prefix = "1#{rand(5..7)}#{rand(0..9)}" + mobile_prefix = "0#{mobile_prefix}" if leading_zero mobile_prefix end # Region prefixes are in range 02..09 with length of 3 to 5 digits def region_prefix(leading_zero = true) region_prefix = rand(2..9).to_s + FFaker.numerify('#' * rand(1..3)).to_s - region_prefix = '0' + region_prefix if leading_zero + region_prefix = "0#{region_prefix}" if leading_zero region_prefix end diff --git a/lib/ffaker/skill.rb b/lib/ffaker/skill.rb index ead22aa3..ef2ded03 100644 --- a/lib/ffaker/skill.rb +++ b/lib/ffaker/skill.rb @@ -14,7 +14,7 @@ def tech_skills(num = 3) end def specialty - format('%s %s', fetch_sample(SPECIALTY_START), fetch_sample(SPECIALTY_END)) + "#{fetch_sample(SPECIALTY_START)} #{fetch_sample(SPECIALTY_END)}" end def specialties(num = 3) diff --git a/lib/ffaker/ssn.rb b/lib/ffaker/ssn.rb index deac9352..b791d046 100644 --- a/lib/ffaker/ssn.rb +++ b/lib/ffaker/ssn.rb @@ -15,8 +15,10 @@ def ssn second_group = rand(1..99) third_group = rand(1..9999) - group_numbers = [first_group, second_group, third_group] - format('%.3d-%.2d-%.4d', *group_numbers) + format( + '%.3d-%.2d-%.4d', + first_group: first_group, second_group: second_group, third_group: third_group + ) end end end diff --git a/lib/ffaker/string.rb b/lib/ffaker/string.rb index 4165d3c1..7c062c86 100644 --- a/lib/ffaker/string.rb +++ b/lib/ffaker/string.rb @@ -21,7 +21,7 @@ def from_regexp(exp) @last_token = nil # Drop surrounding /'s and split into characters - tokens = exp.inspect[1...-1].split('') + tokens = exp.inspect[1...-1].chars result << process_token(tokens) until tokens.empty? result diff --git a/lib/ffaker/utils/module_utils.rb b/lib/ffaker/utils/module_utils.rb index ad893361..379ead5d 100644 --- a/lib/ffaker/utils/module_utils.rb +++ b/lib/ffaker/utils/module_utils.rb @@ -13,7 +13,7 @@ def k(arg) end def const_missing(const_name) - if const_name =~ /[a-z]/ # Not a constant, probably a class/module name. + if const_name.match?(/[a-z]/) # Not a constant, probably a class/module name. super const_name else mod_name = ancestors.first.to_s.split('::').last @@ -40,7 +40,7 @@ def unique(max_retries = 10_000) def luhn_check(number) multiplications = [] - number.split('').each_with_index do |digit, i| + number.chars.each_with_index do |digit, i| multiplications << i.even? ? digit.to_i * 2 : digit.to_i end diff --git a/lib/ffaker/utils/unique_utils.rb b/lib/ffaker/utils/unique_utils.rb index 61b6c07a..8c5008f8 100644 --- a/lib/ffaker/utils/unique_utils.rb +++ b/lib/ffaker/utils/unique_utils.rb @@ -16,7 +16,7 @@ def instances end def clear - instances.values.each(&:clear) + instances.each_value(&:clear) instances.clear end end @@ -45,6 +45,10 @@ def method_missing(name, *args, **kwargs) raise RetryLimitExceeded, "Retry limit exceeded for #{name}" end + def respond_to_missing?(name, *args) + @generator.respond_to?(name, *args) || super + end + def previous_results @previous_results ||= Hash.new { |hash, key| hash[key] = Set.new } end diff --git a/scripts/reference.rb b/scripts/reference.rb index 6b3e181d..f6582923 100644 --- a/scripts/reference.rb +++ b/scripts/reference.rb @@ -11,8 +11,8 @@ warning: '❗' }.freeze -UTILS_MODULES = %i[ArrayUtils ModuleUtils RandomUtils Random] -UTILS_METHODS = %i[k underscore fetch_sample rand shuffle unique luhn_check] +UTILS_MODULES = %i[ArrayUtils ModuleUtils RandomUtils Random].freeze +UTILS_METHODS = %i[k underscore fetch_sample rand shuffle unique luhn_check].freeze # Get a list of sections def faker_modules @@ -36,12 +36,14 @@ def faker_methods(mod) # Catch deprecation warnings. # This `#warn` overrides Kernel#warn def warn(msg) - $warnings << msg if $warnings + return unless Kernel.instance_variable_get(:@ffaker_warnings) + + Kernel.instance_variable_set(:@ffaker_warnings, Kernel.instance_variable_get(:@ffaker_warnings) << msg) end def catch_warnings - $warnings = [] - [yield, $warnings] + Kernel.instance_variable_set(:@ffaker_warnings, []) + [yield, Kernel.instance_variable_get(:@ffaker_warnings)] end def escape(str) @@ -70,11 +72,7 @@ def escape(str) examples, warnings = catch_warnings do Array.new(3) { mod.unique.send meth } end - right = if warnings.any? - "#{ICONS[:warning]} *#{warnings.first}*" - else - (escape examples.join(', ')).to_s - end + right = warnings.any? ? "#{ICONS[:warning]} *#{warnings.first}*" : (escape examples.join(', ')).to_s rescue StandardError => e right = "#{ICONS[:error]} #{e.class}: #{e.message}" end diff --git a/test/helper.rb b/test/helper.rb index 65db25c2..84e501d5 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -14,7 +14,7 @@ def self.included(base) # the internal Random Number Generator state and compared the results of # each execution to make sure they are the same. def assert_deterministic(options = {}, &block) - raise ArgumentError, 'Must pass a block' unless block_given? + raise ArgumentError, 'Must pass a block' unless block options = { message: 'Results are not repeatable' }.merge(options) diff --git a/test/test_avatar.rb b/test/test_avatar.rb index 7b6f14cc..d754de44 100644 --- a/test/test_avatar.rb +++ b/test/test_avatar.rb @@ -18,40 +18,63 @@ def test_avatar @tester.image) end + def test_image_output_with_keyword_arguments + output = capture_output do + @tester.image(format: 'jpg') + end + + assert_equal ['', ''], output + end + + def test_image_with_slug_as_positional_argument + assert_equal("#{ROBOHASH}/foobar.png?size=300x300", @tester.image('foobar')) + end + + def test_image_output_with_positional_arguments + output = capture_output do + @tester.image('foobar') + end + + assert_equal( + ['', "Positional arguments for Avatar#image are deprecated. Please use keyword arguments.\n"], + output + ) + end + def test_avatar_with_param - assert_equal("#{ROBOHASH}/faker.png?size=300x300", @tester.image('faker')) + assert_equal("#{ROBOHASH}/faker.png?size=300x300", @tester.image(slug: 'faker')) end def test_avatar_with_correct_size assert_equal("#{ROBOHASH}/faker.png?size=150x320", - @tester.image('faker', '150x320')) + @tester.image(slug: 'faker', size: '150x320')) end def test_avatar_with_incorrect_size assert_raise ArgumentError do - @tester.image(nil, '150x320z') + @tester.image(size: '150x320z') end end def test_avatar_with_supported_format assert_equal("#{ROBOHASH}/faker.jpg?size=300x300", - @tester.image('faker', '300x300', 'jpg')) + @tester.image(slug: 'faker', size: '300x300', format: 'jpg')) end def test_avatar_with_incorrect_format assert_raise ArgumentError do - @tester.image(nil, '300x300', 'wrong_format') + @tester.image(size: '300x300', format: 'wrong_format') end end def test_avatar_with_correct_background assert_equal("#{ROBOHASH}/faker.png?size=300x300&bgset=bg1", - @tester.image('faker', '300x300', 'png', '1')) + @tester.image(slug: 'faker', size: '300x300', format: 'png', bgset: '1')) end def test_avatar_with_incorrect_background assert_raise ArgumentError do - @tester.image('faker', '300x300', 'png', 'not_a_number') + @tester.image(slug: 'faker', size: '300x300', format: 'png', bgset: 'not_a_number') end end end diff --git a/test/test_book.rb b/test/test_book.rb index a8218c4e..44decb7d 100644 --- a/test/test_book.rb +++ b/test/test_book.rb @@ -39,6 +39,35 @@ def test_cover @tester.cover) end + def test_cover_with_format + assert_match(%r{\Ahttps://robohash\.org/.+\.jpg\?size=300x300\z}, + @tester.cover(format: 'jpg')) + end + + def test_cover_output_with_keyword_arguments + output = capture_output do + @tester.cover(format: 'jpg') + end + + assert_equal ['', ''], output + end + + def test_cover_with_slug_as_positional_argument + assert_match(%r{\Ahttps://robohash\.org/foobar\.png\?size=300x300\z}, + @tester.cover('foobar')) + end + + def test_cover_output_with_positional_arguments + output = capture_output do + @tester.cover('foobar') + end + + assert_equal( + ['', "Positional arguments for Book#cover are deprecated. Please use keyword arguments.\n"], + output + ) + end + def test_orly_cover assert_match(%r{\Ahttps://orly-appstore\.herokuapp\.com/generate}, @tester.orly_cover) diff --git a/test/test_filesystem.rb b/test/test_filesystem.rb index 88489e15..c2dc9e6b 100644 --- a/test/test_filesystem.rb +++ b/test/test_filesystem.rb @@ -30,7 +30,33 @@ def test_directory end def test_file_name - assert_match %r{\A(?:[a-z_-]+[\\/])+[a-z_-]+\.\w{2,4}\z}, - @tester.file_name + assert_match %r{\A(?:[a-z_-]+[\\/])+[a-z_-]+\.\w{2,4}\z}, @tester.file_name + end + + def test_file_name_with_extension + assert_match %r{\A(?:[a-z_-]+[\\/])+[a-z_-]+\.rb\z}, @tester.file_name(ext: 'rb') + end + + def test_file_name_output_with_keyword_arguments + output = capture_output do + @tester.file_name(ext: 'rb') + end + + assert_equal ['', ''], output + end + + def test_file_name_with_directory_as_positional_argument + assert_match %r{\Asome_directory/[a-z_-]+\.\w{2,4}\z}, @tester.file_name('some_directory') + end + + def test_file_name_output_with_positional_arguments + output = capture_output do + @tester.file_name('some_directory') + end + + assert_equal( + ['', "Positional arguments for Filesystem#file_name are deprecated. Please use keyword arguments.\n"], + output + ) end end diff --git a/test/test_healthcare_ru.rb b/test/test_healthcare_ru.rb index a133d5c1..66e17813 100644 --- a/test/test_healthcare_ru.rb +++ b/test/test_healthcare_ru.rb @@ -5,7 +5,7 @@ class TestHealthcareRU < Test::Unit::TestCase include DeterministicHelper - SPECIALIZATION_REGEX = /\A[А-Яа-я\ ()\-]+\z/ + SPECIALIZATION_REGEX = /\A[А-Яа-я\ ()-]+\z/ assert_methods_are_deterministic(FFaker::HealthcareRU, :doctor_specialization) diff --git a/test/test_identification.rb b/test/test_identification.rb index 764df27a..0f75a23a 100644 --- a/test/test_identification.rb +++ b/test/test_identification.rb @@ -15,7 +15,6 @@ def setup end def test_drivers_license - ###-###-##-###-# drivers_license_regex = /\A[A-Z]\d{3}-\d{3}-\d{2}-\d{3}-\d{1}\z/ assert_match(drivers_license_regex, @tester.drivers_license) end @@ -29,7 +28,18 @@ def test_gender end def test_ethnicity - ethnicity_regex = %r{\A(African American|Asian/Pacific Islander|Caucasian|Hispanic|Native American|Multiracial|Other|Prefer not to respond)\z} + ethnicity_regex = %r{ + \A( + African\ American| + Asian/Pacific\ Islander| + Caucasian| + Hispanic| + Native\ American| + Multiracial| + Other| + Prefer\ not\ to\ respond + )\z + }x assert_match(ethnicity_regex, @tester.ethnicity) end end diff --git a/test/test_identification_ec.rb b/test/test_identification_ec.rb index 6c2f5a53..49f917a0 100644 --- a/test/test_identification_ec.rb +++ b/test/test_identification_ec.rb @@ -27,7 +27,7 @@ def test_last_digit end end mod = sum % 10 - mod = 10 - mod if mod > 0 + mod = 10 - mod if mod.positive? assert(ci[9].to_i == mod) end end diff --git a/test/test_identification_es_mx.rb b/test/test_identification_es_mx.rb index 7b9e8eb6..aec7542d 100644 --- a/test/test_identification_es_mx.rb +++ b/test/test_identification_es_mx.rb @@ -22,7 +22,9 @@ def test_rfc_persona_fisica def test_curp or_curp_states_abbr = FFaker::IdentificationMX::ESTADOS_CURP.join('|') - re = /\A[a-z][aeioux][a-z]{2}\d{2}[0-1]\d[0-3]\d[hm](?:#{or_curp_states_abbr})[bcdfghjklmñpqrstvwxyz]{3}[0-9a-z]\d\z/ui + re = / + \A[a-z][aeioux][a-z]{2}\d{2}[0-1]\d[0-3]\d[hm](?:#{or_curp_states_abbr})[bcdfghjklmñpqrstvwxyz]{3}[0-9a-z]\d\z + /uix assert_match(re, FFaker::IdentificationMX.curp) end end diff --git a/test/test_identification_it.rb b/test/test_identification_it.rb index b5c3966d..4d1cfd3a 100644 --- a/test/test_identification_it.rb +++ b/test/test_identification_it.rb @@ -28,7 +28,18 @@ def test_gender end def test_ethnicity - ethnicity_regex = %r{\A(Afroamericano|Asiatico/isolano del Pacifico|Caucasico|Ispanico|Nativo americano|Multirazziale|Altro|Preferisco non rispondere)\z} + ethnicity_regex = %r{ + \A( + Afroamericano| + Asiatico/isolano\ del\ Pacifico| + Caucasico| + Ispanico| + Nativo\ americano| + Multirazziale| + Altro| + Preferisco\ non\ rispondere + )\z + }x assert_match(ethnicity_regex, @tester.ethnicity) end end diff --git a/test/test_identification_kr.rb b/test/test_identification_kr.rb index 5f28060e..7768e04f 100644 --- a/test/test_identification_kr.rb +++ b/test/test_identification_kr.rb @@ -2,13 +2,13 @@ require_relative 'helper' -class TestFakerIdentificationKr < Test::Unit::TestCase +class TestFakerIdentificationKR < Test::Unit::TestCase include DeterministicHelper - assert_methods_are_deterministic(FFaker::IdentificationKr, :rrn) + assert_methods_are_deterministic(FFaker::IdentificationKR, :rrn) def setup - @tester = FFaker::IdentificationKr + @tester = FFaker::IdentificationKR end def test_rrn diff --git a/test/test_image.rb b/test/test_image.rb index b35cc227..a664d623 100644 --- a/test/test_image.rb +++ b/test/test_image.rb @@ -18,34 +18,80 @@ def test_url @tester.url) end + def test_url_output_with_keyword_arguments + output = capture_output do + @tester.url(format: 'jpg') + end + + assert_equal ['', ''], output + end + + def test_url_with_size_as_positional_argument + assert_match(%r(#{Regexp.quote(PLACEHOLDER)}150x320/[0-9a-f]{6}/[0-9a-f]{6}\.png\?text=), @tester.url('150x320')) + end + + def test_url_output_with_positional_arguments + output = capture_output do + @tester.url('150x320') + end + + assert_equal( + ['', "Positional arguments for Image#url are deprecated. Please use keyword arguments.\n"], + output + ) + end + def test_image_url_with_param assert_equal("#{PLACEHOLDER}300x300//.png?text=", - @tester.url('300x300', 'png', nil, nil)) + @tester.url(size: '300x300', format: 'png', bg_color: nil, text_color: nil)) end def test_image_url_with_correct_size assert_match(%r(#{Regexp.quote(PLACEHOLDER)}150x320/[0-9a-f]{6}/[0-9a-f]{6}\.png\?text=), - @tester.url('150x320')) + @tester.url(size: '150x320')) end def test_image_url_with_incorrect_size assert_raise ArgumentError do - @tester.url('150x320z') + @tester.url(size: '150x320z') end end def test_image_url_with_supported_format assert_match(%r(#{Regexp.quote(PLACEHOLDER)}300x300/[0-9a-f]{6}/[0-9a-f]{6}\.jpg\?text=), - @tester.url('300x300', 'jpg')) + @tester.url(size: '300x300', format: 'jpg')) end def test_image_url_with_incorrect_format assert_raise ArgumentError do - @tester.url('300x300', 'wrong_format') + @tester.url(size: '300x300', format: 'wrong_format') end end def test_image_file assert_equal(@tester.file.class.name, 'File') end + + def test_file_output_with_keyword_arguments + output = capture_output do + @tester.file(format: 'jpg') + end + + assert_equal ['', ''], output + end + + def test_file_with_size_as_positional_argument + assert_equal(@tester.file('150x320').class.name, 'File') + end + + def test_file_output_with_positional_arguments + output = capture_output do + @tester.file('150x320') + end + + assert_equal( + ['', "Positional arguments for Image#file are deprecated. Please use keyword arguments.\n"], + output + ) + end end diff --git a/test/test_internet.rb b/test/test_internet.rb index 17bca8ea..9218b729 100644 --- a/test/test_internet.rb +++ b/test/test_internet.rb @@ -29,7 +29,9 @@ def test_free_email end def test_disposable_email - assert @tester.disposable_email.match(/.+@(mailinator\.com|suremail\.info|spamherelots\.com|binkmail\.com|safetymail\.info)/) + assert @tester.disposable_email.match( + /.+@(mailinator\.com|suremail\.info|spamherelots\.com|binkmail\.com|safetymail\.info)/ + ) end def test_safe_email diff --git a/test/test_internet_se.rb b/test/test_internet_se.rb index e1bacbac..9c0754d7 100644 --- a/test/test_internet_se.rb +++ b/test/test_internet_se.rb @@ -25,7 +25,9 @@ def test_email_frozen end def test_disposable_email - assert @tester.disposable_email.match(/.+@(mailinator\.com|suremail\.info|spamherelots\.com|binkmail\.com|safetymail\.info)/) + assert @tester.disposable_email.match( + /.+@(mailinator\.com|suremail\.info|spamherelots\.com|binkmail\.com|safetymail\.info)/ + ) end def test_free_email diff --git a/test/test_lorem_br.rb b/test/test_lorem_br.rb index f1611e86..776ce8c2 100644 --- a/test/test_lorem_br.rb +++ b/test/test_lorem_br.rb @@ -12,7 +12,7 @@ class TestLoremBR < Test::Unit::TestCase CHARACTERS = /\A[A-zÀ-ü0-9]+\z/i WORD = /\A[A-zÀ-ü-]+\z/i - WORDS = /[ A-zÀ-ü-.]+/i + WORDS = /[ A-zÀ-ü\-.]+/i def test_paragraph assert_match(WORDS, FFaker::LoremBR.paragraph) diff --git a/test/test_lorem_ru.rb b/test/test_lorem_ru.rb index 091ea343..89b2059b 100644 --- a/test/test_lorem_ru.rb +++ b/test/test_lorem_ru.rb @@ -7,7 +7,7 @@ class TestLoremRU < Test::Unit::TestCase SENTENCE_MATCHER = /\A[а-яА-ЯёЁ\-\s.!?,]+\z/ WORDS_MATCHER = /\A[А-Яа-яёЁ\-\s]+\z/ - WORD_MATCHER = /\A[А-Яа-яёЁ\-]+\z/ + WORD_MATCHER = /\A[А-Яа-яёЁ-]+\z/ assert_methods_are_deterministic( FFaker::LoremRU, diff --git a/test/test_lorem_ua.rb b/test/test_lorem_ua.rb index 2bef5dd8..053405e7 100644 --- a/test/test_lorem_ua.rb +++ b/test/test_lorem_ua.rb @@ -7,7 +7,7 @@ class TestLoremUA < Test::Unit::TestCase SENTENCE_MATCHER = /\A[а-яА-ЯіїєґІЇЄҐ’\-\s.!?,]+\z/ WORDS_MATCHER = /\A[а-яА-ЯіїєґІЇЄҐ’\-\s]+\z/ - WORD_MATCHER = /\A[а-яА-ЯіїєґІЇЄҐ’\-]+\z/ + WORD_MATCHER = /\A[а-яА-ЯіїєґІЇЄҐ’-]+\z/ assert_methods_are_deterministic( FFaker::LoremUA, diff --git a/test/test_name_ua.rb b/test/test_name_ua.rb index d045095e..99717d6c 100644 --- a/test/test_name_ua.rb +++ b/test/test_name_ua.rb @@ -15,7 +15,7 @@ class TestNameUA < Test::Unit::TestCase def setup @tester = FFaker::NameUA - @single_word_name_regexp = /\A[а-яА-ЯіїєґІЇЄҐ’\-]+\z/ + @single_word_name_regexp = /\A[а-яА-ЯіїєґІЇЄҐ’-]+\z/ @multiple_words_name_regexp = /\A[а-яА-ЯіїєґІЇЄҐ’\-\s]+\z/ end diff --git a/test/test_phone_number_nl.rb b/test/test_phone_number_nl.rb index 2023cd1e..f106c3ea 100644 --- a/test/test_phone_number_nl.rb +++ b/test/test_phone_number_nl.rb @@ -24,7 +24,7 @@ def test_phone_prefix def test_phone_number 10.times do - assert_match(/^0([\s\-]*\d){9}$/, @tester.phone_number) + assert_match(/^0([\s-]*\d){9}$/, @tester.phone_number) end end diff --git a/test/test_units.rb b/test/test_units.rb index ec507aa8..4a49a976 100644 --- a/test/test_units.rb +++ b/test/test_units.rb @@ -13,25 +13,25 @@ class TestUnits < Test::Unit::TestCase def setup @tester = FFaker::Unit - @time_units = @tester::TIME_UNITS.map { |u| OpenStruct.new u } - @temperature_units = @tester::TEMPERATURE_UNITS.map { |u| OpenStruct.new u } + @time_units = @tester::TIME_UNITS + @temperature_units = @tester::TEMPERATURE_UNITS end def test_time_name - assert_include @time_units.map(&:name), @tester.time_name + assert_include @time_units.map { |unit| unit[:name] }, @tester.time_name end def test_time_abbr - assert_include @time_units.map(&:abbreviation), @tester.time_abbr + assert_include @time_units.map { |unit| unit[:abbreviation] }, @tester.time_abbr end def test_temperature_name - assert_include @temperature_units.map(&:name), @tester.temperature_name + assert_include @temperature_units.map { |unit| unit[:name] }, @tester.temperature_name end def test_temperature_abbr assert_include \ - @temperature_units.map(&:abbreviation), + @temperature_units.map { |unit| unit[:abbreviation] }, @tester.temperature_abbr end end diff --git a/test/test_units_english.rb b/test/test_units_english.rb index 84b2757a..f25c1c8b 100644 --- a/test/test_units_english.rb +++ b/test/test_units_english.rb @@ -14,50 +14,50 @@ class TestUnitsEnglish < Test::Unit::TestCase def setup @tester = FFaker::UnitEnglish - @length_units = @tester::LENGTH_UNITS.map { |u| OpenStruct.new u } - @mass_units = @tester::MASS_UNITS.map { |u| OpenStruct.new u } - @liquid_units = @tester::LIQUID_UNITS.map { |u| OpenStruct.new u } - @volume_units = @tester::VOLUME_UNITS.map { |u| OpenStruct.new u } - @area_units = @tester::AREA_UNITS.map { |u| OpenStruct.new u } + @length_units = @tester::LENGTH_UNITS + @mass_units = @tester::MASS_UNITS + @liquid_units = @tester::LIQUID_UNITS + @volume_units = @tester::VOLUME_UNITS + @area_units = @tester::AREA_UNITS end def test_length_name - assert_include @length_units.map(&:name), @tester.length_name + assert_include @length_units.map { |unit| unit[:name] }, @tester.length_name end def test_length_abbrev - assert_include @length_units.map(&:abbreviation), @tester.length_abbr + assert_include @length_units.map { |unit| unit[:abbreviation] }, @tester.length_abbr end def test_mass_name - assert_include @mass_units.map(&:name), @tester.mass_name + assert_include @mass_units.map { |unit| unit[:name] }, @tester.mass_name end def test_mass_abbr - assert_include @mass_units.map(&:abbreviation), @tester.mass_abbr + assert_include @mass_units.map { |unit| unit[:abbreviation] }, @tester.mass_abbr end def test_liquid_name - assert_include @liquid_units.map(&:name), @tester.liquid_name + assert_include @liquid_units.map { |unit| unit[:name] }, @tester.liquid_name end def test_liquid_abbr - assert_include @liquid_units.map(&:abbreviation), @tester.liquid_abbr + assert_include @liquid_units.map { |unit| unit[:abbreviation] }, @tester.liquid_abbr end def test_volume_name - assert_include @volume_units.map(&:name), @tester.volume_name + assert_include @volume_units.map { |unit| unit[:name] }, @tester.volume_name end def test_volume_abbr - assert_include @volume_units.map(&:abbreviation), @tester.volume_abbr + assert_include @volume_units.map { |unit| unit[:abbreviation] }, @tester.volume_abbr end def test_area_name - assert_include @area_units.map(&:name), @tester.area_name + assert_include @area_units.map { |unit| unit[:name] }, @tester.area_name end def test_area_abbr - assert_include @area_units.map(&:abbreviation), @tester.area_abbr + assert_include @area_units.map { |unit| unit[:abbreviation] }, @tester.area_abbr end end diff --git a/test/test_units_metric.rb b/test/test_units_metric.rb index 2eaf3c69..cf7f7f58 100644 --- a/test/test_units_metric.rb +++ b/test/test_units_metric.rb @@ -14,50 +14,50 @@ class TestUnitsMetric < Test::Unit::TestCase def setup @tester = FFaker::UnitMetric - @length_units = @tester::LENGTH_UNITS.map { |u| OpenStruct.new u } - @mass_units = @tester::MASS_UNITS.map { |u| OpenStruct.new u } - @liquid_units = @tester::LIQUID_UNITS.map { |u| OpenStruct.new u } - @volume_units = @tester::VOLUME_UNITS.map { |u| OpenStruct.new u } - @area_units = @tester::AREA_UNITS.map { |u| OpenStruct.new u } + @length_units = @tester::LENGTH_UNITS + @mass_units = @tester::MASS_UNITS + @liquid_units = @tester::LIQUID_UNITS + @volume_units = @tester::VOLUME_UNITS + @area_units = @tester::AREA_UNITS end def test_length_name - assert_include @length_units.map(&:name), @tester.length_name + assert_include @length_units.map { |unit| unit[:name] }, @tester.length_name end def test_length_abbrev - assert_include @length_units.map(&:abbreviation), @tester.length_abbr + assert_include @length_units.map { |unit| unit[:abbreviation] }, @tester.length_abbr end def test_mass_name - assert_include @mass_units.map(&:name), @tester.mass_name + assert_include @mass_units.map { |unit| unit[:name] }, @tester.mass_name end def test_mass_abbr - assert_include @mass_units.map(&:abbreviation), @tester.mass_abbr + assert_include @mass_units.map { |unit| unit[:abbreviation] }, @tester.mass_abbr end def test_liquid_name - assert_include @liquid_units.map(&:name), @tester.liquid_name + assert_include @liquid_units.map { |unit| unit[:name] }, @tester.liquid_name end def test_liquid_abbr - assert_include @liquid_units.map(&:abbreviation), @tester.liquid_abbr + assert_include @liquid_units.map { |unit| unit[:abbreviation] }, @tester.liquid_abbr end def test_volume_name - assert_include @volume_units.map(&:name), @tester.volume_name + assert_include @volume_units.map { |unit| unit[:name] }, @tester.volume_name end def test_volume_abbr - assert_include @volume_units.map(&:abbreviation), @tester.volume_abbr + assert_include @volume_units.map { |unit| unit[:abbreviation] }, @tester.volume_abbr end def test_area_name - assert_include @area_units.map(&:name), @tester.area_name + assert_include @area_units.map { |unit| unit[:name] }, @tester.area_name end def test_area_abbr - assert_include @area_units.map(&:abbreviation), @tester.area_abbr + assert_include @area_units.map { |unit| unit[:abbreviation] }, @tester.area_abbr end end