class Coverfield::Source::File
Represents a ruby source file which consists of classes
Attributes
classes[R]
test_file[R]
Public Class Methods
new(config, file_name)
click to toggle source
Constructor
# File lib/coverfield/source/file.rb, line 14 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
Public Instance Methods
allowed_test_files()
click to toggle source
# File lib/coverfield/source/file.rb, line 79 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)
click to toggle source
Tells if a method node is located within two :nocov: tags
# File lib/coverfield/source/file.rb, line 40 def nocov?(method_body_node) @nocov_ranges.each do |nocov_range| return true if nocov_range.includes?(method_body_node) end false end
Private Instance Methods
calculate_coverage()
click to toggle source
Iterates over all classes and calculates their test coverage
# File lib/coverfield/source/file.rb, line 34 def calculate_coverage classes.each { |cls| cls.calculate_coverage } end
find_classes()
click to toggle source
Finds all class definitions within that file
# File lib/coverfield/source/file.rb, line 50 def find_classes @processed_source.ast.each_node(:class) do |node| name, superclass, body = *node _scope, const_name, value = *name module_name = node.parent_module_name # If the module_name is 'Object', the notation is not Coverfield::Source::TestFile but nested modules/class if module_name == 'Object' nothing, scope_name, nothing = *_scope module_name = scope_name.to_s end # Create a new class object and push that to the @classes array @classes << Coverfield::Source::Class.new(const_name, module_name, node, self) end end
find_nocov_ranges()
click to toggle source
Collects all :nocov: tag ranges in the file
# File lib/coverfield/source/file.rb, line 88 def find_nocov_ranges first = true line = 0 @processed_source.comments.each do |comment| if comment.type == :inline && comment.text.strip =~ /^#\s*:nocov:/ if first line = comment.loc.expression.first_line else @nocov_ranges << Coverfield::Source::NocovRange.new(line, comment.loc.expression.first_line) end first = !first end end end
find_test_file()
click to toggle source
Find the spec file for that class
# File lib/coverfield/source/file.rb, line 69 def find_test_file allowed_test_files.each do |file| @test_file = Coverfield::Source::TestFile.new(@config, file) # break return false if test_file.file_exists? end end