Skip to content
Newer
Older
100644 63 lines (51 sloc) 1.46 KB
9d755f1 @jeremy require abstract_unit directly since test is in load path
jeremy authored Jan 5, 2008
1 require 'abstract_unit'
c990117 @jeremy benchmark helper takes an optional log level, defaults to :info. Chos…
jeremy authored Jul 5, 2005
2
a15e02d @josevalim Unify benchmark APIs.
josevalim authored Oct 9, 2009
3 class BenchmarkableTest < ActiveSupport::TestCase
4 include ActiveSupport::Benchmarkable
c990117 @jeremy benchmark helper takes an optional log level, defaults to :info. Chos…
jeremy authored Jul 5, 2005
5
04ef93d @tenderlove * ActiveSupport::BufferedLogger#silence is deprecated. If you want to…
tenderlove authored Dec 9, 2011
6 attr_reader :buffer, :logger
7
8 class Buffer
9 include Enumerable
10
11 def initialize; @lines = []; end
12 def each(&block); @lines.each(&block); end
13 def write(x); @lines << x; end
14 def close; end
15 def last; @lines.last; end
16 def size; @lines.size; end
17 def empty?; @lines.empty?; end
18 end
19
20 def setup
21 @buffer = Buffer.new
22 @logger = ActiveSupport::BufferedLogger.new(@buffer)
c990117 @jeremy benchmark helper takes an optional log level, defaults to :info. Chos…
jeremy authored Jul 5, 2005
23 end
24
25 def test_without_block
26 assert_raise(LocalJumpError) { benchmark }
a1fb57a @dhh Added :silence option to BenchmarkHelper#benchmark and turned log_lev…
dhh authored Jan 1, 2009
27 assert buffer.empty?
c990117 @jeremy benchmark helper takes an optional log level, defaults to :info. Chos…
jeremy authored Jul 5, 2005
28 end
29
30 def test_defaults
31 i_was_run = false
32 benchmark { i_was_run = true }
33 assert i_was_run
34 assert_last_logged
35 end
36
37 def test_with_message
38 i_was_run = false
39 benchmark('test_run') { i_was_run = true }
40 assert i_was_run
41 assert_last_logged 'test_run'
42 end
43
a1fb57a @dhh Added :silence option to BenchmarkHelper#benchmark and turned log_lev…
dhh authored Jan 1, 2009
44 def test_within_level
a15e02d @josevalim Unify benchmark APIs.
josevalim authored Oct 9, 2009
45 logger.level = ActiveSupport::BufferedLogger::DEBUG
a1fb57a @dhh Added :silence option to BenchmarkHelper#benchmark and turned log_lev…
dhh authored Jan 1, 2009
46 benchmark('included_debug_run', :level => :debug) { }
47 assert_last_logged 'included_debug_run'
48 end
49
50 def test_outside_level
a15e02d @josevalim Unify benchmark APIs.
josevalim authored Oct 9, 2009
51 logger.level = ActiveSupport::BufferedLogger::ERROR
a1fb57a @dhh Added :silence option to BenchmarkHelper#benchmark and turned log_lev…
dhh authored Jan 1, 2009
52 benchmark('skipped_debug_run', :level => :debug) { }
53 assert_no_match(/skipped_debug_run/, buffer.last)
54 ensure
a15e02d @josevalim Unify benchmark APIs.
josevalim authored Oct 9, 2009
55 logger.level = ActiveSupport::BufferedLogger::DEBUG
c990117 @jeremy benchmark helper takes an optional log level, defaults to :info. Chos…
jeremy authored Jul 5, 2005
56 end
57
58 private
a1fb57a @dhh Added :silence option to BenchmarkHelper#benchmark and turned log_lev…
dhh authored Jan 1, 2009
59 def assert_last_logged(message = 'Benchmarking')
60 assert_match(/^#{message} \(.*\)$/, buffer.last)
c990117 @jeremy benchmark helper takes an optional log level, defaults to :info. Chos…
jeremy authored Jul 5, 2005
61 end
62 end
Something went wrong with that request. Please try again.