gitlab_flavored_markdown_spec.rb 12.3 KB
Newer Older
Riyad Preukschas's avatar
Riyad Preukschas committed
1 2
require "spec_helper"

randx's avatar
randx committed
3
describe GitlabMarkdownHelper do
Riyad Preukschas's avatar
Riyad Preukschas committed
4
  before do
5
    @project = Factory(:project)
Riyad Preukschas's avatar
Riyad Preukschas committed
6 7
    @commit = @project.repo.commits.first.parents.first
    @commit = CommitDecorator.decorate(Commit.new(@commit))
8 9
    @other_project = Factory :project, path: "OtherPath", code: "OtherCode"
    @fake_user = Factory :user, name: "fred"
Riyad Preukschas's avatar
Riyad Preukschas committed
10 11 12
  end

  describe "#gfm" do
randx's avatar
randx committed
13
    it "should return text if @project is not set" do
14 15
      @project = nil

randx's avatar
randx committed
16
      gfm("foo").should == "foo"
17 18
    end

Riyad Preukschas's avatar
Riyad Preukschas committed
19 20
    describe "referencing a commit" do
      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 "}"
Riyad Preukschas's avatar
Riyad Preukschas committed
22 23 24
      end

      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 "}"
Riyad Preukschas's avatar
Riyad Preukschas committed
26 27 28
      end

      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 "})"
Riyad Preukschas's avatar
Riyad Preukschas committed
30 31 32 33 34 35 36 37 38 39 40
      end

      it "should not link with an invalid id" do
        gfm("What happened in 12345678?").should == "What happened in 12345678?"
      end
    end

    describe "referencing a team member" do
      it "should link using a simple name" do
        user = Factory :user, name: "barry"
        @project.users << user
41
        member = @project.users_projects.where(user_id: user).first
Riyad Preukschas's avatar
Riyad Preukschas committed
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"
Riyad Preukschas's avatar
Riyad Preukschas committed
44 45 46 47 48
      end

      it "should link using a name with dots" do
        user = Factory :user, name: "alphA.Beta"
        @project.users << user
49
        member = @project.users_projects.where(user_id: user).first
Riyad Preukschas's avatar
Riyad Preukschas committed
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"
Riyad Preukschas's avatar
Riyad Preukschas committed
52 53 54 55 56
      end

      it "should link using name with underscores" do
        user = Factory :user, name: "ping_pong_king"
        @project.users << user
57
        member = @project.users_projects.where(user_id: user).first
Riyad Preukschas's avatar
Riyad Preukschas committed
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"
Riyad Preukschas's avatar
Riyad Preukschas committed
60 61 62
      end

      it "should link with adjecent text" do
63
        user = Factory.create(:user, name: "ace")
Riyad Preukschas's avatar
Riyad Preukschas committed
64
        @project.users << user
65
        member = @project.users_projects.where(user_id: user).first
Riyad Preukschas's avatar
Riyad Preukschas committed
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 "})"
Riyad Preukschas's avatar
Riyad Preukschas committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      end

      it "should add styles" do
        user = Factory :user, name: "barry"
        @project.users << user
        gfm("@#{user.name} you are right").should have_selector(".gfm.gfm-team_member")
      end

      it "should not link using a bogus name" do
        gfm("What hapened to @foo?").should == "What hapened to @foo?"
      end
    end

    describe "referencing an issue" do
      before do
83 84
        @issue = Factory :issue, assignee: @fake_user, author: @fake_user, project: @project
        @invalid_issue = Factory :issue, assignee: @fake_user, author: @fake_user, project: @other_project
Riyad Preukschas's avatar
Riyad Preukschas committed
85 86 87
      end

      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 "}"
Riyad Preukschas's avatar
Riyad Preukschas committed
89 90 91
      end

      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 "})"
Riyad Preukschas's avatar
Riyad Preukschas committed
93 94 95 96 97 98 99 100 101 102 103 104 105
      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

    describe "referencing a merge request" do
      before do
106 107
        @merge_request = Factory :merge_request, assignee: @fake_user, author: @fake_user, project: @project
        @invalid_merge_request = Factory :merge_request, assignee: @fake_user, author: @fake_user, project: @other_project
Riyad Preukschas's avatar
Riyad Preukschas committed
108 109 110
      end

      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 "}"
Riyad Preukschas's avatar
Riyad Preukschas committed
112 113 114
      end

      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 "})"
Riyad Preukschas's avatar
Riyad Preukschas committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129
      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

    describe "referencing a snippet" do
      before do
        @snippet = Factory.create(:snippet,
130 131 132
                                  title: "Render asset to string",
                                  author: @fake_user,
                                  project: @project)
Riyad Preukschas's avatar
Riyad Preukschas committed
133 134 135
      end

      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 "}"
Riyad Preukschas's avatar
Riyad Preukschas committed
137 138 139
      end

      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 "})"
Riyad Preukschas's avatar
Riyad Preukschas committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154
      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

    it "should link to multiple things" do
      user = Factory :user, name: "barry"
      @project.users << user
155
      member = @project.users_projects.where(user_id: user).first
Riyad Preukschas's avatar
Riyad Preukschas committed
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 "}"
Riyad Preukschas's avatar
Riyad Preukschas committed
158 159
    end

160
    it "should not trip over other stuff" do
Riyad Preukschas's avatar
Riyad Preukschas committed
161 162 163 164 165 166 167 168
      gfm("_Please_ *stop* 'helping' and all the other b*$#%' you do.").should == "_Please_ *stop* 'helping' and all the other b*$#%' you do."
    end

    it "should not touch HTML entities" do
      gfm("We&#39;ll accept good pull requests.").should == "We&#39;ll accept good pull requests."
    end

    it "should forward HTML options to links" do
169
      gfm("fixed in #{@commit.id}", class: "foo").should have_selector("a.foo")
Riyad Preukschas's avatar
Riyad Preukschas committed
170 171
    end
  end
172 173

  describe "#link_to_gfm" do
174 175
    let(:issue1) { Factory :issue, assignee: @fake_user, author: @fake_user, project: @project }
    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 180 181
    end

    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 184
    end
  end
185 186 187

  describe "#markdown" do
    before do
188 189
      @issue = Factory :issue, assignee: @fake_user, author: @fake_user, project: @project
      @merge_request = Factory :merge_request, assignee: @fake_user, author: @fake_user, project: @project
190
      @note = Factory.create(:note,
191 192 193 194 195
                              note: "Screenshot of the new feature",
                              project: @project,
                              noteable_id: @commit.id,
                              noteable_type: "Commit",
                              attachment: "screenshot123.jpg")
196
      @snippet = Factory.create(:snippet,
197 198 199
                                title: "Render asset to string",
                                author: @fake_user,
                                project: @project)
200 201 202

      @other_user = Factory :user, name: "bill"
      @project.users << @other_user
203
      @member = @project.users_projects.where(user_id: @other_user).first
204 205 206
    end

    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 209 210
    end

    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 213 214
    end

    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 217 218
    end

    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 221 222 223 224 225 226 227 228 229 230 231
    end

    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```\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

    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"
    end
  end
Riyad Preukschas's avatar
Riyad Preukschas committed
232
end