Add Location struct

This commit is contained in:
Michael Miller 2024-06-11 19:47:07 -06:00
parent 38f61dc74e
commit 50701ed131
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
2 changed files with 75 additions and 0 deletions

View file

@ -0,0 +1,41 @@
require "../../spec_helper"
describe Spectator::Core::Location do
describe ".parse" do
it "parses a location" do
location = Spectator::Core::Location.parse("foo:10")
location.file.should eq("foo")
location.line?.should eq(10)
end
it "parses a location with no line number" do
location = Spectator::Core::Location.parse("foo")
location.file.should eq("foo")
location.line?.should be_nil
end
it "parses a Windows path with a colon" do
location = Spectator::Core::Location.parse("C:\\foo:10")
location.file.should eq("C:\\foo")
location.line?.should eq(10)
end
it "parses a Windows path with no line number" do
location = Spectator::Core::Location.parse("C:\\foo")
location.file.should eq("C:\\foo")
location.line?.should be_nil
end
end
describe "#to_s" do
it "constructs a string representation" do
location = Spectator::Core::Location.new("foo", 10)
location.to_s.should eq("foo:10")
end
it "constructs a string representation with no line number" do
location = Spectator::Core::Location.new("foo")
location.to_s.should eq("foo")
end
end
end

View file

@ -0,0 +1,34 @@
module Spectator::Core
# Information about a location in the source code.
struct Location
# Name of the source file.
getter file : String
# Line number in the source file.
getter! line : Int32
# Creates a new location.
def initialize(@file, @line = nil)
end
# Parses a string into a location.
def self.parse(string : String) : self
# Avoid issues on Windows with colons in filenames (drive letters).
colon_index = string.rindex(':')
return new(string) unless colon_index
file = string[0...colon_index]
line = string[colon_index + 1, string.size - colon_index - 1].to_i?
# Use the whole string if the line number can't be parsed.
# This likely means it is a Windows path with a colon and no line number.
line ? new(file, line) : new(string)
end
# Constructs a string representation of the location.
def to_s(io : IO) : Nil
io << @file
if line = @line
io << ':' << line
end
end
end
end