Improve Condition DSL framework:
Add the necessary API so that we can implement new tests easily as new standalone classes. In Condition::DSL: * Add #tests accessor; * Modify constructor so that it accepts the env; * Implement .define_test(keyword, class) method.
This commit is contained in:
parent
639bdc1c73
commit
3a0ce82799
@ -4,13 +4,23 @@ module Producer
|
|||||||
class DSL
|
class DSL
|
||||||
class << self
|
class << self
|
||||||
def evaluate(env, &block)
|
def evaluate(env, &block)
|
||||||
dsl = new(&block)
|
dsl = new(env, &block)
|
||||||
Condition.new(dsl.evaluate)
|
Condition.new(dsl.evaluate)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def define_test(keyword, klass)
|
||||||
|
define_method(keyword) do |*args|
|
||||||
|
@tests << klass.new(@env, *args)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(&block)
|
attr_accessor :tests
|
||||||
@block = block
|
|
||||||
|
def initialize(env, &block)
|
||||||
|
@env = env
|
||||||
|
@block = block
|
||||||
|
@tests = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def evaluate
|
def evaluate
|
||||||
|
@ -4,11 +4,12 @@ module Producer::Core
|
|||||||
describe Condition::DSL do
|
describe Condition::DSL do
|
||||||
let(:block) { proc { :some_condition_code } }
|
let(:block) { proc { :some_condition_code } }
|
||||||
let(:env) { double('env') }
|
let(:env) { double('env') }
|
||||||
subject(:dsl) { Condition::DSL.new(&block) }
|
subject(:dsl) { Condition::DSL.new(env, &block) }
|
||||||
|
|
||||||
describe '.evaluate' do
|
describe '.evaluate' do
|
||||||
it 'builds a new DSL sandbox with given code' do
|
it 'builds a new DSL sandbox with given env and code' do
|
||||||
expect(Condition::DSL).to receive(:new).with(&block).and_call_original
|
expect(Condition::DSL)
|
||||||
|
.to receive(:new).with(env, &block).and_call_original
|
||||||
Condition::DSL.evaluate(env, &block)
|
Condition::DSL.evaluate(env, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -32,10 +33,53 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '.define_test' do
|
||||||
|
let(:some_test_class) { double('SomeTest class') }
|
||||||
|
|
||||||
|
before do
|
||||||
|
Condition::DSL.define_test(:some_test, some_test_class)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'defines a new test keyword' do
|
||||||
|
expect(dsl).to respond_to :some_test
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'defined test keyword' do
|
||||||
|
let(:arguments) { [:some, :arguments] }
|
||||||
|
|
||||||
|
it 'builds a new test with the env and given arguments' do
|
||||||
|
expect(some_test_class).to receive(:new).with(env, arguments)
|
||||||
|
dsl.some_test(arguments)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'registers the new test' do
|
||||||
|
some_test = double('SomeTest instance')
|
||||||
|
allow(some_test_class).to receive(:new) { some_test }
|
||||||
|
dsl.some_test(arguments)
|
||||||
|
expect(dsl.tests).to include(some_test)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#initialize' do
|
describe '#initialize' do
|
||||||
it 'assigns the code' do
|
it 'assigns the code' do
|
||||||
expect(dsl.instance_eval { @block }).to be block
|
expect(dsl.instance_eval { @block }).to be block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'assigns the env' do
|
||||||
|
expect(dsl.instance_eval { @env }).to be env
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'assigns no test' do
|
||||||
|
expect(dsl.tests).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#tests' do
|
||||||
|
it 'returns the assigned tests' do
|
||||||
|
dsl.instance_eval { @tests = [:some_test] }
|
||||||
|
expect(dsl.tests).to eq [:some_test]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#evaluate' do
|
describe '#evaluate' do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user