Class: AppMap::RSpec::Recording

Inherits:
Struct
  • Object
show all
Defined in:
lib/appmap/rspec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example) ⇒ Recording

Returns a new instance of Recording.



115
116
117
118
119
120
# File 'lib/appmap/rspec.rb', line 115

def initialize(example)
  super

  warn "Starting recording of example #{example}" if AppMap::RSpec::LOG
  @trace = AppMap.tracing.trace
end

Instance Attribute Details

#exampleObject

Returns the value of attribute example

Returns:

  • (Object)

    the current value of example



114
115
116
# File 'lib/appmap/rspec.rb', line 114

def example
  @example
end

Instance Method Details

#finishObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/appmap/rspec.rb', line 122

def finish
  warn "Finishing recording of example #{example}" if AppMap::RSpec::LOG

  events = []
  AppMap.tracing.delete @trace

  events << @trace.next_event.to_h while @trace.event?

  AppMap::RSpec.add_event_methods @trace.event_methods

  class_map = AppMap.class_map(AppMap::RSpec.config, @trace.event_methods)

  description = []
  scope = ScopeExample.new(example)
  feature_group = feature = nil

  labels = []
  while scope
    labels += scope.labels
    description << scope.description
    feature ||= scope.feature
    feature_group ||= scope.feature_group
    scope = scope.parent
  end

  labels = labels.map(&:to_s).map(&:strip).reject(&:blank?).map(&:downcase).uniq
  description.reject!(&:nil?).reject(&:blank?)
  default_description = description.last
  description.reverse!

  normalize = lambda do |desc|
    desc.gsub('it should behave like', '')
        .gsub(/Controller$/, '')
        .gsub(/\s+/, ' ')
        .strip
  end

  full_description = normalize.call(description.join(' '))

  compute_feature_name = lambda do
    return 'unknown' if description.empty?

    feature_description = description.dup
    num_tokens = [2, feature_description.length - 1].min
    feature_description[0...num_tokens].map(&:strip).join(' ')
  end

  feature_group ||= normalize.call(default_description).underscore.gsub('/', '_').humanize
  feature_name = feature || compute_feature_name.call if feature_group
  feature_name = normalize.call(feature_name) if feature_name

  AppMap::RSpec.save full_description,
                     class_map,
                     events: events,
                     feature_name: feature_name,
                     feature_group_name: feature_group,
                     labels: labels.blank? ? nil : labels
end