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.



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

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



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

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



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

def _raise(*args, &block)
  block ||= @object
  args << @message  if @message
  assert_nothing_raised(*args, &block)
end

#add_assertionObject



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

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

#be(*value) ⇒ Object



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

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



260
261
262
263
# File 'lib/test/spec.rb', line 260

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: ==



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

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

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



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

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

#nilObject



256
257
258
# File 'lib/test/spec.rb', line 256

def nil
  assert_not_nil @object, @message
end

#not(*args) ⇒ Object



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

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



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

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

#satisfy(&block) ⇒ Object



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

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

#throwObject



252
253
254
# File 'lib/test/spec.rb', line 252

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