1 # Specifications for behavior common to all Mentionable implementations.
2 # Requires a shared context containing:
3 # - let(:subject) { "the mentionable implementation" }
4 # - let(:backref_text) { "the way that +subject+ should refer to itself in backreferences " }
5 # - let(:set_mentionable_text) { lambda { |txt| "block that assigns txt to the subject's mentionable_text" } }
7 def common_mentionable_setup
8 # Avoid name collisions with let(:project) or let(:author) in the surrounding scope.
9 let(:mproject) { create :project }
10 let(:mauthor) { subject.author }
12 let(:mentioned_issue) { create :issue, project: mproject }
13 let(:other_issue) { create :issue, project: mproject }
14 let(:mentioned_mr) { create :merge_request, source_project: mproject, source_branch: 'different' }
15 let(:mentioned_commit) { double('commit', sha: '1234567890abcdef').as_null_object }
17 # Override to add known commits to the repository stub.
18 let(:extra_commits) { [] }
20 # A string that mentions each of the +mentioned_.*+ objects above. Mentionables should add a self-reference
21 # to this string and place it in their +mentionable_text+.
23 "mentions ##{mentioned_issue.iid} twice ##{mentioned_issue.iid}, !#{mentioned_mr.iid}, " +
24 "#{mentioned_commit.sha[0..5]} and itself as #{backref_text}"
28 # Wire the project's repository to return the mentioned commit, and +nil+ for any
29 # unrecognized commits.
30 commitmap = { '123456' => mentioned_commit }
31 extra_commits.each { |c| commitmap[c.sha[0..5]] = c }
33 repo = double('repository')
34 repo.stub(:commit) { |sha| commitmap[sha] }
35 mproject.stub(repository: repo)
37 set_mentionable_text.call(ref_string)
41 shared_examples 'a mentionable' do
42 common_mentionable_setup
44 it 'generates a descriptive back-reference' do
45 subject.gfm_reference.should == backref_text
48 it "extracts references from its reference property" do
49 # De-duplicate and omit itself
50 refs = subject.references(mproject)
52 refs.should have(3).items
53 refs.should include(mentioned_issue)
54 refs.should include(mentioned_mr)
55 refs.should include(mentioned_commit)
58 it 'creates cross-reference notes' do
59 [mentioned_issue, mentioned_mr, mentioned_commit].each do |referenced|
60 Note.should_receive(:create_cross_reference_note).with(referenced, subject.local_reference, mauthor, mproject)
63 subject.create_cross_references!(mproject, mauthor)
66 it 'detects existing cross-references' do
67 Note.create_cross_reference_note(mentioned_issue, subject.local_reference, mauthor, mproject)
69 subject.has_mentioned?(mentioned_issue).should be_true
70 subject.has_mentioned?(mentioned_mr).should be_false
74 shared_examples 'an editable mentionable' do
75 common_mentionable_setup
77 it_behaves_like 'a mentionable'
79 it 'creates new cross-reference notes when the mentionable text is edited' do
80 new_text = "this text still mentions ##{mentioned_issue.iid} and #{mentioned_commit.sha[0..5]}, " +
81 "but now it mentions ##{other_issue.iid}, too."
83 [mentioned_issue, mentioned_commit].each do |oldref|
84 Note.should_not_receive(:create_cross_reference_note).with(oldref, subject.local_reference,
88 Note.should_receive(:create_cross_reference_note).with(other_issue, subject.local_reference, mauthor, mproject)
91 set_mentionable_text.call(new_text)
92 subject.notice_added_references(mproject, mauthor)