Class: Handshake::MethodContract

Inherits:
Object
  • Object
show all
Defined in:
lib/handshake/handshake.rb

Overview

Class representing method contracts. Not for external use.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name) ⇒ MethodContract

Returns a new instance of MethodContract.



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

def initialize(method_name)
  @method_name = method_name
  @preconditions, @postconditions, @accepts, @returns = [], [], [], []
end

Instance Attribute Details

#acceptsObject

Returns the value of attribute accepts.



362
363
364
# File 'lib/handshake/handshake.rb', line 362

def accepts
  @accepts
end

#postconditionsObject

:nodoc:



361
362
363
# File 'lib/handshake/handshake.rb', line 361

def postconditions
  @postconditions
end

#preconditionsObject

:nodoc:



361
362
363
# File 'lib/handshake/handshake.rb', line 361

def preconditions
  @preconditions
end

#returnsObject

:nodoc:



361
362
363
# File 'lib/handshake/handshake.rb', line 361

def returns
  @returns
end

Instance Method Details

#accepts_varargs?Boolean

Returns:

  • (Boolean)


423
424
425
# File 'lib/handshake/handshake.rb', line 423

def accepts_varargs?
  @accepts.last.is_a? Array
end

#check_accepts!(*args, &block) ⇒ Object



427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/handshake/handshake.rb', line 427

def check_accepts!(*args, &block)
  @accepts.each_with_index do |expected_arg, i|
    # Varargs: consume all remaining arguments.
    if expected_arg.is_a? Array
      check_varargs!(args, expected_arg.first, i) and break
    end
    check_equivalence!(args[i], expected_arg)
  end
  if expects_block?
    check_equivalence!(block, @block)
  end
end

#check_conditions!(o, args, conditions) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/handshake/handshake.rb', line 391

def check_conditions!(o, args, conditions)
  conditions.each do |condition|
    o.class.instance_eval do
      define_method(:bound_condition_passes?, &(condition.block))
    end
    begin
      o.bound_condition_passes?(*args)
    rescue Test::Unit::AssertionFailedError => afe
      throw :contract, AssertionFailed.new(afe.message)
    rescue Exception => e
      throw :contract, e
    end
    o.class.send(:remove_method, :bound_condition_passes?)
  end
end

#check_equivalence!(given, expected) ⇒ Object



450
451
452
453
454
455
# File 'lib/handshake/handshake.rb', line 450

def check_equivalence!(given, expected)
  unless expected === given
    mesg = "expected #{expected.inspect}, received #{given.inspect}"
    throw :contract, ContractViolation.new(mesg)
  end
end

#check_post!(o, *args) ⇒ Object

Checks the postconditions of this contract against the given object and return values. Any assertions thrown are re-raised as Handshake::AssertionViolation errors.



380
381
382
# File 'lib/handshake/handshake.rb', line 380

def check_post!(o, *args)
  check_conditions!(o, args, @postconditions)
end

#check_pre!(o, *args) ⇒ Object

Checks the preconditions of this contract against the given object and arugment values. Any assertions thrown are re-raised as Handshake::AssertionFailed errors.



387
388
389
# File 'lib/handshake/handshake.rb', line 387

def check_pre!(o, *args)
  check_conditions!(o, args, @preconditions)
end

#check_returns!(*args) ⇒ Object



440
441
442
443
444
# File 'lib/handshake/handshake.rb', line 440

def check_returns!(*args)
  @returns.each_with_index do |expected, i|
    check_equivalence!(args[i], expected)
  end
end

#check_varargs!(given_args, expected, index) ⇒ Object



446
447
448
# File 'lib/handshake/handshake.rb', line 446

def check_varargs!(given_args, expected, index)
  given_args[index..-1].each {|arg| check_equivalence!(arg, expected)}
end

#defined?Boolean

Returns true only if this MethodContract has been set up to check for one or more contract conditions.

Returns:

  • (Boolean)


371
372
373
374
375
# File 'lib/handshake/handshake.rb', line 371

def defined?
  [ @preconditions, @postconditions, @accepts, @returns ].all? do |ary|
    ary.empty?
  end
end

#expects_block?Boolean

Returns:

  • (Boolean)


419
420
421
# File 'lib/handshake/handshake.rb', line 419

def expects_block?
  not @block.nil?
end