Welcome to mirror list, hosted at ThFree Co, Russian Federation.

string_regex_marker_spec.rb « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0cbe44eacf4d9ebe9325306cec47686254123537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::StringRegexMarker do
  describe '#mark' do
    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
        described_class.new(raw, rich).mark(/"[^"]+":\s*"(?<name>[^"]+)"/, group: :name) do |text, left:, right:, mode:|
          %{<a href="#">#{text}</a>}.html_safe
        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
      end
    end

    context 'with multiple occurrences' do
      let(:raw)  { %{a <b> <c> d} }
      let(:rich) { %{a &lt;b&gt; &lt;c&gt; d}.html_safe }
      let(:regexp) { /<[a-z]>/ }

      subject do
        described_class.new(raw, rich).mark(regexp) do |text, left:, right:, mode:|
          %{<strong>#{text}</strong>}.html_safe
        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

      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
    end
  end
end