-
Notifications
You must be signed in to change notification settings - Fork 11
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
Allowing passing a block to Warning.clear #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,11 +32,41 @@ module Processor | |
|
||
# Clear all current ignored warnings, warning processors, and duplicate check cache. | ||
# Also disables deduplicating warnings if that is currently enabled. | ||
def clear | ||
synchronize do | ||
@ignore.clear | ||
@process.clear | ||
@dedup = false | ||
# | ||
# If a block is passed, the values are only cleared within the block and | ||
# restored after the block ends. | ||
# | ||
# Examples: | ||
# | ||
# # Clear all values | ||
# Warning.clear | ||
# | ||
# # Clear values only within the block. | ||
# Warning.clear do | ||
# ... | ||
# end | ||
# # The values are stored to previous state. | ||
def clear(&block) | ||
if block | ||
begin | ||
ignore = @ignore.dup | ||
process = @process.dup | ||
dedup = @dedup.dup | ||
clear | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't love the recursive call but it's convenient. Alternatively, we could move the clear logic into a private method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This recursive call seems fine, considering it only recurses a single level. |
||
yield | ||
ensure | ||
synchronize do | ||
@ignore = ignore | ||
@process = process | ||
@dedup = dedup | ||
end | ||
end | ||
else | ||
synchronize do | ||
@ignore.clear | ||
@process.clear | ||
@dedup = false | ||
end | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,6 +46,92 @@ def teardown | |||||
Warning.clear | ||||||
end | ||||||
|
||||||
def test_warning_clear_ignore | ||||||
Warning.ignore(/.*/) | ||||||
|
||||||
assert_warning '' do | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To reduce horizontal space we could use:
Suggested change
Not sure if this is compatible with the style of this gem though 🤷 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is fine how it is. |
||||||
Warning.warn 'foo' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to use It's also worth noting that I guess this is not related to this $ irb
>> warn "foo"
foo
=> nil
>> Warning.warn "foo"
foo=> nil There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's expected that |
||||||
end | ||||||
|
||||||
Warning.clear do | ||||||
assert_warning 'foo' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
end | ||||||
|
||||||
assert_warning '' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
|
||||||
Warning.clear | ||||||
|
||||||
assert_warning 'foo' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
end | ||||||
|
||||||
def test_warning_clear_process | ||||||
Warning.process('', /foo/ => :raise) | ||||||
|
||||||
e = assert_raises(RuntimeError) do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
assert_equal('foo', e.message) | ||||||
|
||||||
Warning.clear do | ||||||
assert_warning 'foo' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
end | ||||||
|
||||||
e = assert_raises(RuntimeError) do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
assert_equal('foo', e.message) | ||||||
|
||||||
Warning.clear | ||||||
|
||||||
assert_warning 'foo' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
end | ||||||
|
||||||
def test_warning_clear_dedup | ||||||
Warning.dedup | ||||||
|
||||||
assert_warning 'foo' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
|
||||||
assert_warning '' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
|
||||||
Warning.clear do | ||||||
assert_warning 'foo' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
|
||||||
assert_warning 'foo' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
end | ||||||
|
||||||
assert_warning '' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
|
||||||
Warning.clear | ||||||
|
||||||
assert_warning 'foo' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
|
||||||
assert_warning 'foo' do | ||||||
Warning.warn 'foo' | ||||||
end | ||||||
end | ||||||
|
||||||
def test_warning_dedup | ||||||
gvar = ->{$test_warning_dedup} | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This docs is kind of clumsy and needs to be improved 😅