History

Make Docs::URL#relative_path_to work with unqualified URLs

Thibaut committed 12 years ago

Commit d964fd41 parent 4df387d2 files · +83 −84Browse files

Unified · Split

2 files
  1. lib/docs/core/url.rb +1 −9
  2. test/lib/docs/core/url_test.rb +82 −75
lib/docs/core/url.rb+1−9
@@ -86,7 +86,7 @@ module Docs · 85 unmodified lines
8686 end
8787
8888 def relative_path_to(url)
89− assert_absolute self, url = self.class.parse(url)
89+ url = self.class.parse(url)
9090 return unless origin == url.origin
9191
9292 base_dir = Pathname.new(normalized_path)
@@ -107,13 +107,5 @@ module Docs · 14 unmodified lines
107107 def relative_path_from(url)
108108 self.class.parse(url).relative_path_to(self)
109109 end
110−
111− private
112−
113− def assert_absolute(*args)
114− args.each do |url|
115− raise ArgumentError, "Expected absolute URL, got: #{url.to_s}" if url.relative?
116− end
117− end
118110 end
119111 end
test/lib/docs/core/url_test.rb+82−75
@@ -277,154 +277,161 @@ class DocsUrlTest < MiniTest::Spec · 276 unmodified lines
277277 end
278278
279279 describe "#relative_path_to" do
280− context "when the URL is relative" do
280+ context "when the URL is '/'" do
281281 let :url do
282282 URL.parse '/'
283283 end
284284
285− it "raises an error with a relative url" do
286− assert_raises(ArgumentError) { url.relative_path_to '/' }
285+ it "returns '.' with '/'" do
286+ assert_equal '.', url.relative_path_to('/')
287287 end
288288
289− it "raises an error with an absolute url" do
290− assert_raises(ArgumentError) { url.relative_path_to 'http://example.com' }
289+ it "returns 'file' with '/file'" do
290+ assert_equal 'file', url.relative_path_to('/file')
291291 end
292− end
293292
294− context "when the URL is 'http://example.com'" do
295− let :url do
296− URL.parse 'http://example.com'
293+ it "returns 'file/' with '/file/'" do
294+ assert_equal 'file/', url.relative_path_to('/file/')
297295 end
298296
299− it "raises an error with a relative url" do
300− assert_raises(ArgumentError) { url.relative_path_to '/' }
297+ it "raises an error with 'file'" do
298+ assert_raises ArgumentError do
299+ url.relative_path_to 'file'
300+ end
301301 end
302+ end
302303
303− it "returns '.' with 'http://example.com'" do
304− assert_equal '.', url.relative_path_to('http://example.com')
304+ context "when the URL is '/path/to'" do
305+ let :url do
306+ URL.parse '/path/to'
305307 end
306308
307− it "returns '.' with 'http://example.com/'" do
308− assert_equal '.', url.relative_path_to('http://example.com/')
309+ it "returns '../' with '/'" do
310+ assert_equal '../', url.relative_path_to('/')
309311 end
310312
311− it "returns 'file' with 'http://example.com/file'" do
312− assert_equal 'file', url.relative_path_to('http://example.com/file')
313+ it "returns '../path' with '/path'" do
314+ assert_equal '../path', url.relative_path_to('/path')
313315 end
314316
315− it "returns 'file/' with 'http://example.com/file/'" do
316− assert_equal 'file/', url.relative_path_to('http://example.com/file/')
317+ it "returns '.' with '/path/'" do
318+ assert_equal '.', url.relative_path_to('/path/')
317319 end
318320
319− it "returns nil with 'https://example.com'" do
320− assert_nil url.relative_path_to('https://example.com')
321+ it "returns 'to' with '/path/to'" do
322+ assert_equal 'to', url.relative_path_to('/path/to')
321323 end
322324
323− it "returns nil with 'http://not.example.com/file'" do
324− assert_nil url.relative_path_to('http://not.example.com/file')
325+ it "returns '../PATH/to' with '/PATH/to'" do
326+ assert_equal '../PATH/to', url.relative_path_to('/PATH/to')
325327 end
326− end
327328
328− context "when the URL is 'http://example.com/'" do
329− let :url do
330− URL.parse 'http://example.com/'
329+ it "returns 'to/' with '/path/to/'" do
330+ assert_equal 'to/', url.relative_path_to('/path/to/')
331331 end
332332
333− it "returns '.' with 'http://example.com'" do
334− assert_equal '.', url.relative_path_to('http://example.com')
333+ it "returns 'to/file' with '/path/to/file'" do
334+ assert_equal 'to/file', url.relative_path_to('/path/to/file')
335335 end
336− end
337336
338− context "when the URL is 'http://example.com/path/to'" do
339− let :url do
340− URL.parse 'http://example.com/path/to'
337+ it "returns 'to/file/' with '/path/to/file/'" do
338+ assert_equal 'to/file/', url.relative_path_to('/path/to/file/')
341339 end
340+ end
342341
343− it "returns '../' with 'http://example.com'" do
344− assert_equal '../', url.relative_path_to('http://example.com')
342+ context "when the URL is '/path/to/'" do
343+ let :url do
344+ URL.parse '/path/to/'
345345 end
346346
347− it "returns '../' with 'http://example.com/'" do
348− assert_equal '../', url.relative_path_to('http://example.com/')
347+ it "returns '../../' with ''" do
348+ assert_equal '../../', url.relative_path_to('')
349349 end
350350
351− it "returns '../path' with 'http://example.com/path'" do
352− assert_equal '../path', url.relative_path_to('http://example.com/path')
351+ it "returns '../../' with '/'" do
352+ assert_equal '../../', url.relative_path_to('/')
353353 end
354354
355− it "returns '.' with 'http://example.com/path/'" do
356− assert_equal '.', url.relative_path_to('http://example.com/path/')
355+ it "returns '../../path' with '/path'" do
356+ assert_equal '../../path', url.relative_path_to('/path')
357357 end
358358
359− it "returns 'to' with 'http://example.com/path/to'" do
360− assert_equal 'to', url.relative_path_to('http://example.com/path/to')
359+ it "returns '../' with '/path/'" do
360+ assert_equal '../', url.relative_path_to('/path/')
361361 end
362362
363− it "returns '../PATH/to' with 'http://example.com/PATH/to'" do
364− assert_equal '../PATH/to', url.relative_path_to('http://example.com/PATH/to')
363+ it "returns '../to' with '/path/to'" do
364+ assert_equal '../to', url.relative_path_to('/path/to')
365365 end
366366
367− it "returns 'to/' with 'http://example.com/path/to/'" do
368− assert_equal 'to/', url.relative_path_to('http://example.com/path/to/')
367+ it "returns '.' with '/path/to/'" do
368+ assert_equal '.', url.relative_path_to('/path/to/')
369369 end
370370
371− it "returns 'to/file' with 'http://example.com/path/to/file'" do
372− assert_equal 'to/file', url.relative_path_to('http://example.com/path/to/file')
371+ it "returns 'file' with '/path/to/file'" do
372+ assert_equal 'file', url.relative_path_to('/path/to/file')
373373 end
374374
375− it "returns 'to/file/' with 'http://example.com/path/to/file/'" do
376− assert_equal 'to/file/', url.relative_path_to('http://example.com/path/to/file/')
375+ it "returns 'file/' with '/path/to/file/'" do
376+ assert_equal 'file/', url.relative_path_to('/path/to/file/')
377377 end
378378 end
379379
380− context "when the URL is 'http://example.com/path/to/'" do
380+ context "when the URL is 'http://example.com'" do
381381 let :url do
382− URL.parse 'http://example.com/path/to/'
382+ URL.parse 'http://example.com'
383+ end
384+
385+ it "returns '.' with 'http://example.com'" do
386+ assert_equal '.', url.relative_path_to('http://example.com')
383387 end
384388
385− it "returns '../../' with 'http://example.com'" do
386− assert_equal '../../', url.relative_path_to('http://example.com')
389+ it "returns '.' with 'http://example.com/'" do
390+ assert_equal '.', url.relative_path_to('http://example.com/')
387391 end
388392
389− it "returns '../../' with 'http://example.com/'" do
390− assert_equal '../../', url.relative_path_to('http://example.com/')
393+ it "returns 'file' with 'http://example.com/file?query#frag'" do
394+ assert_equal 'file', url.relative_path_to('http://example.com/file?query#frag')
391395 end
392396
393− it "returns '../../path' with 'http://example.com/path'" do
394− assert_equal '../../path', url.relative_path_to('http://example.com/path')
397+ it "returns nil with '/file'" do
398+ assert_nil url.relative_path_to('/file')
395399 end
396400
397− it "returns '../' with 'http://example.com/path/'" do
398− assert_equal '../', url.relative_path_to('http://example.com/path/')
401+ it "returns nil with 'file'" do
402+ assert_nil url.relative_path_to('file')
399403 end
400404
401− it "returns '../to' with 'http://example.com/path/to'" do
402− assert_equal '../to', url.relative_path_to('http://example.com/path/to')
405+ it "returns nil with 'https://example.com'" do
406+ assert_nil url.relative_path_to('https://example.com')
403407 end
404408
405− it "returns '.' with 'http://example.com/path/to/'" do
406− assert_equal '.', url.relative_path_to('http://example.com/path/to/')
409+ it "returns nil with 'http://not.example.com/file'" do
410+ assert_nil url.relative_path_to('http://not.example.com/file')
407411 end
412+ end
408413
409− it "returns 'file' with 'http://example.com/path/to/file'" do
410− assert_equal 'file', url.relative_path_to('http://example.com/path/to/file')
414+ context "when the URL is 'http://example.com/'" do
415+ let :url do
416+ URL.parse 'http://example.com/'
411417 end
412418
413− it "returns 'file/' with 'http://example.com/path/to/file/'" do
414− assert_equal 'file/', url.relative_path_to('http://example.com/path/to/file/')
419+ it "returns '.' with 'http://example.com'" do
420+ assert_equal '.', url.relative_path_to('http://example.com')
415421 end
416422 end
417423 end
418424
419425 describe "#relative_path_from" do
420− context "when the URL is 'http://example.com/path/file'" do
421− let :url do
422− URL.parse 'http://example.com/path/file'
423− end
426+ let :url do
427+ URL.new
428+ end
424429
425− it "returns 'path/file' with 'http://example.com/path'" do
426− assert_equal 'path/file', url.relative_path_from('http://example.com/path')
430+ it "returns the given url's #relative_path_to to self" do
431+ any_instance_of URL do |instance|
432+ stub(instance).relative_path_to(url) { 'path' }
427433 end
434+ assert_equal 'path', url.relative_path_from('url')
428435 end
429436 end
430437 end