Class: SixthSense::Analyzers::QualityAssertionDensity

Inherits:
SixthSense::Analyzer show all
Defined in:
lib/sixth_sense/analyzers/quality_assertion_density.rb

Instance Method Summary collapse

Methods inherited from SixthSense::Analyzer

analyzer_id, axis, inherited, level, reference, references

Instance Method Details

#analyze(test_file, context) ⇒ Object

Paper basis: Kudrjavets/Nagappan/Ball 2006 (doi:10.1109/ISSRE.2006.14) links assertion density with fault outcomes.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sixth_sense/analyzers/quality_assertion_density.rb', line 20

def analyze(test_file, context)
  executable_loc = [test_file.executable_loc, 1].max
  density = test_file.assertions.length.to_f / executable_loc
  density_min = context.config_fetch(:quality, :assertion_density_min, default: 0.05)
  return result(score: 100.0, findings: [], confidence: :high) if density >= density_min

  penalty = (1.0 - (density / density_min)) * 10.0
  findings = [
    finding(
      rule_id: "low_assertion_density",
      severity: :warning,
      location: test_file.test_cases.first&.location,
      message: "Assertion density is #{density.round(3)} per executable test line.",
      suggestion: "Add explicit expectations for the behavior this file exercises."
    )
  ]

  result(score: (100.0 - penalty).round(2), findings: findings, confidence: :high)
end