Make Recipe::DSL methods public

This commit is contained in:
Thibault Jouan 2014-01-19 18:11:24 +00:00
parent 42a82c9c80
commit 161c784cbe
2 changed files with 35 additions and 63 deletions

View File

@ -20,8 +20,6 @@ module Producer
self self
end end
private
def source(filepath) def source(filepath)
instance_eval File.read("./#{filepath}.rb"), "#{filepath}.rb" instance_eval File.read("./#{filepath}.rb"), "#{filepath}.rb"
end end

View File

@ -5,7 +5,7 @@ module Producer::Core
include FixturesHelpers include FixturesHelpers
let(:code) { proc { :some_recipe_code } } let(:code) { proc { :some_recipe_code } }
let(:env) { double('env').as_null_object } let(:env) { Env.new }
subject(:dsl) { Recipe::DSL.new(env, &code) } subject(:dsl) { Recipe::DSL.new(env, &code) }
describe '#initialize' do describe '#initialize' do
@ -53,76 +53,50 @@ module Producer::Core
end end
end end
context 'DSL specific methods' do
subject(:dsl) { described_class.new(env, &code).evaluate }
describe '#env' do
let(:code) { proc { env.some_message } }
it 'returns the current environment' do
expect(env).to receive :some_message
dsl.evaluate
end
end
describe '#source' do describe '#source' do
let(:filepath) { fixture_path_for 'recipes/throw' } let(:filepath) { fixture_path_for 'recipes/throw' }
let(:code) { "source '#{filepath}'" }
subject(:dsl) { described_class.new(env, code) }
it 'sources the recipe given as argument' do it 'sources the recipe given as argument' do
expect { dsl.evaluate }.to throw_symbol :recipe_code expect { dsl.source filepath }.to throw_symbol :recipe_code
end end
end end
describe '#target' do describe '#target' do
let(:code) { proc { target 'some_host.example' } } let(:host) { 'some_host.example' }
it 'registers the target host in the env' do it 'registers the target host in the env' do
expect(env).to receive(:target=).with('some_host.example') dsl.target host
dsl expect(env.target).to eq host
end end
end end
describe '#task' do describe '#task' do
let(:code) { proc { task(:some_task, :some, :arg) { :some_value } } } it 'registers a new evaluated task' do
expect { dsl.task(:some_task) { :some_task_code } }
it 'builds a new evaluated task' do .to change { dsl.tasks.count }.by 1
expect(Task)
.to receive(:evaluate).with(:some_task, env, :some, :arg) do |&b|
expect(b.call).to eq :some_value
end
dsl
end
it 'registers the new task' do
task = double('task').as_null_object
allow(Task).to receive(:new) { task }
expect(dsl.tasks).to include(task)
end end
end end
describe '#macro' do describe '#macro' do
let(:code) { proc { macro(:hello) { echo 'hello' } } }
it 'defines the new recipe keyword' do it 'defines the new recipe keyword' do
dsl.macro :hello
expect(dsl).to respond_to(:hello) expect(dsl).to respond_to(:hello)
end end
context 'when the new keyword is called' do context 'when a defined macro is called' do
let(:code) { proc { macro(:hello) { echo 'hello' }; hello } } before { dsl.macro(:hello) { :some_macro_code } }
it 'registers the new task' do it 'registers the new task' do
expect(dsl.tasks.first.actions.first).to be_an Actions::Echo expect { dsl.hello }.to change { dsl.tasks.count }.by 1
end end
end end
context 'when macro takes arguments' do context 'when a defined macro is called with arguments' do
let(:code) { proc { macro(:hello) { |e| echo e }; hello :arg } } before { dsl.macro(:hello) { |a, b| echo a, b } }
it 'evaluates task code with arguments' do it 'evaluates task code with arguments' do
expect(dsl.tasks.first.actions.first.arguments.first).to be :arg dsl.hello :some, :args
end expect(dsl.tasks.first.actions.first.arguments).to eq [:some, :args]
end end
end end
end end