Class: TimedSpecs

Inherits:
RSpec::Core::Formatters::DocumentationFormatter
  • Object
show all
Defined in:
lib/timed_specs.rb

Constant Summary collapse

SLOWEST_EXAMPLES_COUNT =
(ENV['SLOWEST_EXAMPLES_COUNT'] || 20).to_i

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TimedSpecs

Returns a new instance of TimedSpecs.



7
8
9
10
# File 'lib/timed_specs.rb', line 7

def initialize(*args)
  super
  @examples_with_execution_time = []
end

Instance Method Details

#example_group_finished(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/timed_specs.rb', line 22

def example_group_finished(*args)
  super
  if @group_time
    output.puts current_indentation + white("#{(Time.now - @group_time).round(3)}s")
    @group_time = nil
  else
    output.puts
  end
end

#example_group_started(*args) ⇒ Object



17
18
19
20
# File 'lib/timed_specs.rb', line 17

def example_group_started(*args)
  @group_time = Time.now
  super
end

#example_passed(example) ⇒ Object

Overridden method of the base documentation formatter Is executed after each passed example



34
35
36
37
38
39
40
41
# File 'lib/timed_specs.rb', line 34

def example_passed(example)
  super(example)
  @examples_with_execution_time << [
    example.description,
    example.[:execution_result][:run_time], # this is the time taken to execute the example
    format_caller(example.location) # relative path to the example file
  ]
end

#passed_output(example) ⇒ Object

Overridden method of the base documentation formatter Used by DocumentationFormatter to display the example info after each passed example super - displays the example description Then display the execution time beside it



47
48
49
# File 'lib/timed_specs.rb', line 47

def passed_output(example)
  super + cyan(" : #{example.[:execution_result][:run_time].round(3)}s")
end

#start(*args) ⇒ Object



12
13
14
15
# File 'lib/timed_specs.rb', line 12

def start(*args)
  @output.puts "Time Specs Enabled"
  super
end

#start_dumpObject



51
52
53
54
# File 'lib/timed_specs.rb', line 51

def start_dump
  output_slowest_examples unless @examples_with_execution_time.empty?
  super
end