summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/expression/lexeme/matches_spec.rb
blob: 49e5af52f4d64a384c64dd470dc0a8efe6195767 (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
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
    it 'creates a new instance of the token' do
      expect(described_class.build('=~', left, right))
        .to be_a(described_class)
    end
  end

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

  describe '#evaluate' do
    it 'returns false when left and right do not match' do
      allow(left).to receive(:evaluate).and_return('my-string')
      allow(right).to receive(:evaluate)
        .and_return(Gitlab::UntrustedRegexp.new('something'))

      operator = described_class.new(left, right)

      expect(operator.evaluate).to eq false
    end

    it 'returns true when left and right match' do
      allow(left).to receive(:evaluate).and_return('my-awesome-string')
      allow(right).to receive(:evaluate)
        .and_return(Gitlab::UntrustedRegexp.new('awesome.string$'))

      operator = described_class.new(left, right)

      expect(operator.evaluate).to eq true
    end

    it 'supports matching against a nil value' do
      allow(left).to receive(:evaluate).and_return(nil)
      allow(right).to receive(:evaluate)
        .and_return(Gitlab::UntrustedRegexp.new('pattern'))

      operator = described_class.new(left, right)

      expect(operator.evaluate).to eq false
    end

    it 'supports multiline strings' do
      allow(left).to receive(:evaluate).and_return <<~TEXT
        My awesome contents

        My-text-string!
      TEXT

      allow(right).to receive(:evaluate)
        .and_return(Gitlab::UntrustedRegexp.new('text-string'))

      operator = described_class.new(left, right)

      expect(operator.evaluate).to eq true
    end

    it 'supports regexp flags' do
      allow(left).to receive(:evaluate).and_return <<~TEXT
        My AWESOME content
      TEXT

      allow(right).to receive(:evaluate)
        .and_return(Gitlab::UntrustedRegexp.new('(?i)awesome'))

      operator = described_class.new(left, right)

      expect(operator.evaluate).to eq true
    end
  end
end