2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2007-01-28 02:16:55 -05:00
|
|
|
require 'action_view/helpers/benchmark_helper'
|
2005-07-05 15:33:25 -04:00
|
|
|
|
2008-04-19 14:06:57 -04:00
|
|
|
class BenchmarkHelperTest < ActionView::TestCase
|
|
|
|
tests ActionView::Helpers::BenchmarkHelper
|
2005-07-05 15:33:25 -04:00
|
|
|
|
|
|
|
class MockLogger
|
|
|
|
attr_reader :logged
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@logged = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(method, *args)
|
|
|
|
@logged << [method, args]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-30 16:00:15 -04:00
|
|
|
def controller
|
|
|
|
@controller ||= Struct.new(:logger).new(MockLogger.new)
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_without_block
|
|
|
|
assert_raise(LocalJumpError) { benchmark }
|
2008-04-30 16:00:15 -04:00
|
|
|
assert controller.logger.logged.empty?
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_defaults
|
|
|
|
i_was_run = false
|
|
|
|
benchmark { i_was_run = true }
|
|
|
|
assert i_was_run
|
2008-04-30 16:00:15 -04:00
|
|
|
assert 1, controller.logger.logged.size
|
2005-07-05 15:33:25 -04:00
|
|
|
assert_last_logged
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_message
|
|
|
|
i_was_run = false
|
|
|
|
benchmark('test_run') { i_was_run = true }
|
|
|
|
assert i_was_run
|
2008-04-30 16:00:15 -04:00
|
|
|
assert 1, controller.logger.logged.size
|
2005-07-05 15:33:25 -04:00
|
|
|
assert_last_logged 'test_run'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_message_and_level
|
|
|
|
i_was_run = false
|
|
|
|
benchmark('debug_run', :debug) { i_was_run = true }
|
|
|
|
assert i_was_run
|
2008-04-30 16:00:15 -04:00
|
|
|
assert 1, controller.logger.logged.size
|
2005-07-05 15:33:25 -04:00
|
|
|
assert_last_logged 'debug_run', :debug
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def assert_last_logged(message = 'Benchmarking', level = :info)
|
2008-04-30 16:00:15 -04:00
|
|
|
last = controller.logger.logged.last
|
2005-07-05 15:33:25 -04:00
|
|
|
assert 2, last.size
|
|
|
|
assert_equal level, last.first
|
|
|
|
assert 1, last[1].size
|
|
|
|
assert last[1][0] =~ /^#{message} \(.*\)$/
|
|
|
|
end
|
|
|
|
end
|