Module: ShapeOf::Assertions

Defined in:
lib/shape_of.rb

Overview

To be included in a MiniTest test class

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.use_proper_expected_actual_order!Object



150
151
152
# File 'lib/shape_of.rb', line 150

def self.use_proper_expected_actual_order!
  @proper_expected_actual_order = true
end

Instance Method Details

#assert_shape_of(arg1, arg2) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/shape_of.rb', line 154

def assert_shape_of(arg1, arg2)
  shape = object = nil
  if @proper_expected_actual_order
    shape = arg1
    object = arg2
  else
    shape = arg2
    object = arg1
  end

  validator = nil
  if shape.respond_to? :shape_of?
    validator = Validator.new(shape: shape, object: object)
  elsif shape.instance_of? ::Array
    validator = Validator.new(shape: Array[shape.first], object: object)
  elsif shape.instance_of? ::Hash
    validator = Validator.new(shape: Hash[shape], object: object)
  else
    raise TypeError, "Expected #{Shape.inspect}, an #{::Array.inspect}, or a #{::Hash.inspect} as the shape"
  end

  assert validator.valid?, validator.error_message
end

#refute_shape_of(arg1, arg2) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/shape_of.rb', line 178

def refute_shape_of(arg1, arg2)
  shape = object = nil
  if @proper_expected_actual_order
    shape = arg1
    object = arg2
  else
    shape = arg2
    object = arg1
  end

  validator = nil
  if shape.respond_to? :shape_of?
    validator = Validator.new(shape: shape, object: object)
  elsif shape.instance_of? ::Array
    validator = Validator.new(shape: Array[shape.first], object: object)
  elsif shape.instance_of? ::Hash
    validator = Validator.new(shape: Hash[shape], object: object)
  else
    raise TypeError, "Expected #{Shape.inspect}, an #{::Array.inspect}, or a #{::Hash.inspect} as the shape"
  end

  refute validator.valid?, "#{shape} is shape_of? #{object}"
end