Introduce Recipe::DSL to eval recipe DSL code

This commit is contained in:
Thibault Jouan 2013-07-28 18:08:16 +00:00
parent e5d5fc0a41
commit 8e4f670fbf
2 changed files with 28 additions and 6 deletions

View File

@ -12,7 +12,18 @@ module Producer
end end
def evaluate def evaluate
Object.new.instance_eval @code dsl = DSL.new(@code)
end
class DSL
def initialize(code = nil, &block)
if code
instance_eval code
else
instance_eval &block
end
end
end end
end end
end end

View File

@ -4,7 +4,7 @@ module Producer::Core
describe Recipe do describe Recipe do
include FixturesHelpers include FixturesHelpers
let(:code) { 'some ruby code' } let(:code) { 'nil' }
subject(:recipe) { Recipe.new(code) } subject(:recipe) { Recipe.new(code) }
describe '.from_file' do describe '.from_file' do
@ -28,11 +28,22 @@ module Producer::Core
end end
describe '#evaluate' do describe '#evaluate' do
let(:message) { 'error from recipe' } it 'builds a recipe DSL sandbox' do
let(:code) { "raise '#{message}'" } expect(Recipe).to receive(:new).with(code).and_call_original
recipe.evaluate
end
end
it 'evaluates its code' do
expect { recipe.evaluate }.to raise_error(RuntimeError, message) describe Recipe::DSL do
let(:dsl) { Recipe::DSL.new &code }
describe '#new' do
let(:code) { Proc.new { raise 'error from recipe' } }
it 'evaluates its code' do
expect { dsl }.to raise_error(RuntimeError, 'error from recipe')
end
end end
end end
end end