Implement basic condition DSL framework for tests:

Instead of interrupting task evaluation when condition is not met,
allow the whole task to be evaluated (including condition and
evaluation) so that the interpreter will get all tasks actions (whether
condition is met or not) and be able to query the condition.

* Modify Interpreter#process_task: test if task condition is met before
  applying the actions;
* Implement condition handling in Task and Task::DSL;
* Implement Condition and Condition::DSL (useless as they are, but
  needed to implement later test keywords as part of the condition DSL.
This commit is contained in:
Thibault Jouan
2013-08-08 01:52:48 +00:00
parent 0008f0255a
commit 639bdc1c73
12 changed files with 237 additions and 41 deletions

View File

@@ -0,0 +1,19 @@
module Producer
module Core
class Condition
class << self
def evaluate(env, &block)
DSL.evaluate(env, &block)
end
end
def initialize(expression)
@expression = expression
end
def !
!@expression
end
end
end
end

View File

@@ -0,0 +1,22 @@
module Producer
module Core
class Condition
class DSL
class << self
def evaluate(env, &block)
dsl = new(&block)
Condition.new(dsl.evaluate)
end
end
def initialize(&block)
@block = block
end
def evaluate
@block.call
end
end
end
end
end

View File

@@ -6,7 +6,7 @@ module Producer
end
def process_task(task)
task.actions.each(&:apply)
task.actions.each(&:apply) if task.condition_met?
end
end
end

View File

@@ -1,15 +1,20 @@
module Producer
module Core
class Task
attr_reader :name, :actions
attr_reader :name, :actions, :condition
def self.evaluate(name, env, &block)
DSL.evaluate(name, env, &block)
end
def initialize(name, actions = [])
@name = name
@actions = actions
def initialize(name, actions = [], condition = true)
@name = name
@actions = actions
@condition = condition
end
def condition_met?
!!@condition
end
end
end

View File

@@ -6,7 +6,7 @@ module Producer
def evaluate(name, env, &block)
dsl = new(&block)
dsl.evaluate(env)
Task.new(name, dsl.actions)
Task.new(name, dsl.actions, dsl.condition)
end
def define_action(keyword, klass)
@@ -22,20 +22,19 @@ module Producer
attr_accessor :actions
def initialize(&block)
@block = block
@actions = []
@block = block
@actions = []
@condition = true
end
def evaluate(env)
@env = env
instance_eval &@block
rescue ConditionNotMetError
end
private
def condition(&block)
fail ConditionNotMetError unless block.call
@condition = Condition.evaluate(@env, &block) if block
@condition
end
end
end