| @@ -277,154 +277,161 @@ class DocsUrlTest < MiniTest::Spec · 276 unmodified lines |
| 277 | end | 277 | end |
| 278 | | 278 | |
| 279 | describe "#relative_path_to" do | 279 | describe "#relative_path_to" do |
| 280 | context "when the URL is relative" do | 280 | context "when the URL is '/'" do |
| 281 | let :url do | 281 | let :url do |
| 282 | URL.parse '/' | 282 | URL.parse '/' |
| 283 | end | 283 | end |
| 284 | | 284 | |
| 285 | it "raises an error with a relative url" do | 285 | it "returns '.' with '/'" do |
| 286 | assert_raises(ArgumentError) { url.relative_path_to '/' } | 286 | assert_equal '.', url.relative_path_to('/') |
| 287 | end | 287 | end |
| 288 | | 288 | |
| 289 | it "raises an error with an absolute url" do | 289 | 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 | end | 291 | end |
| 292 | end | | |
| 293 | | 292 | |
| 294 | context "when the URL is 'http://example.com'" do | 293 | it "returns 'file/' with '/file/'" do |
| 295 | let :url do | 294 | assert_equal 'file/', url.relative_path_to('/file/') |
| 296 | URL.parse 'http://example.com' | | |
| 297 | end | 295 | end |
| 298 | | 296 | |
| 299 | it "raises an error with a relative url" do | 297 | 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 | end | 301 | end |
| | 302 | end |
| 302 | | 303 | |
| 303 | it "returns '.' with 'http://example.com'" do | 304 | 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 | end | 307 | end |
| 306 | | 308 | |
| 307 | it "returns '.' with 'http://example.com/'" do | 309 | it "returns '../' with '/'" do |
| 308 | assert_equal '.', url.relative_path_to('http://example.com/') | 310 | assert_equal '../', url.relative_path_to('/') |
| 309 | end | 311 | end |
| 310 | | 312 | |
| 311 | it "returns 'file' with 'http://example.com/file'" do | 313 | 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 | end | 315 | end |
| 314 | | 316 | |
| 315 | it "returns 'file/' with 'http://example.com/file/'" do | 317 | 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 | end | 319 | end |
| 318 | | 320 | |
| 319 | it "returns nil with 'https://example.com'" do | 321 | 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 | end | 323 | end |
| 322 | | 324 | |
| 323 | it "returns nil with 'http://not.example.com/file'" do | 325 | 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 | end | 327 | end |
| 326 | end | | |
| 327 | | 328 | |
| 328 | context "when the URL is 'http://example.com/'" do | 329 | it "returns 'to/' with '/path/to/'" do |
| 329 | let :url do | 330 | assert_equal 'to/', url.relative_path_to('/path/to/') |
| 330 | URL.parse 'http://example.com/' | | |
| 331 | end | 331 | end |
| 332 | | 332 | |
| 333 | it "returns '.' with 'http://example.com'" do | 333 | 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 | end | 335 | end |
| 336 | end | | |
| 337 | | 336 | |
| 338 | context "when the URL is 'http://example.com/path/to'" do | 337 | it "returns 'to/file/' with '/path/to/file/'" do |
| 339 | let :url do | 338 | assert_equal 'to/file/', url.relative_path_to('/path/to/file/') |
| 340 | URL.parse 'http://example.com/path/to' | | |
| 341 | end | 339 | end |
| | 340 | end |
| 342 | | 341 | |
| 343 | it "returns '../' with 'http://example.com'" do | 342 | 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 | end | 345 | end |
| 346 | | 346 | |
| 347 | it "returns '../' with 'http://example.com/'" do | 347 | it "returns '../../' with ''" do |
| 348 | assert_equal '../', url.relative_path_to('http://example.com/') | 348 | assert_equal '../../', url.relative_path_to('') |
| 349 | end | 349 | end |
| 350 | | 350 | |
| 351 | it "returns '../path' with 'http://example.com/path'" do | 351 | it "returns '../../' with '/'" do |
| 352 | assert_equal '../path', url.relative_path_to('http://example.com/path') | 352 | assert_equal '../../', url.relative_path_to('/') |
| 353 | end | 353 | end |
| 354 | | 354 | |
| 355 | it "returns '.' with 'http://example.com/path/'" do | 355 | 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 | end | 357 | end |
| 358 | | 358 | |
| 359 | it "returns 'to' with 'http://example.com/path/to'" do | 359 | 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 | end | 361 | end |
| 362 | | 362 | |
| 363 | it "returns '../PATH/to' with 'http://example.com/PATH/to'" do | 363 | 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 | end | 365 | end |
| 366 | | 366 | |
| 367 | it "returns 'to/' with 'http://example.com/path/to/'" do | 367 | 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 | end | 369 | end |
| 370 | | 370 | |
| 371 | it "returns 'to/file' with 'http://example.com/path/to/file'" do | 371 | 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 | end | 373 | end |
| 374 | | 374 | |
| 375 | it "returns 'to/file/' with 'http://example.com/path/to/file/'" do | 375 | 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 | end | 377 | end |
| 378 | end | 378 | end |
| 379 | | 379 | |
| 380 | context "when the URL is 'http://example.com/path/to/'" do | 380 | context "when the URL is 'http://example.com'" do |
| 381 | let :url do | 381 | 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 | end | 387 | end |
| 384 | | 388 | |
| 385 | it "returns '../../' with 'http://example.com'" do | 389 | 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 | end | 391 | end |
| 388 | | 392 | |
| 389 | it "returns '../../' with 'http://example.com/'" do | 393 | 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 | end | 395 | end |
| 392 | | 396 | |
| 393 | it "returns '../../path' with 'http://example.com/path'" do | 397 | 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 | end | 399 | end |
| 396 | | 400 | |
| 397 | it "returns '../' with 'http://example.com/path/'" do | 401 | 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 | end | 403 | end |
| 400 | | 404 | |
| 401 | it "returns '../to' with 'http://example.com/path/to'" do | 405 | 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 | end | 407 | end |
| 404 | | 408 | |
| 405 | it "returns '.' with 'http://example.com/path/to/'" do | 409 | 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 | end | 411 | end |
| | 412 | end |
| 408 | | 413 | |
| 409 | it "returns 'file' with 'http://example.com/path/to/file'" do | 414 | 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 | end | 417 | end |
| 412 | | 418 | |
| 413 | it "returns 'file/' with 'http://example.com/path/to/file/'" do | 419 | 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 | end | 421 | end |
| 416 | end | 422 | end |
| 417 | end | 423 | end |
| 418 | | 424 | |
| 419 | describe "#relative_path_from" do | 425 | describe "#relative_path_from" do |
| 420 | context "when the URL is 'http://example.com/path/file'" do | 426 | let :url do |
| 421 | let :url do | 427 | 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'" do | 430 | 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 | end | 433 | end |
| | 434 | assert_equal 'path', url.relative_path_from('url') |
| 428 | end | 435 | end |
| 429 | end | 436 | end |
| 430 | end | 437 | end |