Class: Should

Inherits:
Object show all
Defined in:
lib/bacon.rb

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Should

Returns a new instance of Should.



300
301
302
303
# File 'lib/bacon.rb', line 300

def initialize(object)
  @object = object
  @negated = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/bacon.rb', line 344

def method_missing(name, *args, &block)
  name = "#{name}?"  if name.to_s =~ /\w[^?]\z/

  desc = @negated ? "not " : ""
  if (name == :'==') || (name == :'!=')
    desc << "#{name}\n"
    desc << "#{@object.inspect}\n"
    desc << "#{args[0].inspect}"
  else
    desc << @object.inspect << "." << name.to_s
    desc << "(" << args.map{|x|x.inspect}.join(", ") << ") failed"
  end

  satisfy(desc) { |x| x.__send__(name, *args, &block) }
end

Instance Method Details

#be(*args, &block) ⇒ Object Also known as: a, an



315
316
317
318
319
320
321
322
# File 'lib/bacon.rb', line 315

def be(*args, &block)
  if args.empty?
    self
  else
    block = args.shift  unless block_given?
    satisfy(*args, &block)
  end
end

#equal(value) ⇒ Object



360
# File 'lib/bacon.rb', line 360

def equal(value)         self == value      end

#flunk(reason = "Flunked") ⇒ Object

Raises:



365
366
367
# File 'lib/bacon.rb', line 365

def flunk(reason="Flunked")
  raise Bacon::Error.new(:failed, reason)
end

#identical_to(value) ⇒ Object Also known as: same_as



362
# File 'lib/bacon.rb', line 362

def identical_to(value)  self.equal? value  end

#match(value) ⇒ Object



361
# File 'lib/bacon.rb', line 361

def match(value)         self =~ value      end

#not(*args, &block) ⇒ Object



305
306
307
308
309
310
311
312
313
# File 'lib/bacon.rb', line 305

def not(*args, &block)
  @negated = !@negated

  if args.empty?
    self
  else
    be(*args, &block)
  end
end

#satisfy(*args, &block) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/bacon.rb', line 327

def satisfy(*args, &block)
  if args.size == 1 && String === args.first
    description = args.shift
  else
    description = ""
  end

  r = yield(@object, *args)
  if Bacon::Counter[:depth] > 0
    Bacon::Counter[:requirements] += 1
    raise Bacon::Error.new(:failed, description)  unless @negated ^ r
    r
  else
    @negated ? !r : !!r
  end
end