Class: FlakeySpecCatcher::ChangeCapsule

Inherits:
Object
  • Object
show all
Defined in:
lib/flakey_spec_catcher/change_capsule.rb

Overview

ChangeCapsule class

Summarizes a git change for a single file.

A ChangeCapsule object will represent the changes made to a block of code. It accomplishes this using ChangeContext and ChangeSummary objects.

Constant Summary collapse

SCOPE_SPECIFIERS =
%w[it context describe scenario].freeze
SHARED_EXAMPLES =
%w[include_examples it_behaves_like it_should_behave_like matching].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, change_summary, change_contexts = []) ⇒ ChangeCapsule

Returns a new instance of ChangeCapsule.



15
16
17
18
19
20
21
# File 'lib/flakey_spec_catcher/change_capsule.rb', line 15

def initialize(file_name, change_summary, change_contexts = [])
  @file_name = file_name
  @change_summary = change_summary
  @change_contexts = []
  @spec_tree = {}
  handle_initial_change_contexts(change_contexts)
end

Instance Attribute Details

#change_contextsObject (readonly)

Returns the value of attribute change_contexts.



11
12
13
# File 'lib/flakey_spec_catcher/change_capsule.rb', line 11

def change_contexts
  @change_contexts
end

#change_summaryObject (readonly)

Returns the value of attribute change_summary.



11
12
13
# File 'lib/flakey_spec_catcher/change_capsule.rb', line 11

def change_summary
  @change_summary
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



11
12
13
# File 'lib/flakey_spec_catcher/change_capsule.rb', line 11

def file_name
  @file_name
end

#spec_treeObject (readonly)

Returns the value of attribute spec_tree.



11
12
13
# File 'lib/flakey_spec_catcher/change_capsule.rb', line 11

def spec_tree
  @spec_tree
end

Instance Method Details

#changed_examplesObject



23
24
25
# File 'lib/flakey_spec_catcher/change_capsule.rb', line 23

def changed_examples
  @change_contexts.map(&:rerun_info)
end

#fill_contextsObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



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
# File 'lib/flakey_spec_catcher/change_capsule.rb', line 28

def fill_contexts
  change_context_stack = []
  ignore_scope_closure = 0
  lines_in_file = File.read(@file_name).split("\n")
  lines_in_file.each_with_index do |line, index|
    shared_example_identified = false

    # Check if line matches an rspec example or examplegroup format
    if line =~ spec_scope
      handle_change_context(line, index, change_context_stack)
    # Else, ignore other blocks that might pollute context stack
    elsif line =~ shared_example_scope
      handle_change_context(line, index, change_context_stack)
      shared_example_identified = true
    elsif line_matches_method_or_block(line)
      ignore_scope_closure += 1
    end

    fill_context(line, index, change_context_stack)

    if shared_example_identified
      change_context_stack.pop
    elsif line =~ pop_scope
      if ignore_scope_closure.positive?
        ignore_scope_closure -= 1
      else
        change_context_stack.pop
      end
    end
  end
end