| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- require 'test_helper'
- require 'docs'
- class DocsUrlTest < MiniTest::Spec
- URL = Docs::URL
- describe ".new" do
- it "works with no arguments" do
- assert_instance_of URL, URL.new
- end
- it "works with a Hash of components" do
- assert_equal '/path', URL.new(path: '/path').to_s
- end
- it "raises an error with an invalid component" do
- assert_raises(ArgumentError) { URL.new test: nil }
- end
- end
- describe ".parse" do
- it "returns a URL when given a string" do
- assert_instance_of URL, URL.parse('http://example.com')
- end
- it "returns the same URL when given a URL" do
- url = URL.new
- assert_same url, URL.parse(url)
- end
- end
- describe "#join" do
- it "joins urls" do
- url = URL.parse 'http://example.com/path/to/'
- assert_equal 'http://example.com/path/to/file', url.join('..', 'to/file').to_s
- end
- end
- describe "#merge!" do
- it "works with a Hash of components" do
- assert_equal '/path', URL.new.merge!(path: '/path').to_s
- end
- it "raises an error with an invalid component" do
- assert_raises(ArgumentError) { URL.new.merge! test: nil }
- end
- end
- describe "#origin" do
- it "returns 'http://example.com' when the URL is 'http://example.com/path?#'" do
- assert_equal 'http://example.com', URL.parse('http://example.com/path?#').origin
- end
- it "returns 'http://example.com' when the URL is 'HTTP://EXAMPLE.COM'" do
- assert_equal 'http://example.com', URL.parse('HTTP://EXAMPLE.COM').origin
- end
- it "returns 'http://example.com:8080' when the URL is 'http://example.com:8080'" do
- assert_equal 'http://example.com:8080', URL.parse('http://example.com:8080').origin
- end
- it "returns nil when the URL is 'example.com'" do
- assert_nil URL.parse('example.com').origin
- end
- it "returns nil when the URL is 'mailto:test@example.com'" do
- assert_nil URL.parse('mailto:test@example.com').origin
- end
- end
- describe "#normalized_path" do
- it "returns '/' when the URL is ''" do
- assert_equal '/', URL.parse('').normalized_path
- end
- it "returns '/path' when the URL is '/path'" do
- assert_equal '/path', URL.parse('/path').normalized_path
- end
- end
- describe "#subpath_to" do
- context "when the URL is '/'" do
- let :url do
- URL.parse '/'
- end
- it "returns nil with ''" do
- assert_nil url.subpath_to('')
- end
- it "returns '' with '/'" do
- assert_equal '', url.subpath_to('/')
- end
- it "returns 'path' with '/path'" do
- assert_equal 'path', url.subpath_to('/path')
- end
- it "returns nil with 'path'" do
- assert_nil url.subpath_to('path')
- end
- it "returns nil with 'http://example.com/'" do
- assert_nil url.subpath_to('http://example.com/')
- end
- end
- context "when the URL is '/path/to'" do
- let :url do
- URL.parse '/path/to'
- end
- it "returns nil with '/path/'" do
- assert_nil url.subpath_to('/path/')
- end
- it "returns '' with '/path/to'" do
- assert_equal '', url.subpath_to('/path/to')
- end
- it "returns '/file' with '/path/to/file'" do
- assert_equal '/file', url.subpath_to('/path/to/file')
- end
- it "returns nil with 'path/to/file'" do
- assert_nil url.subpath_to('path/to/file')
- end
- it "returns nil with '/path/tofile'" do
- assert_nil url.subpath_to('/path/tofile')
- end
- it "returns nil with '/PATH/to/file'" do
- assert_nil url.subpath_to('/PATH/to/file')
- end
- context "and :ignore_case is true" do
- it "returns '/file' with '/PATH/to/file'" do
- assert_equal '/file', url.subpath_to('/PATH/to/file', ignore_case: true)
- end
- end
- end
- context "when the URL is '/path/to/'" do
- let :url do
- URL.parse '/path/to/'
- end
- it "returns nil with '/path/to'" do
- assert_nil url.subpath_to('/path/to')
- end
- it "returns 'file' with '/path/to/file'" do
- assert_equal 'file', url.subpath_to('/path/to/file')
- end
- end
- context "when the URL is 'path/to'" do
- let :url do
- URL.parse 'path/to'
- end
- it "returns nil with '/path/to'" do
- assert_nil url.subpath_to('/path/to')
- end
- it "returns '/file' with 'path/to/file'" do
- assert_equal '/file', url.subpath_to('path/to/file')
- end
- end
- context "when the URL is 'http://example.com'" do
- let :url do
- URL.parse 'http://example.com'
- end
- it "returns '' with 'HTTP://EXAMPLE.COM'" do
- assert_equal '', url.subpath_to('HTTP://EXAMPLE.COM')
- end
- it "returns '/path' with 'http://example.com/path?query#frag'" do
- assert_equal '/path', url.subpath_to('http://example.com/path?query#frag')
- end
- it "returns nil with 'https://example.com/'" do
- assert_nil url.subpath_to('https://example.com/')
- end
- it "returns nil with 'http://not.example.com/'" do
- assert_nil url.subpath_to('http://not.example.com/')
- end
- end
- context "when the URL is 'http://example.com/'" do
- let :url do
- URL.parse 'http://example.com/'
- end
- it "returns nil with 'http://example.com'" do
- assert_equal nil, url.subpath_to('http://example.com')
- end
- end
- context "when the URL is 'http://example.com/path/to'" do
- let :url do
- URL.parse 'http://example.com/path/to'
- end
- it "returns '/file' with 'http://example.com/path/to/file'" do
- assert_equal '/file', url.subpath_to('http://example.com/path/to/file')
- end
- it "returns nil with 'http://example.com/path/tofile'" do
- assert_nil url.subpath_to('http://example.com/path/tofile')
- end
- it "returns nil with '/path/to/file'" do
- assert_nil url.subpath_to('/path/tofile')
- end
- end
- end
- describe "#subpath_from" do
- let :url do
- URL.new
- end
- before do
- any_instance_of URL do |instance|
- stub(instance).subpath_to
- end
- end
- it "returns the given url's #subpath_to to self" do
- any_instance_of URL do |instance|
- stub(instance).subpath_to(url, nil) { 'subpath' }
- end
- assert_equal 'subpath', url.subpath_from('url')
- end
- it "calls #subpath_to with the given options" do
- any_instance_of URL do |instance|
- stub(instance).subpath_to(url, 'options') { 'subpath' }
- end
- assert_equal 'subpath', url.subpath_from('url', 'options')
- end
- end
- describe "#contains?" do
- let :url do
- URL.new
- end
- before do
- stub(url).subpath_to
- end
- it "calls #subpath_to with the given url" do
- mock(url).subpath_to('url', nil)
- url.contains?('url')
- end
- it "calls #subpath_to with the given options" do
- mock(url).subpath_to('url', 'options')
- url.contains?('url', 'options')
- end
- it "returns true when the #subpath_to the given url is a string" do
- stub(url).subpath_to { '' }
- assert url.contains?('url')
- end
- it "returns true when the #subpath_to the given url is nil" do
- stub(url).subpath_to { nil }
- refute url.contains?('url')
- end
- end
- describe "#relative_path_to" do
- context "when the URL is '/'" do
- let :url do
- URL.parse '/'
- end
- it "returns '.' with '/'" do
- assert_equal '.', url.relative_path_to('/')
- end
- it "returns 'file' with '/file'" do
- assert_equal 'file', url.relative_path_to('/file')
- end
- it "returns 'file/' with '/file/'" do
- assert_equal 'file/', url.relative_path_to('/file/')
- end
- it "raises an error with 'file'" do
- assert_raises ArgumentError do
- url.relative_path_to 'file'
- end
- end
- end
- context "when the URL is '/path/to'" do
- let :url do
- URL.parse '/path/to'
- end
- it "returns '../' with '/'" do
- assert_equal '../', url.relative_path_to('/')
- end
- it "returns '../path' with '/path'" do
- assert_equal '../path', url.relative_path_to('/path')
- end
- it "returns '.' with '/path/'" do
- assert_equal '.', url.relative_path_to('/path/')
- end
- it "returns 'to' with '/path/to'" do
- assert_equal 'to', url.relative_path_to('/path/to')
- end
- it "returns '../PATH/to' with '/PATH/to'" do
- assert_equal '../PATH/to', url.relative_path_to('/PATH/to')
- end
- it "returns 'to/' with '/path/to/'" do
- assert_equal 'to/', url.relative_path_to('/path/to/')
- end
- it "returns 'to/file' with '/path/to/file'" do
- assert_equal 'to/file', url.relative_path_to('/path/to/file')
- end
- it "returns 'to/file/' with '/path/to/file/'" do
- assert_equal 'to/file/', url.relative_path_to('/path/to/file/')
- end
- end
- context "when the URL is '/path/to/'" do
- let :url do
- URL.parse '/path/to/'
- end
- it "returns '../../' with ''" do
- assert_equal '../../', url.relative_path_to('')
- end
- it "returns '../../' with '/'" do
- assert_equal '../../', url.relative_path_to('/')
- end
- it "returns '../../path' with '/path'" do
- assert_equal '../../path', url.relative_path_to('/path')
- end
- it "returns '../' with '/path/'" do
- assert_equal '../', url.relative_path_to('/path/')
- end
- it "returns '../to' with '/path/to'" do
- assert_equal '../to', url.relative_path_to('/path/to')
- end
- it "returns '.' with '/path/to/'" do
- assert_equal '.', url.relative_path_to('/path/to/')
- end
- it "returns 'file' with '/path/to/file'" do
- assert_equal 'file', url.relative_path_to('/path/to/file')
- end
- it "returns 'file/' with '/path/to/file/'" do
- assert_equal 'file/', url.relative_path_to('/path/to/file/')
- end
- end
- context "when the URL is 'http://example.com'" do
- let :url do
- URL.parse 'http://example.com'
- end
- it "returns '.' with 'http://example.com'" do
- assert_equal '.', url.relative_path_to('http://example.com')
- end
- it "returns '.' with 'http://example.com/'" do
- assert_equal '.', url.relative_path_to('http://example.com/')
- end
- it "returns 'file' with 'http://example.com/file?query#frag'" do
- assert_equal 'file', url.relative_path_to('http://example.com/file?query#frag')
- end
- it "returns 'some:file' with 'http://example.com/some:file'" do
- assert_equal 'some:file', url.relative_path_to('http://example.com/some:file')
- end
- it "returns 'some:file' with 'http://example.com/some:file?query#frag'" do
- assert_equal 'some:file', url.relative_path_to('http://example.com/some:file?query#frag')
- end
- it "returns nil with '/file'" do
- assert_nil url.relative_path_to('/file')
- end
- it "returns nil with 'file'" do
- assert_nil url.relative_path_to('file')
- end
- it "returns nil with 'https://example.com'" do
- assert_nil url.relative_path_to('https://example.com')
- end
- it "returns nil with 'http://not.example.com/file'" do
- assert_nil url.relative_path_to('http://not.example.com/file')
- end
- end
- context "when the URL is 'http://example.com/'" do
- let :url do
- URL.parse 'http://example.com/'
- end
- it "returns '.' with 'http://example.com'" do
- assert_equal '.', url.relative_path_to('http://example.com')
- end
- end
- end
- describe "#relative_path_from" do
- let :url do
- URL.new
- end
- it "returns the given url's #relative_path_to to self" do
- any_instance_of URL do |instance|
- stub(instance).relative_path_to(url) { 'path' }
- end
- assert_equal 'path', url.relative_path_from('url')
- end
- end
- end
|