Module: Rspec::Tabular

Defined in:
lib/rspec/tabular.rb,
lib/rspec/tabular/version.rb

Overview

rubocop:enable all

Constant Summary collapse

VERSION =
'0.2.0'

Instance Method Summary collapse

Instance Method Details

#inputs(*args) ⇒ Object



85
86
87
# File 'lib/rspec/tabular.rb', line 85

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

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rspec/tabular.rb', line 89

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 #{Hash[metadata[:inputs].zip input_values]}") do
    [:inputs].each_index do |i|
      key = [:inputs][i]
      let(key) { input_values[i] }
    end

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

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



126
127
128
129
130
131
132
133
134
135
136
137
138
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
# File 'lib/rspec/tabular.rb', line 126

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]
      let(key) { input_values[i] }
    end

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

#raise_error_with(*args) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/rspec/tabular.rb', line 117

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



108
109
110
111
112
113
114
115
# 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