Only yield valid references in ReferenceFilter.references_in

This commit is contained in:
Douwe Maan 2017-02-16 14:06:22 -06:00
parent a2801ec429
commit 02aad721f1
3 changed files with 33 additions and 2 deletions

View file

@ -0,0 +1,4 @@
---
title: Only yield valid references in ReferenceFilter.references_in
merge_request:
author:

View file

@ -33,7 +33,12 @@ module Banzai
# Returns a String replaced with the return of the block.
def self.references_in(text, pattern = object_class.reference_pattern)
text.gsub(pattern) do |match|
yield match, $~[object_sym].to_i, $~[:project], $~[:namespace], $~
symbol = $~[object_sym]
if object_class.reference_valid?(symbol)
yield match, symbol.to_i, $~[:project], $~[:namespace], $~
else
match
end
end
end

View file

@ -311,7 +311,7 @@ describe Banzai::Filter::IssueReferenceFilter, lib: true do
end
end
describe '#issues_per_Project' do
describe '#issues_per_project' do
context 'using an internal issue tracker' do
it 'returns a Hash containing the issues per project' do
doc = Nokogiri::HTML.fragment('')
@ -346,4 +346,26 @@ describe Banzai::Filter::IssueReferenceFilter, lib: true do
end
end
end
describe '.references_in' do
let(:merge_request) { create(:merge_request) }
it 'yields valid references' do
expect do |b|
described_class.references_in(issue.to_reference, &b)
end.to yield_with_args(issue.to_reference, issue.iid, nil, nil, MatchData)
end
it "doesn't yield invalid references" do
expect do |b|
described_class.references_in('#0', &b)
end.not_to yield_control
end
it "doesn't yield unsupported references" do
expect do |b|
described_class.references_in(merge_request.to_reference, &b)
end.not_to yield_control
end
end
end