summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb
blob: bba5db7904aa43f8a69f4ce99ef0755ed6907246 (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
139
140
141
142
143
144
145
146
147
148
require 'fast_spec_helper'
require 'rspec-parameterized'

describe Gitlab::Ci::Pipeline::Expression::Statement do
  subject do
    described_class.new(text, variables)
  end

  let(:variables) do
    { 'PRESENT_VARIABLE' => 'my variable',
      EMPTY_VARIABLE: '' }
  end

  describe '.new' do
    context 'when variables are not provided' do
      it 'allows to properly initializes the statement' do
        statement = described_class.new('$PRESENT_VARIABLE')

        expect(statement.evaluate).to be_nil
      end
    end
  end

  describe '#parse_tree' do
    context 'when expression is empty' do
      let(:text) { '' }

      it 'raises an error' do
        expect { subject.parse_tree }
          .to raise_error described_class::StatementError
      end
    end

    context 'when expression grammar is incorrect' do
      table = [
        '$VAR "text"',   # missing operator
        '== "123"',      # invalid left side
        '"some string"', # only string provided
        '$VAR ==',       # invalid right side
        'null',          # missing lexemes
        ''               # empty statement
      ]

      table.each do |syntax|
        context "when expression grammar is #{syntax.inspect}" do
          let(:text) { syntax }

          it 'raises a statement error exception' do
            expect { subject.parse_tree }
              .to raise_error described_class::StatementError
          end

          it 'is an invalid statement' do
            expect(subject).not_to be_valid
          end
        end
      end
    end

    context 'when expression grammar is correct' do
      context 'when using an operator' do
        let(:text) { '$VAR == "value"' }

        it 'returns a reverse descent parse tree' do
          expect(subject.parse_tree)
            .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Equals
        end

        it 'is a valid statement' do
          expect(subject).to be_valid
        end
      end

      context 'when using a single token' do
        let(:text) { '$PRESENT_VARIABLE' }

        it 'returns a single token instance' do
          expect(subject.parse_tree)
            .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Variable
        end
      end
    end
  end

  describe '#evaluate' do
    using RSpec::Parameterized::TableSyntax

    where(:expression, :value) do
      '$PRESENT_VARIABLE == "my variable"' | true
      '"my variable" == $PRESENT_VARIABLE' | true
      '$PRESENT_VARIABLE == null'          | false
      '$EMPTY_VARIABLE == null'            | false
      '"" == $EMPTY_VARIABLE'              | true
      '$EMPTY_VARIABLE'                    | ''
      '$UNDEFINED_VARIABLE == null'        | true
      'null == $UNDEFINED_VARIABLE'        | true
      '$PRESENT_VARIABLE'                  | 'my variable'
      '$UNDEFINED_VARIABLE'                | nil
      "$PRESENT_VARIABLE =~ /var.*e$/"     | true
      "$PRESENT_VARIABLE =~ /^var.*/"      | false
      "$EMPTY_VARIABLE =~ /var.*/"         | false
      "$UNDEFINED_VARIABLE =~ /var.*/"     | false
    end

    with_them do
      let(:text) { expression }

      it "evaluates to `#{params[:value].inspect}`" do
        expect(subject.evaluate).to eq value
      end
    end
  end

  describe '#truthful?' do
    using RSpec::Parameterized::TableSyntax

    where(:expression, :value) do
      '$PRESENT_VARIABLE == "my variable"' | true
      "$PRESENT_VARIABLE == 'no match'"    | false
      '$UNDEFINED_VARIABLE == null'        | true
      '$PRESENT_VARIABLE'                  | true
      '$UNDEFINED_VARIABLE'                | false
      '$EMPTY_VARIABLE'                    | false
      '$INVALID = 1'                       | false
      "$PRESENT_VARIABLE =~ /var.*/"       | true
      "$UNDEFINED_VARIABLE =~ /var.*/"     | false
    end

    with_them do
      let(:text) { expression }

      it "returns `#{params[:value].inspect}`" do
        expect(subject.truthful?).to eq value
      end
    end

    context 'when evaluating expression raises an error' do
      let(:text) { '$PRESENT_VARIABLE' }

      it 'returns false' do
        allow(subject).to receive(:evaluate)
          .and_raise(described_class::StatementError)

        expect(subject.truthful?).to be_falsey
      end
    end
  end
end