Sha256: 67a84b10d8982f41309afe75eb9943de1fe7ca37cc7fea7a3c4182ae269b60d8

Contents?: true

Size: 997 Bytes

Versions: 4

Compression:

Stored size: 997 Bytes

Contents

if Kernel::const_defined? :MiniTest
  class ContextAsserterBase < MiniTest::Unit::TestCase

  end
else
  class ContextAsserterBase

  end
end

class ContextAsserter < ContextAsserterBase
  def initialize(contracts, obj, is_asserting = true)
    raise 'Contracts must be supplied' unless contracts
    @obj = obj
    @contracts = contracts
    @is_asserting = is_asserting
  end

  def is_test
    Kernel::const_defined? :MiniTest
  end

  def can_play(role)
    msg = "#{@is_asserting ? 'Was' : "wasn't"} expected to be able to play #{role}".intern
    if is_test
      assert_equal(@is_asserting, can_play?(role), msg)
    else
      raise msg if @is_asserting != can_play?(role)
    end

  end

  private
  def can_play?(role)
    required_methods = @contracts[role]
    methods = @obj.public_methods
    missing = required_methods.select do |rm|
      !methods.include? rm.to_sym
    end
    missing.length == 0
  end
end
Context::with_contracts = true

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
maroon-0.8.0 lib/maroon/contracts.rb
maroon-0.7.1 lib/maroon/contracts.rb
maroon-0.7.0 lib/maroon/contracts.rb
maroon-0.6.5 lib/maroon/contracts.rb