Class: RuboCop::Cop::RSpecParity::SufficientContexts::ScanState

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/rspec_parity/sufficient_contexts.rb

Overview

Line-by-line scanner state shared by both spec-counting paths. Tracks the scenario tallies (mirroring the prior behaviour) and, when CoversAnnotations is on, gathers ‘# rspec_parity:covers` labels and collapses an annotated context’s body so its examples count as the one annotated branch rather than as separate scenarios.

Instance Method Summary collapse

Constructor Details

#initialize(annotations_enabled) ⇒ ScanState

Returns a new instance of ScanState.



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 99

def initialize(annotations_enabled)
  @annotations_enabled = annotations_enabled
  @context_count = 0
  @example_count = 0
  @has_examples = false
  @has_direct_examples = false
  @child_indent = nil
  @annotations = []
  @annotated_indent = nil
  @last_context_indent = nil
  @last_context_counted = false
end

Instance Method Details

#collect_standalone_annotation(line) ⇒ Object

A comment-only annotation line, typically placed just inside a context block when a trailing comment would overflow the line length. It is attributed to the enclosing context, which then collapses like a context annotated on its opening line. Returns the labels (truthy) when it consumes the line, else nil. Place it before the context’s examples; an annotation after an example only collapses what follows.



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 140

def collect_standalone_annotation(line)
  return unless @annotations_enabled
  return unless line.lstrip.start_with?("#")

  labels = annotation_labels(line)
  return if labels.empty?

  @annotations.concat(labels)
  annotate_enclosing_context
  labels
end

#count_context(line, indent) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 124

def count_context(line, indent)
  @last_context_indent = indent
  labels = annotation_labels(line)
  return annotate_context_line(indent, labels) if labels.any?

  @child_indent ||= indent
  @context_count += 1
  @last_context_counted = true
end

#count_example(line, indent) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 160

def count_example(line, indent)
  labels = annotation_labels(line)
  if labels.any?
    @annotations.concat(labels)
  else
    @has_examples = true
    @example_count += 1
    @child_indent ||= indent
    @has_direct_examples = true if indent == @child_indent
  end
end

#count_named_context(line) ⇒ Object

Coarse path for method-named context lines (e.g. ‘context ’.call’‘): count the block and gather any annotation, but don’t collapse — this path doesn’t track the block’s interior.



155
156
157
158
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 155

def count_named_context(line)
  @context_count += 1
  @annotations.concat(annotation_labels(line))
end

#exit_annotated_context(indent) ⇒ Object



120
121
122
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 120

def exit_annotated_context(indent)
  @annotated_indent = nil if @annotated_indent && indent <= @annotated_indent
end

#inside_annotated_context?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 116

def inside_annotated_context?
  !@annotated_indent.nil?
end

#reset_child_indentObject



112
113
114
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 112

def reset_child_indent
  @child_indent = nil
end

#to_parsed_specObject



172
173
174
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 172

def to_parsed_spec
  ParsedSpec.new(@context_count, @example_count, @has_examples, @has_direct_examples, @annotations)
end