Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 37495e3

Browse files
committed
refactor formatters to delegate profiling to the profiler
1 parent 72c90c0 commit 37495e3

File tree

2 files changed

+6
-48
lines changed

2 files changed

+6
-48
lines changed

lib/rspec/core/formatters/json_formatter.rb

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,17 @@ module Core
66
module Formatters
77
# @private
88
class JsonFormatter < BaseFormatter
9-
Formatters.register self, :message, :dump_summary, :dump_profile, :stop, :close,
10-
:example_group_started, :example_group_finished,
11-
:example_started
9+
Formatters.register self, :message, :dump_summary, :dump_profile, :stop, :close
1210

1311
attr_reader :output_hash
1412

1513
def initialize(output)
1614
super
17-
@example_groups= {} #todo rename, maybe example_groups_data ...
1815
@output_hash = {
1916
:version => RSpec::Core::Version::STRING
2017
}
2118
end
2219

23-
#todo remove duplication with lib/rspec/core/formatters/profile_formatter.rb line 16
24-
def example_group_started(notification)
25-
@example_groups[notification.group.id] = Hash.new(0)
26-
@example_groups[notification.group.id][:start] = Time.now
27-
@example_groups[notification.group.id][:description] = notification.group.top_level_description
28-
end
29-
30-
def example_group_finished(notification)
31-
@example_groups[notification.group.id][:total_time] = Time.now - @example_groups[notification.group.id][:start]
32-
end
33-
34-
def example_started(notification)
35-
group = notification.example.example_group.parent_groups.last.id
36-
@example_groups[group][:count] += 1
37-
end
38-
3920
def message(notification)
4021
(@output_hash[:messages] ||= []) << notification.message
4122
end
@@ -91,9 +72,8 @@ def dump_profile_slowest_examples(profile)
9172

9273
# @api private
9374
def dump_profile_slowest_example_groups(profile)
94-
slowest_groups = profile.calculate_slowest_groups(@example_groups)
9575
@output_hash[:profile] ||= {}
96-
@output_hash[:profile][:groups] = slowest_groups.map do |loc, hash|
76+
@output_hash[:profile][:groups] = profile.slowest_groups.map do |loc, hash|
9777
hash.update(:location => loc)
9878
end
9979
end

lib/rspec/core/formatters/profile_formatter.rb

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,15 @@ module Formatters
66
# @api private
77
# Formatter for providing profile output.
88
class ProfileFormatter
9-
Formatters.register self, :dump_profile, :example_group_started, :example_group_finished, :example_started
9+
Formatters.register self, :dump_profile
1010

1111
def initialize(output)
12-
@example_groups = {} #todo rename, maybe groups_data, groups_information or profile_information
1312
@output = output
1413
end
1514

1615
# @private
1716
attr_reader :output
1817

19-
# @private
20-
def example_group_started(notification)
21-
@example_groups[notification.group.id] = Hash.new(0)
22-
@example_groups[notification.group.id][:start] = Time.now
23-
@example_groups[notification.group.id][:description] = notification.group.top_level_description
24-
end
25-
26-
# @private
27-
def example_group_finished(notification)
28-
@example_groups[notification.group.id][:total_time] = Time.now - @example_groups[notification.group.id][:start]
29-
end
30-
31-
# @private
32-
def example_started(notification)
33-
#todo: maybe move example_group.parent_groups.last to an example or notification method like example.last_anscestor_group
34-
group = notification.example.example_group.parent_groups.last.id
35-
@example_groups[group][:count] += 1
36-
end
37-
38-
3918
# @api public
4019
#
4120
# This method is invoked after the dumping the summary if profiling is
@@ -63,11 +42,10 @@ def dump_profile_slowest_examples(profile)
6342
end
6443

6544
def dump_profile_slowest_example_groups(profile)
66-
slowest_groups = profile.calculate_slowest_groups(@example_groups)
67-
return if slowest_groups.empty?
45+
return if profile.slowest_groups.empty?
6846

69-
@output.puts "\nTop #{slowest_groups.size} slowest example groups:"
70-
slowest_groups.each do |loc, hash|
47+
@output.puts "\nTop #{profile.slowest_groups.size} slowest example groups:"
48+
profile.slowest_groups.each do |loc, hash|
7149
average = "#{bold(Helpers.format_seconds(hash[:average]))} #{bold("seconds")} average"
7250
total = "#{Helpers.format_seconds(hash[:total_time])} seconds"
7351
count = Helpers.pluralize(hash[:count], "example")

0 commit comments

Comments
 (0)