Skip to content

Commit

Permalink
Add Item base class
Browse files Browse the repository at this point in the history
  • Loading branch information
icy-arctic-fox committed Jun 19, 2024
1 parent 914daed commit afa1b34
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
32 changes: 32 additions & 0 deletions spec/spectator/core/item_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "../../spec_helper"

# TODO: Use a mock.
private class TestItem < Spectator::Core::Item
end

describe Spectator::Core::Item do
describe "#description" do
it "is the string passed to #initialize" do
item = TestItem.new("foo")
item.description.should eq("foo")
end

it "is nil if the item does not have a description" do
item = TestItem.new
item.description?.should be_nil
end

it "is the result of #inspect when passing a non-string to #initialize" do
item = TestItem.new(:xyz)
item.description.should eq(":xyz")
end
end

describe "#location" do
it "is the value passed to #initialize" do
location = Spectator::Core::LocationRange.new("foo", 10)
item = TestItem.new("foo", location)
item.location.should eq(location)
end
end
end
9 changes: 8 additions & 1 deletion src/spectator/core/item.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require "./location_range"

module Spectator::Core
# Base class for all items in the test suite.
abstract class Item
# The description of the item.
# This may be nil if the item does not have a description.
getter! description : String

# The location of the item in the source code.
Expand All @@ -10,7 +13,11 @@ module Spectator::Core
getter! location : LocationRange

# Creates a new item.
def initialize(@description)
# The *description* can be a string, nil, or any other object.
# When it is a string or nil, it will be stored as-is.
# Any other types will be converted to a string by calling `#inspect` on it.
def initialize(description = nil, @location = nil)
@description = description.is_a?(String) ? description : description.try &.inspect
end
end
end

0 comments on commit afa1b34

Please sign in to comment.