Skip to content

Commit

Permalink
Allowing passing a block to clear
Browse files Browse the repository at this point in the history
If a block is passed, the values are only cleared within the block and
restored after the block ends.
  • Loading branch information
splattael committed Jul 8, 2022
1 parent b775e11 commit b06ecdd
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 5 deletions.
40 changes: 35 additions & 5 deletions lib/warning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
yield
ensure
synchronize do
@ignore = ignore
@process = process
@dedup = dedup
end
end
else
synchronize do
@ignore.clear
@process.clear
@dedup = false
end
end
end

Expand Down
84 changes: 84 additions & 0 deletions test/test_warning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,90 @@ def teardown
Warning.clear
end

def test_warning_clear_ignore
Warning.ignore(/.*/)

assert_warning '' do
warn 'foo'
end

Warning.clear do
assert_warning "foo\n" do
warn 'foo'
end
end

assert_warning '' do
warn 'foo'
end

Warning.clear

assert_warning "foo\n" do
warn 'foo'
end
end

def test_warning_clear_process
Warning.process('', /foo/ => :raise)

assert_raises(RuntimeError, 'foo') do
warn 'foo'
end

Warning.clear do
assert_warning "foo\n" do
warn 'foo'
end
end

assert_raises(RuntimeError, 'foo') do
warn 'foo'
end

Warning.clear

assert_warning "foo\n" do
warn 'foo'
end
end

def test_warning_clear_dedup
Warning.dedup

assert_warning "foo\n" do
warn 'foo'
end

assert_warning '' do
warn 'foo'
end

Warning.clear do
assert_warning "foo\n" do
warn 'foo'
end

assert_warning "foo\n" do
warn 'foo'
end
end

assert_warning '' do
warn 'foo'
end

Warning.clear

assert_warning "foo\n" do
warn 'foo'
end

assert_warning "foo\n" do
warn 'foo'
end
end

def test_warning_dedup
gvar = ->{$test_warning_dedup}

Expand Down

0 comments on commit b06ecdd

Please sign in to comment.