diff options
| -rw-r--r-- | test/support/capture_warnings.rb | 20 | ||||
| -rw-r--r-- | test/tc_package.rb | 15 |
2 files changed, 23 insertions, 12 deletions
diff --git a/test/support/capture_warnings.rb b/test/support/capture_warnings.rb new file mode 100644 index 00000000..60d4f24d --- /dev/null +++ b/test/support/capture_warnings.rb @@ -0,0 +1,20 @@ +module CaptureWarnings + def capture_warnings(&block) + # Turn off warnings with setting $VERBOSE to nil + original_verbose = $VERBOSE + $VERBOSE = nil + + # Redefine warn to redirect warning into our array + original_warn = Kernel.instance_method(:warn) + warnings = [] + Kernel.send(:define_method, :warn) { |string| warnings << string } + block.call + + # Revert to the original warn method and set back $VERBOSE to previous value + Kernel.send(:define_method, :warn, original_warn) + $VERBOSE = original_verbose + + # Give back the received warnings + warnings + end +end diff --git a/test/tc_package.rb b/test/tc_package.rb index 6a943635..79fe20f5 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -1,7 +1,10 @@ # encoding: UTF-8 require 'tc_helper.rb' +require 'support/capture_warnings' class TestPackage < Test::Unit::TestCase + include CaptureWarnings + def setup @package = Axlsx::Package.new ws = @package.workbook.add_worksheet @@ -194,18 +197,6 @@ class TestPackage < Test::Unit::TestCase File.delete(@fname) end - def capture_warnings(&block) - original_warn = Kernel.instance_method(:warn) - warnings = [] - Kernel.send(:define_method, :warn) { |string| warnings << string } - block.call - original_verbose = $VERBOSE - $VERBOSE = nil - Kernel.send(:define_method, :warn, original_warn) - $VERBOSE = original_verbose - warnings - end - # See comment for Package#zip_entry_for_part def test_serialization_creates_identical_files_at_any_time_if_created_at_is_set @package.core.created = Time.now |
