Rename ReferenceFilterSpecHelper to FilterSpecHelper

And make it more generalized for all filter specs.
This commit is contained in:
Robert Speicher 2015-06-02 13:27:53 -04:00
parent 442a0663da
commit 79c4e3899f
15 changed files with 56 additions and 61 deletions

View File

@ -2,11 +2,9 @@ require 'spec_helper'
module Gitlab::Markdown
describe AutolinkFilter do
let(:link) { 'http://about.gitlab.com/' }
include FilterSpecHelper
def filter(html, options = {})
described_class.call(html, options)
end
let(:link) { 'http://about.gitlab.com/' }
it 'does nothing when :autolink is false' do
exp = act = link

View File

@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe CommitRangeReferenceFilter do
include ReferenceFilterSpecHelper
include FilterSpecHelper
let(:project) { create(:project) }
let(:commit1) { project.commit }

View File

@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe CommitReferenceFilter do
include ReferenceFilterSpecHelper
include FilterSpecHelper
let(:project) { create(:project) }
let(:commit) { project.commit }

View File

@ -2,9 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe EmojiFilter do
def filter(html, contexts = {})
described_class.call(html, contexts)
end
include FilterSpecHelper
before do
ActionController::Base.asset_host = 'https://foo.com'

View File

@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe ExternalIssueReferenceFilter do
include ReferenceFilterSpecHelper
include FilterSpecHelper
def helper
IssuesHelper

View File

@ -2,9 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe ExternalLinkFilter do
def filter(html, options = {})
described_class.call(html, options)
end
include FilterSpecHelper
it 'ignores elements without an href attribute' do
exp = act = %q(<a id="ignored">Ignore Me</a>)

View File

@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe IssueReferenceFilter do
include ReferenceFilterSpecHelper
include FilterSpecHelper
def helper
IssuesHelper

View File

@ -3,7 +3,7 @@ require 'html/pipeline'
module Gitlab::Markdown
describe LabelReferenceFilter do
include ReferenceFilterSpecHelper
include FilterSpecHelper
let(:project) { create(:empty_project) }
let(:label) { create(:label, project: project) }

View File

@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe MergeRequestReferenceFilter do
include ReferenceFilterSpecHelper
include FilterSpecHelper
let(:project) { create(:project) }
let(:merge) { create(:merge_request, source_project: project) }

View File

@ -2,9 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe SanitizationFilter do
def filter(html, options = {})
described_class.call(html, options)
end
include FilterSpecHelper
describe 'default whitelist' do
it 'sanitizes tags that are not whitelisted' do

View File

@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe SnippetReferenceFilter do
include ReferenceFilterSpecHelper
include FilterSpecHelper
let(:project) { create(:empty_project) }
let(:snippet) { create(:project_snippet, project: project) }

View File

@ -4,9 +4,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe TableOfContentsFilter do
def filter(html, options = {})
described_class.call(html, options)
end
include FilterSpecHelper
def header(level, text)
"<h#{level}>#{text}</h#{level}>\n"

View File

@ -2,9 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe TaskListFilter do
def filter(html, options = {})
described_class.call(html, options)
end
include FilterSpecHelper
it 'does not apply `task-list` class to non-task lists' do
exp = act = %(<ul><li>Item</li></ul>)

View File

@ -2,7 +2,7 @@ require 'spec_helper'
module Gitlab::Markdown
describe UserReferenceFilter do
include ReferenceFilterSpecHelper
include FilterSpecHelper
let(:project) { create(:empty_project) }
let(:user) { create(:user) }

View File

@ -1,13 +1,38 @@
# Common methods and setup for Gitlab::Markdown reference filter specs
# Helper methods for Gitlab::Markdown filter specs
#
# Must be included into specs manually
module ReferenceFilterSpecHelper
module FilterSpecHelper
extend ActiveSupport::Concern
# Shortcut to Rails' auto-generated routes helpers, to avoid including the
# module
def urls
Rails.application.routes.url_helpers
# Perform `call` on the described class
#
# Automatically passes the current `project` value, if defined, to the context
# if none is provided.
#
# html - HTML String to pass to the filter's `call` method.
# contexts - Hash context for the filter. (default: {project: project})
#
# Returns a Nokogiri::XML::DocumentFragment
def filter(html, contexts = {})
if defined?(project)
contexts.reverse_merge!(project: project)
end
described_class.call(html, contexts)
end
# Run text through HTML::Pipeline with the current filter and return the
# result Hash
#
# body - String text to run through the pipeline
# contexts - Hash context for the filter. (default: {project: project})
#
# Returns the Hash
def pipeline_result(body, contexts = {})
contexts.reverse_merge!(project: project)
pipeline = HTML::Pipeline.new([described_class], contexts)
pipeline.call(body)
end
# Modify a String reference to make it invalid
@ -30,41 +55,23 @@ module ReferenceFilterSpecHelper
end
end
# Perform `call` on the described class
#
# Automatically passes the current `project` value to the context if none is
# provided.
#
# html - String text to pass to the filter's `call` method.
# contexts - Hash context for the filter. (default: {project: project})
#
# Returns the String text returned by the filter's `call` method.
def filter(html, contexts = {})
contexts.reverse_merge!(project: project)
described_class.call(html, contexts)
end
# Run text through HTML::Pipeline with the current filter and return the
# result Hash
#
# body - String text to run through the pipeline
# contexts - Hash context for the filter. (default: {project: project})
#
# Returns the Hash of the pipeline result
def pipeline_result(body, contexts = {})
contexts.reverse_merge!(project: project)
pipeline = HTML::Pipeline.new([described_class], contexts)
pipeline.call(body)
end
# Stub CrossProjectReference#user_can_reference_project? to return true for
# the current test
def allow_cross_reference!
allow_any_instance_of(described_class).
to receive(:user_can_reference_project?).and_return(true)
end
# Stub CrossProjectReference#user_can_reference_project? to return false for
# the current test
def disallow_cross_reference!
allow_any_instance_of(described_class).
to receive(:user_can_reference_project?).and_return(false)
end
# Shortcut to Rails' auto-generated routes helpers, to avoid including the
# module
def urls
Rails.application.routes.url_helpers
end
end