2008-05-18 06:22:34 +02:00
|
|
|
require 'abstract_unit'
|
2007-12-21 08:48:59 +01:00
|
|
|
require 'action_view/helpers/benchmark_helper'
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
class BenchmarkHelperTest < ActionView::TestCase
|
|
|
|
tests ActionView::Helpers::BenchmarkHelper
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-12-01 02:38:34 +01:00
|
|
|
def setup
|
|
|
|
super
|
|
|
|
controller.logger = ActiveSupport::BufferedLogger.new(StringIO.new)
|
|
|
|
controller.logger.auto_flushing = false
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-12-01 02:38:34 +01:00
|
|
|
def teardown
|
|
|
|
controller.logger.send(:clear_buffer)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_without_block
|
|
|
|
assert_raise(LocalJumpError) { benchmark }
|
2009-02-04 21:26:08 +01:00
|
|
|
assert buffer.empty?
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_defaults
|
|
|
|
i_was_run = false
|
|
|
|
benchmark { i_was_run = true }
|
|
|
|
assert i_was_run
|
|
|
|
assert_last_logged
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_message
|
|
|
|
i_was_run = false
|
|
|
|
benchmark('test_run') { i_was_run = true }
|
|
|
|
assert i_was_run
|
|
|
|
assert_last_logged 'test_run'
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def test_with_message_and_deprecated_level
|
2007-01-22 14:43:50 +01:00
|
|
|
i_was_run = false
|
2009-02-04 21:26:08 +01:00
|
|
|
|
|
|
|
assert_deprecated do
|
|
|
|
benchmark('debug_run', :debug) { i_was_run = true }
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
assert i_was_run
|
2009-02-04 21:26:08 +01:00
|
|
|
assert_last_logged 'debug_run'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_within_level
|
|
|
|
controller.logger.level = ActiveSupport::BufferedLogger::DEBUG
|
|
|
|
benchmark('included_debug_run', :level => :debug) { }
|
|
|
|
assert_last_logged 'included_debug_run'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_outside_level
|
|
|
|
controller.logger.level = ActiveSupport::BufferedLogger::ERROR
|
|
|
|
benchmark('skipped_debug_run', :level => :debug) { }
|
|
|
|
assert_no_match(/skipped_debug_run/, buffer.last)
|
|
|
|
ensure
|
|
|
|
controller.logger.level = ActiveSupport::BufferedLogger::DEBUG
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def test_without_silencing
|
|
|
|
benchmark('debug_run', :silence => false) do
|
|
|
|
controller.logger.info "not silenced!"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 2, buffer.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_silencing
|
|
|
|
benchmark('debug_run', :silence => true) do
|
|
|
|
controller.logger.info "silenced!"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 1, buffer.size
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
private
|
2009-02-04 21:26:08 +01:00
|
|
|
def buffer
|
|
|
|
controller.logger.send(:buffer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_last_logged(message = 'Benchmarking')
|
|
|
|
assert_match(/^#{message} \(.*\)$/, buffer.last)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|