summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/expression/lexeme/matches_spec.rb
blob: d41b97742bf2e21106cab9dd3793c7b3ffe54e1f (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

require 'fast_spec_helper'
require_dependency 're2'

describe Gitlab::Ci::Pipeline::Expression::Lexeme::Matches do
  let(:left) { double('left') }
  let(:right) { double('right') }

  describe '.build' do
    context 'with non-evaluable operands' do
      it 'creates a new instance of the token' do
        expect { described_class.build('=~', left, right) }
          .to raise_error Gitlab::Ci::Pipeline::Expression::Lexeme::Operator::OperatorError
      end
    end

    context 'with evaluable operands' do
      it 'creates a new instance of the token' do
        allow(left).to receive(:evaluate).and_return('my-string')
        allow(right).to receive(:evaluate).and_return('/my-string/')

        expect(described_class.build('=~', left, right))
          .to be_a(described_class)
      end
    end
  end

  describe '.type' do
    it 'is an operator' do
      expect(described_class.type).to eq :operator
    end
  end

  describe '.precedence' do
    it 'has a precedence' do
      expect(described_class.precedence).to be_an Integer
    end
  end

  describe '#evaluate' do
    let(:operator) { described_class.new(left, right) }

    subject { operator.evaluate }

    before do
      allow(left).to receive(:evaluate).and_return(left_value)
      allow(right).to receive(:evaluate).and_return(right_value)
    end

    context 'when left and right do not match' do
      let(:left_value)  { 'my-string' }
      let(:right_value) { Gitlab::UntrustedRegexp.new('something') }

      it { is_expected.to eq(false) }
    end

    context 'when left and right match' do
      let(:left_value)  { 'my-awesome-string' }
      let(:right_value) { Gitlab::UntrustedRegexp.new('awesome.string$') }

      it { is_expected.to eq(true) }
    end

    context 'when left is nil' do
      let(:left_value)  { nil }
      let(:right_value) { Gitlab::UntrustedRegexp.new('pattern') }

      it { is_expected.to eq(false) }
    end

    context 'when right is nil' do
      let(:left_value)  { 'my-awesome-string' }
      let(:right_value) { nil }

      it { is_expected.to eq(false) }
    end

    context 'when left and right are nil' do
      let(:left_value)  { nil }
      let(:right_value) { nil }

      it { is_expected.to eq(false) }
    end

    context 'when left is a multiline string and matches right' do
      let(:left_value) do
        <<~TEXT
          My awesome contents

          My-text-string!
        TEXT
      end

      let(:right_value) { Gitlab::UntrustedRegexp.new('text-string') }

      it { is_expected.to eq(true) }
    end

    context 'when left is a multiline string and does not match right' do
      let(:left_value) do
        <<~TEXT
          My awesome contents

          My-terrible-string!
        TEXT
      end

      let(:right_value) { Gitlab::UntrustedRegexp.new('text-string') }

      it { is_expected.to eq(false) }
    end

    context 'when a matching pattern uses regex flags' do
      let(:left_value) do
        <<~TEXT
          My AWESOME content
        TEXT
      end

      let(:right_value) { Gitlab::UntrustedRegexp.new('(?i)awesome') }

      it { is_expected.to eq(true) }
    end

    context 'when a non-matching pattern uses regex flags' do
      let(:left_value) do
        <<~TEXT
          My AWESOME content
        TEXT
      end

      let(:right_value) { Gitlab::UntrustedRegexp.new('(?i)terrible') }

      it { is_expected.to eq(false) }
    end
  end
end