| @@ -21,10 +21,6 @@ class DocsScraperTest < MiniTest::Spec · 20 unmodified lines |
| 21 | 21 | Struct.new(:body, :url).new |
| 22 | 22 | end |
| 23 | 23 | |
| 24 | | − let :processed_response do |
| 25 | | − Hash.new |
| 26 | | − end |
| 27 | | − |
| 28 | 24 | describe ".inherited" do |
| 29 | 25 | let :subclass do |
| 30 | 26 | Class.new Scraper |
| @@ -88,33 +84,40 @@ class DocsScraperTest < MiniTest::Spec · 57 unmodified lines |
| 88 | 84 | end |
| 89 | 85 | |
| 90 | 86 | describe "#root_url" do |
| 87 | + let :root_url do |
| 88 | + scraper.root_url |
| 89 | + end |
| 90 | + |
| 91 | 91 | context "when #root_path? is false" do |
| 92 | 92 | before do |
| 93 | 93 | stub(scraper).root_path? { false } |
| 94 | 94 | end |
| 95 | 95 | |
| 96 | | − it "returns a Docs::URL" do |
| 97 | | − assert_instance_of Docs::URL, scraper.root_url |
| 96 | + it "returns a memoized Docs::URL" do |
| 97 | + assert_instance_of Docs::URL, root_url |
| 98 | + assert_same root_url, scraper.root_url |
| 98 | 99 | end |
| 99 | 100 | |
| 100 | | − it "returns the normalized base url" do |
| 101 | + it "returns the normalized .base_url" do |
| 101 | 102 | stub(Scraper).base_url { 'http://example.com' } |
| 102 | | − assert_equal 'http://example.com/', scraper.root_url.to_s |
| 103 | + assert_equal 'http://example.com/', root_url.to_s |
| 103 | 104 | end |
| 104 | 105 | end |
| 105 | 106 | |
| 106 | | − context "when .root_path isn't blank" do |
| 107 | + context "when #root_path? is true" do |
| 107 | 108 | before do |
| 108 | 109 | stub(scraper).root_path? { true } |
| 109 | 110 | end |
| 110 | 111 | |
| 111 | | − it "returns a Docs::URL" do |
| 112 | | − assert_instance_of Docs::URL, scraper.root_url |
| 112 | + it "returns a memoized Docs::URL" do |
| 113 | + assert_instance_of Docs::URL, root_url |
| 114 | + assert_same root_url, scraper.root_url |
| 113 | 115 | end |
| 114 | 116 | |
| 115 | | − it "returns base url + root path" do |
| 117 | + it "returns .base_url + .root_path" do |
| 116 | 118 | stub(Scraper).base_url { 'http://example.com/path/' } |
| 117 | | − assert_equal 'http://example.com/path/root', scraper.root_url.to_s |
| 119 | + stub(Scraper).root_path { '/root' } |
| 120 | + assert_equal 'http://example.com/path/root', root_url.to_s |
| 118 | 121 | end |
| 119 | 122 | end |
| 120 | 123 | end |
| @@ -201,6 +204,10 @@ class DocsScraperTest < MiniTest::Spec · 80 unmodified lines |
| 201 | 204 | Proc.new {} |
| 202 | 205 | end |
| 203 | 206 | |
| 207 | + let :processed_response do |
| 208 | + Hash.new |
| 209 | + end |
| 210 | + |
| 204 | 211 | it "requests the #initial_urls" do |
| 205 | 212 | mock(scraper).request_all(scraper.initial_urls) |
| 206 | 213 | scraper.build_pages(&block) |
| @@ -224,11 +231,10 @@ class DocsScraperTest < MiniTest::Spec · 17 unmodified lines |
| 224 | 231 | |
| 225 | 232 | it "yields the processed response" do |
| 226 | 233 | scraper.build_pages { |arg| @arg = arg } |
| 227 | | − assert @arg |
| 228 | | − assert_equal processed_response, @arg |
| 234 | + assert_same processed_response, @arg |
| 229 | 235 | end |
| 230 | 236 | |
| 231 | | − context "when response[:internal_urls] is empty" do |
| 237 | + context "when :internal_urls is empty" do |
| 232 | 238 | before do |
| 233 | 239 | processed_response[:internal_urls] = [] |
| 234 | 240 | end |
| @@ -244,22 +250,21 @@ class DocsScraperTest < MiniTest::Spec · 9 unmodified lines |
| 244 | 250 | end |
| 245 | 251 | end |
| 246 | 252 | |
| 247 | | − context "when response[:internal_urls] isn't empty" do |
| 248 | | − let :urls do |
| 253 | + context "when :internal_urls isn't empty" do |
| 254 | + let :internal_urls do |
| 249 | 255 | ['Url'] |
| 250 | 256 | end |
| 251 | 257 | |
| 252 | 258 | before do |
| 253 | | − processed_response[:internal_urls] = urls |
| 259 | + processed_response[:internal_urls] = internal_urls |
| 254 | 260 | end |
| 255 | 261 | |
| 256 | 262 | it "requests the urls" do |
| 257 | 263 | scraper.build_pages(&block) |
| 258 | | − assert_equal urls, @next_urls |
| 264 | + assert_equal internal_urls, @next_urls |
| 259 | 265 | end |
| 260 | 266 | |
| 261 | 267 | it "doesn't request the same url twice irrespective of case" do |
| 262 | | − stub(Scraper).root_path { 'PATH' } |
| 263 | 268 | processed_response[:internal_urls] = scraper.initial_urls.map(&:swapcase) |
| 264 | 269 | scraper.build_pages(&block) |
| 265 | 270 | assert_empty @next_urls |
| @@ -269,7 +274,7 @@ class DocsScraperTest < MiniTest::Spec · 3 unmodified lines |
| 269 | 274 | scraper.build_pages(&block) |
| 270 | 275 | assert scraper.last_instrumentation |
| 271 | 276 | assert_equal 'queued.scraper', scraper.last_instrumentation[:event] |
| 272 | | − assert_equal urls, scraper.last_instrumentation[:payload][:urls] |
| 277 | + assert_equal internal_urls, scraper.last_instrumentation[:payload][:urls] |
| 273 | 278 | end |
| 274 | 279 | end |
| 275 | 280 | end |
| @@ -286,34 +291,59 @@ class DocsScraperTest < MiniTest::Spec · 10 unmodified lines |
| 286 | 291 | |
| 287 | 292 | describe "#options" do |
| 288 | 293 | let :options do |
| 294 | + Hash.new |
| 295 | + end |
| 296 | + |
| 297 | + let :result do |
| 289 | 298 | scraper.options |
| 290 | 299 | end |
| 291 | 300 | |
| 301 | + before do |
| 302 | + stub(Scraper).options { options } |
| 303 | + end |
| 304 | + |
| 292 | 305 | it "returns a frozen, memoized Hash" do |
| 293 | | − assert_instance_of Hash, options |
| 294 | | − assert options.frozen? |
| 295 | | − assert_same options, scraper.options |
| 306 | + assert_instance_of Hash, result |
| 307 | + assert result.frozen? |
| 308 | + assert_same result, scraper.options |
| 296 | 309 | end |
| 297 | 310 | |
| 298 | 311 | it "includes .options" do |
| 299 | | − stub(Scraper).options { { test: true } } |
| 300 | | − assert options[:test] |
| 312 | + options[:test] = true |
| 313 | + assert result[:test] |
| 301 | 314 | end |
| 302 | 315 | |
| 303 | 316 | it "includes #base_url" do |
| 304 | | − assert_equal scraper.base_url, options[:base_url] |
| 317 | + assert_equal scraper.base_url, result[:base_url] |
| 305 | 318 | end |
| 306 | 319 | |
| 307 | 320 | it "includes #root_url" do |
| 308 | | − assert_equal scraper.root_url, options[:root_url] |
| 321 | + assert_equal scraper.root_url, result[:root_url] |
| 322 | + end |
| 323 | + |
| 324 | + it "includes #root_path" do |
| 325 | + assert_equal '/root', result[:root_path] |
| 326 | + end |
| 327 | + |
| 328 | + it "includes #initial_paths" do |
| 329 | + assert_equal ['/initial'], result[:initial_paths] |
| 330 | + end |
| 331 | + |
| 332 | + it "adds #initial_paths to :only when it is an array" do |
| 333 | + options[:only] = ['/path'] |
| 334 | + assert_includes result[:only], options[:only].first |
| 335 | + assert_includes result[:only], '/initial' |
| 309 | 336 | end |
| 310 | 337 | |
| 311 | | − it "includes .root_path" do |
| 312 | | − assert_equal '/root', options[:root_path] |
| 338 | + it "adds #initial_paths to :only when :only_patterns is an array" do |
| 339 | + options[:only_patterns] = [] |
| 340 | + assert_includes result[:only], '/initial' |
| 313 | 341 | end |
| 314 | 342 | |
| 315 | | − it "includes .initial_paths" do |
| 316 | | − assert_equal ['/initial'], options[:initial_paths] |
| 343 | + it "doesn't modify :only in place" do |
| 344 | + options[:only] = [] |
| 345 | + result |
| 346 | + assert_empty options[:only] |
| 317 | 347 | end |
| 318 | 348 | |
| 319 | 349 | context "when #root_path? is false" do |
| @@ -322,42 +352,27 @@ class DocsScraperTest < MiniTest::Spec · 2 unmodified lines |
| 322 | 352 | end |
| 323 | 353 | |
| 324 | 354 | it "doesn't modify :skip" do |
| 325 | | − assert_nil options[:skip] |
| 355 | + options[:skip] = [] |
| 356 | + assert_equal options[:skip], result[:skip] |
| 326 | 357 | end |
| 327 | 358 | |
| 328 | | − context "and :only is an array" do |
| 329 | | − before do |
| 330 | | − stub(Scraper).options { { only: ['/path'] } } |
| 331 | | − end |
| 332 | | − |
| 333 | | − it "adds ['', '/']" do |
| 334 | | − assert_includes options[:only], '/path' |
| 335 | | − assert_includes options[:only], '' |
| 336 | | − assert_includes options[:only], '/' |
| 337 | | − end |
| 338 | | − |
| 339 | | − it "adds .initial_paths" do |
| 340 | | − assert_includes options[:only], '/initial' |
| 341 | | − end |
| 342 | | − |
| 343 | | − it "doesn't modify the array in place" do |
| 344 | | − assert_equal ['/path'], Scraper.options[:only] |
| 345 | | − end |
| 359 | + it "adds '' and '/' to :only when it is an array" do |
| 360 | + options[:only] = ['/path'] |
| 361 | + assert_includes result[:only], options[:only].first |
| 362 | + assert_includes result[:only], '' |
| 363 | + assert_includes result[:only], '/' |
| 346 | 364 | end |
| 347 | 365 | |
| 348 | | − context "and :only_patterns is an array" do |
| 349 | | − before do |
| 350 | | − stub(Scraper).options { { only_patterns: [] } } |
| 351 | | − end |
| 352 | | − |
| 353 | | − it "adds ['', '/'] to :only" do |
| 354 | | − assert_includes options[:only], '' |
| 355 | | − assert_includes options[:only], '/' |
| 356 | | − end |
| 366 | + it "adds '' and '/' to :only when :only_patterns is an array" do |
| 367 | + options[:only_patterns] = [] |
| 368 | + assert_includes result[:only], '' |
| 369 | + assert_includes result[:only], '/' |
| 370 | + end |
| 357 | 371 | |
| 358 | | − it "adds .initial_paths to :only" do |
| 359 | | − assert_includes options[:only], '/initial' |
| 360 | | − end |
| 372 | + it "doesn't modify :only in place" do |
| 373 | + options[:only] = [] |
| 374 | + result |
| 375 | + assert_empty options[:only] |
| 361 | 376 | end |
| 362 | 377 | end |
| 363 | 378 | |
| @@ -366,40 +381,33 @@ class DocsScraperTest < MiniTest::Spec · 2 unmodified lines |
| 366 | 381 | stub(scraper).root_path? { true } |
| 367 | 382 | end |
| 368 | 383 | |
| 369 | | − context "and :skip is nil" do |
| 370 | | − it "assigns it ['', '/']" do |
| 371 | | − assert_equal ['', '/'], options[:skip] |
| 372 | | − end |
| 384 | + it "adds '' and '/' to :skip when it is nil" do |
| 385 | + assert_includes result[:skip], '' |
| 386 | + assert_includes result[:skip], '/' |
| 373 | 387 | end |
| 374 | 388 | |
| 375 | | − context "and :skip is an array" do |
| 376 | | − before do |
| 377 | | − stub(Scraper).options { { skip: ['/path'] } } |
| 378 | | − end |
| 379 | | − |
| 380 | | − it "adds ['', '/']" do |
| 381 | | − assert_includes options[:skip], '/path' |
| 382 | | − assert_includes options[:skip], '' |
| 383 | | − assert_includes options[:skip], '/' |
| 384 | | − end |
| 389 | + it "adds '' and '/' to :skip when it is an array" do |
| 390 | + options[:skip] = ['/path'] |
| 391 | + assert_includes result[:skip], options[:skip].first |
| 392 | + assert_includes result[:skip], '' |
| 393 | + assert_includes result[:skip], '/' |
| 394 | + end |
| 385 | 395 | |
| 386 | | − it "doesn't modify the array in place" do |
| 387 | | − assert_equal ['/path'], Scraper.options[:skip] |
| 388 | | − end |
| 396 | + it "doesn't modify :skip in place" do |
| 397 | + options[:skip] = [] |
| 398 | + result |
| 399 | + assert_empty options[:skip] |
| 389 | 400 | end |
| 390 | 401 | |
| 391 | | − context "and :only is an array" do |
| 392 | | − it "adds .root_path" do |
| 393 | | − stub(Scraper).options { { only: [] } } |
| 394 | | − assert_includes options[:only], '/root' |
| 395 | | − end |
| 402 | + it "adds #root_path to :only when it is an array" do |
| 403 | + options[:only] = ['/path'] |
| 404 | + assert_includes result[:only], options[:only].first |
| 405 | + assert_includes result[:only], '/root' |
| 396 | 406 | end |
| 397 | 407 | |
| 398 | | − context "and :only_patterns is an array" do |
| 399 | | − it "adds .root_path to :only" do |
| 400 | | − stub(Scraper).options { { only_patterns: [] } } |
| 401 | | − assert_includes options[:only], '/root' |
| 402 | | − end |
| 408 | + it "adds #root_path to :only when :only_patterns is an array" do |
| 409 | + options[:only_patterns] = [] |
| 410 | + assert_includes result[:only], '/root' |
| 403 | 411 | end |
| 404 | 412 | end |
| 405 | 413 | end |
| @@ -484,18 +492,22 @@ class DocsScraperTest < MiniTest::Spec · 78 unmodified lines |
| 484 | 492 | end |
| 485 | 493 | |
| 486 | 494 | describe "#pipeline" do |
| 487 | | − it "returns an HTML::Pipeline with .filters" do |
| 488 | | − stub(Scraper).filters { [1] } |
| 489 | | − assert_instance_of ::HTML::Pipeline, scraper.pipeline |
| 490 | | − assert_equal Scraper.filters, scraper.pipeline.filters |
| 495 | + let :pipeline do |
| 496 | + scraper.pipeline |
| 491 | 497 | end |
| 492 | 498 | |
| 493 | | − it "is memoized" do |
| 494 | | − assert_same scraper.pipeline, scraper.pipeline |
| 499 | + it "returns a memoized HTML::Pipeline" do |
| 500 | + assert_instance_of ::HTML::Pipeline, pipeline |
| 501 | + assert_same pipeline, scraper.pipeline |
| 502 | + end |
| 503 | + |
| 504 | + it "returns a pipeline with the filters stored in .filters" do |
| 505 | + stub(Scraper).filters { [1] } |
| 506 | + assert_equal Scraper.filters, pipeline.filters |
| 495 | 507 | end |
| 496 | 508 | |
| 497 | | − it "assigns Docs as the pipeline's instrumentation service" do |
| 498 | | − assert_equal Docs, scraper.pipeline.instrumentation_service |
| 509 | + it "returns a pipeline with Docs as instrumentation service" do |
| 510 | + assert_equal Docs, pipeline.instrumentation_service |
| 499 | 511 | end |
| 500 | 512 | end |
| 501 | 513 | end |