Commit 98f7377a authored by Coraline Ehmke's avatar Coraline Ehmke

LineOfCode model.

parent 5be3427b
......@@ -12,6 +12,7 @@ require_relative "snuffle/formatters/csv"
require_relative "snuffle/formatters/html"
require_relative "snuffle/formatters/html_index"
require_relative "snuffle/formatters/text"
require_relative "snuffle/line_of_code"
require_relative "snuffle/node"
require_relative "snuffle/source_file"
require_relative "snuffle/summary"
......
module Snuffle
class LineOfCode
include PoroPlus
include Ephemeral::Base
attr_accessor :index, :range, :source
def self.containing(locs, start_index, end_index)
locs.inject([]) do |a, loc|
a << loc if loc.in_range?(start_index) || loc.in_range?(end_index)
a
end.compact
end
def in_range?(index)
self.range.include?(index)
end
end
end
\ No newline at end of file
......@@ -8,7 +8,7 @@ module Snuffle
include PoroPlus
attr_accessor :path_to_file, :source
attr_accessor :path_to_file, :source, :lines_of_code
def class_name
return @class_name if @class_name
......@@ -24,7 +24,15 @@ module Snuffle
end
def source
@source ||= File.read(self.path_to_file)
return @source if @source
end_pos = 0
self.lines_of_code = []
@source = File.readlines(self.path_to_file).each_with_index do |line, index|
start_pos = end_pos + 1
end_pos += line.size
self.lines_of_code << LineOfCode.new(index: index, range: (start_pos..end_pos))
line
end.join("\r\n")
end
def summary
......
require 'spec_helper'
require 'pry'
describe Snuffle::LineOfCode do
class SourceFileMock
include Ephemeral::Base
collects :lines_of_code, class_name: "Snuffle::LineOfCode"
attr_accessor :locs
end
let(:source_file) { SourceFileMock.new }
let(:loc_1) { Snuffle::LineOfCode.new(range: (0..24) ) }
let(:loc_2) { Snuffle::LineOfCode.new(range: (25..34)) }
let(:loc_3) { Snuffle::LineOfCode.new(range: (35..44)) }
before do
source_file.lines_of_code << loc_1
source_file.lines_of_code << loc_2
source_file.lines_of_code << loc_3
end
describe ".find" do
it "locates a range within a line" do
locs = Snuffle::LineOfCode.containing(source_file.lines_of_code, 26, 34)
expect(locs).to eq([loc_2])
end
it "locates a range spanning lines" do
locs = Snuffle::LineOfCode.containing(source_file.lines_of_code, 26, 42)
expect(locs).to eq([loc_2, loc_3])
end
end
end
......@@ -30,17 +30,17 @@ describe Snuffle::SourceFile do
let(:source_file) { Snuffle::SourceFile.new }
it "picks up a non-nested class name" do
source_file.stub(:source) { top_level }
source_file.source = top_level
expect(source_file.class_name).to eq("Foo")
end
it "picks up a namespaced class name" do
source_file.stub(:source) { namespaced_1 }
source_file.source = namespaced_1
expect(source_file.class_name).to eq("Foo::Bar")
end
it "picks up a class name inside nested modules" do
source_file.stub(:source) { namespaced_2 }
source_file.source = namespaced_2
expect(source_file.class_name).to eq("Foo::Bar::Baz")
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment