Skip to content

Commit

Permalink
Refactor TestFakerDeprecation
Browse files Browse the repository at this point in the history
This is a proposed change to the `TestFakerDeprecation` class that makes
it easier for developers to add new tests for deprecations.  The
introduced `DEPRECATION_MAPPINGS` hash allows for devs to add new tests
in one location.
  • Loading branch information
kirkkwang committed Feb 26, 2024
1 parent 1b3119f commit a7129ce
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions test/test_faker_deprecator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
require_relative 'test_helper'

class TestFakerDeprecation < Test::Unit::TestCase
##
# A hash that maps deprecated methods to their new counterparts.
#
# @return [Hash{Array<String, String> => Array<String, String>}]
#
# @example Add the following into the hash
# ['Faker::OldKlass', 'old_method'] => ['Faker::NewKlass', 'new_method']
DEPRECATION_MAPPINGS = {
['Faker::IDNumber', 'valid'] => ['Faker::IdNumber', 'valid'],
['Faker::JapaneseMedia::FmaBrotherhood', 'character'] => ['Faker::JapaneseMedia::FullmetalAlchemistBrotherhood', 'character']
}.freeze

def test_using_a_deprecated_generator_returns_a_warning_message
actual_stdout, actual_stderr = capture_output do
Faker::IDNumber.valid
Faker::JapaneseMedia::FmaBrotherhood.character
DEPRECATION_MAPPINGS.each_key { |klass, method| Object.const_get(klass).send(method) }
end

assert_includes(actual_stdout, 'DEPRECATION WARNING: Faker::IDNumber is deprecated. Use Faker::IdNumber instead')
assert_includes(actual_stdout, 'DEPRECATION WARNING: Faker::JapaneseMedia::FmaBrotherhood is deprecated. Use Faker::JapaneseMedia::FullmetalAlchemistBrotherhood instead')
DEPRECATION_MAPPINGS.each do |(deprecated_klass, _deprecated_method), (new_klass, _new_method)|
assert_includes(actual_stdout, "DEPRECATION WARNING: #{deprecated_klass} is deprecated. Use #{new_klass} instead")
end
assert_empty(actual_stderr)
end

def test_using_a_non_deprecated_generator_does_not_return_a_warning_message
actual_stdout, actual_stderr = capture_output do
Faker::IdNumber.valid
Faker::JapaneseMedia::FullmetalAlchemistBrotherhood.character
DEPRECATION_MAPPINGS.each_value { |klass, method| Object.const_get(klass).send(method) }
end

assert_empty(actual_stdout)
Expand Down

0 comments on commit a7129ce

Please sign in to comment.