mirror of
https://gitea.invidious.io/iv-org/shard-radix.git
synced 2024-08-15 00:43:21 +00:00
9003075ec7
After Crystal 0.15, compiler will require declare the types used by instance variables on classes. This require changes to the usage of `Radix::Tree` by introducing the type of payload elements it will handle: # Will only support symbols as payload tree = Radix::Tree(Symbol).new tree.add "/", :root # Error: cannot add node with anything other than Symbol tree.add "/meaning-of-life", 42 The changes ensure future compatibility with Crystal and also enforces a more declarative usage of `Radix::Tree`. If necessary, you can combine multiple types to ensure a tree can contain all the wide range of payloads you need: tree = Radix::Tree.new(Foo | Bar | Symbol).new tree.add "/", :root tree.add "/foo", foo_instance This change includes: - Tree, Node and Result has been updated to require types. - Node is capable of have optional payload (from defined type). - Documentation has been updated to reflect this change.
76 lines
1.8 KiB
Crystal
76 lines
1.8 KiB
Crystal
require "../spec_helper"
|
|
|
|
module Radix
|
|
describe Result do
|
|
describe "#found?" do
|
|
context "a new instance" do
|
|
it "returns false when no payload is associated" do
|
|
result = Result(Nil).new
|
|
result.found?.should be_false
|
|
end
|
|
end
|
|
|
|
context "with a payload" do
|
|
it "returns true" do
|
|
node = Node(Symbol).new("/", :root)
|
|
result = Result(Symbol).new
|
|
result.use node
|
|
|
|
result.found?.should be_true
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "#key" do
|
|
context "a new instance" do
|
|
it "returns an empty key" do
|
|
result = Result(Nil).new
|
|
result.key.should eq("")
|
|
end
|
|
end
|
|
|
|
context "given one used node" do
|
|
it "returns the node key" do
|
|
node = Node(Symbol).new("/", :root)
|
|
result = Result(Symbol).new
|
|
result.use node
|
|
|
|
result.key.should eq("/")
|
|
end
|
|
end
|
|
|
|
context "using multiple nodes" do
|
|
it "combines the node keys" do
|
|
node1 = Node(Symbol).new("/", :root)
|
|
node2 = Node(Symbol).new("about", :about)
|
|
result = Result(Symbol).new
|
|
result.use node1
|
|
result.use node2
|
|
|
|
result.key.should eq("/about")
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "#use" do
|
|
it "uses the node payload" do
|
|
node = Node(Symbol).new("/", :root)
|
|
result = Result(Symbol).new
|
|
result.payload?.should be_falsey
|
|
|
|
result.use node
|
|
result.payload?.should be_truthy
|
|
result.payload.should eq(node.payload)
|
|
end
|
|
|
|
it "allow not to assign payload" do
|
|
node = Node(Symbol).new("/", :root)
|
|
result = Result(Symbol).new
|
|
result.payload?.should be_falsey
|
|
|
|
result.use node, payload: false
|
|
result.payload?.should be_falsey
|
|
end
|
|
end
|
|
end
|
|
end
|