Extract task DSL code and spec
This commit is contained in:
parent
2939123574
commit
b1c99dbeeb
@ -3,4 +3,5 @@ require 'producer/core/env'
|
||||
require 'producer/core/recipe'
|
||||
require 'producer/core/recipe/dsl'
|
||||
require 'producer/core/task'
|
||||
require 'producer/core/task/dsl'
|
||||
require 'producer/core/version'
|
||||
|
@ -11,20 +11,6 @@ module Producer
|
||||
def evaluate
|
||||
DSL.new &@block
|
||||
end
|
||||
|
||||
|
||||
class DSL
|
||||
ConditionNotMetError = Class.new(StandardError)
|
||||
|
||||
def initialize(&block)
|
||||
instance_eval &block
|
||||
rescue ConditionNotMetError
|
||||
end
|
||||
|
||||
def condition(&block)
|
||||
raise ConditionNotMetError unless block.call
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
18
lib/producer/core/task/dsl.rb
Normal file
18
lib/producer/core/task/dsl.rb
Normal file
@ -0,0 +1,18 @@
|
||||
module Producer
|
||||
module Core
|
||||
class Task
|
||||
class DSL
|
||||
ConditionNotMetError = Class.new(StandardError)
|
||||
|
||||
def initialize(&block)
|
||||
instance_eval &block
|
||||
rescue ConditionNotMetError
|
||||
end
|
||||
|
||||
def condition(&block)
|
||||
raise ConditionNotMetError unless block.call
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
39
spec/producer/core/task/dsl_spec.rb
Normal file
39
spec/producer/core/task/dsl_spec.rb
Normal file
@ -0,0 +1,39 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Producer::Core
|
||||
describe Task::DSL do
|
||||
subject(:dsl) { Task::DSL.new &block }
|
||||
|
||||
describe '#initialize' do
|
||||
let(:block) { Proc.new { raise 'error from task' } }
|
||||
|
||||
it 'evaluates its block' do
|
||||
expect { dsl }.to raise_error(RuntimeError, 'error from task')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#condition' do
|
||||
context 'condition is met (block evals to true)' do
|
||||
let(:block) { Proc.new {
|
||||
condition { true }
|
||||
raise 'error after condition'
|
||||
} }
|
||||
|
||||
it 'evaluates all the block' do
|
||||
expect { dsl }.to raise_error(RuntimeError, 'error after condition')
|
||||
end
|
||||
end
|
||||
|
||||
context 'condition is not met (block evals to false)' do
|
||||
let(:block) { Proc.new {
|
||||
condition { false }
|
||||
raise
|
||||
} }
|
||||
|
||||
it 'stops block evaluation' do
|
||||
expect { dsl }.not_to raise_error
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -18,41 +18,5 @@ module Producer::Core
|
||||
task.evaluate
|
||||
end
|
||||
end
|
||||
|
||||
describe Task::DSL do
|
||||
subject(:dsl) { Task::DSL.new &block }
|
||||
|
||||
describe '#initialize' do
|
||||
let(:block) { Proc.new { raise 'error from task' } }
|
||||
|
||||
it 'evaluates its block' do
|
||||
expect { dsl }.to raise_error(RuntimeError, 'error from task')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#condition' do
|
||||
context 'condition is met (block evals to true)' do
|
||||
let(:block) { Proc.new {
|
||||
condition { true }
|
||||
raise 'error after condition'
|
||||
} }
|
||||
|
||||
it 'evaluates all the block' do
|
||||
expect { dsl }.to raise_error(RuntimeError, 'error after condition')
|
||||
end
|
||||
end
|
||||
|
||||
context 'condition is not met (block evals to false)' do
|
||||
let(:block) { Proc.new {
|
||||
condition { false }
|
||||
raise
|
||||
} }
|
||||
|
||||
it 'stops block evaluation' do
|
||||
expect { dsl }.not_to raise_error
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user