Build conditions with DSL evaluated tests:
In Condition: * Modify constructor to accepts tests and a default return value; * Implement #met?; * Modify #! so that it return the negated value returned by #met?. In Condition::DSL: * Modify .evaluate to build the condition with tests and the value returned by a the evaluated condition block. Add a basic Test base class, with env and arguments as attributes. Add some spec helpers to build some easily testable kind of Test instances (as test doubles).
This commit is contained in:
@@ -2,8 +2,10 @@ require 'spec_helper'
|
||||
|
||||
module Producer::Core
|
||||
describe Condition do
|
||||
let(:expression) { double('expression') }
|
||||
let(:condition) { Condition.new(expression) }
|
||||
include TestsHelpers
|
||||
|
||||
let(:tests) { [test_ok, test_ko] }
|
||||
subject(:condition) { Condition.new(tests) }
|
||||
|
||||
describe '.evaluate' do
|
||||
let(:env) { double('env') }
|
||||
@@ -25,14 +27,69 @@ module Producer::Core
|
||||
end
|
||||
|
||||
describe '#initialize' do
|
||||
it 'assigns the expression' do
|
||||
expect(condition.instance_eval { @expression }).to be expression
|
||||
it 'assigns the tests' do
|
||||
expect(condition.instance_eval { @tests }).to eq tests
|
||||
end
|
||||
|
||||
it 'assigns nil as a default return value' do
|
||||
expect(condition.instance_eval { @return_value }).to be nil
|
||||
end
|
||||
|
||||
context 'when a return value is given as argument' do
|
||||
let(:return_value) { :some_return_value }
|
||||
subject(:condition) { Condition.new(tests, return_value) }
|
||||
|
||||
it 'assigns the return value' do
|
||||
expect(condition.instance_eval { @return_value }).to eq return_value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#met?' do
|
||||
context 'when all tests are successful' do
|
||||
let(:tests) { [test_ok, test_ok] }
|
||||
|
||||
it 'returns true' do
|
||||
expect(condition.met?).to be true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when one test is unsuccessful' do
|
||||
let(:tests) { [test_ok, test_ko] }
|
||||
|
||||
it 'returns false' do
|
||||
expect(condition.met?).to be false
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there are no test' do
|
||||
subject(:condition) { Condition.new([], return_value) }
|
||||
|
||||
context 'and return value is truthy' do
|
||||
let(:return_value) { :some_truthy_value }
|
||||
|
||||
it 'returns true' do
|
||||
expect(condition.met?).to be true
|
||||
end
|
||||
end
|
||||
|
||||
context 'and return value is falsy' do
|
||||
let(:return_value) { nil }
|
||||
|
||||
it 'returns false' do
|
||||
expect(condition.met?).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#!' do
|
||||
it 'returns the negated expression' do
|
||||
expect(condition.!).to be !expression
|
||||
%w[true false].each do |b|
|
||||
context "when #met? return #{b}" do
|
||||
it 'returns the negated #met?' do
|
||||
expect(condition.!).to be !condition.met?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user