Class: FixtureKit::Analyzer::GroupAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/fixture_kit/analyzer/group_analyzer.rb

Instance Method Summary collapse

Constructor Details

#initialize(detector: AstFactoryDetector.new) ⇒ GroupAnalyzer

Returns a new instance of GroupAnalyzer.



6
7
8
# File 'lib/fixture_kit/analyzer/group_analyzer.rb', line 6

def initialize(detector: AstFactoryDetector.new)
  @detector = detector
end

Instance Method Details

#analyze(group, results = []) ⇒ Object

Walk tree, collect factory lets



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
# File 'lib/fixture_kit/analyzer/group_analyzer.rb', line 34

def analyze(group, results = [])
  own_mod = begin
    group.const_get(:LetDefinitions, false)
  rescue NameError
    nil
  end

  if own_mod
    own_mod.instance_methods(false).each do |method_name|
      factories = @detector.detect(own_mod, method_name)
      next if factories.empty?

      loc = own_mod.instance_method(method_name).source_location
      example_count = examples_using_let(group, method_name.to_s)

      results << LetDefinition.new(
        name: method_name.to_s,
        factories: factories,
        example_count: example_count,
        file: loc&.first,
        line: loc&.last,
        group_description: group.description.to_s[0, 80],
      )
    end
  end

  group.children.each { |child| analyze(child, results) }
  results
end

#examples_using_let(group, let_name) ⇒ Object

Count examples that inherit a let defined at ‘group`, stopping at overrides



19
20
21
22
23
24
25
26
# File 'lib/fixture_kit/analyzer/group_analyzer.rb', line 19

def examples_using_let(group, let_name)
  count = group.examples.count
  group.children.each do |child|
    next if group_defines_let?(child, let_name)
    count += examples_using_let(child, let_name)
  end
  count
end

#group_defines_let?(group, let_name) ⇒ Boolean

Check if a group directly defines a let with this name

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/fixture_kit/analyzer/group_analyzer.rb', line 11

def group_defines_let?(group, let_name)
  own_mod = group.const_get(:LetDefinitions, false)
  own_mod.instance_methods(false).include?(let_name.to_sym)
rescue NameError
  false
end

#total_examples(group) ⇒ Object

Total examples in group + all descendants



29
30
31
# File 'lib/fixture_kit/analyzer/group_analyzer.rb', line 29

def total_examples(group)
  group.examples.count + group.children.sum { |c| total_examples(c) }
end