Class: RSpecParameterizedContext::Context

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

Instance Method Summary collapse

Constructor Details

#initialize(rspec_context) ⇒ Context

Returns a new instance of Context.



5
6
7
8
9
10
11
12
# File 'lib/rspec_parameterized_context.rb', line 5

def initialize(rspec_context)
  @rspec_context = rspec_context
  @where_names = []
  @where_block = nil
  @where_size = nil

  @with_them_block = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



58
59
60
61
62
63
64
# File 'lib/rspec_parameterized_context.rb', line 58

def method_missing(action, *args, &block)
  if @rspec_context.respond_to?(action)
    @rspec_context.public_send(action, *args, &block)
  else
    super
  end
end

Instance Method Details

#define_specObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rspec_parameterized_context.rb', line 25

def define_spec
  raise '.where is not called' unless @where_block.respond_to?(:call)
  raise '.with_them is not called' unless @with_them_block.respond_to?(:call)

  where_block = @where_block
  where_names = @where_names
  where_size = @where_size
  where_focus_index = @where_focus_index
  with_them_block = @with_them_block

  where_size.times do |index|
    next if where_focus_index && index != where_focus_index

    @rspec_context.context("parameterized phase ##{index}") do
      let(:_parameters) do
        instance_exec(&where_block)
      end

      before do
        raise "expected where(..., size: #{_parameters.size})" unless _parameters.size == where_size
      end

      where_names.each_with_index do |name, parameter_index|
        let(name) do
          _parameters[index][parameter_index]
        end
      end

      instance_exec(&with_them_block)
    end
  end
end

#respond_to_missing?(action, include_private) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/rspec_parameterized_context.rb', line 68

def respond_to_missing?(action, include_private)
  @rspec_context.respond_to?(action, include_private) || super
end

#where(*names, size:, focus_index: nil, &block) ⇒ Object



14
15
16
17
18
19
# File 'lib/rspec_parameterized_context.rb', line 14

def where(*names, size:, focus_index: nil, &block)
  @where_names = names
  @where_size = size
  @where_focus_index = focus_index
  @where_block = block
end

#with_them(&block) ⇒ Object



21
22
23
# File 'lib/rspec_parameterized_context.rb', line 21

def with_them(&block)
  @with_them_block = block
end