Implement `test_macro' recipe keyword
This commit is contained in:
@@ -16,6 +16,7 @@ require 'producer/core/actions/file_writer'
|
||||
|
||||
# condition tests (need to be defined before the condition DSL)
|
||||
require 'producer/core/test'
|
||||
require 'producer/core/tests/condition_test'
|
||||
require 'producer/core/tests/file_contains'
|
||||
require 'producer/core/tests/has_dir'
|
||||
require 'producer/core/tests/has_env'
|
||||
|
@@ -2,9 +2,9 @@ module Producer
|
||||
module Core
|
||||
class Condition
|
||||
class << self
|
||||
def evaluate(env, &block)
|
||||
def evaluate(env, *args, &block)
|
||||
dsl = DSL.new(env, &block)
|
||||
return_value = dsl.evaluate
|
||||
return_value = dsl.evaluate *args
|
||||
Condition.new(dsl.tests, return_value)
|
||||
end
|
||||
end
|
||||
|
@@ -3,12 +3,21 @@ module Producer
|
||||
class Condition
|
||||
class DSL
|
||||
class << self
|
||||
def define_test(keyword, klass)
|
||||
define_method(keyword) do |*args|
|
||||
@tests << klass.new(@env, *args)
|
||||
end
|
||||
define_method("no_#{keyword}") do |*args|
|
||||
@tests << klass.new(@env, *args, negated: true)
|
||||
def define_test(keyword, test)
|
||||
{
|
||||
keyword => false,
|
||||
"no_#{keyword}" => true
|
||||
}.each do |kw, negated|
|
||||
define_method(kw) do |*args|
|
||||
if test.respond_to? :call
|
||||
args = [test, *args]
|
||||
klass = Tests::ConditionTest
|
||||
else
|
||||
klass = test
|
||||
end
|
||||
t = klass.new(@env, *args, negated: negated)
|
||||
@tests << t
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -29,8 +38,8 @@ module Producer
|
||||
@tests = []
|
||||
end
|
||||
|
||||
def evaluate
|
||||
instance_eval &@block
|
||||
def evaluate(*args)
|
||||
instance_exec *args, &@block
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@@ -38,6 +38,10 @@ module Producer
|
||||
end
|
||||
end
|
||||
|
||||
def test_macro(name, dsl: Condition::DSL, &block)
|
||||
dsl.define_test(name, block)
|
||||
end
|
||||
|
||||
def set(key, value)
|
||||
env[key] = value
|
||||
end
|
||||
|
23
lib/producer/core/tests/condition_test.rb
Normal file
23
lib/producer/core/tests/condition_test.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
module Producer
|
||||
module Core
|
||||
module Tests
|
||||
class ConditionTest < Test
|
||||
def verify
|
||||
condition.met?
|
||||
end
|
||||
|
||||
def condition
|
||||
Condition.evaluate(env, *condition_args, &condition_block)
|
||||
end
|
||||
|
||||
def condition_args
|
||||
arguments.drop 1
|
||||
end
|
||||
|
||||
def condition_block
|
||||
arguments.first
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user