markdown_matchers.rb 5.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# MarkdownMatchers
#
# Custom matchers for our custom HTML::Pipeline filters. These are used to test
# that specific filters are or are not used by our defined pipelines.
#
# Must be included manually.
module MarkdownMatchers
  extend RSpec::Matchers::DSL
  include Capybara::Node::Matchers

  # RelativeLinkFilter
  matcher :parse_relative_links do
    set_default_markdown_messages

    match do |actual|
16
      link = actual.at_css('a:contains("Relative Link")')
17 18 19 20 21 22 23 24 25 26 27 28 29
      image = actual.at_css('img[alt="Relative Image"]')

      expect(link['href']).to end_with('master/doc/README.md')
      expect(image['src']).to end_with('master/app/assets/images/touch-icon-ipad.png')
    end
  end

  # EmojiFilter
  matcher :parse_emoji do
    set_default_markdown_messages

    match do |actual|
      expect(actual).to have_selector('img.emoji', count: 10)
30 31 32

      image = actual.at_css('img.emoji')
      expect(image['src'].to_s).to start_with(Gitlab.config.gitlab.url + '/assets')
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    end
  end

  # TableOfContentsFilter
  matcher :create_header_links do
    set_default_markdown_messages

    match do |actual|
      expect(actual).to have_selector('h1 a#gitlab-markdown')
      expect(actual).to have_selector('h2 a#markdown')
      expect(actual).to have_selector('h3 a#autolinkfilter')
    end
  end

  # AutolinkFilter
  matcher :create_autolinks do
    def have_autolink(link)
      have_link(link, href: link)
    end

    set_default_markdown_messages

    match do |actual|
      expect(actual).to have_autolink('http://about.gitlab.com/')
      expect(actual).to have_autolink('https://google.com/')
      expect(actual).to have_autolink('ftp://ftp.us.debian.org/debian/')
      expect(actual).to have_autolink('smb://foo/bar/baz')
      expect(actual).to have_autolink('irc://irc.freenode.net/git')
      expect(actual).to have_autolink('http://localhost:3000')

      %w(code a kbd).each do |elem|
        expect(body).not_to have_selector("#{elem} a")
      end
    end
  end

69 70 71
  # GollumTagsFilter
  matcher :parse_gollum_tags do
    def have_image(src)
72
      have_css("img[src$='#{src}']")
73 74
    end

75
    prefix = '/namespace1/gitlabhq/wikis'
76 77 78
    set_default_markdown_messages

    match do |actual|
79 80
      expect(actual).to have_link('linked-resource', href: "#{prefix}/linked-resource")
      expect(actual).to have_link('link-text', href: "#{prefix}/linked-resource")
81 82
      expect(actual).to have_link('http://example.com', href: 'http://example.com')
      expect(actual).to have_link('link-text', href: 'http://example.com/pdfs/gollum.pdf')
83
      expect(actual).to have_image("#{prefix}/images/example.jpg")
84 85 86 87
      expect(actual).to have_image('http://example.com/images/example.jpg')
    end
  end

88 89 90 91 92
  # UserReferenceFilter
  matcher :reference_users do
    set_default_markdown_messages

    match do |actual|
93
      expect(actual).to have_selector('a.gfm.gfm-project_member', count: 4)
94 95 96 97 98 99 100 101
    end
  end

  # IssueReferenceFilter
  matcher :reference_issues do
    set_default_markdown_messages

    match do |actual|
102
      expect(actual).to have_selector('a.gfm.gfm-issue', count: 6)
103 104 105 106 107 108 109 110
    end
  end

  # MergeRequestReferenceFilter
  matcher :reference_merge_requests do
    set_default_markdown_messages

    match do |actual|
111
      expect(actual).to have_selector('a.gfm.gfm-merge_request', count: 6)
112 113 114 115 116 117 118 119 120
      expect(actual).to have_selector('em a.gfm-merge_request')
    end
  end

  # SnippetReferenceFilter
  matcher :reference_snippets do
    set_default_markdown_messages

    match do |actual|
121
      expect(actual).to have_selector('a.gfm.gfm-snippet', count: 5)
122 123 124 125 126 127 128 129
    end
  end

  # CommitRangeReferenceFilter
  matcher :reference_commit_ranges do
    set_default_markdown_messages

    match do |actual|
130
      expect(actual).to have_selector('a.gfm.gfm-commit_range', count: 5)
131 132 133 134 135 136 137 138
    end
  end

  # CommitReferenceFilter
  matcher :reference_commits do
    set_default_markdown_messages

    match do |actual|
139
      expect(actual).to have_selector('a.gfm.gfm-commit', count: 5)
140 141 142 143 144 145 146 147
    end
  end

  # LabelReferenceFilter
  matcher :reference_labels do
    set_default_markdown_messages

    match do |actual|
148
      expect(actual).to have_selector('a.gfm.gfm-label', count: 4)
149 150 151
    end
  end

152 153 154 155 156
  # MilestoneReferenceFilter
  matcher :reference_milestones do
    set_default_markdown_messages

    match do |actual|
157
      expect(actual).to have_selector('a.gfm.gfm-milestone', count: 6)
158 159 160
    end
  end

161 162 163 164 165 166 167 168 169 170
  # TaskListFilter
  matcher :parse_task_lists do
    set_default_markdown_messages

    match do |actual|
      expect(actual).to have_selector('ul.task-list', count: 2)
      expect(actual).to have_selector('li.task-list-item', count: 7)
      expect(actual).to have_selector('input[checked]', count: 3)
    end
  end
171 172 173 174 175 176 177 178 179 180

  # InlineDiffFilter
  matcher :parse_inline_diffs do
    set_default_markdown_messages

    match do |actual|
      expect(actual).to have_selector('span.idiff.addition', count: 2)
      expect(actual).to have_selector('span.idiff.deletion', count: 2)
    end
  end
181 182 183 184 185 186 187 188 189 190 191

  # VideoLinkFilter
  matcher :parse_video_links do
    set_default_markdown_messages

    match do |actual|
      video = actual.at_css('video')

      expect(video['src']).to end_with('/assets/videos/gitlab-demo.mp4')
    end
  end
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
end

# Monkeypatch the matcher DSL so that we can reduce some noisy duplication for
# setting the failure messages for these matchers
module RSpec::Matchers::DSL::Macros
  def set_default_markdown_messages
    failure_message do
      # expected to parse emoji, but didn't
      "expected to #{description}, but didn't"
    end

    failure_message_when_negated do
      # expected not to parse task lists, but did
      "expected not to #{description}, but did"
    end
  end
end