diff --git a/lib/utopia/path.rb b/lib/utopia/path.rb index ec8bfdde..d9140a89 100644 --- a/lib/utopia/path.rb +++ b/lib/utopia/path.rb @@ -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 @@ -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 diff --git a/test/utopia/path.rb b/test/utopia/path.rb index 57a78b70..fc3561c6 100755 --- a/test/utopia/path.rb +++ b/test/utopia/path.rb @@ -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)} @@ -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 == "") @@ -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)} @@ -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 @@ -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"] diff --git a/test/utopia/path/matcher.rb b/test/utopia/path/matcher.rb index fdcc1953..4130aedf 100644 --- a/test/utopia/path/matcher.rb +++ b/test/utopia/path/matcher.rb @@ -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