debian-mirror-gitlab/spec/lib/gitlab/string_regex_marker_spec.rb

50 lines
1.6 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::StringRegexMarker do
2017-09-10 17:25:29 +05:30
describe '#mark' do
2018-03-27 19:54:05 +05:30
context 'with a single occurrence' do
let(:raw) { %{"name": "AFNetworking"} }
let(:rich) { %{<span class="key">"name"</span><span class="punctuation">: </span><span class="value">"AFNetworking"</span>}.html_safe }
subject do
2021-04-17 20:07:23 +05:30
described_class.new(raw, rich).mark(/"[^"]+":\s*"(?<name>[^"]+)"/, group: :name) do |text, left:, right:, mode:|
2020-01-01 13:55:28 +05:30
%{<a href="#">#{text}</a>}.html_safe
2018-03-27 19:54:05 +05:30
end
end
it 'marks the match' do
expect(subject).to eq(%{<span class="key">"name"</span><span class="punctuation">: </span><span class="value">"<a href="#">AFNetworking</a>"</span>})
expect(subject).to be_html_safe
2017-09-10 17:25:29 +05:30
end
end
2018-03-27 19:54:05 +05:30
context 'with multiple occurrences' do
let(:raw) { %{a <b> <c> d} }
let(:rich) { %{a &lt;b&gt; &lt;c&gt; d}.html_safe }
2021-09-30 23:02:18 +05:30
let(:regexp) { /<[a-z]>/ }
2018-03-27 19:54:05 +05:30
subject do
2021-09-30 23:02:18 +05:30
described_class.new(raw, rich).mark(regexp) do |text, left:, right:, mode:|
2020-01-01 13:55:28 +05:30
%{<strong>#{text}</strong>}.html_safe
2018-03-27 19:54:05 +05:30
end
end
it 'marks the matches' do
expect(subject).to eq(%{a <strong>&lt;b&gt;</strong> <strong>&lt;c&gt;</strong> d})
expect(subject).to be_html_safe
end
2021-09-30 23:02:18 +05:30
context 'with a Gitlab::UntrustedRegexp' do
let(:regexp) { Gitlab::UntrustedRegexp.new('<[a-z]>') }
it 'marks the matches' do
expect(subject).to eq(%{a <strong>&lt;b&gt;</strong> <strong>&lt;c&gt;</strong> d})
expect(subject).to be_html_safe
end
end
2017-09-10 17:25:29 +05:30
end
end
end