Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added skip_warning ability to deprecators for tests #2956

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions lib/faker/locations/australia.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

module Faker
extend Deprecator

class Locations
class Australia < Base
class << self
Expand Down Expand Up @@ -48,5 +46,7 @@ def state
end
end
end

include Deprecator
deprecate_generator('Australia', Locations::Australia)
end
27 changes: 26 additions & 1 deletion lib/helpers/deprecator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
# rubocop:disable Style/ClassVars
module Faker
module Deprecator
@@skip_warning = false
keshavbiswa marked this conversation as resolved.
Show resolved Hide resolved

def self.included(base)
extension = Module.new do
def const_missing(missing_const_name)
if class_variable_defined?(:@@_deprecated_constants) && (replacement = class_variable_get(:@@_deprecated_constants)[missing_const_name.to_s])
$stdout.puts("DEPRECATION WARNING: #{name}::#{replacement[:old_generator]} is deprecated. Use #{replacement[:new_constant]} instead.")
unless Faker::Deprecator.skip_warning?
keshavbiswa marked this conversation as resolved.
Show resolved Hide resolved
$stdout.puts("DEPRECATION WARNING: #{name}::#{replacement[:old_generator]} is deprecated. Use #{replacement[:new_constant]} instead.")
end

return replacement[:new_constant]
end

Expand All @@ -25,6 +30,26 @@ def deprecate_generator(old_generator_name, new_generator_constant)

base.singleton_class.prepend extension
end

def self.skip_warning
original = Faker::Deprecator.skip
Faker::Deprecator.skip = true
yield
ensure
Faker::Deprecator.skip = original
end

def self.skip_warning?
skip == true
end

def self.skip
@skip ||= false
end

def self.skip=(value)
@skip = value
end
end
end
# rubocop:enable Style/ClassVars
4 changes: 3 additions & 1 deletion test/faker/default/test_faker_id_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ def assert_valid_south_african_id_number(sample)

class TestFakerIDNumber < Test::Unit::TestCase
def setup
@tester = Faker::IDNumber
Faker::Deprecator.skip_warning do
@tester = Faker::IDNumber
end
end

def test_valid_ssn
Expand Down
12 changes: 9 additions & 3 deletions test/faker/default/test_faker_theater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ def test_play

# with test_faker_show.rb
class TestFakerShow < Test::Unit::TestCase
def setup
Faker::Deprecator.skip_warning do
@tester = Faker::Show
end
end
keshavbiswa marked this conversation as resolved.
Show resolved Hide resolved

def test_adult_musical
assert_match(/\w+/, Faker::Show.adult_musical)
assert_match(/\w+/, @tester.adult_musical)
end

def test_kids_musical
assert_match(/\w+/, Faker::Show.kids_musical)
assert_match(/\w+/, @tester.kids_musical)
end

def test_play
assert_match(/\w+/, Faker::Show.play)
assert_match(/\w+/, @tester.play)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

class TestFakerFmaBrotherhood < Test::Unit::TestCase
def setup
@tester = Faker::JapaneseMedia::FmaBrotherhood
Faker::Deprecator.skip_warning do
@tester = Faker::JapaneseMedia::FmaBrotherhood
end
end

def test_character
Expand Down
12 changes: 9 additions & 3 deletions test/faker/locations/test_faker_australia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ def test_state
end

class TestFakerAustralia < Test::Unit::TestCase
def setup
Faker::Deprecator.skip_warning do
@tester = Faker::Australia
end
end

def test_deprecated_location
assert_match(/\w+/, Faker::Australia.location)
assert_match(/\w+/, @tester.location)
end

def test_deprecated_animal
assert_match(/\w+/, Faker::Australia.animal)
assert_match(/\w+/, @tester.animal)
end

def test_state
assert_match(/\w+/, Faker::Australia.state)
assert_match(/\w+/, @tester.state)
end
end
Loading