Class: SimpleCov::SourceFile

Inherits:
Object
  • Object
show all
Includes:
BuilderContext
Defined in:
lib/simplecov/source_file.rb,
lib/simplecov/source_file/line.rb,
lib/simplecov/source_file/branch.rb,
lib/simplecov/source_file/method.rb,
lib/simplecov/source_file/statistics.rb,
lib/simplecov/source_file/skip_chunks.rb,
lib/simplecov/source_file/line_builder.rb,
lib/simplecov/source_file/source_loader.rb,
lib/simplecov/source_file/branch_builder.rb,
lib/simplecov/source_file/method_builder.rb,
lib/simplecov/source_file/builder_context.rb,
lib/simplecov/source_file/ruby_data_parser.rb,
sig/simplecov.rbs,
sig/simplecov.rbs,
sig/simplecov.rbs,
sig/simplecov.rbs,
sig/simplecov.rbs,
sig/simplecov.rbs

Defined Under Namespace

Modules: BuilderContext, RubyDataParser, SourceLoader Classes: Branch, BranchBuilder, Line, LineBuilder, Method, MethodBuilder, SkipChunks, Statistics

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BuilderContext

#real_source_positions, #skip_chunks_for

Constructor Details

#initialize(filename, coverage_data, loaded: true) ⇒ SourceFile

Returns a new instance of SourceFile.

Parameters:

  • filename (String)
  • coverage_data (Hash[String, untyped])
  • loaded: (Boolean) (defaults to: true)


20
21
22
23
24
# File 'lib/simplecov/source_file.rb', line 20

def initialize(filename, coverage_data, loaded: true)
  @filename = filename
  @coverage_data = coverage_data
  @loaded = loaded
end

Instance Attribute Details

#coverage_dataHash[String, untyped] (readonly)

Returns the value of attribute coverage_data.

Returns:

  • (Hash[String, untyped])


18
19
20
# File 'lib/simplecov/source_file.rb', line 18

def coverage_data
  @coverage_data
end

#filenameString (readonly)

Returns the value of attribute filename.

Returns:

  • (String)


18
19
20
# File 'lib/simplecov/source_file.rb', line 18

def filename
  @filename
end

Instance Method Details

#branchesArray[Branch]

Returns:



95
96
97
# File 'lib/simplecov/source_file.rb', line 95

def branches
  @branches ||= BranchBuilder.new(self).call
end

#branches_coverage_percentFloat?

Returns:

  • (Float, nil)


103
104
105
106
107
# File 'lib/simplecov/source_file.rb', line 103

def branches_coverage_percent
  Deprecation.warn("`SimpleCov::SourceFile#branches_coverage_percent` is deprecated. " \
                   "Use `covered_percent(:branch)`.")
  covered_percent(:branch)
end

#branches_for_line(line_number) ⇒ Array[[ Symbol, Integer ]]

Parameters:

  • line_number (Integer)

Returns:

  • (Array[[ Symbol, Integer ]])


129
130
131
# File 'lib/simplecov/source_file.rb', line 129

def branches_for_line(line_number)
  branches_report.fetch(line_number, [])
end

#branches_reportHash[Integer, Array[[ Symbol, Integer ]]]

Returns:

  • (Hash[Integer, Array[[ Symbol, Integer ]]])


113
114
115
116
# File 'lib/simplecov/source_file.rb', line 113

def branches_report
  @branches_report ||=
    branches.reject(&:skipped?).group_by(&:report_line).transform_values { |bs| bs.map(&:report) }
end

#coverage_statisticsHash[criterion, CoverageStatistics] #coverage_statistics(arg0) ⇒ CoverageStatistics?

The per-criterion coverage statistics for this file. With no argument answers the {line:, branch:, method:} Hash, and with a criterion symbol that one CoverageStatistics. Every criterion is present even when it wasn't enabled during the run, collapsed to 0/0/0; FileList is the layer that filters to the enabled set.

Overloads:



41
42
43
44
# File 'lib/simplecov/source_file.rb', line 41

def coverage_statistics(criterion = nil)
  stats = (@coverage_statistics ||= Statistics.new(self).call)
  criterion ? stats[criterion] : stats
end

#covered_branchesArray[Branch]

A tree schema, because some conditions like case may have an additional else that isn't declared in code but is given by default by the coverage report.

Returns:



121
122
123
# File 'lib/simplecov/source_file.rb', line 121

def covered_branches
  @covered_branches ||= branches.select(&:covered?)
end

#covered_linesArray[Line]

Returns:



51
52
53
# File 'lib/simplecov/source_file.rb', line 51

def covered_lines
  @covered_lines ||= lines.select(&:covered?)
end

#covered_methodsArray[Method]

Returns:



141
142
143
# File 'lib/simplecov/source_file.rb', line 141

def covered_methods
  @covered_methods ||= methods.select(&:covered?)
end

#covered_percent(criterion = :line) ⇒ Float?

Nil if the criterion was not measured.

Parameters:

  • (criterion)

Returns:

  • (Float, nil)


77
78
79
# File 'lib/simplecov/source_file.rb', line 77

def covered_percent(criterion = :line)
  coverage_statistics(criterion)&.percent
end

#covered_strength(criterion = :line) ⇒ Float?

Parameters:

  • (criterion)

Returns:

  • (Float, nil)


81
82
83
# File 'lib/simplecov/source_file.rb', line 81

def covered_strength(criterion = :line)
  coverage_statistics(criterion)&.strength
end

#line(number) ⇒ Line?

Answers nil for a number this file has no line for.

Parameters:

  • number (Integer)

Returns:



72
73
74
# File 'lib/simplecov/source_file.rb', line 72

def line(number)
  lines.at(number - 1)
end

#line_with_missed_branch?(line_number) ⇒ Boolean

Parameters:

  • line_number (Integer)

Returns:

  • (Boolean)


133
134
135
# File 'lib/simplecov/source_file.rb', line 133

def line_with_missed_branch?(line_number)
  branches_for_line(line_number).any? { |_type, count| count.zero? }
end

#linesArray[Line] Also known as: source_lines

Returns:



46
47
48
# File 'lib/simplecov/source_file.rb', line 46

def lines
  @lines ||= LineBuilder.new(self).call
end

#lines_of_codeInteger

Returns:

  • (Integer)


67
68
69
# File 'lib/simplecov/source_file.rb', line 67

def lines_of_code
  coverage_statistics[:line]&.total || 0
end

#methodsArray[Method]

Returns:



137
138
139
# File 'lib/simplecov/source_file.rb', line 137

def methods
  @methods ||= MethodBuilder.new(self).call
end

#methods_coverage_percentFloat?

Returns:

  • (Float, nil)


149
150
151
152
153
# File 'lib/simplecov/source_file.rb', line 149

def methods_coverage_percent
  Deprecation.warn("`SimpleCov::SourceFile#methods_coverage_percent` is deprecated. " \
                   "Use `covered_percent(:method)`.")
  covered_percent(:method)
end

#missed_branchesArray[Branch]

Returns:



125
126
127
# File 'lib/simplecov/source_file.rb', line 125

def missed_branches
  @missed_branches ||= branches.select(&:missed?)
end

#missed_linesArray[Line]

Returns:



55
56
57
# File 'lib/simplecov/source_file.rb', line 55

def missed_lines
  @missed_lines ||= lines.select(&:missed?)
end

#missed_methodsArray[Method]

Returns:



145
146
147
# File 'lib/simplecov/source_file.rb', line 145

def missed_methods
  @missed_methods ||= methods.select(&:missed?)
end

#never_linesArray[Line]

Returns:



59
60
61
# File 'lib/simplecov/source_file.rb', line 59

def never_lines
  @never_lines ||= lines.select(&:never?)
end

#no_branches?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/simplecov/source_file.rb', line 99

def no_branches?
  total_branches.empty?
end

#no_lines?Boolean

Every line the file has is irrelevant, a file with no lines at all included.

Returns:

  • (Boolean)


87
88
89
# File 'lib/simplecov/source_file.rb', line 87

def no_lines?
  lines.all?(&:never?)
end

#not_loaded?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/simplecov/source_file.rb', line 155

def not_loaded?
  !@loaded
end

#project_filenameString

Returns:

  • (String)


26
27
28
# File 'lib/simplecov/source_file.rb', line 26

def project_filename
  @filename.delete_prefix(SimpleCov.root).sub(%r{\A[/\\]}, "")
end

#relevant_linesInteger

Returns:

  • (Integer)


91
92
93
# File 'lib/simplecov/source_file.rb', line 91

def relevant_lines
  lines.size - never_lines.size - skipped_lines.size
end

#skipped_linesArray[Line]

Returns:



63
64
65
# File 'lib/simplecov/source_file.rb', line 63

def skipped_lines
  @skipped_lines ||= lines.select(&:skipped?)
end

#srcArray[String] Also known as: source

Read lazily to suppress reading unused source code.

Returns:

  • (Array[String])


31
32
33
# File 'lib/simplecov/source_file.rb', line 31

def src
  @src ||= SourceLoader.call(filename)
end

#total_branchesArray[Branch]

Returns:



109
110
111
# File 'lib/simplecov/source_file.rb', line 109

def total_branches
  @total_branches ||= covered_branches + missed_branches
end