require 'test/unit' $LOAD_PATH << "../lib" require 'contract' class TC_Integration_Signature < Test::Unit::TestCase class EnumerableContract < Contract provides :each end module List module NonEmpty; end end class ListContract < Contract implies List def self.check_count() 3 end provides :each provides :[] provides :size do assert(@object.size >= 0) end end class NonEmptyListContract < ListContract implies List::NonEmpty provides :size do assert(@object.size > 0) end end class EmptyList def each() end def [](idx) end def size() 0 end fulfills ListContract end class NonEmptyList < EmptyList def size() 1 end fulfills NonEmptyListContract end class BrokenContract < Contract provides :size do [][] end end def test_contract_test() result = EnumerableContract.test([]) assert_equal(nil, result) result = EnumerableContract.test(5) assert_instance_of(Contract::ContractMismatch, result) end def test_contract_test_all() result = ListContract.test_all([]) assert_equal(nil, result) result = ListContract.test_all(Object.new) assert_instance_of(Array, result) assert_equal(ListContract.check_count, result.size) end def test_contract_fulfilled_by?() result = EnumerableContract.fulfilled_by?([]) assert_equal(true, result) result = EnumerableContract.fulfilled_by?(5) assert_equal(false, result) end def test_contract_enforce() assert_nothing_raised { ListContract.enforce([]) } assert_raise(Contract::ContractMismatch) do ListContract.enforce(5) end end def test_broken_contract() result = BrokenContract.test(5) assert_instance_of(Contract::ContractError, result) end def test_implies_non_module() assert_raise(TypeError) do Class.new(Contract) { implies 5 } end end def test_implications() assert_equal([List], ListContract.implications) end def test_inherited_implications() assert_equal([List, List::NonEmpty], NonEmptyListContract.implications) end def test_implies() assert_equal(true, EmptyList.ancestors.include?(List)) end def test_inherited_implies() assert_equal(true, NonEmptyList.ancestors.include?(List::NonEmpty)) end end