producer-core/spec/producer/core/recipe_spec.rb
Thibault Jouan 30e1930719 Implement Recipe::DSL#evaluate method:
Move recipe DSL evaluation in a dedicated #evaluate method, instead of
evaluating the code during the DSL instantiation.
2013-07-30 14:46:27 +00:00

108 lines
2.8 KiB
Ruby

require 'spec_helper'
module Producer::Core
describe Recipe do
include FixturesHelpers
let(:code) { 'nil' }
subject(:recipe) { Recipe.new(code) }
describe '.from_file' do
let (:filepath) { fixture_path_for 'recipes/empty.rb' }
subject(:recipe) { Recipe.from_file(filepath) }
it 'builds a recipe whose code is read from given file path' do
expect(recipe.code).to eq File.read(filepath)
end
it 'builds a recipe whose file path is set from given file path' do
expect(recipe.filepath).to eq filepath
end
end
describe '#initialize' do
it 'builds a recipe' do
expect(recipe).to be_a Recipe
end
end
describe '#code' do
it 'returns the assigned code' do
expect(recipe.code).to eq code
end
end
describe '#evaluate' do
it 'builds a recipe DSL sandbox' do
expect(Recipe::DSL).to receive(:new).with(code).and_call_original
recipe.evaluate
end
end
describe Recipe::DSL do
let(:code) { Proc.new { } }
subject(:dsl) { Recipe::DSL.new &code }
describe '#initialize' do
context 'when a string of code is given as argument' do
subject(:dsl) { Recipe::DSL.new 'nil' }
it 'returns the DSL instance' do
expect(dsl).to be_a Recipe::DSL
end
end
context 'when a code block is given as argument' do
subject(:dsl) { Recipe::DSL.new Proc.new { } }
it 'returns the DSL instance' do
expect(dsl).to be_a Recipe::DSL
end
end
end
describe '#evaluate' do
it 'evaluates its code' do
dsl = Recipe::DSL.new { raise 'error from recipe' }
expect { dsl.evaluate }.to raise_error(RuntimeError, 'error from recipe')
end
it 'returns itself' do
expect(dsl.evaluate).to eq dsl
end
end
context 'DSL specific methods' do
subject(:dsl) { Recipe::DSL.new(&code).evaluate }
describe '#source' do
let(:code) { "source '#{fixture_path_for 'recipes/error'}'" }
subject(:dsl) { Recipe::DSL.new code }
it 'sources the recipe given as argument' do
expect { dsl.evaluate }.to raise_error(RuntimeError, 'error from recipe')
end
end
describe '#tasks' do
let(:code) { Proc.new { task(:some_task) } }
it 'returns registered tasks' do
expect(dsl.tasks[0].name).to eq :some_task
end
end
describe '#task' do
let(:code) { Proc.new { task(:first); task(:last) } }
it 'registers tasks in declaration order' do
expect(dsl.tasks[0].name).to eq :first
expect(dsl.tasks[1].name).to eq :last
end
end
end
end
end
end