2012-08-02 00:26:21 +00:00
require " spec_helper "
2012-08-08 08:52:09 +00:00
describe GitlabMarkdownHelper do
2013-01-16 21:37:39 +00:00
include ApplicationHelper
2013-01-23 14:13:28 +00:00
include IssuesHelper
2013-01-16 21:37:39 +00:00
2014-01-22 19:03:52 +00:00
let! ( :project ) { create ( :project ) }
2014-06-26 07:13:38 +00:00
let ( :empty_project ) { create ( :empty_project ) }
2012-09-02 06:13:13 +00:00
2012-12-07 03:40:42 +00:00
let ( :user ) { create ( :user , username : 'gfm' ) }
2013-04-01 15:16:08 +00:00
let ( :commit ) { project . repository . commit }
2012-09-02 06:13:13 +00:00
let ( :issue ) { create ( :issue , project : project ) }
2013-04-25 14:15:33 +00:00
let ( :merge_request ) { create ( :merge_request , source_project : project , target_project : project ) }
2013-03-25 07:20:14 +00:00
let ( :snippet ) { create ( :project_snippet , project : project ) }
2014-09-14 16:32:51 +00:00
let ( :member ) { project . project_members . where ( user_id : user ) . first }
2012-09-02 06:13:13 +00:00
2014-09-01 15:47:28 +00:00
def url_helper ( image_name )
File . join ( root_url , 'assets' , image_name )
end
2012-08-02 00:26:21 +00:00
before do
2012-09-02 06:13:13 +00:00
# Helper expects a @project instance variable
@project = project
2014-08-06 09:15:05 +00:00
@ref = 'markdown'
2014-01-29 13:48:59 +00:00
@repository = project . repository
2012-08-02 00:26:21 +00:00
end
describe " # gfm " do
2012-09-02 06:13:13 +00:00
it " should return unaltered text if project is nil " do
2013-08-20 13:56:05 +00:00
actual = " Testing references: # #{ issue . iid } "
2012-09-02 06:13:13 +00:00
gfm ( actual ) . should_not == actual
2012-08-06 00:32:17 +00:00
@project = nil
2012-09-02 06:13:13 +00:00
gfm ( actual ) . should == actual
end
2012-08-06 00:32:17 +00:00
2012-09-02 06:13:13 +00:00
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
2012-09-13 19:20:00 +00:00
@project . issues . stub ( :where ) . with ( id : '39' ) . and_return ( [ issue ] )
2014-09-01 15:47:28 +00:00
actual = 'We'll accept good pull requests.'
gfm ( actual ) . should == " We'll accept good pull requests. "
2012-09-02 06:13:13 +00:00
end
it " should forward HTML options to links " do
2014-05-26 12:40:10 +00:00
gfm ( " Fixed in #{ commit . id } " , @project , class : 'foo' ) .
should have_selector ( 'a.gfm.foo' )
2012-08-06 00:32:17 +00:00
end
2012-08-02 00:26:21 +00:00
describe " referencing a commit " do
2012-09-02 06:13:13 +00:00
let ( :expected ) { project_commit_path ( project , commit ) }
2012-08-02 00:26:21 +00:00
it " should link using a full id " do
2012-09-02 06:13:13 +00:00
actual = " Reverts #{ commit . id } "
gfm ( actual ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
it " should link using a short id " do
2012-09-02 06:13:13 +00:00
actual = " Backported from #{ commit . short_id ( 6 ) } "
gfm ( actual ) . should match ( expected )
end
it " should link with adjacent text " do
actual = " Reverted (see #{ commit . id } ) "
gfm ( actual ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should keep whitespace intact " do
actual = " Changes #{ commit . id } dramatically "
expected = / Changes <a.+> #{ commit . id } < \/ a> dramatically /
gfm ( actual ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
it " should not link with an invalid id " do
2012-09-02 06:13:13 +00:00
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?" / )
2012-08-02 00:26:21 +00:00
end
end
describe " referencing a team member " do
2012-12-07 03:40:42 +00:00
let ( :actual ) { " @ #{ user . username } you are right. " }
2013-03-20 11:41:18 +00:00
let ( :expected ) { user_path ( user ) }
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
before do
2013-01-04 16:50:31 +00:00
project . team << [ user , :master ]
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should link using a simple name " do
gfm ( actual ) . should match ( expected )
end
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
it " should link using a name with dots " do
user . update_attributes ( name : " alphA.Beta " )
gfm ( actual ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
it " should link using name with underscores " do
2012-09-02 06:13:13 +00:00
user . update_attributes ( name : " ping_pong_king " )
gfm ( actual ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should link with adjacent text " do
2012-12-07 03:40:42 +00:00
actual = " Mail the admin (@ #{ user . username } ) "
2012-09-02 06:13:13 +00:00
gfm ( actual ) . should match ( expected )
end
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
it " should keep whitespace intact " do
2012-12-07 03:40:42 +00:00
actual = " Yes, @ #{ user . username } is right. "
expected = / Yes, <a.+>@ #{ user . username } < \/ a> is right /
2012-09-02 06:13:13 +00:00
gfm ( actual ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should not link with an invalid id " do
2012-12-07 03:40:42 +00:00
actual = expected = " @ #{ user . username . reverse } you are right. "
2012-09-02 06:13:13 +00:00
gfm ( actual ) . should == expected
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should include standard gfm classes " do
gfm ( actual ) . should match ( / class=" \ s?gfm gfm-team_member \ s?" / )
2012-08-02 00:26:21 +00:00
end
end
2012-09-02 06:13:13 +00:00
# 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 )
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should link with adjacent text " do
# Wrap the reference in parenthesis
gfm ( actual . gsub ( reference , " ( #{ reference } ) " ) ) . should match ( expected )
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
# Append some text to the end of the reference
gfm ( actual . gsub ( reference , " #{ reference } , right? " ) ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should keep whitespace intact " do
actual = " Referenced #{ reference } already. "
expected = / Referenced <a.+>[^ \ s]+< \/ a> already /
gfm ( actual ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
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
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should include a title attribute " do
title = " #{ object . class . to_s . titlecase } : #{ object . title } "
gfm ( actual ) . should match ( / title=" #{ title } " / )
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should include standard gfm classes " do
css = object . class . to_s . underscore
gfm ( actual ) . should match ( / class=" \ s?gfm gfm- #{ css } \ s?" / )
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
end
2012-08-02 00:26:21 +00:00
2014-10-01 15:31:13 +00:00
# Shared examples for referencing an object in a different project
#
# 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)
# - other_project - The project that owns the target object
#
# Currently limited to Snippets, Issues and MergeRequests
shared_examples 'cross-project referenced object' do
let ( :project_path ) { @other_project . path_with_namespace }
let ( :full_reference ) { " #{ project_path } #{ reference } " }
let ( :actual ) { " Reference to #{ full_reference } " }
let ( :expected ) do
if object . is_a? ( Commit )
project_commit_path ( @other_project , object )
else
polymorphic_path ( [ @other_project , object ] )
end
end
it 'should link using a valid id' do
2014-10-05 15:46:52 +00:00
gfm ( actual ) . should match (
/ #{ expected } .* #{ Regexp . escape ( full_reference ) } /
)
2014-10-01 15:31:13 +00:00
end
it 'should link with adjacent text' do
# Wrap the reference in parenthesis
gfm ( actual . gsub ( full_reference , " ( #{ full_reference } ) " ) ) . should (
match ( expected )
)
# Append some text to the end of the reference
gfm ( actual . gsub ( full_reference , " #{ full_reference } , right? " ) ) . should (
match ( expected )
)
end
it 'should keep whitespace intact' do
actual = " Referenced #{ full_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
if object . is_a? ( Commit )
reference . gsub! ( / ^(.).+$ / , '\1' + '12345abcd' )
else
reference . gsub! ( / ^(.)( \ d+)$ / , '\1' + ( '\2' * 2 ) )
end
gfm ( actual ) . should == actual
end
it 'should include a title attribute' do
if object . is_a? ( Commit )
title = object . link_title
else
title = " #{ object . class . to_s . titlecase } : #{ object . title } "
end
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
2012-09-02 06:13:13 +00:00
describe " referencing an issue " do
let ( :object ) { issue }
2013-08-20 13:56:05 +00:00
let ( :reference ) { " # #{ issue . iid } " }
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
include_examples 'referenced object'
end
2012-08-02 00:26:21 +00:00
2014-10-01 15:31:13 +00:00
context 'cross-repo references' do
before ( :all ) do
@other_project = create ( :project , :public )
@commit2 = @other_project . repository . commit
@issue2 = create ( :issue , project : @other_project )
@merge_request2 = create ( :merge_request ,
source_project : @other_project ,
target_project : @other_project )
end
describe 'referencing an issue in another project' do
let ( :object ) { @issue2 }
let ( :reference ) { " # #{ @issue2 . iid } " }
include_examples 'cross-project referenced object'
end
describe 'referencing an merge request in another project' do
let ( :object ) { @merge_request2 }
let ( :reference ) { " ! #{ @merge_request2 . iid } " }
include_examples 'cross-project referenced object'
end
describe 'referencing a commit in another project' do
let ( :object ) { @commit2 }
let ( :reference ) { " @ #{ @commit2 . id } " }
include_examples 'cross-project referenced object'
end
end
2014-05-06 17:51:56 +00:00
describe " referencing a Jira issue " do
let ( :actual ) { " Reference to JIRA- #{ issue . iid } " }
let ( :expected ) { " http://jira.example/browse/JIRA- #{ issue . iid } " }
let ( :reference ) { " JIRA- #{ issue . iid } " }
before do
2014-05-07 10:09:05 +00:00
issue_tracker_config = { " jira " = > { " title " = > " JIRA tracker " , " issues_url " = > " http://jira.example/browse/:id " } }
Gitlab . config . stub ( :issues_tracker ) . and_return ( issue_tracker_config )
2014-05-06 17:51:56 +00:00
@project . stub ( :issues_tracker ) . and_return ( " jira " )
@project . stub ( :issues_tracker_id ) . and_return ( " JIRA " )
end
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
invalid_reference = actual . gsub ( / ( \ d+)$ / , " r45 " )
gfm ( invalid_reference ) . should == invalid_reference
end
it " should include a title attribute " do
title = " Issue in JIRA tracker "
gfm ( actual ) . should match ( / title=" #{ title } " / )
end
it " should include standard gfm classes " do
gfm ( actual ) . should match ( / class=" \ s?gfm gfm-issue \ s?" / )
end
end
2012-09-02 06:13:13 +00:00
describe " referencing a merge request " do
let ( :object ) { merge_request }
2013-08-20 13:56:05 +00:00
let ( :reference ) { " ! #{ merge_request . iid } " }
2012-09-02 06:13:13 +00:00
include_examples 'referenced object'
2012-08-02 00:26:21 +00:00
end
describe " referencing a snippet " do
2012-09-02 06:13:13 +00:00
let ( :object ) { snippet }
let ( :reference ) { " $ #{ snippet . id } " }
2013-03-25 07:20:14 +00:00
let ( :actual ) { " Reference to #{ reference } " }
let ( :expected ) { project_snippet_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 = " Snippet: #{ 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-snippet \ s?" / )
end
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
end
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
describe " referencing multiple objects " do
2013-08-20 13:56:05 +00:00
let ( :actual ) { " ! #{ merge_request . iid } -> #{ commit . id } -> # #{ issue . iid } " }
2012-09-02 06:13:13 +00:00
it " should link to the merge request " do
expected = project_merge_request_path ( project , merge_request )
gfm ( actual ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should link to the commit " do
expected = project_commit_path ( project , commit )
gfm ( actual ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
2012-09-02 06:13:13 +00:00
it " should link to the issue " do
expected = project_issue_path ( project , issue )
gfm ( actual ) . should match ( expected )
2012-08-02 00:26:21 +00:00
end
end
2012-09-05 20:05:20 +00:00
describe " emoji " do
it " matches at the start of a string " do
gfm ( " :+1: " ) . should match ( / <img / )
end
it " matches at the end of a string " do
gfm ( " This gets a :-1: " ) . should match ( / <img / )
end
it " matches with adjacent text " do
gfm ( " +1 (:+1:) " ) . should match ( / <img / )
end
it " has a title attribute " do
gfm ( " :-1: " ) . should match ( / title=":-1:" / )
end
it " has an alt attribute " do
gfm ( " :-1: " ) . should match ( / alt=":-1:" / )
end
it " has an emoji class " do
gfm ( " :+1: " ) . should match ( 'class="emoji"' )
end
2012-09-05 20:14:54 +00:00
it " sets height and width " do
actual = gfm ( " :+1: " )
actual . should match ( / width="20" / )
actual . should match ( / height="20" / )
end
2012-09-05 20:05:20 +00:00
it " keeps whitespace intact " do
2014-09-01 15:47:28 +00:00
gfm ( 'This deserves a :+1: big time.' ) .
should match ( / deserves a <img.+> big time / )
2012-09-05 20:05:20 +00:00
end
it " ignores invalid emoji " do
gfm ( " :invalid-emoji: " ) . should_not match ( / <img / )
end
2012-09-07 22:49:17 +00:00
2013-07-29 10:46:00 +00:00
it " should work independent of reference links (i.e. without @project being set) " do
2012-09-07 22:49:17 +00:00
@project = nil
gfm ( " :+1: " ) . should match ( / <img / )
end
2012-09-05 20:05:20 +00:00
end
2012-09-02 06:13:13 +00:00
end
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
describe " # link_to_gfm " do
let ( :commit_path ) { project_commit_path ( project , commit ) }
let ( :issues ) { create_list ( :issue , 2 , project : project ) }
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
it " should handle references nested in links with all the text " do
2013-08-20 13:56:05 +00:00
actual = link_to_gfm ( " This should finally fix # #{ issues [ 0 ] . iid } and # #{ issues [ 1 ] . iid } for real " , commit_path )
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
# Break the result into groups of links with their content, without
# closing tags
groups = actual . split ( " </a> " )
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
# Leading commit link
groups [ 0 ] . should match ( / href=" #{ commit_path } " / )
groups [ 0 ] . should match ( / This should finally fix $ / )
2012-08-02 00:26:21 +00:00
2012-09-02 06:13:13 +00:00
# First issue link
2013-01-16 21:08:01 +00:00
groups [ 1 ] . should match ( / href=" #{ project_issue_url ( project , issues [ 0 ] ) } " / )
2013-08-20 13:56:05 +00:00
groups [ 1 ] . should match ( / # #{ issues [ 0 ] . iid } $ / )
2012-08-02 00:28:50 +00:00
2012-09-02 06:13:13 +00:00
# Internal commit link
groups [ 2 ] . should match ( / href=" #{ commit_path } " / )
groups [ 2 ] . should match ( / and / )
2012-08-02 00:28:50 +00:00
2012-09-02 06:13:13 +00:00
# Second issue link
2013-01-16 21:08:01 +00:00
groups [ 3 ] . should match ( / href=" #{ project_issue_url ( project , issues [ 1 ] ) } " / )
2013-08-20 13:56:05 +00:00
groups [ 3 ] . should match ( / # #{ issues [ 1 ] . iid } $ / )
2012-09-02 06:13:13 +00:00
# Trailing commit link
groups [ 4 ] . should match ( / href=" #{ commit_path } " / )
groups [ 4 ] . should match ( / for real$ / )
2012-08-02 00:28:50 +00:00
end
it " should forward HTML options " do
2012-09-02 06:13:13 +00:00
actual = link_to_gfm ( " Fixed in #{ commit . id } " , commit_path , class : 'foo' )
actual . should have_selector 'a.gfm.gfm-commit.foo'
2012-08-02 00:28:50 +00:00
end
2012-09-19 23:42:26 +00:00
it " escapes HTML passed in as the body " do
2013-08-20 13:56:05 +00:00
actual = " This is a <h1>test</h1> - see # #{ issues [ 0 ] . iid } "
2012-09-19 23:42:26 +00:00
link_to_gfm ( actual , commit_path ) . should match ( '<h1>test</h1>' )
end
2012-08-02 00:28:50 +00:00
end
2012-08-02 00:29:15 +00:00
describe " # markdown " do
it " should handle references in paragraphs " do
2012-09-19 23:42:26 +00:00
actual = " \n \n Lorem ipsum dolor sit amet. #{ commit . id } Nam pulvinar sapien eget. \n "
expected = project_commit_path ( project , commit )
markdown ( actual ) . should match ( expected )
2012-08-02 00:29:15 +00:00
end
it " should handle references in headers " do
2013-08-20 13:56:05 +00:00
actual = " \n # Working around # #{ issue . iid } \n # # Apply ! #{ merge_request . iid } "
2012-09-02 06:13:13 +00:00
2014-02-04 07:48:33 +00:00
markdown ( actual , { no_header_anchors : true } ) . should match ( %r{ <h1[^<]*>Working around <a.+> # #{ issue . iid } </a></h1> } )
markdown ( actual , { no_header_anchors : true } ) . should match ( %r{ <h2[^<]*>Apply <a.+>! #{ merge_request . iid } </a></h2> } )
end
it " should add ids and links to headers " do
# Test every rule except nested tags.
text = '..Ab_c-d. e..'
id = 'ab_c-d-e'
markdown ( " # #{ text } " ) . should match ( %r{ <h1 id=" #{ id } "> #{ text } <a href="[^"]* # #{ id } "></a></h1> } )
markdown ( " # #{ text } " , { no_header_anchors : true } ) . should == " <h1> #{ text } </h1> "
id = 'link-text'
markdown ( " # [link text](url) ![img alt](url) " ) . should match (
%r{ <h1 id=" #{ id } "><a href="[^"]*url">link text</a> <img[^>]*><a href="[^"]* # #{ id } "></a></h1> }
)
2012-08-02 00:29:15 +00:00
end
it " should handle references in lists " do
2013-01-04 16:50:31 +00:00
project . team << [ user , :master ]
2012-09-02 06:13:13 +00:00
2013-08-20 13:56:05 +00:00
actual = " \n * dark: # #{ issue . iid } \n * light by @ #{ member . user . username } "
2012-09-02 06:13:13 +00:00
2013-08-20 13:56:05 +00:00
markdown ( actual ) . should match ( %r{ <li>dark: <a.+> # #{ issue . iid } </a></li> } )
2012-12-07 03:40:42 +00:00
markdown ( actual ) . should match ( %r{ <li>light by <a.+>@ #{ member . user . username } </a></li> } )
2012-08-02 00:29:15 +00:00
end
2014-10-09 12:02:10 +00:00
it " should not link the apostrophe to issue 39 " do
project . team << [ user , :master ]
project . issues . stub ( :where ) . with ( iid : '39' ) . and_return ( [ issue ] )
actual = " Yes, it is @ #{ member . user . username } 's task. "
2014-10-09 12:20:49 +00:00
expected = / Yes, it is <a.+>@ #{ member . user . username } < \/ a>’ s task /
2014-10-09 12:02:10 +00:00
markdown ( actual ) . should match ( expected )
end
2014-10-09 14:12:17 +00:00
it " should not link the apostrophe to issue 39 in code blocks " do
project . team << [ user , :master ]
project . issues . stub ( :where ) . with ( iid : '39' ) . and_return ( [ issue ] )
actual = " Yes, `it is @ #{ member . user . username } 's task.` "
expected = / Yes, <code>it is @gfm \ 's task.< \/ code> /
markdown ( actual ) . should match ( expected )
end
2014-10-09 12:02:10 +00:00
2012-08-02 00:29:15 +00:00
it " should handle references in <em> " do
2013-08-20 13:56:05 +00:00
actual = " Apply _! #{ merge_request . iid } _ ASAP "
2012-09-02 06:13:13 +00:00
2013-08-20 13:56:05 +00:00
markdown ( actual ) . should match ( %r{ Apply <em><a.+>! #{ merge_request . iid } </a></em> } )
2012-08-02 00:29:15 +00:00
end
2013-08-26 12:07:07 +00:00
it " should handle tables " do
actual = %Q{ | header 1 | header 2 |
| - - - - - - - - | - - - - - - - - |
| cell 1 | cell 2 |
| cell 3 | cell 4 | }
markdown ( actual ) . should match ( / \ A<table / )
end
2012-08-02 00:29:15 +00:00
it " should leave code blocks untouched " do
2012-11-28 15:05:11 +00:00
helper . stub ( :user_color_scheme_class ) . and_return ( :white )
2012-08-02 00:29:15 +00:00
2014-01-27 16:18:18 +00:00
target_html = " \n <div class= \" highlighted-data white \" > \n <div class= \" highlight \" > \n <pre><code class= \" \" >some code from $ #{ snippet . id } \n here too \n </code></pre> \n </div> \n </div> \n \n "
2012-11-28 15:05:11 +00:00
2014-01-27 15:36:30 +00:00
helper . markdown ( " \n some code from $ #{ snippet . id } \n here too \n " ) . should == target_html
helper . markdown ( " \n ``` \n some code from $ #{ snippet . id } \n here too \n ``` \n " ) . should == target_html
2012-08-02 00:29:15 +00:00
end
it " should leave inline code untouched " do
2014-09-01 15:47:28 +00:00
markdown ( " \n Don't use `$ #{ snippet . id } ` here. \n " ) . should ==
2014-10-09 12:20:49 +00:00
" <p>Don’ t use <code>$ #{ snippet . id } </code> here.</p> \n "
2012-08-02 00:29:15 +00:00
end
2013-01-16 21:08:01 +00:00
2013-01-16 21:37:39 +00:00
it " should leave ref-like autolinks untouched " do
2013-08-20 13:56:05 +00:00
markdown ( " look at http://example.tld/ # ! #{ merge_request . iid } " ) . should == " <p>look at <a href= \" http://example.tld/ # ! #{ merge_request . iid } \" >http://example.tld/ # ! #{ merge_request . iid } </a></p> \n "
2013-01-16 21:37:39 +00:00
end
it " should leave ref-like href of 'manual' links untouched " do
2013-12-10 11:38:31 +00:00
markdown ( " why not [inspect ! #{ merge_request . iid } ](http://example.tld/ # ! #{ merge_request . iid } ) " ) . should == " <p>why not <a href= \" http://example.tld/ # ! #{ merge_request . iid } \" >inspect </a><a class= \" gfm gfm-merge_request \" href= \" #{ project_merge_request_url ( project , merge_request ) } \" title= \" Merge Request: #{ merge_request . title } \" >! #{ merge_request . iid } </a><a href= \" http://example.tld/ # ! #{ merge_request . iid } \" ></a></p> \n "
2013-01-16 21:37:39 +00:00
end
it " should leave ref-like src of images untouched " do
2013-08-20 13:56:05 +00:00
markdown ( " screen shot: ![some image](http://example.tld/ # ! #{ merge_request . iid } ) " ) . should == " <p>screen shot: <img src= \" http://example.tld/ # ! #{ merge_request . iid } \" alt= \" some image \" ></p> \n "
2013-01-16 21:37:39 +00:00
end
2013-01-16 21:08:01 +00:00
it " should generate absolute urls for refs " do
2013-08-20 13:56:05 +00:00
markdown ( " # #{ issue . iid } " ) . should include ( project_issue_url ( project , issue ) )
2013-01-16 21:08:01 +00:00
end
2013-01-16 21:39:45 +00:00
it " should generate absolute urls for emoji " do
2014-09-01 15:47:28 +00:00
markdown ( " :smile: " ) . should include ( " src= \" #{ url_helper ( 'emoji/smile' ) } " )
2013-01-16 21:39:45 +00:00
end
2013-10-09 13:01:02 +00:00
it " should handle relative urls for a file in master " do
actual = " [GitLab API doc](doc/api/README.md) \n "
2014-08-06 09:15:05 +00:00
expected = " <p><a href= \" / #{ project . path_with_namespace } /blob/ #{ @ref } /doc/api/README.md \" >GitLab API doc</a></p> \n "
2013-10-09 13:01:02 +00:00
markdown ( actual ) . should match ( expected )
end
it " should handle relative urls for a directory in master " do
actual = " [GitLab API doc](doc/api) \n "
2014-08-06 09:15:05 +00:00
expected = " <p><a href= \" / #{ project . path_with_namespace } /tree/ #{ @ref } /doc/api \" >GitLab API doc</a></p> \n "
2013-10-09 13:01:02 +00:00
markdown ( actual ) . should match ( expected )
end
it " should handle absolute urls " do
actual = " [GitLab](https://www.gitlab.com) \n "
expected = " <p><a href= \" https://www.gitlab.com \" >GitLab</a></p> \n "
markdown ( actual ) . should match ( expected )
end
2014-01-23 09:43:04 +00:00
it " should handle relative urls in reference links for a file in master " do
actual = " [GitLab API doc][GitLab readme] \n [GitLab readme]: doc/api/README.md \n "
2014-08-06 09:15:05 +00:00
expected = " <p><a href= \" / #{ project . path_with_namespace } /blob/ #{ @ref } /doc/api/README.md \" >GitLab API doc</a></p> \n "
2014-01-23 09:43:04 +00:00
markdown ( actual ) . should match ( expected )
end
it " should handle relative urls in reference links for a directory in master " do
actual = " [GitLab API doc directory][GitLab readmes] \n [GitLab readmes]: doc/api/ \n "
2014-08-06 09:15:05 +00:00
expected = " <p><a href= \" / #{ project . path_with_namespace } /tree/ #{ @ref } /doc/api \" >GitLab API doc directory</a></p> \n "
2014-01-23 09:43:04 +00:00
markdown ( actual ) . should match ( expected )
end
it " should not handle malformed relative urls in reference links for a file in master " do
actual = " [GitLab readme]: doc/api/README.md \n "
expected = " "
markdown ( actual ) . should match ( expected )
end
2012-08-02 00:29:15 +00:00
end
2013-03-14 06:31:08 +00:00
2014-10-06 02:17:28 +00:00
describe 'markdown for empty repository' do
2014-06-26 07:13:38 +00:00
before do
@project = empty_project
@repository = empty_project . repository
end
it " should not touch relative urls " do
actual = " [GitLab API doc][GitLab readme] \n [GitLab readme]: doc/api/README.md \n "
expected = " <p><a href= \" doc/api/README.md \" >GitLab API doc</a></p> \n "
markdown ( actual ) . should match ( expected )
end
end
2013-03-14 06:31:08 +00:00
describe " # render_wiki_content " do
before do
2013-12-14 13:43:48 +00:00
@wiki = double ( 'WikiPage' )
2013-03-14 06:31:08 +00:00
@wiki . stub ( :content ) . and_return ( 'wiki content' )
end
2013-05-19 09:13:39 +00:00
it " should use GitLab Flavored Markdown for markdown files " do
2013-03-14 06:31:08 +00:00
@wiki . stub ( :format ) . and_return ( :markdown )
helper . should_receive ( :markdown ) . with ( 'wiki content' )
helper . render_wiki_content ( @wiki )
end
it " should use the Gollum renderer for all other file types " do
@wiki . stub ( :format ) . and_return ( :rdoc )
2013-12-14 13:43:48 +00:00
formatted_content_stub = double ( 'formatted_content' )
2013-03-14 06:31:08 +00:00
formatted_content_stub . should_receive ( :html_safe )
@wiki . stub ( :formatted_content ) . and_return ( formatted_content_stub )
helper . render_wiki_content ( @wiki )
end
end
2014-10-06 02:17:28 +00:00
describe '#gfm_with_tasks' do
before ( :all ) do
@source_text_asterisk = <<EOT.gsub(/^\s{8}/, '')
* [ ] valid unchecked task
* [ x ] valid lowercase checked task
* [ X ] valid uppercase checked task
* [ ] valid unchecked nested task
* [ x ] valid checked nested task
[ ] not an unchecked task - no list item
[ x ] not a checked task - no list item
* [ ] not an unchecked task - too many spaces
* [ x ] not a checked task - too many spaces
* [ ] not an unchecked task - no spaces
* Not a task [ ] - not at beginning
EOT
@source_text_dash = <<EOT.gsub(/^\s{8}/, '')
- [ ] valid unchecked task
- [ x ] valid lowercase checked task
- [ X ] valid uppercase checked task
- [ ] valid unchecked nested task
- [ x ] valid checked nested task
EOT
end
it 'should render checkboxes at beginning of asterisk list items' do
rendered_text = markdown ( @source_text_asterisk , parse_tasks : true )
expect ( rendered_text ) . to match ( / <input.*checkbox.*valid unchecked task / )
expect ( rendered_text ) . to match (
/ <input.*checkbox.*valid lowercase checked task /
)
expect ( rendered_text ) . to match (
/ <input.*checkbox.*valid uppercase checked task /
)
end
it 'should render checkboxes at beginning of dash list items' do
rendered_text = markdown ( @source_text_dash , parse_tasks : true )
expect ( rendered_text ) . to match ( / <input.*checkbox.*valid unchecked task / )
expect ( rendered_text ) . to match (
/ <input.*checkbox.*valid lowercase checked task /
)
expect ( rendered_text ) . to match (
/ <input.*checkbox.*valid uppercase checked task /
)
end
it 'should not be confused by whitespace before bullets' do
rendered_text_asterisk = markdown ( @source_text_asterisk ,
parse_tasks : true )
rendered_text_dash = markdown ( @source_text_dash , parse_tasks : true )
expect ( rendered_text_asterisk ) . to match (
/ <input.*checkbox.*valid unchecked nested task /
)
expect ( rendered_text_asterisk ) . to match (
/ <input.*checkbox.*valid checked nested task /
)
expect ( rendered_text_dash ) . to match (
/ <input.*checkbox.*valid unchecked nested task /
)
expect ( rendered_text_dash ) . to match (
/ <input.*checkbox.*valid checked nested task /
)
end
it 'should not render checkboxes outside of list items' do
rendered_text = markdown ( @source_text_asterisk , parse_tasks : true )
expect ( rendered_text ) . not_to match (
/ <input.*checkbox.*not an unchecked task - no list item /
)
expect ( rendered_text ) . not_to match (
/ <input.*checkbox.*not a checked task - no list item /
)
end
it 'should not render checkboxes with invalid formatting' do
rendered_text = markdown ( @source_text_asterisk , parse_tasks : true )
expect ( rendered_text ) . not_to match (
/ <input.*checkbox.*not an unchecked task - too many spaces /
)
expect ( rendered_text ) . not_to match (
/ <input.*checkbox.*not a checked task - too many spaces /
)
expect ( rendered_text ) . not_to match (
/ <input.*checkbox.*not an unchecked task - no spaces /
)
expect ( rendered_text ) . not_to match (
/ Not a task.*<input.*checkbox.*not at beginning /
)
end
end
2012-08-02 00:26:21 +00:00
end