Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions lib/utopia/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,6 @@ def to_absolute
end
end

# Remove the first component when this path is relative.
# @returns [String | Nil] The removed component, or `nil` when the path is absolute.
def to_relative!
@components.shift if relative?
end

# Convert this object to a string.
# @returns [String] The resulting string.
def to_str
Expand Down Expand Up @@ -540,11 +534,4 @@ def adjust_index(index)
return index
end
end

# Coerce a value into a {Path}.
# @parameter path [Utopia::Path | String] The path.
# @returns [Path | Nil] The coerced path.
def self.Path(path)
Path.create(path)
end
end
79 changes: 79 additions & 0 deletions test/utopia/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
require "utopia/path"

describe Utopia::Path do
with ".unescape" do
it "decodes spaces and percent-encoded bytes" do
expect(subject.unescape("hello+world%21")).to be == "hello world!"
end
end

with ".split" do
it "coerces path-like values into components" do
path = subject["foo/bar"]
components = ["foo", "bar"]

expect(subject.split(path)).to be == components
expect(subject.split(components)).to be_equal(components)
expect(subject.split(:foo)).to be == [:foo]
end
end

with "#load / #dump" do
let(:string) {"foo/bar/baz"}
let(:path) {subject.load(string)}
Expand Down Expand Up @@ -75,6 +92,7 @@
with value: "" do
it "is empty path" do
expect(path).to be == []
expect(path).to be(:empty?)
expect(path).to be(:relative?)
expect(path).not.to be(:absolute?)
expect(path).to have_attributes(local_path: be == "")
Expand All @@ -100,6 +118,26 @@
end
end

with "#replace" do
it "copies the replacement components" do
path = subject["old/path"]
replacement = subject["new/path"]

path.replace(replacement)
replacement.components << "suffix"

expect(path).to be == subject["new/path"]
end
end

with "absolute and relative conversion" do
it "converts a relative path to an absolute path" do
path = subject["foo/bar"]

expect(path.to_absolute).to be == subject["/foo/bar"]
end
end

with "#first" do
let(:path) {subject.create(value)}

Expand Down Expand Up @@ -188,6 +226,19 @@

expect(root + "/foo/_bar").to be == Utopia::Path["/foo/_bar"]
end

it "appends arrays and other component values" do
path = subject["/foo"]

expect(path + ["bar"]).to be == subject["/foo/bar"]
expect(path + :bar).to be == subject["/foo/bar"]
end
end

with "#with_prefix" do
it "prepends a path" do
expect(subject["bar"].with_prefix("/foo")).to be == subject["/foo/bar"]
end
end

with "#to_url_path" do
Expand Down Expand Up @@ -281,6 +332,34 @@
expect(path.split("c")).to be == [Utopia::Path["/a/b"], Utopia::Path["d/e"]]
end

it "returns nil when the split point is missing" do
path = Utopia::Path["/a/b/c"]

expect(path.split("missing")).to be_nil
end

it "orders paths by their components" do
left = Utopia::Path["/a"]
right = Utopia::Path["/b"]

expect(left <=> right).to be == -1
end

with "indexed components" do
it "indexes ranges without exposing path markers" do
path = subject["/foo/bar/"]

expect(path[0..-1]).to be == ["foo", "bar"]
end

it "deletes components without exposing path markers" do
path = subject["/foo/bar/"]

expect(path.delete_at(-1)).to be == "bar"
expect(path).to be == subject["/foo/"]
end
end

it "shouldn't be able to modify frozen paths" do
path = Utopia::Path["dir/foo.html"]

Expand Down
12 changes: 12 additions & 0 deletions test/utopia/path/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@
expect(match_data[:id]).to be == 20
expect(match_data[:action]).to be == "edit"
end

it "coerces floating-point and custom components" do
component = Struct.new(:value)
path = Utopia::Path["1.5/example/raw"]
matcher = subject[ratio: Float, component: component, raw: nil]

match_data = matcher.match(path)

expect(match_data[:ratio]).to be == 1.5
expect(match_data[:component]).to have_attributes(value: be == "example")
expect(match_data[:raw]).to be == "raw"
end
end
Loading