From 3a0ce8279959dd7bca50de10997ca3e708d07596 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Sat, 17 Aug 2013 20:27:17 +0000 Subject: [PATCH] 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. --- lib/producer/core/condition/dsl.rb | 16 ++++++-- spec/producer/core/condition/dsl_spec.rb | 50 ++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lib/producer/core/condition/dsl.rb b/lib/producer/core/condition/dsl.rb index 9347352..4131e32 100644 --- a/lib/producer/core/condition/dsl.rb +++ b/lib/producer/core/condition/dsl.rb @@ -4,13 +4,23 @@ module Producer class DSL class << self def evaluate(env, &block) - dsl = new(&block) + dsl = new(env, &block) Condition.new(dsl.evaluate) end + + def define_test(keyword, klass) + define_method(keyword) do |*args| + @tests << klass.new(@env, *args) + end + end end - def initialize(&block) - @block = block + attr_accessor :tests + + def initialize(env, &block) + @env = env + @block = block + @tests = [] end def evaluate diff --git a/spec/producer/core/condition/dsl_spec.rb b/spec/producer/core/condition/dsl_spec.rb index 62ac89e..adbdb4c 100644 --- a/spec/producer/core/condition/dsl_spec.rb +++ b/spec/producer/core/condition/dsl_spec.rb @@ -4,11 +4,12 @@ module Producer::Core describe Condition::DSL do let(:block) { proc { :some_condition_code } } let(:env) { double('env') } - subject(:dsl) { Condition::DSL.new(&block) } + subject(:dsl) { Condition::DSL.new(env, &block) } describe '.evaluate' do - it 'builds a new DSL sandbox with given code' do - expect(Condition::DSL).to receive(:new).with(&block).and_call_original + it 'builds a new DSL sandbox with given env and code' do + expect(Condition::DSL) + .to receive(:new).with(env, &block).and_call_original Condition::DSL.evaluate(env, &block) end @@ -32,10 +33,53 @@ module Producer::Core 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 it 'assigns the code' do expect(dsl.instance_eval { @block }).to be block 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 describe '#evaluate' do