Add missing specs for pipeline expression lexemes

This commit is contained in:
Grzegorz Bizon 2018-02-23 11:23:18 +01:00
parent ba70e50ec3
commit 84c14a4ac2
10 changed files with 135 additions and 7 deletions

View file

@ -11,7 +11,7 @@ module Gitlab
@right = right
end
def evaluate(variables)
def evaluate(**variables)
@left.evaluate(variables) == @right.evaluate(variables)
end

View file

@ -10,7 +10,7 @@ module Gitlab
@value = value
end
def evaluate(_)
def evaluate(**_)
nil
end

View file

@ -10,7 +10,7 @@ module Gitlab
@value = value
end
def evaluate(_)
def evaluate(**_)
@value.to_s
end

View file

@ -10,8 +10,8 @@ module Gitlab
@name = name
end
def evaluate(variables)
variables[@name]
def evaluate(**variables)
variables[@name.to_sym]
end
def self.build(string)

View file

@ -18,7 +18,7 @@ module Gitlab
@lexer = Expression::Lexer.new(statement)
@variables = pipeline.variables.map do |variable|
[variable.key, variable.value]
[variable.key.to_sym, variable.value]
end
end

View file

@ -0,0 +1,39 @@
require 'spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Lexeme::Equals do
let(:left) { double('left') }
let(:right) { double('right') }
describe '.build' do
it 'creates a new instance of the token' do
expect(described_class.build('==', left, right))
.to be_a(described_class)
end
end
describe '.type' do
it 'is an operator' do
expect(described_class.type).to eq :operator
end
end
describe '#evaluate' do
it 'returns false when left and right are not equal' do
allow(left).to receive(:evaluate).and_return(1)
allow(right).to receive(:evaluate).and_return(2)
operator = described_class.new(left, right)
expect(operator.evaluate(VARIABLE: 3)).to eq false
end
it 'returns true when left and right are equal' do
allow(left).to receive(:evaluate).and_return(1)
allow(right).to receive(:evaluate).and_return(1)
operator = described_class.new(left, right)
expect(operator.evaluate(VARIABLE: 3)).to eq true
end
end
end

View file

@ -0,0 +1,22 @@
require 'spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Lexeme::Null do
describe '.build' do
it 'creates a new instance of the token' do
expect(described_class.build('null'))
.to be_a(described_class)
end
end
describe '.type' do
it 'is a value lexeme' do
expect(described_class.type).to eq :value
end
end
describe '#evaluate' do
it 'always evaluates to `nil`' do
expect(described_class.new('null').evaluate).to be_nil
end
end
end

View file

@ -0,0 +1,30 @@
require 'spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Lexeme::String do
describe '.build' do
it 'creates a new instance of the token' do
expect(described_class.build('"my string"'))
.to be_a(described_class)
end
end
describe '.type' do
it 'is a value lexeme' do
expect(described_class.type).to eq :value
end
end
describe '#evaluate' do
it 'returns string value it is is present' do
string = described_class.new('my string')
expect(string.evaluate).to eq 'my string'
end
it 'returns an empty string if it is empty' do
string = described_class.new('')
expect(string.evaluate).to eq ''
end
end
end

View file

@ -0,0 +1,37 @@
require 'spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Lexeme::Variable do
describe '.build' do
it 'creates a new instance of the token' do
expect(described_class.build('$VARIABLE'))
.to be_a(described_class)
end
end
describe '.type' do
it 'is a value lexeme' do
expect(described_class.type).to eq :value
end
end
describe '#evaluate' do
it 'returns variable value if it is defined' do
variable = described_class.new('VARIABLE')
expect(variable.evaluate(VARIABLE: 'my variable'))
.to eq 'my variable'
end
it 'returns nil if it is not defined' do
variable = described_class.new('VARIABLE')
expect(variable.evaluate(OTHER: 'variable')).to be_nil
end
it 'returns an empty string if it is empty' do
variable = described_class.new('VARIABLE')
expect(variable.evaluate(VARIABLE: '')).to eq ''
end
end
end

View file

@ -69,7 +69,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
['$VAR == null', true],
['null == $VAR', true],
['$VARIABLE', 'my variable'],
['$VAR', nil],
['$VAR', nil]
]
statements.each do |expression, value|