module Rspec::Tabular

rubocop:enable all

Constants

VERSION

Public Instance Methods

inputs(*args) click to toggle source
# File lib/rspec/tabular.rb, line 85
def inputs(*args)
  metadata[:inputs] ||= args
end
it_with(*input_values, &block) click to toggle source
# File lib/rspec/tabular.rb, line 89
def it_with(*input_values, &block)
  if block.nil? && (metadata[:inputs].size == input_values.size - 1)
    expected_value = input_values.pop
    block = proc { is_expected.to eq(expected_value) }
  end

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

    example(nil, { input_values: input_values }, &block)
  end
end
Also aliased as: specify_with
its_with(attribute, *input_values, &block) click to toggle source
# File lib/rspec/tabular.rb, line 126
def its_with(attribute, *input_values, &block)
  if block.nil? && (metadata[: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

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

    example(nil, { input_values: input_values }, &block)
  end
end
raise_error_with(*args) click to toggle source
# File lib/rspec/tabular.rb, line 117
def raise_error_with(*args)
  raise_error_args = args
  it_with_args     = raise_error_args.slice!(0, metadata[:inputs].size)

  it_with(*it_with_args) do
    expect { subject }.to raise_error(*raise_error_args)
  end
end
should(matcher = nil, message = nil) click to toggle source
# File lib/rspec/tabular.rb, line 144
def should(matcher = nil, message = nil) # rubocop:disable Lint/NestedMethodDefinition
  RSpec::Expectations::PositiveExpectationHandler.handle_matcher(
    __its_subject, matcher, message
  )
end
should_not(matcher = nil, message = nil) click to toggle source
# File lib/rspec/tabular.rb, line 150
def should_not(matcher = nil, message = nil) # rubocop:disable Lint/NestedMethodDefinition
  RSpec::Expectations::NegativeExpectationHandler.handle_matcher(
    __its_subject, matcher, message
  )
end
side_effects_with(*args) click to toggle source

Example with an implicit subject execution

# File lib/rspec/tabular.rb, line 108
def side_effects_with(*args)
  it_with(*args) do
    begin
      subject
    rescue Exception # rubocop:disable Lint/HandleExceptions, Lint/RescueException
    end
  end
end
specify_with(*input_values, &block)
Alias for: it_with