Class: Speq::TestBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/speq.rb,
lib/speq/string_fmt.rb

Overview

Test code executes within the context of this class

Constant Summary collapse

METHODS =
%i[speq on does with of is].freeze

Instance Method Summary collapse

Constructor Details

#initialize(parent, &block) ⇒ TestBlock

Returns a new instance of TestBlock.



18
19
20
21
22
# File 'lib/speq.rb', line 18

def initialize(parent, &block)
  @outer_scope = eval('self', block.binding)
  @parent = parent
  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/speq.rb', line 28

def method_missing(method_name, *args, &block)
  if METHODS.include?(method_name) ||
     @parent.context && Question.question?(method_name)
    Expression.new(@parent).send(method_name, *args, &block)
  elsif @outer_scope.respond_to?(method_name)
    @outer_scope.send(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#inspectObject



7
8
9
# File 'lib/speq/string_fmt.rb', line 7

def inspect
  "block in '#{@parent.description}'"
end

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/speq.rb', line 39

def respond_to_missing?(method_name, *)
  METHODS.include?(method_name) ||
    @outer_scope.respond_to?(method_name) ||
    super
end

#speq(description, &block) ⇒ Object



24
25
26
# File 'lib/speq.rb', line 24

def speq(description, &block)
  @parent.speq(description, &block)
end