Class: RSpec::Covers::StrictVerdict

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/covers/strict_verdict.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(risky:, reason:, undeclared_locations:, declaration:) ⇒ StrictVerdict

Returns a new instance of StrictVerdict.



12
13
14
15
16
17
# File 'lib/rspec/covers/strict_verdict.rb', line 12

def initialize(risky:, reason:, undeclared_locations:, declaration:)
  @risky = risky
  @reason = reason
  @undeclared_locations = undeclared_locations
  @declaration = declaration
end

Instance Attribute Details

#declarationObject (readonly)

Returns the value of attribute declaration.



10
11
12
# File 'lib/rspec/covers/strict_verdict.rb', line 10

def declaration
  @declaration
end

#reasonObject (readonly)

Returns the value of attribute reason.



10
11
12
# File 'lib/rspec/covers/strict_verdict.rb', line 10

def reason
  @reason
end

#riskyObject (readonly)

Returns the value of attribute risky.



10
11
12
# File 'lib/rspec/covers/strict_verdict.rb', line 10

def risky
  @risky
end

#undeclared_locationsObject (readonly)

Returns the value of attribute undeclared_locations.



10
11
12
# File 'lib/rspec/covers/strict_verdict.rb', line 10

def undeclared_locations
  @undeclared_locations
end

Class Method Details

.call(executed_locations:, declaration:, config:) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rspec/covers/strict_verdict.rb', line 60

def self.call(executed_locations:, declaration:, config:)
  executed_locations = Set.new(executed_locations)

  if !declaration.declared?
    risky = config.undeclared == :risky && executed_locations.any?
    return new(
      risky: risky,
      reason: executed_locations.any? ? :undeclared_example : :clean,
      undeclared_locations: executed_locations,
      declaration: declaration
    )
  end

  allowed = declaration.locations
  misses = executed_locations.reject { |location| allowed.include?(location) }.to_set

  new(
    risky: config.strict && misses.any?,
    reason: misses.any? ? :strict_miss : :clean,
    undeclared_locations: misses,
    declaration: declaration
  )
end

Instance Method Details

#clean?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rspec/covers/strict_verdict.rb', line 23

def clean?
  !risky?
end

#grouped_evidence(limit: 10, config: RSpec::Covers.configuration) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rspec/covers/strict_verdict.rb', line 38

def grouped_evidence(limit: 10, config: RSpec::Covers.configuration)
  undeclared_locations.group_by(&:file).flat_map do |file, locations|
    lines = locations.map(&:line).sort.take(limit)
    methods = ProductionInventory.new(config).methods_for_locations(locations)
    [{
      file: file,
      lines: lines,
      methods: methods.map(&:label),
      note: "coverage from threads spawned inside the example may be included"
    }]
  end
end

#messageObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/rspec/covers/strict_verdict.rb', line 27

def message
  case reason
  when :undeclared_example
    "rspec-covers: example executed production code without covers metadata"
  when :strict_miss
    "rspec-covers: example executed production code outside covers/uses metadata"
  else
    "rspec-covers: clean"
  end
end

#risky?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rspec/covers/strict_verdict.rb', line 19

def risky?
  risky
end

#to_hObject



51
52
53
54
55
56
57
58
# File 'lib/rspec/covers/strict_verdict.rb', line 51

def to_h
  {
    risky: risky?,
    reason: reason,
    undeclared_locations: undeclared_locations.map(&:to_h),
    evidence: grouped_evidence
  }
end