mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add LocationRange struct
This commit is contained in:
parent
05e924da70
commit
675d385cbf
2 changed files with 107 additions and 0 deletions
67
spec/spectator/core/location_range_spec.cr
Normal file
67
spec/spectator/core/location_range_spec.cr
Normal file
|
@ -0,0 +1,67 @@
|
|||
require "../../spec_helper"
|
||||
|
||||
describe Spectator::Core::LocationRange do
|
||||
describe "#includes?" do
|
||||
it "returns true if the location is in the range" do
|
||||
range = Spectator::Core::LocationRange.new("foo", 10, 20)
|
||||
location = Spectator::Core::Location.new("foo", 15)
|
||||
range.includes?(location).should be_true
|
||||
end
|
||||
|
||||
it "returns false if the location is not in the range" do
|
||||
range = Spectator::Core::LocationRange.new("foo", 10, 20)
|
||||
location = Spectator::Core::Location.new("bar", 30)
|
||||
range.includes?(location).should be_false
|
||||
end
|
||||
|
||||
it "returns true if the files are the same and the location line is omitted" do
|
||||
range = Spectator::Core::LocationRange.new("foo", 10, 20)
|
||||
location = Spectator::Core::Location.new("foo")
|
||||
range.includes?(location).should be_true
|
||||
end
|
||||
|
||||
it "returns false if the files are different" do
|
||||
range = Spectator::Core::LocationRange.new("foo", 10, 20)
|
||||
location = Spectator::Core::Location.new("bar", 15)
|
||||
range.includes?(location).should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe "#===" do
|
||||
it "returns true if the location is in the range" do
|
||||
range = Spectator::Core::LocationRange.new("foo", 10, 20)
|
||||
location = Spectator::Core::Location.new("foo", 15)
|
||||
(range === location).should be_true
|
||||
end
|
||||
|
||||
it "returns false if the location is not in the range" do
|
||||
range = Spectator::Core::LocationRange.new("foo", 10, 20)
|
||||
location = Spectator::Core::Location.new("bar", 30)
|
||||
(range === location).should be_false
|
||||
end
|
||||
|
||||
it "returns true if the files are the same and the location line is omitted" do
|
||||
range = Spectator::Core::LocationRange.new("foo", 10, 20)
|
||||
location = Spectator::Core::Location.new("foo")
|
||||
(range === location).should be_true
|
||||
end
|
||||
|
||||
it "returns false if the files are different" do
|
||||
range = Spectator::Core::LocationRange.new("foo", 10, 20)
|
||||
location = Spectator::Core::Location.new("bar", 15)
|
||||
(range === location).should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe "#to_s" do
|
||||
it "returns a string representation" do
|
||||
range = Spectator::Core::LocationRange.new("foo", 10, 20)
|
||||
range.to_s.should eq("foo:10-20")
|
||||
end
|
||||
|
||||
it "returns a string representation with no end line" do
|
||||
range = Spectator::Core::LocationRange.new("foo", 10)
|
||||
range.to_s.should eq("foo:10")
|
||||
end
|
||||
end
|
||||
end
|
40
src/spectator/core/location_range.cr
Normal file
40
src/spectator/core/location_range.cr
Normal file
|
@ -0,0 +1,40 @@
|
|||
module Spectator::Core
|
||||
# Information about a range in the source code.
|
||||
struct LocationRange
|
||||
# Name of the source file.
|
||||
getter file : String
|
||||
|
||||
# Starting line number in the source file.
|
||||
getter line : Int32
|
||||
|
||||
# Ending line number in the source file.
|
||||
getter end_line : Int32
|
||||
|
||||
# Creates a new location range.
|
||||
def initialize(@file, @line, @end_line)
|
||||
end
|
||||
|
||||
# Creates a new location range with the same start and end line.
|
||||
def initialize(@file, @line)
|
||||
@end_line = @line
|
||||
end
|
||||
|
||||
# Checks if the location is in the range.
|
||||
def includes?(location : Location)
|
||||
return false if location.file != @file
|
||||
return true unless (line = location.line?)
|
||||
@line <= line && line <= @end_line
|
||||
end
|
||||
|
||||
# Constructs a string representation of the location range.
|
||||
def to_s(io : IO) : Nil
|
||||
io << @file << ':' << @line
|
||||
io << '-' << @end_line if @end_line != @line
|
||||
end
|
||||
|
||||
# Returns true if the location is in the range.
|
||||
def ===(other : Location)
|
||||
includes?(other)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue