Class: SimpleCov::SourceFile::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov/source_file/line.rb,
sig/simplecov.rbs

Overview

A single source line with its coverage: nil (not relevant, e.g. a comment), 0 (missed), or >= 1 (times executed).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, line_number, coverage) ⇒ Line

Raises ArgumentError unless src is a String, line_number an Integer, and coverage an Integer or nil.

Parameters:

  • src (String)
  • line_number (Integer)
  • coverage (Integer, nil)


966
967
968
969
970
971
972
973
974
975
976
977
# File 'sig/simplecov.rbs', line 966

def initialize(src, line_number, coverage)
  raise ArgumentError, "Only String accepted for source" unless src.is_a?(String)
  raise ArgumentError, "Only Integer accepted for line_number" unless line_number.is_a?(Integer)
  unless coverage.is_a?(Integer) || coverage.nil?
    raise ArgumentError, "Only Integer and nil accepted for coverage"
  end

  @src         = src
  @line_number = line_number
  @coverage    = coverage
  @skipped     = false
end

Instance Attribute Details

#coverageInteger? (readonly)

The coverage data for this line: either nil (never), 0 (missed) or >=1 (times covered)

Returns:

  • (Integer, nil)


16
17
18
# File 'lib/simplecov/source_file/line.rb', line 16

def coverage
  @coverage
end

#line_numberInteger (readonly) Also known as: line, number

The line number in the source file. Aliased as :line, :number

Returns:

  • (Integer)


14
15
16
# File 'lib/simplecov/source_file/line.rb', line 14

def line_number
  @line_number
end

#skippedBoolean (readonly)

Whether this line was skipped

Returns:

  • (Boolean)


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

def skipped
  @skipped
end

#srcString (readonly) Also known as: source

The source code for this line. Aliased as :source

Returns:

  • (String)


12
13
14
# File 'lib/simplecov/source_file/line.rb', line 12

def src
  @src
end

Instance Method Details

#covered?Boolean

Returns true if this is a line that has been covered

Returns:

  • (Boolean)


44
45
46
# File 'lib/simplecov/source_file/line.rb', line 44

def covered?
  !never? && !skipped? && coverage.to_i.positive?
end

#missed?Boolean

Returns true if this is a line that should have been covered, but was not

Returns:

  • (Boolean)


39
40
41
# File 'lib/simplecov/source_file/line.rb', line 39

def missed?
  !never? && !skipped? && coverage.to_i.zero?
end

#never?Boolean

Returns true if this line is not relevant for coverage

Returns:

  • (Boolean)


49
50
51
# File 'lib/simplecov/source_file/line.rb', line 49

def never?
  !skipped? && coverage.nil?
end

#skipped!void

This method returns an undefined value.

Flags this line as skipped



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

def skipped!
  @skipped = true
end

#skipped?Boolean

Returns true if this line was skipped, false otherwise. Lines are skipped if they are wrapped with

:nocov: comment lines.

Returns:

  • (Boolean)


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

def skipped?
  skipped
end

#statusString?

"covered", "missed", "skipped", or "never" — useful e.g. as a css class.

Returns:

  • (String, nil)


66
67
68
69
70
71
72
73
74
# File 'lib/simplecov/source_file/line.rb', line 66

def status
  return "skipped" if skipped?
  return "never" if never?
  return "missed" if missed?

  # simplecov:disable — defensive: covered? is the only state left after the three above
  "covered" if covered?
  # simplecov:enable
end