summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb
blob: 672ee9d75c0cee659cf1c586de7864e963020726 (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
require 'spec_helper'

describe Gitlab::Ci::Pipeline::Expression::Statement do
  let(:pipeline) { build(:ci_pipeline) }
  let(:text) { '$VAR "text"' }

  subject do
    described_class.new(text, pipeline)
  end

  before do
    pipeline.variables.build([key: 'VARIABLE', value: 'my variable'])
  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
      let(:text) { '$VAR "text"' }

      it 'raises an error' do
        expect { subject.parse_tree }
          .to raise_error described_class::StatementError
      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
      end

      context 'when using a single token' do
        let(:text) { '$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
end