Parse source strings

This commit is contained in:
Michael Miller 2019-03-23 20:47:41 -06:00
parent 268db53bf8
commit 4f3ca20741
2 changed files with 31 additions and 0 deletions

View File

@ -54,4 +54,23 @@ describe Spectator::Source do
source.to_s.should match(/^(.+?)\:(\d+)$/)
end
end
describe "#parse" do
it "gets the absolute path" do
file = "foo.cr"
path = File.expand_path(file)
source = Spectator::Source.parse("#{file}:42")
source.file.should eq(path)
end
it "gets the relative path" do
source = Spectator::Source.parse("foo.cr:42")
source.path.should eq("foo.cr")
end
it "gets the line number" do
source = Spectator::Source.parse("foo.cr:42")
source.line.should eq(42)
end
end
end

View File

@ -11,6 +11,18 @@ module Spectator
def initialize(@file, @line)
end
# Parses a source from a string.
def self.parse(string)
# Make sure to handle multiple colons.
# If this ran on Windows, there's a possibility of a colon in the path.
# The last element should always be the line number.
parts = string.split(':')
path = parts[0...-1].join(':')
line = parts.last
file = File.expand_path(path)
self.new(file, line.to_i)
end
# The relative path to the file from the current directory.
# If the file isn't in the current directory or a sub-directory,
# then the absolute path is provided.