-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2997 from faker-ruby/sb-deprecator-improvements
Deprecator improvements
- Loading branch information
Showing
7 changed files
with
166 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../test_helper' | ||
|
||
class TestFakerDeprecation < Test::Unit::TestCase | ||
def test_using_a_deprecated_generator_returns_a_warning_message | ||
assert_deprecated do | ||
Faker::Dogs.say | ||
end | ||
|
||
assert_equal 'meow', Faker::Dogs.say | ||
end | ||
|
||
def test_using_a_non_deprecated_generator_does_not_return_a_warning_message | ||
assert_not_deprecated do | ||
Faker::Cats.say | ||
end | ||
assert_equal 'meow', Faker::Cats.say | ||
end | ||
|
||
def test_testing_a_deprecated_generator_with_skip_warning_does_not_return_a_warning_message | ||
actual_stdout, actual_stderr = capture_output do | ||
Faker::Deprecator.skip_warning do | ||
Faker::Dogs.say | ||
end | ||
end | ||
|
||
assert_empty(actual_stdout) | ||
assert_empty(actual_stderr) | ||
assert_equal 'meow', Faker::Dogs.say | ||
end | ||
|
||
def test_deprecated_with_skip_warning_does_not_generate_message | ||
Faker::Deprecator.skip_warning do | ||
assert_not_deprecated do | ||
Faker::Dogs.say | ||
end | ||
end | ||
|
||
assert_equal 'meow', Faker::Dogs.say | ||
end | ||
end | ||
|
||
module Faker | ||
class Cats < Base | ||
def self.say | ||
'meow' | ||
end | ||
end | ||
|
||
include Faker::Deprecator | ||
deprecate_generator('Dogs', Cats) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
# Based on Rails Testing Deprecator | ||
# https://github.com/rails/rails/blob/main/activesupport/lib/active_support/testing/deprecation.rb | ||
|
||
# Asserts that a matching deprecation warning was emitted during the execution of the yielded block. | ||
# | ||
# assert_deprecated do | ||
# DeprecatedGenerator.generate | ||
# end | ||
# | ||
def assert_deprecated(&block) | ||
warning = with_captured_stdout(&block) | ||
result = yield block | ||
|
||
refute_predicate warning, :empty?, 'Expected a deprecation warning within the block but received none' | ||
|
||
result | ||
end | ||
|
||
# Asserts that no deprecation warnings are emitted during the execution of the yielded block. | ||
# | ||
# assert_not_deprecated do | ||
# Faker::Internet.email | ||
# end | ||
def assert_not_deprecated(&block) | ||
warning = with_captured_stdout(&block) | ||
result = yield block | ||
|
||
assert_predicate warning, :empty?, "Expected no deprecation warning within the block but received a deprecation: #{warning}" | ||
result | ||
end | ||
|
||
def with_captured_stdout(&block) | ||
original_stdout = $stdout | ||
$stdout = StringIO.new | ||
yield block | ||
$stdout.string | ||
ensure | ||
$stdout = original_stdout | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters