Alex Young wrote:
> Allow me to shamelessly copy and paste from an old post of mine:
> 
> $ cat testable.rb
> module Test; module Unit; class TestCase; end; end; end
> 
> module Testable
>    def test_case(&block)
>      Class.new(Test::Unit::TestCase).module_eval &block
>    end
> end
> 
> $ cat builtin_tests.rb
> require 'testable'
> 
> class MyWorkingClass
>    extend Testable
> 
>    def foo(a,b)
>      return a+b
>    end
> 
>    test_case {
>      def test_foo_null
>        assert_equal 2, MyWorkingClass.new.foo(1,1)
>      end
>    }
> end
> 
> if __FILE__ == $0
>    puts MyWorkingClass.new.foo(1,1)
> end
> 
> $ ruby builtin_tests.rb
> 2
> $ testrb builtin_tests.rb
> Loaded suite builtin_tests.rb
> Started
> .
> Finished in 0.00095 seconds.
> 
> 1 tests, 1 assertions, 0 failures, 0 errors
> $
> 
> 
> Something like that?

No, the whole approach is different. I'd say the movies I linked to can 
explain it far better than I can.
With DBC, if contracts are enabled (in Eiffel you can disable contracts 
to get better performance), your method is tested everytime you call the 
method. It tests pre-conditions and/or post-conditions (providing you 
the old state for your post-conditions). Also after every call to a 
method the class invariant is tested to validate your object. That's the 
basic idea.

One way that could probably be implemented (though, only as of ruby 1.9 
due to &block params) might look this:

class DayOfTime
  extend DBC
  invariant {
    @hour.between?(0,23)
    @min.between?(0,59)
    @sec.between?(0,59)
  }

  define(:min=) {
    pre_condition { |value|
      value.kind_of?(Numeric)
      value.between?(0,59)
    }
    body { |value|
      @value = value
    }
    post_condition { |old, value|
      @hour == old.hour
      @min  == value
      @sec  == old.second
    }
  }
end

Regards
Stefan
-- 
Posted via http://www.ruby-forum.com/.