1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
# frozen_string_literal: true
module TestProf
module MemoryProf
class RSpecListener
NOTIFICATIONS = %i[
example_started
example_finished
example_group_started
example_group_finished
].freeze
attr_reader :tracker, :printer
def initialize
@tracker = MemoryProf.tracker
@printer = MemoryProf.printer(tracker)
@current_group = nil
@current_example = nil
@tracker.start
end
def example_started(notification)
tracker.example_started(notification.example, example(notification))
end
def example_finished(notification)
tracker.example_finished(notification.example)
end
def example_group_started(notification)
tracker.group_started(notification.group, group(notification))
end
def example_group_finished(notification)
tracker.group_finished(notification.group)
end
def report
tracker.finish
printer.print
end
private
def example(notification)
{
name: notification.example.description,
location: notification.example.metadata[:location]
}
end
def group(notification)
{
name: notification.group.description,
location: notification.group.metadata[:location]
}
end
end
end
end
TestProf.activate("TEST_MEM_PROF") do
RSpec.configure do |config|
listener = nil
config.before(:suite) do
listener = TestProf::MemoryProf::RSpecListener.new
config.reporter.register_listener(
listener, *TestProf::MemoryProf::RSpecListener::NOTIFICATIONS
)
end
config.after(:suite) { listener&.report }
end
end
|