Commit bc3448fc authored by Douwe Maan's avatar Douwe Maan

Improve spec

parent 37cabebe
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
(() => { (() => {
const gfmRules = { const gfmRules = {
// Should have an entry for every filter in lib/banzai/pipeline/gfm_pipeline.rb, // The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert GitLab Flavored Markdown (GFM) to HTML.
// in reverse order. // These handlers consequently convert that same HTML to GFM to be copied to the clipboard.
// Should have test coverage in spec/features/copy_as_gfm_spec.rb. // Every filter in lib/banzai/pipeline/gfm_pipeline.rb that generates HTML from GFM should have a handler here, in reverse order.
// The GFM-to-HTML-to-GFM cycle is tested in spec/features/copy_as_gfm_spec.rb.
InlineDiffFilter: { InlineDiffFilter: {
'span.idiff.addition'(el, text) { 'span.idiff.addition'(el, text) {
return `{+${text}+}`; return `{+${text}+}`;
......
module Banzai module Banzai
module Pipeline module Pipeline
class GfmPipeline < BasePipeline class GfmPipeline < BasePipeline
# Every filter should have an entry in app/assets/javascripts/copy_as_gfm.js.es6, # These filters convert GitLab Flavored Markdown (GFM) to HTML.
# in reverse order. # The handlers defined in app/assets/javascripts/copy_as_gfm.js.es6 consequently convert that same HTML to GFM to be copied to the clipboard.
# Should have test coverage in spec/features/copy_as_gfm_spec.rb. # Every filter that generates HTML from GFM should have a handler in app/assets/javascripts/copy_as_gfm.js.es6, in reverse order.
# The GFM-to-HTML-to-GFM cycle is tested in spec/features/copy_as_gfm_spec.rb.
def self.filters def self.filters
@filters ||= FilterArray[ @filters ||= FilterArray[
Filter::SyntaxHighlightFilter, Filter::SyntaxHighlightFilter,
......
...@@ -13,17 +13,17 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -13,17 +13,17 @@ describe 'Copy as GFM', feature: true, js: true do
visit namespace_project_issue_path(@project.namespace, @project, @feat.issue) visit namespace_project_issue_path(@project.namespace, @project, @feat.issue)
end end
# Should have an entry for every filter in lib/banzai/pipeline/gfm_pipeline.rb # The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert GitLab Flavored Markdown (GFM) to HTML.
# and app/assets/javascripts/copy_as_gfm.js.es6 # The handlers defined in app/assets/javascripts/copy_as_gfm.js.es6 consequently convert that same HTML to GFM.
filters = { # To make sure these filters and handlers are properly aligned, this spec tests the GFM-to-HTML-to-GFM cycle
'any filter' => [ # by verifying (`html_to_gfm(gfm_to_html(gfm)) == gfm`) for a number of examples of GFM for every filter.
[
'crazy nesting', it 'supports nesting' do
'> 1. [x] **[$`2 + 2`$ {-=-}{+=+} 2^2 ~~:thumbsup:~~](http://google.com)**' verify '> 1. [x] **[$`2 + 2`$ {-=-}{+=+} 2^2 ~~:thumbsup:~~](http://google.com)**'
], end
[
'real world example from the gitlab-ce README', it 'supports a real world example from the gitlab-ce README' do
<<-GFM.strip_heredoc verify <<-GFM.strip_heredoc
# GitLab # GitLab
[![Build status](https://gitlab.com/gitlab-org/gitlab-ce/badges/master/build.svg)](https://gitlab.com/gitlab-org/gitlab-ce/commits/master) [![Build status](https://gitlab.com/gitlab-org/gitlab-ce/badges/master/build.svg)](https://gitlab.com/gitlab-org/gitlab-ce/commits/master)
...@@ -52,78 +52,96 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -52,78 +52,96 @@ describe 'Copy as GFM', feature: true, js: true do
- Completely free and open source (MIT Expat license) - Completely free and open source (MIT Expat license)
GFM GFM
] end
],
'InlineDiffFilter' => [ it 'supports InlineDiffFilter' do
verify(
'{-Deleted text-}', '{-Deleted text-}',
'{+Added text+}' '{+Added text+}'
], )
'TaskListFilter' => [ end
it 'supports TaskListFilter' do
verify(
'- [ ] Unchecked task', '- [ ] Unchecked task',
'- [x] Checked task', '- [x] Checked task',
'1. [ ] Unchecked numbered task', '1. [ ] Unchecked numbered task',
'1. [x] Checked numbered task' '1. [x] Checked numbered task'
], )
'ReferenceFilter' => [ end
['issue reference', -> { @feat.issue.to_reference }],
['full issue reference', -> { @feat.issue.to_reference(full: true) }], it 'supports ReferenceFilter' do
['issue URL', -> { namespace_project_issue_url(@project.namespace, @project, @feat.issue) }], verify(
['issue URL with note anchor', -> { namespace_project_issue_url(@project.namespace, @project, @feat.issue, anchor: 'note_123') }], # issue reference
['issue link', -> { "[Issue](#{namespace_project_issue_url(@project.namespace, @project, @feat.issue)})" }], @feat.issue.to_reference,
['issue link with note anchor', -> { "[Issue](#{namespace_project_issue_url(@project.namespace, @project, @feat.issue, anchor: 'note_123')})" }], # full issue reference
], @feat.issue.to_reference(full: true),
'AutolinkFilter' => [ # issue URL
'https://example.com' namespace_project_issue_url(@project.namespace, @project, @feat.issue),
], # issue URL with note anchor
'TableOfContentsFilter' => [ namespace_project_issue_url(@project.namespace, @project, @feat.issue, anchor: 'note_123'),
'[[_TOC_]]' # issue link
], "[Issue](#{namespace_project_issue_url(@project.namespace, @project, @feat.issue)})",
'EmojiFilter' => [ # issue link with note anchor
':thumbsup:' "[Issue](#{namespace_project_issue_url(@project.namespace, @project, @feat.issue, anchor: 'note_123')})",
], )
'ImageLinkFilter' => [ end
'![Image](https://example.com/image.png)'
], it 'supports AutolinkFilter' do
'VideoLinkFilter' => [ verify 'https://example.com'
'![Video](https://example.com/video.mp4)' end
],
'MathFilter' => [ it 'supports TableOfContentsFilter' do
verify '[[_TOC_]]'
end
it 'supports EmojiFilter' do
verify ':thumbsup:'
end
it 'supports ImageLinkFilter' do
verify '![Image](https://example.com/image.png)'
end
it 'supports VideoLinkFilter' do
verify '![Video](https://example.com/video.mp4)'
end
it 'supports MathFilter' do
verify(
'$`c = \pm\sqrt{a^2 + b^2}`$', '$`c = \pm\sqrt{a^2 + b^2}`$',
[ # math block
'math block',
<<-GFM.strip_heredoc <<-GFM.strip_heredoc
```math ```math
c = \pm\sqrt{a^2 + b^2} c = \pm\sqrt{a^2 + b^2}
``` ```
GFM GFM
] )
], end
'SyntaxHighlightFilter' => [
[ it 'supports SyntaxHighlightFilter' do
'code block', verify <<-GFM.strip_heredoc
<<-GFM.strip_heredoc
```ruby ```ruby
def foo def foo
bar bar
end end
``` ```
GFM GFM
] end
],
'MarkdownFilter' => [ it 'supports MarkdownFilter' do
verify(
'`code`', '`code`',
'`` code with ` ticks ``', '`` code with ` ticks ``',
'> Quote', '> Quote',
[ # multiline quote
'multiline quote',
<<-GFM.strip_heredoc, <<-GFM.strip_heredoc,
> Multiline > Multiline
> Quote > Quote
> >
> With multiple paragraphs > With multiple paragraphs
GFM GFM
],
'![Image](https://example.com/image.png)', '![Image](https://example.com/image.png)',
...@@ -132,39 +150,36 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -132,39 +150,36 @@ describe 'Copy as GFM', feature: true, js: true do
'[Link](https://example.com)', '[Link](https://example.com)',
'- List item', '- List item',
[
'multiline list item', # multiline list item
<<-GFM.strip_heredoc, <<-GFM.strip_heredoc,
- Multiline - Multiline
List item List item
GFM GFM
],
[ # nested lists
'nested lists',
<<-GFM.strip_heredoc, <<-GFM.strip_heredoc,
- Nested - Nested
- Lists - Lists
GFM GFM
],
'1. Numbered list item', '1. Numbered list item',
[
'multiline numbered list item', # multiline numbered list item
<<-GFM.strip_heredoc, <<-GFM.strip_heredoc,
1. Multiline 1. Multiline
Numbered list item Numbered list item
GFM GFM
],
[ # nested numbered list
'nested numbered list',
<<-GFM.strip_heredoc, <<-GFM.strip_heredoc,
1. Nested 1. Nested
1. Numbered lists 1. Numbered lists
GFM GFM
],
'# Heading', '# Heading',
'## Heading', '## Heading',
...@@ -183,38 +198,17 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -183,38 +198,17 @@ describe 'Copy as GFM', feature: true, js: true do
'-----', '-----',
[ # table
'table',
<<-GFM.strip_heredoc, <<-GFM.strip_heredoc,
| Centered | Right | Left | | Centered | Right | Left |
| :------: | ----: | ---- | | :------: | ----: | ---- |
| Foo | Bar | **Baz** | | Foo | Bar | **Baz** |
| Foo | Bar | **Baz** | | Foo | Bar | **Baz** |
GFM GFM
] )
]
}
filters.each do |filter, examples|
context filter do
examples.each do |ex|
if ex.is_a?(String)
desc = "'#{ex}'"
gfm = ex
else
desc, gfm = ex
end end
it "transforms #{desc} to HTML and back to GFM" do alias_method :gfm_to_html, :markdown
gfm = instance_exec(&gfm) if gfm.is_a?(Proc)
html = markdown(gfm)
gfm2 = html_to_gfm(html)
expect(gfm2.strip).to eq(gfm.strip)
end
end
end
end
def html_to_gfm(html) def html_to_gfm(html)
js = <<-JS.strip_heredoc js = <<-JS.strip_heredoc
...@@ -227,6 +221,16 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -227,6 +221,16 @@ describe 'Copy as GFM', feature: true, js: true do
page.evaluate_script(js) page.evaluate_script(js)
end end
def verify(*gfms)
aggregate_failures do
gfms.each do |gfm|
html = gfm_to_html(gfm)
output_gfm = html_to_gfm(html)
expect(output_gfm.strip).to eq(gfm.strip)
end
end
end
# Fake a `current_user` helper # Fake a `current_user` helper
def current_user def current_user
@feat.user @feat.user
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment