Module: RSpec::Tabular::ExampleGroup

Defined in:
lib/rspec/tabular.rb

Overview

Example group methods e.g. inside describe/context block

Instance Method Summary collapse

Instance Method Details

#inputs(*args) ⇒ Object



99
100
101
# File 'lib/rspec/tabular.rb', line 99

def inputs(*args)
  [:inputs] ||= args
end

#it_with(*input_values, &block) ⇒ Object Also known as: specify_with



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rspec/tabular.rb', line 103

def it_with(*input_values, &block)
  if block.nil? && ([:inputs].size == input_values.size - 1)
    expected_value = input_values.pop
    block = proc { is_expected.to eq(expected_value) }
  end

  context("with #{[:inputs].zip(input_values).to_h}") do
    [:inputs].each_index do |i|
      key = [:inputs][i]
      value = input_values[i]
      let(key) { _unwrap(value) }
    end

    example(nil, { input_values: input_values }, &block)
  end
end

#its_with(attribute, *input_values, &block) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rspec/tabular.rb', line 139

def its_with(attribute, *input_values, &block)
  if block.nil? && ([:inputs].size == input_values.size - 1)
    expected_value = input_values.pop
    block = proc { should eq(expected_value) }
  end

  describe("#{attribute} with #{input_values.join(', ')}") do
    if attribute.is_a?(Array)
      let(:__its_subject) { subject[*attribute] }
    else
      let(:__its_subject) do
        attribute_chain = attribute.to_s.split('.')
        attribute_chain.inject(subject) do |inner_subject, attr|
          inner_subject.send(attr)
        end
      end
    end

    def should(matcher = nil, message = nil) # rubocop:disable Lint/NestedMethodDefinition
      RSpec::Expectations::PositiveExpectationHandler.handle_matcher(
        __its_subject, matcher, message
      )
    end

    def should_not(matcher = nil, message = nil) # rubocop:disable Lint/NestedMethodDefinition
      RSpec::Expectations::NegativeExpectationHandler.handle_matcher(
        __its_subject, matcher, message
      )
    end

    [:inputs].each_index do |i|
      key = [:inputs][i]
      value = input_values[i]
      let(key) { _unwrap(value) }
    end

    example(nil, { input_values: input_values }, &block)
  end
end

#raise_error_with(*args) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/rspec/tabular.rb', line 130

def raise_error_with(*args)
  raise_error_args = args
  it_with_args     = raise_error_args.slice!(0, [:inputs].size)

  it_with(*it_with_args) do
    expect { subject }.to raise_error(*raise_error_args)
  end
end

#side_effects_with(*args) ⇒ Object

Example with an implicit subject execution



123
124
125
126
127
128
# File 'lib/rspec/tabular.rb', line 123

def side_effects_with(*args)
  it_with(*args) do
    subject
  rescue Exception # rubocop:disable Lint/SuppressedException, Lint/RescueException
  end
end

#with_context(proc, pretty_val = nil) ⇒ Object



179
180
181
# File 'lib/rspec/tabular.rb', line 179

def with_context(proc, pretty_val = nil)
  RSpec::Tabular::Wrapped.new(proc, pretty_val)
end