Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#5 Avoid any structured warning handling when facing incompatible API
  • Loading branch information
schmidt committed Jan 15, 2015
1 parent c94fb6a commit a9823d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/structured_warnings/kernel.rb
Expand Up @@ -53,7 +53,10 @@ def warn(*args)
message = first.to_s
end

super unless args.empty?
# If args is not empty, user passed an incompatible set of arguments.
# Maybe somebody else is overriding warn as well and knows, what to do.
# Better do nothing in this case. See #5
return super unless args.empty?

if warning.active?
output = StructuredWarnings.warner.format(warning, message, caller(1))
Expand Down
24 changes: 18 additions & 6 deletions test/structured_warnings_test.rb
Expand Up @@ -6,10 +6,17 @@ def old_method
warn DeprecatedMethodWarning,
'This method is deprecated. Use new_method instead'
end
end

class Bar
attr_reader :args

def warn(*args)
@args = args
end

def old_method_too
warn :deprecated,
'This method is deprecated. Use new_method instead'
def method_using_incompatible_warn_api
warn :deprecated, 'explanation'
end
end

Expand Down Expand Up @@ -38,10 +45,15 @@ def test_warn
end
end

def test_old_warn
assert_warn(:deprecated) do
Foo.new.old_method_too
def test_warn_using_incompatible_warn_api
bar = Bar.new

assert_no_warn do
bar.method_using_incompatible_warn_api
end

assert_equal :deprecated, bar.args.first
assert_equal "explanation", bar.args.last
end

def test_disable_warning_blockwise
Expand Down

0 comments on commit a9823d9

Please sign in to comment.