Sha256: ee435c189ae1d6684c50f6b80d8841aef7b47066e25ddf1faa5a7a210b2ee3f3

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# This file provides a few new assertions and class methods for expressing
# contracts in a comfortable way that does not require manually writing test
# methods.


class Contract < Test::Unit::TestCase
  # Tests that the tested Object provides the specified methods with
  # the specified behavior.
  #
  # This can be used like this:
  #   class ListContract < Contract
  #     provides :size do
  #       assert(@object.size >= 0, "#size should never be negative.")
  #     end
  #
  #     provides :include? 
  #
  #     provides :each do
  #       count = 0
  #       @object.each do |item|
  #         assert(@object.include?(item),
  #           "#each should only yield items that the list includes.")
  #         count += 1
  #       end
  #       assert_equal(@object.size, count,
  #         "#each should yield #size items.")
  #     end
  #   end
  def self.provides(*symbols, &block)
    symbols.each do |symbol|
      define_method(:"test_provides_#{symbol}") do
        assert_respond_to(@object, symbol)
        instance_eval(&block) if block
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carats-0.3.0 lib/carat-dev/interface_work/contracts/contract/lib/contract/assertions.rb