Module: RSpec::Parameterized::ExampleGroupMethods

Included in:
Core::ExampleGroup
Defined in:
lib/rspec/parameterized.rb

Defined Under Namespace

Classes: Parameter

Instance Method Summary collapse

Instance Method Details

#where(*args, &b) ⇒ Object

Set parameters to be bound in specs under this example group.

## Example1

where(:a, :b, :answer) do
  [
    [1 , 2 , 3],
    [5 , 8 , 13],
    [0 , 0 , 0]
  ]
end

## Example2

using RSpec::Parameterized::TableSyntax
where(:a, :b, :answer) do
  1 | 2 | 3 >
  5 | 8 | 13 >
  0 | 0 | 0
end


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rspec/parameterized.rb', line 42

def where(*args, &b)

  if args.size == 1 && args[0].instance_of?(Hash)
    naming_func = args.first.delete(:case_names)
    params = args[0]
    first, *rest = params.values
    arg_names = params.keys
    arg_values = first.product(*rest)

    if naming_func && naming_func.respond_to?(:call)
      arg_names << :case_name
      arg_values.map! { |row| row << naming_func.call(*row) }
    end

    set_parameters(arg_names) {
      arg_values
    }
  elsif args.size == 0
    set_verbose_parameters(&b)
  else
    set_parameters(args, &b)
  end
end

#with_them(*args, &b) ⇒ Object

Use parameters to execute the block. The given block is converted into describes for each parameter set.

## Example

with_them do
  it "should do additions" do
    (a + b).should == answer
  end
end


76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rspec/parameterized.rb', line 76

def with_them(*args, &b)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  opts[:caller] = caller unless opts[:caller]
  args.push(opts)

  if @parameter.nil?
    @parameterized_pending_cases ||= []
    @parameterized_pending_cases << [args, b]
  else
    define_cases(@parameter, *args, &b)
  end
end