Redesign gfm helper specs

Should now be much clearer about what each spec is actually testing.
For example, instead of testing stuff like link classes and titles in
every single call, we only test those things once, in their own specs.
This commit is contained in:
Robert Speicher 2012-09-02 02:13:13 -04:00
parent 40d619107f
commit ef24576fc2
1 changed files with 200 additions and 144 deletions

View File

@ -1,232 +1,288 @@
require "spec_helper" require "spec_helper"
describe GitlabMarkdownHelper do describe GitlabMarkdownHelper do
let!(:project) { create(:project) }
let(:user) { create(:user, name: 'gfm') }
let(:commit) { CommitDecorator.decorate(project.commit) }
let(:issue) { create(:issue, project: project) }
let(:merge_request) { create(:merge_request, project: project) }
let(:snippet) { create(:snippet, project: project) }
let(:member) { project.users_projects.where(user_id: user).first }
before do before do
@project = Factory(:project) # Helper expects a @project instance variable
@commit = @project.repo.commits.first.parents.first @project = project
@commit = CommitDecorator.decorate(Commit.new(@commit))
@other_project = Factory :project, path: "OtherPath", code: "OtherCode"
@fake_user = Factory :user, name: "fred"
end end
describe "#gfm" do describe "#gfm" do
it "should return text if @project is not set" do it "should return unaltered text if project is nil" do
@project = nil actual = "Testing references: ##{issue.id}"
gfm("foo").should == "foo" gfm(actual).should_not == actual
@project = nil
gfm(actual).should == actual
end
it "should not alter non-references" do
actual = expected = "_Please_ *stop* 'helping' and all the other b*$#%' you do."
gfm(actual).should == expected
end
it "should not touch HTML entities" do
actual = expected = "We'll accept good pull requests."
gfm(actual).should == expected
end
it "should forward HTML options to links" do
gfm("Fixed in #{commit.id}", class: "foo").should have_selector("a.gfm.foo")
end end
describe "referencing a commit" do describe "referencing a commit" do
let(:expected) { project_commit_path(project, commit) }
it "should link using a full id" do it "should link using a full id" do
gfm("Reverts changes from #{@commit.id}").should == "Reverts changes from #{link_to @commit.id, project_commit_path(@project, id: @commit.id), title: "Commit: #{@commit.author_name} - #{@commit.title}", class: "gfm gfm-commit "}" actual = "Reverts #{commit.id}"
gfm(actual).should match(expected)
end end
it "should link using a short id" do it "should link using a short id" do
gfm("Backported from #{@commit.id[0, 6]}").should == "Backported from #{link_to @commit.id[0, 6], project_commit_path(@project, id: @commit.id), title: "Commit: #{@commit.author_name} - #{@commit.title}", class: "gfm gfm-commit "}" actual = "Backported from #{commit.short_id(6)}"
gfm(actual).should match(expected)
end end
it "should link with adjecent text" do it "should link with adjacent text" do
gfm("Reverted (see #{@commit.id})").should == "Reverted (see #{link_to @commit.id, project_commit_path(@project, id: @commit.id), title: "Commit: #{@commit.author_name} - #{@commit.title}", class: "gfm gfm-commit "})" actual = "Reverted (see #{commit.id})"
gfm(actual).should match(expected)
end
it "should keep whitespace intact" do
actual = "Changes #{commit.id} dramatically"
expected = /Changes <a.+>#{commit.id}<\/a> dramatically/
gfm(actual).should match(expected)
end end
it "should not link with an invalid id" do it "should not link with an invalid id" do
gfm("What happened in 12345678?").should == "What happened in 12345678?" actual = expected = "What happened in #{commit.id.reverse}"
gfm(actual).should == expected
end
it "should include a title attribute" do
actual = "Reverts #{commit.id}"
gfm(actual).should match(/title="#{commit.link_title}"/)
end
it "should include standard gfm classes" do
actual = "Reverts #{commit.id}"
gfm(actual).should match(/class="\s?gfm gfm-commit\s?"/)
end end
end end
describe "referencing a team member" do describe "referencing a team member" do
it "should link using a simple name" do let(:actual) { "@#{user.name} you are right." }
user = Factory :user, name: "barry" let(:expected) { project_team_member_path(project, member) }
@project.users << user
member = @project.users_projects.where(user_id: user).first
gfm("@#{user.name} you are right").should == "#{link_to "@#{user.name}", project_team_member_path(@project, member), class: "gfm gfm-team_member "} you are right" before do
project.users << user
end
it "should link using a simple name" do
gfm(actual).should match(expected)
end end
it "should link using a name with dots" do it "should link using a name with dots" do
user = Factory :user, name: "alphA.Beta" user.update_attributes(name: "alphA.Beta")
@project.users << user gfm(actual).should match(expected)
member = @project.users_projects.where(user_id: user).first
gfm("@#{user.name} you are right").should == "#{link_to "@#{user.name}", project_team_member_path(@project, member), class: "gfm gfm-team_member "} you are right"
end end
it "should link using name with underscores" do it "should link using name with underscores" do
user = Factory :user, name: "ping_pong_king" user.update_attributes(name: "ping_pong_king")
@project.users << user gfm(actual).should match(expected)
member = @project.users_projects.where(user_id: user).first
gfm("@#{user.name} you are right").should == "#{link_to "@#{user.name}", project_team_member_path(@project, member), class: "gfm gfm-team_member "} you are right"
end end
it "should link with adjecent text" do it "should link with adjacent text" do
user = Factory.create(:user, name: "ace") actual = "Mail the admin (@gfm)"
@project.users << user gfm(actual).should match(expected)
member = @project.users_projects.where(user_id: user).first
gfm("Mail the Admin (@#{user.name})").should == "Mail the Admin (#{link_to "@#{user.name}", project_team_member_path(@project, member), class: "gfm gfm-team_member "})"
end end
it "should add styles" do it "should keep whitespace intact" do
user = Factory :user, name: "barry" actual = "Yes, @#{user.name} is right."
@project.users << user expected = /Yes, <a.+>@#{user.name}<\/a> is right/
gfm("@#{user.name} you are right").should have_selector(".gfm.gfm-team_member") gfm(actual).should match(expected)
end end
it "should not link using a bogus name" do it "should not link with an invalid id" do
gfm("What hapened to @foo?").should == "What hapened to @foo?" actual = expected = "@#{user.name.reverse} you are right."
gfm(actual).should == expected
end
it "should include standard gfm classes" do
gfm(actual).should match(/class="\s?gfm gfm-team_member\s?"/)
end
end
# Shared examples for referencing an object
#
# Expects the following attributes to be available in the example group:
#
# - object - The object itself
# - reference - The object reference string (e.g., #1234, $1234, !1234)
#
# Currently limited to Snippets, Issues and MergeRequests
shared_examples 'referenced object' do
let(:actual) { "Reference to #{reference}" }
let(:expected) { polymorphic_path([project, object]) }
it "should link using a valid id" do
gfm(actual).should match(expected)
end
it "should link with adjacent text" do
# Wrap the reference in parenthesis
gfm(actual.gsub(reference, "(#{reference})")).should match(expected)
# Append some text to the end of the reference
gfm(actual.gsub(reference, "#{reference}, right?")).should match(expected)
end
it "should keep whitespace intact" do
actual = "Referenced #{reference} already."
expected = /Referenced <a.+>[^\s]+<\/a> already/
gfm(actual).should match(expected)
end
it "should not link with an invalid id" do
# Modify the reference string so it's still parsed, but is invalid
reference.gsub!(/^(.)(\d+)$/, '\1' + ('\2' * 2))
gfm(actual).should == actual
end
it "should include a title attribute" do
title = "#{object.class.to_s.titlecase}: #{object.title}"
gfm(actual).should match(/title="#{title}"/)
end
it "should include standard gfm classes" do
css = object.class.to_s.underscore
gfm(actual).should match(/class="\s?gfm gfm-#{css}\s?"/)
end end
end end
describe "referencing an issue" do describe "referencing an issue" do
before do let(:object) { issue }
@issue = Factory :issue, assignee: @fake_user, author: @fake_user, project: @project let(:reference) { "##{issue.id}" }
@invalid_issue = Factory :issue, assignee: @fake_user, author: @fake_user, project: @other_project
end
it "should link using a correct id" do include_examples 'referenced object'
gfm("Fixes ##{@issue.id}").should == "Fixes #{link_to "##{@issue.id}", project_issue_path(@project, @issue), title: "Issue: #{@issue.title}", class: "gfm gfm-issue "}"
end
it "should link with adjecent text" do
gfm("This has already been discussed (see ##{@issue.id})").should == "This has already been discussed (see #{link_to "##{@issue.id}", project_issue_path(@project, @issue), title: "Issue: #{@issue.title}", class: "gfm gfm-issue "})"
end
it "should add styles" do
gfm("Fixes ##{@issue.id}").should have_selector(".gfm.gfm-issue")
end
it "should not link using an invalid id" do
gfm("##{@invalid_issue.id} has been marked duplicate of this").should == "##{@invalid_issue.id} has been marked duplicate of this"
end
end end
describe "referencing a merge request" do describe "referencing a merge request" do
before do let(:object) { merge_request }
@merge_request = Factory :merge_request, assignee: @fake_user, author: @fake_user, project: @project let(:reference) { "!#{merge_request.id}" }
@invalid_merge_request = Factory :merge_request, assignee: @fake_user, author: @fake_user, project: @other_project
end
it "should link using a correct id" do include_examples 'referenced object'
gfm("Fixed in !#{@merge_request.id}").should == "Fixed in #{link_to "!#{@merge_request.id}", project_merge_request_path(@project, @merge_request), title: "Merge Request: #{@merge_request.title}", class: "gfm gfm-merge_request "}"
end
it "should link with adjecent text" do
gfm("This has been fixed already (see !#{@merge_request.id})").should == "This has been fixed already (see #{link_to "!#{@merge_request.id}", project_merge_request_path(@project, @merge_request), title: "Merge Request: #{@merge_request.title}", class: "gfm gfm-merge_request "})"
end
it "should add styles" do
gfm("Fixed in !#{@merge_request.id}").should have_selector(".gfm.gfm-merge_request")
end
it "should not link using an invalid id" do
gfm("!#{@invalid_merge_request.id} violates our coding guidelines")
end
end end
describe "referencing a snippet" do describe "referencing a snippet" do
before do let(:object) { snippet }
@snippet = Factory.create(:snippet, let(:reference) { "$#{snippet.id}" }
title: "Render asset to string",
author: @fake_user,
project: @project)
end
it "should link using a correct id" do include_examples 'referenced object'
gfm("Check out $#{@snippet.id}").should == "Check out #{link_to "$#{@snippet.id}", project_snippet_path(@project, @snippet), title: "Snippet: #{@snippet.title}", class: "gfm gfm-snippet "}"
end
it "should link with adjecent text" do
gfm("I have created a snippet for that ($#{@snippet.id})").should == "I have created a snippet for that (#{link_to "$#{@snippet.id}", project_snippet_path(@project, @snippet), title: "Snippet: #{@snippet.title}", class: "gfm gfm-snippet "})"
end
it "should add styles" do
gfm("Check out $#{@snippet.id}").should have_selector(".gfm.gfm-snippet")
end
it "should not link using an invalid id" do
gfm("Don't use $1234").should == "Don't use $1234"
end
end end
it "should link to multiple things" do describe "referencing multiple objects" do
user = Factory :user, name: "barry" let(:actual) { "!#{merge_request.id} -> #{commit.id} -> ##{issue.id}" }
@project.users << user
member = @project.users_projects.where(user_id: user).first
gfm("Let @#{user.name} fix the *mess* in #{@commit.id}").should == "Let #{link_to "@#{user.name}", project_team_member_path(@project, member), class: "gfm gfm-team_member "} fix the *mess* in #{link_to @commit.id, project_commit_path(@project, id: @commit.id), title: "Commit: #{@commit.author_name} - #{@commit.title}", class: "gfm gfm-commit "}" it "should link to the merge request" do
end expected = project_merge_request_path(project, merge_request)
gfm(actual).should match(expected)
end
it "should not trip over other stuff" do it "should link to the commit" do
gfm("_Please_ *stop* 'helping' and all the other b*$#%' you do.").should == "_Please_ *stop* 'helping' and all the other b*$#%' you do." expected = project_commit_path(project, commit)
end gfm(actual).should match(expected)
end
it "should not touch HTML entities" do it "should link to the issue" do
gfm("We&#39;ll accept good pull requests.").should == "We&#39;ll accept good pull requests." expected = project_issue_path(project, issue)
end gfm(actual).should match(expected)
end
it "should forward HTML options to links" do
gfm("fixed in #{@commit.id}", class: "foo").should have_selector("a.foo")
end end
end end
describe "#link_to_gfm" do describe "#link_to_gfm" do
let(:issue1) { Factory :issue, assignee: @fake_user, author: @fake_user, project: @project } let(:commit_path) { project_commit_path(project, commit) }
let(:issue2) { Factory :issue, assignee: @fake_user, author: @fake_user, project: @project } let(:issues) { create_list(:issue, 2, project: project) }
it "should handle references nested in links with all the text" do it "should handle references nested in links with all the text" do
link_to_gfm("This should finally fix ##{issue1.id} and ##{issue2.id} for real", project_commit_path(@project, id: @commit.id)).should == "#{link_to "This should finally fix ", project_commit_path(@project, id: @commit.id)}#{link_to "##{issue1.id}", project_issue_path(@project, issue1), title: "Issue: #{issue1.title}", class: "gfm gfm-issue "}#{link_to " and ", project_commit_path(@project, id: @commit.id)}#{link_to "##{issue2.id}", project_issue_path(@project, issue2), title: "Issue: #{issue2.title}", class: "gfm gfm-issue "}#{link_to " for real", project_commit_path(@project, id: @commit.id)}" actual = link_to_gfm("This should finally fix ##{issues[0].id} and ##{issues[1].id} for real", commit_path)
# Break the result into groups of links with their content, without
# closing tags
groups = actual.split("</a>")
# Leading commit link
groups[0].should match(/href="#{commit_path}"/)
groups[0].should match(/This should finally fix $/)
# First issue link
groups[1].should match(/href="#{project_issue_path(project, issues[0])}"/)
groups[1].should match(/##{issues[0].id}$/)
# Internal commit link
groups[2].should match(/href="#{commit_path}"/)
groups[2].should match(/ and /)
# Second issue link
groups[3].should match(/href="#{project_issue_path(project, issues[1])}"/)
groups[3].should match(/##{issues[1].id}$/)
# Trailing commit link
groups[4].should match(/href="#{commit_path}"/)
groups[4].should match(/ for real$/)
end end
it "should forward HTML options" do it "should forward HTML options" do
link_to_gfm("This should finally fix ##{issue1.id} for real", project_commit_path(@project, id: @commit.id), class: "foo").should have_selector(".foo") actual = link_to_gfm("Fixed in #{commit.id}", commit_path, class: 'foo')
actual.should have_selector 'a.gfm.gfm-commit.foo'
end end
end end
describe "#markdown" do describe "#markdown" do
before do
@issue = Factory :issue, assignee: @fake_user, author: @fake_user, project: @project
@merge_request = Factory :merge_request, assignee: @fake_user, author: @fake_user, project: @project
@note = Factory.create(:note,
note: "Screenshot of the new feature",
project: @project,
noteable_id: @commit.id,
noteable_type: "Commit",
attachment: "screenshot123.jpg")
@snippet = Factory.create(:snippet,
title: "Render asset to string",
author: @fake_user,
project: @project)
@other_user = Factory :user, name: "bill"
@project.users << @other_user
@member = @project.users_projects.where(user_id: @other_user).first
end
it "should handle references in paragraphs" do it "should handle references in paragraphs" do
markdown("\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. #{@commit.id} Nam pulvinar sapien eget odio adipiscing at faucibus orci vestibulum.\n").should == "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. #{link_to @commit.id, project_commit_path(@project, id: @commit.id), title: "Commit: #{@commit.author_name} - #{@commit.title}", class: "gfm gfm-commit "} Nam pulvinar sapien eget odio adipiscing at faucibus orci vestibulum.</p>\n" markdown("\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. #{commit.id} Nam pulvinar sapien eget odio adipiscing at faucibus orci vestibulum.\n").should == "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. #{link_to commit.id, project_commit_path(project, commit), title: commit.link_title, class: "gfm gfm-commit "} Nam pulvinar sapien eget odio adipiscing at faucibus orci vestibulum.</p>\n"
end end
it "should handle references in headers" do it "should handle references in headers" do
markdown("\n# Working around ##{@issue.id} for now\n## Apply !#{@merge_request.id}").should == "<h1 id=\"toc_0\">Working around #{link_to "##{@issue.id}", project_issue_path(@project, @issue), title: "Issue: #{@issue.title}", class: "gfm gfm-issue "} for now</h1>\n\n<h2 id=\"toc_1\">Apply #{link_to "!#{@merge_request.id}", project_merge_request_path(@project, @merge_request), title: "Merge Request: #{@merge_request.title}", class: "gfm gfm-merge_request "}</h2>\n" actual = "\n# Working around ##{issue.id}\n## Apply !#{merge_request.id}"
markdown(actual).should match(%r{<h1[^<]*>Working around <a.+>##{issue.id}</a></h1>})
markdown(actual).should match(%r{<h2[^<]*>Apply <a.+>!#{merge_request.id}</a></h2>})
end end
it "should handle references in lists" do it "should handle references in lists" do
markdown("\n* dark: ##{@issue.id}\n* light by @#{@other_user.name}\n").should == "<ul>\n<li>dark: #{link_to "##{@issue.id}", project_issue_path(@project, @issue), title: "Issue: #{@issue.title}", class: "gfm gfm-issue "}</li>\n<li>light by #{link_to "@#{@other_user.name}", project_team_member_path(@project, @member), class: "gfm gfm-team_member "}</li>\n</ul>\n" project.users << user
actual = "\n* dark: ##{issue.id}\n* light by @#{member.user_name}"
markdown(actual).should match(%r{<li>dark: <a.+>##{issue.id}</a></li>})
markdown(actual).should match(%r{<li>light by <a.+>@#{member.user_name}</a></li>})
end end
it "should handle references in <em>" do it "should handle references in <em>" do
markdown("Apply _!#{@merge_request.id}_ ASAP").should == "<p>Apply <em>#{link_to "!#{@merge_request.id}", project_merge_request_path(@project, @merge_request), title: "Merge Request: #{@merge_request.title}", class: "gfm gfm-merge_request "}</em> ASAP</p>\n" actual = "Apply _!#{merge_request.id}_ ASAP"
markdown(actual).should match(%r{Apply <em><a.+>!#{merge_request.id}</a></em>})
end end
it "should leave code blocks untouched" do it "should leave code blocks untouched" do
markdown("\n some code from $#{@snippet.id}\n here too\n").should == "<div class=\"highlight\"><pre><span class=\"n\">some</span> <span class=\"n\">code</span> <span class=\"n\">from</span> $#{@snippet.id}\n<span class=\"n\">here</span> <span class=\"n\">too</span>\n</pre>\n</div>\n" markdown("\n some code from $#{snippet.id}\n here too\n").should == "<div class=\"highlight\"><pre><span class=\"n\">some</span> <span class=\"n\">code</span> <span class=\"n\">from</span> $#{snippet.id}\n<span class=\"n\">here</span> <span class=\"n\">too</span>\n</pre>\n</div>\n"
markdown("\n```\nsome code from $#{@snippet.id}\nhere too\n```\n").should == "<div class=\"highlight\"><pre><span class=\"n\">some</span> <span class=\"n\">code</span> <span class=\"n\">from</span> $#{@snippet.id}\n<span class=\"n\">here</span> <span class=\"n\">too</span>\n</pre>\n</div>\n" markdown("\n```\nsome code from $#{snippet.id}\nhere too\n```\n").should == "<div class=\"highlight\"><pre><span class=\"n\">some</span> <span class=\"n\">code</span> <span class=\"n\">from</span> $#{snippet.id}\n<span class=\"n\">here</span> <span class=\"n\">too</span>\n</pre>\n</div>\n"
end end
it "should leave inline code untouched" do it "should leave inline code untouched" do
markdown("\nDon't use `$#{@snippet.id}` here.\n").should == "<p>Don&#39;t use <code>$#{@snippet.id}</code> here.</p>\n" markdown("\nDon't use `$#{snippet.id}` here.\n").should == "<p>Don&#39;t use <code>$#{snippet.id}</code> here.</p>\n"
end end
end end
end end