Introduce Recipe::DSL to eval recipe DSL code
This commit is contained in:
parent
e5d5fc0a41
commit
8e4f670fbf
@ -12,7 +12,18 @@ module Producer
|
||||
end
|
||||
|
||||
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
|
||||
|
@ -4,7 +4,7 @@ module Producer::Core
|
||||
describe Recipe do
|
||||
include FixturesHelpers
|
||||
|
||||
let(:code) { 'some ruby code' }
|
||||
let(:code) { 'nil' }
|
||||
subject(:recipe) { Recipe.new(code) }
|
||||
|
||||
describe '.from_file' do
|
||||
@ -28,11 +28,22 @@ module Producer::Core
|
||||
end
|
||||
|
||||
describe '#evaluate' do
|
||||
let(:message) { 'error from recipe' }
|
||||
let(:code) { "raise '#{message}'" }
|
||||
it 'builds a recipe DSL sandbox' do
|
||||
expect(Recipe).to receive(:new).with(code).and_call_original
|
||||
recipe.evaluate
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
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 { recipe.evaluate }.to raise_error(RuntimeError, message)
|
||||
expect { dsl }.to raise_error(RuntimeError, 'error from recipe')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user