Class: Test::Spec::ShouldNot

Inherits:
Object
  • Object
show all
Includes:
Unit::Assertions
Defined in:
lib/test/spec.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, message = nil) ⇒ ShouldNot

Returns a new instance of ShouldNot.



203
204
205
206
# File 'lib/test/spec.rb', line 203

def initialize(object, message=nil)
  @object = object
  @message = message
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/test/spec.rb', line 282

def method_missing(name, *args, &block)
  # This will make raise call Kernel.raise, and self.raise call _raise.
  return _raise(*args, &block)  if name == :raise

  if @object.respond_to?("#{name}?")
    assert_block("#{name}? expected to be false. #{@message}") {
      not @object.__send__("#{name}?", *args)
    }
  else
    if @object.respond_to?(name)
      assert_block("#{name} expected to be false. #{@message}") {
        not @object.__send__("#{name}", *args)
      }
    else
      super
    end
  end
end

Instance Method Details

#_raise(*args, &block) ⇒ Object



245
246
247
248
# File 'lib/test/spec.rb', line 245

def _raise(*args, &block)
  block ||= @object
  assert_nothing_raised(*(args+[@message]), &block)
end

#add_assertionObject



208
209
210
# File 'lib/test/spec.rb', line 208

def add_assertion
  $TEST_SPEC_TESTCASE && $TEST_SPEC_TESTCASE.__send__(:add_assertion)
end

#be(*value) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/test/spec.rb', line 224

def be(*value)
  case value.size
  when 0
    self
  when 1
    if CustomShould === value.first 
      pass value.first
    else
      assert_not_same value.first, @object, @message
    end
  else
    Kernel.raise ArgumentError, "should.be needs zero or one argument"
  end
end

#be_nilObject



258
259
260
261
# File 'lib/test/spec.rb', line 258

def be_nil
  warn "Test::Spec::ShouldNot#be_nil is deprecated and will be removed in future versions."
  self.nil
end

#equal(value) ⇒ Object Also known as: ==



219
220
221
# File 'lib/test/spec.rb', line 219

def equal(value)
  assert_not_equal value, @object, @message
end

#match(value) ⇒ Object Also known as: =~



239
240
241
242
# File 'lib/test/spec.rb', line 239

def match(value)
  # Icky Regexp check
  assert_no_match value, @object, @message
end

#nilObject



254
255
256
# File 'lib/test/spec.rb', line 254

def nil
  assert_not_nil @object, @message
end

#not(*args) ⇒ Object



263
264
265
266
267
268
269
270
271
272
# File 'lib/test/spec.rb', line 263

def not(*args)
  case args.size
  when 0
    Should.new(@object, @message)
  when 1
    Should.new(@object, @message).pass(args.first)
  else
    raise ArgumentError, "#not takes zero or one argument(s)."
  end
end

#pass(custom) ⇒ Object



274
275
276
277
278
279
280
# File 'lib/test/spec.rb', line 274

def pass(custom)
  _wrap_assertion {
    begin
      assert !custom.matches?(@object), @message || custom.failure_message
    end
  }
end

#satisfy(&block) ⇒ Object



213
214
215
216
217
# File 'lib/test/spec.rb', line 213

def satisfy(&block)
  assert_block(@message || "not.satisfy block succeded.") {
    not yield @object
  }
end

#throwObject



250
251
252
# File 'lib/test/spec.rb', line 250

def throw
  assert_nothing_thrown(@message, &@object)
end