From 4a2a393ed492fb5aa017a9c0280f3ca4eede3dc6 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Mon, 2 Dec 2013 18:28:48 +0000 Subject: [PATCH] Refactor :follow_links option --- lib/docs/filters/core/internal_urls.rb | 2 +- .../docs/filters/core/internal_urls_test.rb | 60 ++++++++++++------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/lib/docs/filters/core/internal_urls.rb b/lib/docs/filters/core/internal_urls.rb index e5f79c4a..3486c8a0 100644 --- a/lib/docs/filters/core/internal_urls.rb +++ b/lib/docs/filters/core/internal_urls.rb @@ -20,7 +20,7 @@ module Docs internal_urls << url.merge!(fragment: nil).to_s if internal_urls end - result[:internal_urls] = (internal_urls || []).to_a + result[:internal_urls] = internal_urls.to_a if internal_urls doc end diff --git a/test/lib/docs/filters/core/internal_urls_test.rb b/test/lib/docs/filters/core/internal_urls_test.rb index 76d95299..92ed5caa 100644 --- a/test/lib/docs/filters/core/internal_urls_test.rb +++ b/test/lib/docs/filters/core/internal_urls_test.rb @@ -133,28 +133,6 @@ class InternalUrlsFilterTest < MiniTest::Spec assert_includes internal_urls, 'http://example.com/' end end - - context "when context[:follow_links] is a block" do - before do - @body = link_to context[:url] - end - - it "calls the block with the filter instance" do - context[:follow_links] = ->(arg) { @arg = arg; nil } - filter.call - assert_equal filter, @arg - end - - it "is empty when the block returns false" do - context[:follow_links] = ->(_) { false } - assert_empty internal_urls - end - - it "is the default when the block returns true" do - context[:follow_links] = ->(_) { true } - refute_empty internal_urls - end - end end context "when the base url is 'example.com'" do @@ -325,7 +303,7 @@ class InternalUrlsFilterTest < MiniTest::Spec context[:skip_links] = ->(_) { false } end - it "set :internal_urls" do + it "sets :internal_urls" do assert internal_urls end @@ -335,4 +313,40 @@ class InternalUrlsFilterTest < MiniTest::Spec end end end + + context "context[:follow_links] is a block" do + before do + @body = link_to context[:url] + end + + it "calls the block with the filter instance" do + context[:follow_links] = ->(arg) { @arg = arg; nil } + filter.call + assert_equal filter, @arg + end + + context "and the block returns false" do + before do + context[:follow_links] = ->(_) { false } + end + + it "doesn't set :internal_urls" do + refute internal_urls + end + + it "replaces urls" do + refute_equal @body, filter_output_string + end + end + + context "and the block returns true" do + before do + context[:follow_links] = ->(_) { true } + end + + it "sets :internal_urls" do + assert internal_urls + end + end + end end