Commit 227e18ea authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add support for evaluating pipeline expression variables

parent 8c7374ca
......@@ -11,7 +11,7 @@ module Gitlab
@right = right
end
def evaluate(**variables)
def evaluate(variables)
@left.evaluate(variables) == @right.evaluate(variables)
end
......
......@@ -10,9 +10,13 @@ module Gitlab
@value = value
end
def evaluate(**_)
def evaluate(_)
nil
end
def self.build(value)
new(value)
end
end
end
end
......
......@@ -10,7 +10,7 @@ module Gitlab
@value = value
end
def evaluate(**_)
def evaluate(_)
@value.to_s
end
......
......@@ -10,7 +10,8 @@ module Gitlab
@name = name
end
def evaluate(**variables)
def evaluate(variables)
variables[@name]
end
def self.build(string)
......
......@@ -6,6 +6,7 @@ module Gitlab
LEXEMES = [
Expression::Lexeme::Variable,
Expression::Lexeme::String,
Expression::Lexeme::Null,
Expression::Lexeme::Equals
].freeze
......
......@@ -18,7 +18,7 @@ module Gitlab
@lexer = Expression::Lexer.new(statement)
@variables = pipeline.variables.map do |variable|
{ variable.key => variable.value }
[variable.key, variable.value]
end
end
......@@ -33,7 +33,7 @@ module Gitlab
end
def evaluate
parse_tree.evaluate(**@variables)
parse_tree.evaluate(@variables.to_h)
end
end
end
......
......@@ -60,4 +60,24 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
end
end
end
describe '#evaluate' do
statements = [
['$VARIABLE == "my variable"', true],
['"my variable" == $VARIABLE', true],
['$VARIABLE == null', false],
['$VAR == null', true],
['null == $VAR', true],
['$VARIABLE', 'my variable'],
['$VAR', nil],
]
statements.each do |expression, value|
it "evaluates `#{expression}` to `#{value}`" do
statement = described_class.new(expression, pipeline)
expect(statement.evaluate).to eq value
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment