Commit f9711cd81a35904d78f2f3df59ccf3a585bdda30
Exists in
master
and in
4 other branches
Merge pull request #1355 from tsigo/gfm_spec_cleanup
GFM spec redesign
Showing
6 changed files
with
337 additions
and
241 deletions
Show diff stats
app/decorators/commit_decorator.rb
1 | 1 | class CommitDecorator < ApplicationDecorator |
2 | 2 | decorates :commit |
3 | 3 | |
4 | + # Returns a string describing the commit for use in a link title | |
5 | + # | |
6 | + # Example | |
7 | + # | |
8 | + # "Commit: Alex Denisov - Project git clone panel" | |
9 | + def link_title | |
10 | + "Commit: #{author_name} - #{title}" | |
11 | + end | |
12 | + | |
4 | 13 | # Returns the commits title. |
5 | 14 | # |
6 | 15 | # Usually, the commit title is the first line of the commit message. | ... | ... |
app/models/commit.rb
1 | 1 | class Commit |
2 | 2 | include ActiveModel::Conversion |
3 | 3 | include Gitlab::Encode |
4 | + include StaticModel | |
4 | 5 | extend ActiveModel::Naming |
5 | 6 | |
6 | 7 | attr_accessor :commit |
... | ... | @@ -22,8 +23,7 @@ class Commit |
22 | 23 | :to_patch, |
23 | 24 | to: :commit |
24 | 25 | |
25 | - | |
26 | - class << self | |
26 | + class << self | |
27 | 27 | def find_or_first(repo, commit_id = nil, root_ref) |
28 | 28 | commit = if commit_id |
29 | 29 | repo.commit(commit_id) |
... | ... | @@ -85,7 +85,7 @@ class Commit |
85 | 85 | first = project.commit(to.try(:strip)) |
86 | 86 | last = project.commit(from.try(:strip)) |
87 | 87 | |
88 | - result = { | |
88 | + result = { | |
89 | 89 | commits: [], |
90 | 90 | diffs: [], |
91 | 91 | commit: nil |
... | ... | @@ -105,10 +105,6 @@ class Commit |
105 | 105 | end |
106 | 106 | end |
107 | 107 | |
108 | - def persisted? | |
109 | - false | |
110 | - end | |
111 | - | |
112 | 108 | def initialize(raw_commit, head = nil) |
113 | 109 | @commit = raw_commit |
114 | 110 | @head = head |
... | ... | @@ -155,7 +151,7 @@ class Commit |
155 | 151 | prev_commit.try :id |
156 | 152 | end |
157 | 153 | |
158 | - def parents_count | |
154 | + def parents_count | |
159 | 155 | parents && parents.count || 0 |
160 | 156 | end |
161 | 157 | end | ... | ... |
... | ... | @@ -0,0 +1,35 @@ |
1 | +# Provides an ActiveRecord-like interface to a model whose data is not persisted to a database. | |
2 | +module StaticModel | |
3 | + extend ActiveSupport::Concern | |
4 | + | |
5 | + module ClassMethods | |
6 | + # Used by ActiveRecord's polymorphic association to set object_id | |
7 | + def primary_key | |
8 | + 'id' | |
9 | + end | |
10 | + | |
11 | + # Used by ActiveRecord's polymorphic association to set object_type | |
12 | + def base_class | |
13 | + self | |
14 | + end | |
15 | + end | |
16 | + | |
17 | + # Used by AR for fetching attributes | |
18 | + # | |
19 | + # Pass it along if we respond to it. | |
20 | + def [](key) | |
21 | + send(key) if respond_to?(key) | |
22 | + end | |
23 | + | |
24 | + def to_param | |
25 | + id | |
26 | + end | |
27 | + | |
28 | + def persisted? | |
29 | + false | |
30 | + end | |
31 | + | |
32 | + def destroyed? | |
33 | + false | |
34 | + end | |
35 | +end | ... | ... |
lib/gitlab/markdown.rb
... | ... | @@ -100,7 +100,7 @@ module Gitlab |
100 | 100 | |
101 | 101 | def reference_commit(identifier) |
102 | 102 | if commit = @project.commit(identifier) |
103 | - link_to(identifier, project_commit_path(@project, id: commit.id), html_options.merge(title: "Commit: #{commit.author_name} - #{CommitDecorator.new(commit).title}", class: "gfm gfm-commit #{html_options[:class]}")) | |
103 | + link_to(identifier, project_commit_path(@project, id: commit.id), html_options.merge(title: CommitDecorator.new(commit).link_title, class: "gfm gfm-commit #{html_options[:class]}")) | |
104 | 104 | end |
105 | 105 | end |
106 | 106 | end | ... | ... |
spec/helpers/gitlab_flavored_markdown_spec.rb
... | ... | @@ -1,232 +0,0 @@ |
1 | -require "spec_helper" | |
2 | - | |
3 | -describe GitlabMarkdownHelper do | |
4 | - before do | |
5 | - @project = Factory(:project) | |
6 | - @commit = @project.repo.commits.first.parents.first | |
7 | - @commit = CommitDecorator.decorate(Commit.new(@commit)) | |
8 | - @other_project = Factory :project, path: "OtherPath", code: "OtherCode" | |
9 | - @fake_user = Factory :user, name: "fred" | |
10 | - end | |
11 | - | |
12 | - describe "#gfm" do | |
13 | - it "should return text if @project is not set" do | |
14 | - @project = nil | |
15 | - | |
16 | - gfm("foo").should == "foo" | |
17 | - end | |
18 | - | |
19 | - describe "referencing a commit" do | |
20 | - it "should link using a full id" do | |
21 | - 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 "}" | |
22 | - end | |
23 | - | |
24 | - it "should link using a short id" do | |
25 | - 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 "}" | |
26 | - end | |
27 | - | |
28 | - it "should link with adjecent text" do | |
29 | - 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 "})" | |
30 | - end | |
31 | - | |
32 | - it "should not link with an invalid id" do | |
33 | - gfm("What happened in 12345678?").should == "What happened in 12345678?" | |
34 | - end | |
35 | - end | |
36 | - | |
37 | - describe "referencing a team member" do | |
38 | - it "should link using a simple name" do | |
39 | - user = Factory :user, name: "barry" | |
40 | - @project.users << user | |
41 | - member = @project.users_projects.where(user_id: user).first | |
42 | - | |
43 | - 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" | |
44 | - end | |
45 | - | |
46 | - it "should link using a name with dots" do | |
47 | - user = Factory :user, name: "alphA.Beta" | |
48 | - @project.users << user | |
49 | - member = @project.users_projects.where(user_id: user).first | |
50 | - | |
51 | - 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" | |
52 | - end | |
53 | - | |
54 | - it "should link using name with underscores" do | |
55 | - user = Factory :user, name: "ping_pong_king" | |
56 | - @project.users << user | |
57 | - member = @project.users_projects.where(user_id: user).first | |
58 | - | |
59 | - 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" | |
60 | - end | |
61 | - | |
62 | - it "should link with adjecent text" do | |
63 | - user = Factory.create(:user, name: "ace") | |
64 | - @project.users << user | |
65 | - member = @project.users_projects.where(user_id: user).first | |
66 | - | |
67 | - 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 "})" | |
68 | - end | |
69 | - | |
70 | - it "should add styles" do | |
71 | - user = Factory :user, name: "barry" | |
72 | - @project.users << user | |
73 | - gfm("@#{user.name} you are right").should have_selector(".gfm.gfm-team_member") | |
74 | - end | |
75 | - | |
76 | - it "should not link using a bogus name" do | |
77 | - gfm("What hapened to @foo?").should == "What hapened to @foo?" | |
78 | - end | |
79 | - end | |
80 | - | |
81 | - describe "referencing an issue" do | |
82 | - before do | |
83 | - @issue = Factory :issue, assignee: @fake_user, author: @fake_user, project: @project | |
84 | - @invalid_issue = Factory :issue, assignee: @fake_user, author: @fake_user, project: @other_project | |
85 | - end | |
86 | - | |
87 | - it "should link using a correct id" do | |
88 | - gfm("Fixes ##{@issue.id}").should == "Fixes #{link_to "##{@issue.id}", project_issue_path(@project, @issue), title: "Issue: #{@issue.title}", class: "gfm gfm-issue "}" | |
89 | - end | |
90 | - | |
91 | - it "should link with adjecent text" do | |
92 | - 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 "})" | |
93 | - end | |
94 | - | |
95 | - it "should add styles" do | |
96 | - gfm("Fixes ##{@issue.id}").should have_selector(".gfm.gfm-issue") | |
97 | - end | |
98 | - | |
99 | - it "should not link using an invalid id" do | |
100 | - gfm("##{@invalid_issue.id} has been marked duplicate of this").should == "##{@invalid_issue.id} has been marked duplicate of this" | |
101 | - end | |
102 | - end | |
103 | - | |
104 | - describe "referencing a merge request" do | |
105 | - before do | |
106 | - @merge_request = Factory :merge_request, assignee: @fake_user, author: @fake_user, project: @project | |
107 | - @invalid_merge_request = Factory :merge_request, assignee: @fake_user, author: @fake_user, project: @other_project | |
108 | - end | |
109 | - | |
110 | - it "should link using a correct id" do | |
111 | - 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 "}" | |
112 | - end | |
113 | - | |
114 | - it "should link with adjecent text" do | |
115 | - 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 "})" | |
116 | - end | |
117 | - | |
118 | - it "should add styles" do | |
119 | - gfm("Fixed in !#{@merge_request.id}").should have_selector(".gfm.gfm-merge_request") | |
120 | - end | |
121 | - | |
122 | - it "should not link using an invalid id" do | |
123 | - gfm("!#{@invalid_merge_request.id} violates our coding guidelines") | |
124 | - end | |
125 | - end | |
126 | - | |
127 | - describe "referencing a snippet" do | |
128 | - before do | |
129 | - @snippet = Factory.create(:snippet, | |
130 | - title: "Render asset to string", | |
131 | - author: @fake_user, | |
132 | - project: @project) | |
133 | - end | |
134 | - | |
135 | - it "should link using a correct id" do | |
136 | - 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 "}" | |
137 | - end | |
138 | - | |
139 | - it "should link with adjecent text" do | |
140 | - 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 "})" | |
141 | - end | |
142 | - | |
143 | - it "should add styles" do | |
144 | - gfm("Check out $#{@snippet.id}").should have_selector(".gfm.gfm-snippet") | |
145 | - end | |
146 | - | |
147 | - it "should not link using an invalid id" do | |
148 | - gfm("Don't use $1234").should == "Don't use $1234" | |
149 | - end | |
150 | - end | |
151 | - | |
152 | - it "should link to multiple things" do | |
153 | - user = Factory :user, name: "barry" | |
154 | - @project.users << user | |
155 | - member = @project.users_projects.where(user_id: user).first | |
156 | - | |
157 | - 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 "}" | |
158 | - end | |
159 | - | |
160 | - it "should not trip over other stuff" do | |
161 | - gfm("_Please_ *stop* 'helping' and all the other b*$#%' you do.").should == "_Please_ *stop* 'helping' and all the other b*$#%' you do." | |
162 | - end | |
163 | - | |
164 | - it "should not touch HTML entities" do | |
165 | - gfm("We'll accept good pull requests.").should == "We'll accept good pull requests." | |
166 | - end | |
167 | - | |
168 | - it "should forward HTML options to links" do | |
169 | - gfm("fixed in #{@commit.id}", class: "foo").should have_selector("a.foo") | |
170 | - end | |
171 | - end | |
172 | - | |
173 | - describe "#link_to_gfm" do | |
174 | - let(:issue1) { Factory :issue, assignee: @fake_user, author: @fake_user, project: @project } | |
175 | - let(:issue2) { Factory :issue, assignee: @fake_user, author: @fake_user, project: @project } | |
176 | - | |
177 | - it "should handle references nested in links with all the text" do | |
178 | - 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)}" | |
179 | - end | |
180 | - | |
181 | - it "should forward HTML options" do | |
182 | - link_to_gfm("This should finally fix ##{issue1.id} for real", project_commit_path(@project, id: @commit.id), class: "foo").should have_selector(".foo") | |
183 | - end | |
184 | - end | |
185 | - | |
186 | - describe "#markdown" do | |
187 | - before do | |
188 | - @issue = Factory :issue, assignee: @fake_user, author: @fake_user, project: @project | |
189 | - @merge_request = Factory :merge_request, assignee: @fake_user, author: @fake_user, project: @project | |
190 | - @note = Factory.create(:note, | |
191 | - note: "Screenshot of the new feature", | |
192 | - project: @project, | |
193 | - noteable_id: @commit.id, | |
194 | - noteable_type: "Commit", | |
195 | - attachment: "screenshot123.jpg") | |
196 | - @snippet = Factory.create(:snippet, | |
197 | - title: "Render asset to string", | |
198 | - author: @fake_user, | |
199 | - project: @project) | |
200 | - | |
201 | - @other_user = Factory :user, name: "bill" | |
202 | - @project.users << @other_user | |
203 | - @member = @project.users_projects.where(user_id: @other_user).first | |
204 | - end | |
205 | - | |
206 | - it "should handle references in paragraphs" do | |
207 | - 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" | |
208 | - end | |
209 | - | |
210 | - it "should handle references in headers" do | |
211 | - 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" | |
212 | - end | |
213 | - | |
214 | - it "should handle references in lists" do | |
215 | - 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" | |
216 | - end | |
217 | - | |
218 | - it "should handle references in <em>" do | |
219 | - 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" | |
220 | - end | |
221 | - | |
222 | - it "should leave code blocks untouched" do | |
223 | - 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" | |
224 | - | |
225 | - 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" | |
226 | - end | |
227 | - | |
228 | - it "should leave inline code untouched" do | |
229 | - markdown("\nDon't use `$#{@snippet.id}` here.\n").should == "<p>Don't use <code>$#{@snippet.id}</code> here.</p>\n" | |
230 | - end | |
231 | - end | |
232 | -end |
... | ... | @@ -0,0 +1,288 @@ |
1 | +require "spec_helper" | |
2 | + | |
3 | +describe GitlabMarkdownHelper do | |
4 | + let!(:project) { create(:project) } | |
5 | + | |
6 | + let(:user) { create(:user, name: 'gfm') } | |
7 | + let(:commit) { CommitDecorator.decorate(project.commit) } | |
8 | + let(:issue) { create(:issue, project: project) } | |
9 | + let(:merge_request) { create(:merge_request, project: project) } | |
10 | + let(:snippet) { create(:snippet, project: project) } | |
11 | + let(:member) { project.users_projects.where(user_id: user).first } | |
12 | + | |
13 | + before do | |
14 | + # Helper expects a @project instance variable | |
15 | + @project = project | |
16 | + end | |
17 | + | |
18 | + describe "#gfm" do | |
19 | + it "should return unaltered text if project is nil" do | |
20 | + actual = "Testing references: ##{issue.id}" | |
21 | + | |
22 | + gfm(actual).should_not == actual | |
23 | + | |
24 | + @project = nil | |
25 | + gfm(actual).should == actual | |
26 | + end | |
27 | + | |
28 | + it "should not alter non-references" do | |
29 | + actual = expected = "_Please_ *stop* 'helping' and all the other b*$#%' you do." | |
30 | + gfm(actual).should == expected | |
31 | + end | |
32 | + | |
33 | + it "should not touch HTML entities" do | |
34 | + actual = expected = "We'll accept good pull requests." | |
35 | + gfm(actual).should == expected | |
36 | + end | |
37 | + | |
38 | + it "should forward HTML options to links" do | |
39 | + gfm("Fixed in #{commit.id}", class: "foo").should have_selector("a.gfm.foo") | |
40 | + end | |
41 | + | |
42 | + describe "referencing a commit" do | |
43 | + let(:expected) { project_commit_path(project, commit) } | |
44 | + | |
45 | + it "should link using a full id" do | |
46 | + actual = "Reverts #{commit.id}" | |
47 | + gfm(actual).should match(expected) | |
48 | + end | |
49 | + | |
50 | + it "should link using a short id" do | |
51 | + actual = "Backported from #{commit.short_id(6)}" | |
52 | + gfm(actual).should match(expected) | |
53 | + end | |
54 | + | |
55 | + it "should link with adjacent text" do | |
56 | + actual = "Reverted (see #{commit.id})" | |
57 | + gfm(actual).should match(expected) | |
58 | + end | |
59 | + | |
60 | + it "should keep whitespace intact" do | |
61 | + actual = "Changes #{commit.id} dramatically" | |
62 | + expected = /Changes <a.+>#{commit.id}<\/a> dramatically/ | |
63 | + gfm(actual).should match(expected) | |
64 | + end | |
65 | + | |
66 | + it "should not link with an invalid id" do | |
67 | + actual = expected = "What happened in #{commit.id.reverse}" | |
68 | + gfm(actual).should == expected | |
69 | + end | |
70 | + | |
71 | + it "should include a title attribute" do | |
72 | + actual = "Reverts #{commit.id}" | |
73 | + gfm(actual).should match(/title="#{commit.link_title}"/) | |
74 | + end | |
75 | + | |
76 | + it "should include standard gfm classes" do | |
77 | + actual = "Reverts #{commit.id}" | |
78 | + gfm(actual).should match(/class="\s?gfm gfm-commit\s?"/) | |
79 | + end | |
80 | + end | |
81 | + | |
82 | + describe "referencing a team member" do | |
83 | + let(:actual) { "@#{user.name} you are right." } | |
84 | + let(:expected) { project_team_member_path(project, member) } | |
85 | + | |
86 | + before do | |
87 | + project.users << user | |
88 | + end | |
89 | + | |
90 | + it "should link using a simple name" do | |
91 | + gfm(actual).should match(expected) | |
92 | + end | |
93 | + | |
94 | + it "should link using a name with dots" do | |
95 | + user.update_attributes(name: "alphA.Beta") | |
96 | + gfm(actual).should match(expected) | |
97 | + end | |
98 | + | |
99 | + it "should link using name with underscores" do | |
100 | + user.update_attributes(name: "ping_pong_king") | |
101 | + gfm(actual).should match(expected) | |
102 | + end | |
103 | + | |
104 | + it "should link with adjacent text" do | |
105 | + actual = "Mail the admin (@gfm)" | |
106 | + gfm(actual).should match(expected) | |
107 | + end | |
108 | + | |
109 | + it "should keep whitespace intact" do | |
110 | + actual = "Yes, @#{user.name} is right." | |
111 | + expected = /Yes, <a.+>@#{user.name}<\/a> is right/ | |
112 | + gfm(actual).should match(expected) | |
113 | + end | |
114 | + | |
115 | + it "should not link with an invalid id" do | |
116 | + actual = expected = "@#{user.name.reverse} you are right." | |
117 | + gfm(actual).should == expected | |
118 | + end | |
119 | + | |
120 | + it "should include standard gfm classes" do | |
121 | + gfm(actual).should match(/class="\s?gfm gfm-team_member\s?"/) | |
122 | + end | |
123 | + end | |
124 | + | |
125 | + # Shared examples for referencing an object | |
126 | + # | |
127 | + # Expects the following attributes to be available in the example group: | |
128 | + # | |
129 | + # - object - The object itself | |
130 | + # - reference - The object reference string (e.g., #1234, $1234, !1234) | |
131 | + # | |
132 | + # Currently limited to Snippets, Issues and MergeRequests | |
133 | + shared_examples 'referenced object' do | |
134 | + let(:actual) { "Reference to #{reference}" } | |
135 | + let(:expected) { polymorphic_path([project, object]) } | |
136 | + | |
137 | + it "should link using a valid id" do | |
138 | + gfm(actual).should match(expected) | |
139 | + end | |
140 | + | |
141 | + it "should link with adjacent text" do | |
142 | + # Wrap the reference in parenthesis | |
143 | + gfm(actual.gsub(reference, "(#{reference})")).should match(expected) | |
144 | + | |
145 | + # Append some text to the end of the reference | |
146 | + gfm(actual.gsub(reference, "#{reference}, right?")).should match(expected) | |
147 | + end | |
148 | + | |
149 | + it "should keep whitespace intact" do | |
150 | + actual = "Referenced #{reference} already." | |
151 | + expected = /Referenced <a.+>[^\s]+<\/a> already/ | |
152 | + gfm(actual).should match(expected) | |
153 | + end | |
154 | + | |
155 | + it "should not link with an invalid id" do | |
156 | + # Modify the reference string so it's still parsed, but is invalid | |
157 | + reference.gsub!(/^(.)(\d+)$/, '\1' + ('\2' * 2)) | |
158 | + gfm(actual).should == actual | |
159 | + end | |
160 | + | |
161 | + it "should include a title attribute" do | |
162 | + title = "#{object.class.to_s.titlecase}: #{object.title}" | |
163 | + gfm(actual).should match(/title="#{title}"/) | |
164 | + end | |
165 | + | |
166 | + it "should include standard gfm classes" do | |
167 | + css = object.class.to_s.underscore | |
168 | + gfm(actual).should match(/class="\s?gfm gfm-#{css}\s?"/) | |
169 | + end | |
170 | + end | |
171 | + | |
172 | + describe "referencing an issue" do | |
173 | + let(:object) { issue } | |
174 | + let(:reference) { "##{issue.id}" } | |
175 | + | |
176 | + include_examples 'referenced object' | |
177 | + end | |
178 | + | |
179 | + describe "referencing a merge request" do | |
180 | + let(:object) { merge_request } | |
181 | + let(:reference) { "!#{merge_request.id}" } | |
182 | + | |
183 | + include_examples 'referenced object' | |
184 | + end | |
185 | + | |
186 | + describe "referencing a snippet" do | |
187 | + let(:object) { snippet } | |
188 | + let(:reference) { "$#{snippet.id}" } | |
189 | + | |
190 | + include_examples 'referenced object' | |
191 | + end | |
192 | + | |
193 | + describe "referencing multiple objects" do | |
194 | + let(:actual) { "!#{merge_request.id} -> #{commit.id} -> ##{issue.id}" } | |
195 | + | |
196 | + it "should link to the merge request" do | |
197 | + expected = project_merge_request_path(project, merge_request) | |
198 | + gfm(actual).should match(expected) | |
199 | + end | |
200 | + | |
201 | + it "should link to the commit" do | |
202 | + expected = project_commit_path(project, commit) | |
203 | + gfm(actual).should match(expected) | |
204 | + end | |
205 | + | |
206 | + it "should link to the issue" do | |
207 | + expected = project_issue_path(project, issue) | |
208 | + gfm(actual).should match(expected) | |
209 | + end | |
210 | + end | |
211 | + end | |
212 | + | |
213 | + describe "#link_to_gfm" do | |
214 | + let(:commit_path) { project_commit_path(project, commit) } | |
215 | + let(:issues) { create_list(:issue, 2, project: project) } | |
216 | + | |
217 | + it "should handle references nested in links with all the text" do | |
218 | + actual = link_to_gfm("This should finally fix ##{issues[0].id} and ##{issues[1].id} for real", commit_path) | |
219 | + | |
220 | + # Break the result into groups of links with their content, without | |
221 | + # closing tags | |
222 | + groups = actual.split("</a>") | |
223 | + | |
224 | + # Leading commit link | |
225 | + groups[0].should match(/href="#{commit_path}"/) | |
226 | + groups[0].should match(/This should finally fix $/) | |
227 | + | |
228 | + # First issue link | |
229 | + groups[1].should match(/href="#{project_issue_path(project, issues[0])}"/) | |
230 | + groups[1].should match(/##{issues[0].id}$/) | |
231 | + | |
232 | + # Internal commit link | |
233 | + groups[2].should match(/href="#{commit_path}"/) | |
234 | + groups[2].should match(/ and /) | |
235 | + | |
236 | + # Second issue link | |
237 | + groups[3].should match(/href="#{project_issue_path(project, issues[1])}"/) | |
238 | + groups[3].should match(/##{issues[1].id}$/) | |
239 | + | |
240 | + # Trailing commit link | |
241 | + groups[4].should match(/href="#{commit_path}"/) | |
242 | + groups[4].should match(/ for real$/) | |
243 | + end | |
244 | + | |
245 | + it "should forward HTML options" do | |
246 | + actual = link_to_gfm("Fixed in #{commit.id}", commit_path, class: 'foo') | |
247 | + actual.should have_selector 'a.gfm.gfm-commit.foo' | |
248 | + end | |
249 | + end | |
250 | + | |
251 | + describe "#markdown" do | |
252 | + it "should handle references in paragraphs" do | |
253 | + 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" | |
254 | + end | |
255 | + | |
256 | + it "should handle references in headers" do | |
257 | + actual = "\n# Working around ##{issue.id}\n## Apply !#{merge_request.id}" | |
258 | + | |
259 | + markdown(actual).should match(%r{<h1[^<]*>Working around <a.+>##{issue.id}</a></h1>}) | |
260 | + markdown(actual).should match(%r{<h2[^<]*>Apply <a.+>!#{merge_request.id}</a></h2>}) | |
261 | + end | |
262 | + | |
263 | + it "should handle references in lists" do | |
264 | + project.users << user | |
265 | + | |
266 | + actual = "\n* dark: ##{issue.id}\n* light by @#{member.user_name}" | |
267 | + | |
268 | + markdown(actual).should match(%r{<li>dark: <a.+>##{issue.id}</a></li>}) | |
269 | + markdown(actual).should match(%r{<li>light by <a.+>@#{member.user_name}</a></li>}) | |
270 | + end | |
271 | + | |
272 | + it "should handle references in <em>" do | |
273 | + actual = "Apply _!#{merge_request.id}_ ASAP" | |
274 | + | |
275 | + markdown(actual).should match(%r{Apply <em><a.+>!#{merge_request.id}</a></em>}) | |
276 | + end | |
277 | + | |
278 | + it "should leave code blocks untouched" do | |
279 | + 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" | |
280 | + | |
281 | + 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" | |
282 | + end | |
283 | + | |
284 | + it "should leave inline code untouched" do | |
285 | + markdown("\nDon't use `$#{snippet.id}` here.\n").should == "<p>Don't use <code>$#{snippet.id}</code> here.</p>\n" | |
286 | + end | |
287 | + end | |
288 | +end | ... | ... |