Class: Cucumber::Glue::InvokeInWorld

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/glue/invoke_in_world.rb

Overview

Utility methods for executing step definitions with nice backtraces etc. TODO: add unit tests TODO: refactor for readability

Constant Summary collapse

INSTANCE_EXEC_OFFSET =
-3

Class Method Summary collapse

Class Method Details

.cucumber_compatible_arity?(args, block) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/cucumber/glue/invoke_in_world.rb', line 42

def self.cucumber_compatible_arity?(args, block)
  return true if block.arity == args.length
  return true if block.arity.negative? && args.length >= (block.arity.abs - 1)

  false
end

.cucumber_instance_exec_in(world, check_arity, pseudo_method, *args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cucumber/glue/invoke_in_world.rb', line 26

def self.cucumber_instance_exec_in(world, check_arity, pseudo_method, *args, &block)
  cucumber_run_with_backtrace_filtering(pseudo_method) do
    if check_arity && !cucumber_compatible_arity?(args, block)
      world.instance_exec do
        ari = block.arity
        ari = ari.negative? ? "#{ari.abs - 1}+" : ari
        s1 = ari == 1 ? '' : 's'
        s2 = args.length == 1 ? '' : 's'
        raise ArityMismatchError, "Your block takes #{ari} argument#{s1}, but the Regexp matched #{args.length} argument#{s2}."
      end
    else
      world.instance_exec(*args, &block)
    end
  end
end

.cucumber_run_with_backtrace_filtering(pseudo_method) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/cucumber/glue/invoke_in_world.rb', line 49

def self.cucumber_run_with_backtrace_filtering(pseudo_method)
  yield
rescue Exception => e
  instance_exec_invocation_line = "#{__FILE__}:#{__LINE__ - 2}:in `cucumber_run_with_backtrace_filtering'"
  replace_instance_exec_invocation_line!((e.backtrace || []), instance_exec_invocation_line, pseudo_method)
  raise e
end

.replace_instance_exec_invocation_line!(backtrace, instance_exec_invocation_line, pseudo_method) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cucumber/glue/invoke_in_world.rb', line 10

def self.replace_instance_exec_invocation_line!(backtrace, instance_exec_invocation_line, pseudo_method)
  return if Cucumber.use_full_backtrace

  instance_exec_pos = backtrace.index(instance_exec_invocation_line)
  return unless instance_exec_pos

  replacement_line = instance_exec_pos + INSTANCE_EXEC_OFFSET
  backtrace[replacement_line].gsub!(/`.*'/, "`#{pseudo_method}'") if pseudo_method

  depth = backtrace.count { |line| line == instance_exec_invocation_line }
  end_pos = depth > 1 ? instance_exec_pos : -1

  backtrace[replacement_line + 1..end_pos] = nil
  backtrace.compact!
end