debian-mirror-gitlab/spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb

94 lines
3.2 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2020-10-24 23:57:45 +05:30
require 'spec_helper'
2018-03-27 19:54:05 +05:30
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Ci::Pipeline::Expression::Parser do
2018-03-27 19:54:05 +05:30
describe '#tree' do
2020-10-24 23:57:45 +05:30
context 'validates simple operators' do
using RSpec::Parameterized::TableSyntax
where(:expression, :result_tree) do
'$VAR1 == "123"' | 'equals($VAR1, "123")'
'$VAR1 == "123" == $VAR2' | 'equals(equals($VAR1, "123"), $VAR2)'
'$VAR' | '$VAR'
'"some value"' | '"some value"'
'null' | 'null'
'$VAR1 || $VAR2 && $VAR3' | 'or($VAR1, and($VAR2, $VAR3))'
'$VAR1 && $VAR2 || $VAR3' | 'or(and($VAR1, $VAR2), $VAR3)'
'$VAR1 && $VAR2 || $VAR3 && $VAR4' | 'or(and($VAR1, $VAR2), and($VAR3, $VAR4))'
'$VAR1 && ($VAR2 || $VAR3) && $VAR4' | 'and(and($VAR1, or($VAR2, $VAR3)), $VAR4)'
2019-09-04 21:01:54 +05:30
end
2020-10-24 23:57:45 +05:30
with_them do
it { expect(described_class.seed(expression).tree.inspect).to eq(result_tree) }
2018-03-27 19:54:05 +05:30
end
end
2020-10-24 23:57:45 +05:30
context 'when combining && and OR operators' do
subject { described_class.seed('$VAR1 == "a" || $VAR2 == "b" && $VAR3 == "c" || $VAR4 == "d" && $VAR5 == "e"').tree }
2020-11-24 15:15:51 +05:30
it 'returns operations in a correct order' do
expect(subject.inspect)
.to eq('or(or(equals($VAR1, "a"), and(equals($VAR2, "b"), equals($VAR3, "c"))), and(equals($VAR4, "d"), equals($VAR5, "e")))')
2018-03-27 19:54:05 +05:30
end
end
2020-10-24 23:57:45 +05:30
context 'when using parenthesis' do
subject { described_class.seed('(($VAR1 == "a" || $VAR2 == "b") && $VAR3 == "c" || $VAR4 == "d") && $VAR5 == "e"').tree }
it 'returns operations in a correct order' do
expect(subject.inspect)
.to eq('and(or(and(or(equals($VAR1, "a"), equals($VAR2, "b")), equals($VAR3, "c")), equals($VAR4, "d")), equals($VAR5, "e"))')
2019-09-04 21:01:54 +05:30
end
end
2018-03-27 19:54:05 +05:30
context 'when expression is empty' do
2020-10-24 23:57:45 +05:30
it 'raises a parsing error' do
2019-09-04 21:01:54 +05:30
expect { described_class.seed('').tree }
.to raise_error Gitlab::Ci::Pipeline::Expression::Parser::ParseError
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
2020-10-24 23:57:45 +05:30
context 'when parenthesis are unmatched' do
2020-11-24 15:15:51 +05:30
where(:expression) do
[
'$VAR == (',
'$VAR2 == ("aa"',
'$VAR2 == ("aa"))',
'$VAR2 == "aa")',
'(($VAR2 == "aa")',
'($VAR2 == "aa"))'
]
2020-10-24 23:57:45 +05:30
end
2020-11-24 15:15:51 +05:30
with_them do
it 'raises a ParseError' do
expect { described_class.seed(expression).tree }
.to raise_error Gitlab::Ci::Pipeline::Expression::Parser::ParseError
2020-10-24 23:57:45 +05:30
end
end
end
2018-03-27 19:54:05 +05:30
end
end