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
86 end86 end
87 87
88 def relative_path_to(url)88 def relative_path_to(url)
89 assert_absolute self, url = self.class.parse(url)89 url = self.class.parse(url)
90 return unless origin == url.origin90 return unless origin == url.origin
91 91
92 base_dir = Pathname.new(normalized_path)92 base_dir = Pathname.new(normalized_path)
@@ -107,13 +107,5 @@ module Docs · 14 unmodified lines
107 def relative_path_from(url)107 def relative_path_from(url)
108 self.class.parse(url).relative_path_to(self)108 self.class.parse(url).relative_path_to(self)
109 end109 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
118 end110 end
119end111end
test/lib/docs/core/url_test.rb+82−75
@@ -277,154 +277,161 @@ class DocsUrlTest < MiniTest::Spec · 276 unmodified lines
277 end277 end
278 278
279 describe "#relative_path_to" do279 describe "#relative_path_to" do
280 context "when the URL is relative" do280 context "when the URL is '/'" do
281 let :url do281 let :url do
282 URL.parse '/'282 URL.parse '/'
283 end283 end
284 284
285 it "raises an error with a relative url" do285 it "returns '.' with '/'" do
286 assert_raises(ArgumentError) { url.relative_path_to '/' }286 assert_equal '.', url.relative_path_to('/')
287 end287 end
288 288
289 it "raises an error with an absolute url" do289 it "returns 'file' with '/file'" do
290 assert_raises(ArgumentError) { url.relative_path_to 'http://example.com' }290 assert_equal 'file', url.relative_path_to('/file')
291 end291 end
292 end
293 292
294 context "when the URL is 'http://example.com'" do293 it "returns 'file/' with '/file/'" do
295 let :url do294 assert_equal 'file/', url.relative_path_to('/file/')
296 URL.parse 'http://example.com'
297 end295 end
298 296
299 it "raises an error with a relative url" do297 it "raises an error with 'file'" do
300 assert_raises(ArgumentError) { url.relative_path_to '/' }298 assert_raises ArgumentError do
299 url.relative_path_to 'file'
300 end
301 end301 end
302 end
302 303
303 it "returns '.' with 'http://example.com'" do304 context "when the URL is '/path/to'" do
304 assert_equal '.', url.relative_path_to('http://example.com')305 let :url do
306 URL.parse '/path/to'
305 end307 end
306 308
307 it "returns '.' with 'http://example.com/'" do309 it "returns '../' with '/'" do
308 assert_equal '.', url.relative_path_to('http://example.com/')310 assert_equal '../', url.relative_path_to('/')
309 end311 end
310 312
311 it "returns 'file' with 'http://example.com/file'" do313 it "returns '../path' with '/path'" do
312 assert_equal 'file', url.relative_path_to('http://example.com/file')314 assert_equal '../path', url.relative_path_to('/path')
313 end315 end
314 316
315 it "returns 'file/' with 'http://example.com/file/'" do317 it "returns '.' with '/path/'" do
316 assert_equal 'file/', url.relative_path_to('http://example.com/file/')318 assert_equal '.', url.relative_path_to('/path/')
317 end319 end
318 320
319 it "returns nil with 'https://example.com'" do321 it "returns 'to' with '/path/to'" do
320 assert_nil url.relative_path_to('https://example.com')322 assert_equal 'to', url.relative_path_to('/path/to')
321 end323 end
322 324
323 it "returns nil with 'http://not.example.com/file'" do325 it "returns '../PATH/to' with '/PATH/to'" do
324 assert_nil url.relative_path_to('http://not.example.com/file')326 assert_equal '../PATH/to', url.relative_path_to('/PATH/to')
325 end327 end
326 end
327 328
328 context "when the URL is 'http://example.com/'" do329 it "returns 'to/' with '/path/to/'" do
329 let :url do330 assert_equal 'to/', url.relative_path_to('/path/to/')
330 URL.parse 'http://example.com/'
331 end331 end
332 332
333 it "returns '.' with 'http://example.com'" do333 it "returns 'to/file' with '/path/to/file'" do
334 assert_equal '.', url.relative_path_to('http://example.com')334 assert_equal 'to/file', url.relative_path_to('/path/to/file')
335 end335 end
336 end
337 336
338 context "when the URL is 'http://example.com/path/to'" do337 it "returns 'to/file/' with '/path/to/file/'" do
339 let :url do338 assert_equal 'to/file/', url.relative_path_to('/path/to/file/')
340 URL.parse 'http://example.com/path/to'
341 end339 end
340 end
342 341
343 it "returns '../' with 'http://example.com'" do342 context "when the URL is '/path/to/'" do
344 assert_equal '../', url.relative_path_to('http://example.com')343 let :url do
344 URL.parse '/path/to/'
345 end345 end
346 346
347 it "returns '../' with 'http://example.com/'" do347 it "returns '../../' with ''" do
348 assert_equal '../', url.relative_path_to('http://example.com/')348 assert_equal '../../', url.relative_path_to('')
349 end349 end
350 350
351 it "returns '../path' with 'http://example.com/path'" do351 it "returns '../../' with '/'" do
352 assert_equal '../path', url.relative_path_to('http://example.com/path')352 assert_equal '../../', url.relative_path_to('/')
353 end353 end
354 354
355 it "returns '.' with 'http://example.com/path/'" do355 it "returns '../../path' with '/path'" do
356 assert_equal '.', url.relative_path_to('http://example.com/path/')356 assert_equal '../../path', url.relative_path_to('/path')
357 end357 end
358 358
359 it "returns 'to' with 'http://example.com/path/to'" do359 it "returns '../' with '/path/'" do
360 assert_equal 'to', url.relative_path_to('http://example.com/path/to')360 assert_equal '../', url.relative_path_to('/path/')
361 end361 end
362 362
363 it "returns '../PATH/to' with 'http://example.com/PATH/to'" do363 it "returns '../to' with '/path/to'" do
364 assert_equal '../PATH/to', url.relative_path_to('http://example.com/PATH/to')364 assert_equal '../to', url.relative_path_to('/path/to')
365 end365 end
366 366
367 it "returns 'to/' with 'http://example.com/path/to/'" do367 it "returns '.' with '/path/to/'" do
368 assert_equal 'to/', url.relative_path_to('http://example.com/path/to/')368 assert_equal '.', url.relative_path_to('/path/to/')
369 end369 end
370 370
371 it "returns 'to/file' with 'http://example.com/path/to/file'" do371 it "returns 'file' with '/path/to/file'" do
372 assert_equal 'to/file', url.relative_path_to('http://example.com/path/to/file')372 assert_equal 'file', url.relative_path_to('/path/to/file')
373 end373 end
374 374
375 it "returns 'to/file/' with 'http://example.com/path/to/file/'" do375 it "returns 'file/' with '/path/to/file/'" do
376 assert_equal 'to/file/', url.relative_path_to('http://example.com/path/to/file/')376 assert_equal 'file/', url.relative_path_to('/path/to/file/')
377 end377 end
378 end378 end
379 379
380 context "when the URL is 'http://example.com/path/to/'" do380 context "when the URL is 'http://example.com'" do
381 let :url do381 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')
383 end387 end
384 388
385 it "returns '../../' with 'http://example.com'" do389 it "returns '.' with 'http://example.com/'" do
386 assert_equal '../../', url.relative_path_to('http://example.com')390 assert_equal '.', url.relative_path_to('http://example.com/')
387 end391 end
388 392
389 it "returns '../../' with 'http://example.com/'" do393 it "returns 'file' with 'http://example.com/file?query#frag'" do
390 assert_equal '../../', url.relative_path_to('http://example.com/')394 assert_equal 'file', url.relative_path_to('http://example.com/file?query#frag')
391 end395 end
392 396
393 it "returns '../../path' with 'http://example.com/path'" do397 it "returns nil with '/file'" do
394 assert_equal '../../path', url.relative_path_to('http://example.com/path')398 assert_nil url.relative_path_to('/file')
395 end399 end
396 400
397 it "returns '../' with 'http://example.com/path/'" do401 it "returns nil with 'file'" do
398 assert_equal '../', url.relative_path_to('http://example.com/path/')402 assert_nil url.relative_path_to('file')
399 end403 end
400 404
401 it "returns '../to' with 'http://example.com/path/to'" do405 it "returns nil with 'https://example.com'" do
402 assert_equal '../to', url.relative_path_to('http://example.com/path/to')406 assert_nil url.relative_path_to('https://example.com')
403 end407 end
404 408
405 it "returns '.' with 'http://example.com/path/to/'" do409 it "returns nil with 'http://not.example.com/file'" do
406 assert_equal '.', url.relative_path_to('http://example.com/path/to/')410 assert_nil url.relative_path_to('http://not.example.com/file')
407 end411 end
412 end
408 413
409 it "returns 'file' with 'http://example.com/path/to/file'" do414 context "when the URL is 'http://example.com/'" do
410 assert_equal 'file', url.relative_path_to('http://example.com/path/to/file')415 let :url do
416 URL.parse 'http://example.com/'
411 end417 end
412 418
413 it "returns 'file/' with 'http://example.com/path/to/file/'" do419 it "returns '.' with 'http://example.com'" do
414 assert_equal 'file/', url.relative_path_to('http://example.com/path/to/file/')420 assert_equal '.', url.relative_path_to('http://example.com')
415 end421 end
416 end422 end
417 end423 end
418 424
419 describe "#relative_path_from" do425 describe "#relative_path_from" do
420 context "when the URL is 'http://example.com/path/file'" do426 let :url do
421 let :url do427 URL.new
422 URL.parse 'http://example.com/path/file'428 end
423 end
424 429
425 it "returns 'path/file' with 'http://example.com/path'" do430 it "returns the given url's #relative_path_to to self" do
426 assert_equal 'path/file', url.relative_path_from('http://example.com/path')431 any_instance_of URL do |instance|
432 stub(instance).relative_path_to(url) { 'path' }
427 end433 end
434 assert_equal 'path', url.relative_path_from('url')
428 end435 end
429 end436 end
430end437end