summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb
blob: e88ec5561b654e17888fed7cc8380ed3890d23a2 (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
require 'fast_spec_helper'

describe Gitlab::Ci::Pipeline::Expression::Parser do
  describe '#tree' do
    context 'when using two operators' do
      it 'returns a reverse descent parse tree' do
        expect(described_class.seed('$VAR1 == "123"').tree)
          .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Equals
      end
    end

    context 'when using three operators' do
      it 'returns a reverse descent parse tree' do
        expect(described_class.seed('$VAR1 == "123" == $VAR2').tree)
          .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Equals
      end
    end

    context 'when using a single variable token' do
      it 'returns a single token instance' do
        expect(described_class.seed('$VAR').tree)
          .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Variable
      end
    end

    context 'when using a single string token' do
      it 'returns a single token instance' do
        expect(described_class.seed('"some value"').tree)
          .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::String
      end
    end

    context 'when expression is empty' do
      it 'returns a null token' do
        expect { described_class.seed('').tree }
          .to raise_error Gitlab::Ci::Pipeline::Expression::Parser::ParseError
      end
    end

    context 'when expression is null' do
      it 'returns a null token' do
        expect(described_class.seed('null').tree)
          .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Null
      end
    end

    context 'when two value tokens have no operator' do
      it 'raises a parsing error' do
        expect { described_class.seed('$VAR "text"').tree }
          .to raise_error Gitlab::Ci::Pipeline::Expression::Parser::ParseError
      end
    end

    context 'when an operator has no left side' do
      it 'raises an OperatorError' do
        expect { described_class.seed('== "123"').tree }
            .to raise_error Gitlab::Ci::Pipeline::Expression::Lexeme::Operator::OperatorError
      end
    end

    context 'when an operator has no right side' do
      it 'raises an OperatorError' do
        expect { described_class.seed('$VAR ==').tree }
            .to raise_error Gitlab::Ci::Pipeline::Expression::Lexeme::Operator::OperatorError
      end
    end
  end
end