Commit 7babc59e authored by Grzegorz Bizon's avatar Grzegorz Bizon

Use parameterized RSpec to improve variables expressions specs

parent 65f4e7b2
require 'fast_spec_helper' require 'fast_spec_helper'
require 'rspec-parameterized'
describe Gitlab::Ci::Pipeline::Expression::Statement do describe Gitlab::Ci::Pipeline::Expression::Statement do
subject do subject do
...@@ -82,56 +83,56 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -82,56 +83,56 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
end end
describe '#evaluate' do describe '#evaluate' do
statements = [ using RSpec::Parameterized::TableSyntax
['$PRESENT_VARIABLE == "my variable"', true],
['"my variable" == $PRESENT_VARIABLE', true], where(:expression, :value) do
['$PRESENT_VARIABLE == null', false], '$PRESENT_VARIABLE == "my variable"' | true
['$EMPTY_VARIABLE == null', false], '"my variable" == $PRESENT_VARIABLE' | true
['"" == $EMPTY_VARIABLE', true], '$PRESENT_VARIABLE == null' | false
['$EMPTY_VARIABLE', ''], '$EMPTY_VARIABLE == null' | false
['$UNDEFINED_VARIABLE == null', true], '"" == $EMPTY_VARIABLE' | true
['null == $UNDEFINED_VARIABLE', true], '$EMPTY_VARIABLE' | ''
['$PRESENT_VARIABLE', 'my variable'], '$UNDEFINED_VARIABLE == null' | true
['$UNDEFINED_VARIABLE', nil], 'null == $UNDEFINED_VARIABLE' | true
["$PRESENT_VARIABLE =~ /var.*e$/", true], '$PRESENT_VARIABLE' | 'my variable'
["$PRESENT_VARIABLE =~ /^var.*/", false], '$UNDEFINED_VARIABLE' | nil
["$EMPTY_VARIABLE =~ /var.*/", false], "$PRESENT_VARIABLE =~ /var.*e$/" | true
["$UNDEFINED_VARIABLE =~ /var.*/", false] "$PRESENT_VARIABLE =~ /^var.*/" | false
] "$EMPTY_VARIABLE =~ /var.*/" | false
"$UNDEFINED_VARIABLE =~ /var.*/" | false
statements.each do |expression, value| end
context "when using expression `#{expression}`" do
with_them do
let(:text) { expression } let(:text) { expression }
it "evaluates to `#{value.inspect}`" do it "evaluates to `#{params[:value].inspect}`" do
expect(subject.evaluate).to eq value expect(subject.evaluate).to eq value
end end
end end
end end
end
describe '#truthful?' do describe '#truthful?' do
statements = [ using RSpec::Parameterized::TableSyntax
['$PRESENT_VARIABLE == "my variable"', true],
["$PRESENT_VARIABLE == 'no match'", false],
['$UNDEFINED_VARIABLE == null', true],
['$PRESENT_VARIABLE', true],
['$UNDEFINED_VARIABLE', false],
['$EMPTY_VARIABLE', false],
['$INVALID = 1', false],
["$PRESENT_VARIABLE =~ /var.*/", true],
["$UNDEFINED_VARIABLE =~ /var.*/", false]
]
statements.each do |expression, value| where(:expression, :value) do
context "when using expression `#{expression}`" do '$PRESENT_VARIABLE == "my variable"' | true
"$PRESENT_VARIABLE == 'no match'" | false
'$UNDEFINED_VARIABLE == null' | true
'$PRESENT_VARIABLE' | true
'$UNDEFINED_VARIABLE' | false
'$EMPTY_VARIABLE' | false
'$INVALID = 1' | false
"$PRESENT_VARIABLE =~ /var.*/" | true
"$UNDEFINED_VARIABLE =~ /var.*/" | false
end
with_them do
let(:text) { expression } let(:text) { expression }
it "returns `#{value.inspect}`" do it "returns `#{params[:value].inspect}`" do
expect(subject.truthful?).to eq value expect(subject.truthful?).to eq value
end end
end end
end
context 'when evaluating expression raises an error' do context 'when evaluating expression raises an error' do
let(:text) { '$PRESENT_VARIABLE' } let(:text) { '$PRESENT_VARIABLE' }
......
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