Class: MountainBerryFields::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/mountain_berry_fields/evaluator.rb

Overview

This class evaluates a block of code that was emitted from the parser. It has a document that the evalutable code can append to. It implements the visible and invisible methods (see the parser for more on these) that MountainBerryFields supports.

It also evaluates tests, and tracks whether they pass or fail (currently only tracking the last failure)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to_evaluate) ⇒ Evaluator

Returns a new instance of Evaluator.


14
15
16
# File 'lib/mountain_berry_fields/evaluator.rb', line 14

def initialize(to_evaluate)
  @to_evaluate = to_evaluate
end

Instance Attribute Details

#to_evaluateObject (readonly)

Returns the value of attribute to_evaluate


12
13
14
# File 'lib/mountain_berry_fields/evaluator.rb', line 12

def to_evaluate
  @to_evaluate
end

Class Method Details

.invisible_commandsObject


22
23
24
# File 'lib/mountain_berry_fields/evaluator.rb', line 22

def self.invisible_commands
  [:setup, :context]
end

.visible_commandsObject


18
19
20
# File 'lib/mountain_berry_fields/evaluator.rb', line 18

def self.visible_commands
  [:test]
end

Instance Method Details

#context(context_name, options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)

30
31
32
33
34
# File 'lib/mountain_berry_fields/evaluator.rb', line 30

def context(context_name, options={}, &block)
  contexts[context_name] = in_context options[:context], block.call
  return if contexts[context_name]['__CODE__']
  raise ArgumentError, "Context #{context_name.inspect} does not have a __CODE__ block"
end

#contextsObject


36
37
38
# File 'lib/mountain_berry_fields/evaluator.rb', line 36

def contexts
  @contexts ||= Hash.new
end

#documentObject


79
80
81
82
# File 'lib/mountain_berry_fields/evaluator.rb', line 79

def document
  evaluate
  @document ||= ''
end

#evaluateObject


84
85
86
87
88
# File 'lib/mountain_berry_fields/evaluator.rb', line 84

def evaluate
  return if @evaluated
  @evaluated = true
  instance_eval to_evaluate
end

#failure_messageObject


71
72
73
# File 'lib/mountain_berry_fields/evaluator.rb', line 71

def failure_message
  @failing_strategy.failure_message
end

#failure_nameObject


67
68
69
# File 'lib/mountain_berry_fields/evaluator.rb', line 67

def failure_name
  @failing_test.name
end

#in_context(context_name, code) ⇒ Object

Raises:

  • (NameError)

56
57
58
59
60
# File 'lib/mountain_berry_fields/evaluator.rb', line 56

def in_context(context_name, code)
  return code unless context_name
  return contexts[context_name].gsub '__CODE__', code if contexts[context_name]
  raise NameError, "There is no context #{context_name.inspect}, only #{contexts.keys.inspect}"
end

#setupObject


26
27
28
# File 'lib/mountain_berry_fields/evaluator.rb', line 26

def setup
  setup_code << yield
end

#setup_codeObject


40
41
42
# File 'lib/mountain_berry_fields/evaluator.rb', line 40

def setup_code
  @setup_code ||= ''
end

#test(name, options = {}, &block) ⇒ Object


44
45
46
47
48
49
50
51
52
53
# File 'lib/mountain_berry_fields/evaluator.rb', line 44

def test(name, options={}, &block)
  code = setup_code + in_context(options[:context], block.call.to_s)
  test = Test.new(name, options.merge(code: code))
  strategy = Test::Strategy.for(test.strategy).new(test.code)
  unless strategy.pass?
    @failing_strategy = strategy
    @failing_test     = test
  end
  tests << test
end

#testsObject


75
76
77
# File 'lib/mountain_berry_fields/evaluator.rb', line 75

def tests
  @tests ||= []
end

#tests_pass?Boolean

Returns:

  • (Boolean)

62
63
64
65
# File 'lib/mountain_berry_fields/evaluator.rb', line 62

def tests_pass?
  evaluate
  !@failing_test
end