Class: Coverfield::Source::File

Inherits:
Object
  • Object
show all
Includes:
FileMethods
Defined in:
lib/coverfield/source/file.rb

Overview

Represents a ruby source file which consists of classes

Instance Attribute Summary collapse

Attributes included from FileMethods

#file_name

Instance Method Summary collapse

Methods included from FileMethods

#relative_file_name

Constructor Details

#initialize(config, file_name) ⇒ File

Constructor



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/coverfield/source/file.rb', line 14

public def initialize(config, file_name)
  @config = config
  @file_name = file_name
  @classes = []
  @nocov_ranges = []

  # Ignore empty files
  unless File.zero?(file_name)
    parse_code
    find_nocov_ranges
    find_classes
    find_test_file
    calculate_coverage
  end
rescue Exception => e
  raise RuntimeError, "Error while processing file #{file_name}: #{e.message}", e.backtrace
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



10
11
12
# File 'lib/coverfield/source/file.rb', line 10

def classes
  @classes
end

#test_fileObject (readonly)

Returns the value of attribute test_file.



10
11
12
# File 'lib/coverfield/source/file.rb', line 10

def test_file
  @test_file
end

Instance Method Details

#allowed_test_filesObject



79
80
81
82
83
84
# File 'lib/coverfield/source/file.rb', line 79

public def allowed_test_files
  template = (@config.spec_dir + relative_file_name).gsub('.rb', '_spec.rb')
  allowed_files = *template
  allowed_files << template.gsub(/^\/(lib|app)/, '')
  allowed_files
end

#nocov?(method_body_node) ⇒ Boolean

Tells if a method node is located within two :nocov: tags

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/coverfield/source/file.rb', line 40

public def nocov?(method_body_node)
  @nocov_ranges.each do |nocov_range|
    return true if nocov_range.includes?(method_body_node)
  end

  false
end