Module: RSpec::Paramz::ExampleGroupMethods

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

Instance Method Summary collapse

Instance Method Details

#paramz(*args, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/rspec/paramz.rb', line 9

def paramz(*args, &block)
  labels = args.first

  if !block_given? && !labels.any? { |label| subject_label?(label) }
    raise ArgumentError, "No block or subject given to paramz."
  end

  args[1..-1].each_slice(labels.length).with_index do |arg, index|
    pairs = [labels, arg].transpose.to_h

    context_name = '[' + pairs.map { |k, v| "#{RSpec::Paramz::PrettyPrint.inspect(k)} = #{RSpec::Paramz::PrettyPrint.inspect(v, false)}" }.join(' | ') + ']'

    context context_name do
      pairs.each do |label, val|
        if subject_label?(label)
          if label == :subject
            module_exec { val.is_a?(Proc) ? let(:_paramz_subject, &val) : let(:_paramz_subject) { val } }
            it { should == _paramz_subject }
            next
          end

          _subject, _subject_name = parse_subject(label)

          module_exec { _subject.is_a?(Proc) ? subject(_subject_name, &_subject) : subject(_subject_name) { _subject }  }

          unless block_given?
            module_exec { val.is_a?(Proc) ? let(:_paramz_subject, &val) : let(:_paramz_subject) { val } }
            it { should == _paramz_subject }
          end
        else
          module_exec { val.is_a?(Proc) ? let(label, &val) : let(label) { val } }
        end
      end

      module_eval(&block) if block_given?

      after(:each) do |example|
        if example.exception
          index_info = arg.map { |v| RSpec::Paramz::PrettyPrint.inspect(v, false) }.join(', ')
          example.exception.backtrace.push("failed paramz index is:#{index + 1}:[#{index_info}]")
        end
      end
    end
  end
end