On Mon, 2003-02-17 at 16:54, Daniel Carrera wrote:
> I'm trying to figure out how to ust Test::Unit.  I've read the 
> documentation in:
> http://testunit.talbott.ws/doc/index.html
> 
> [...] here is some feedback:
> 
> 1) Include your name and email so that it's easier to send you feedback.

On the web page that you reference, there is a section called "Contact
Information".  It gives an email for feedback:  testunit / talbott.ws

:-)

> 2) Could you include an example of how I'd use Test::Unit?
> 
> Here is a simple case.  Say I write a private method to add two numbers:
> 
> class MyClass
>   private
>     def add(a,b)
>         return a+b
>     end
> end
> 
> And I want to - say - assert that the return value is larger than the 
> first input value.  How can I use Test::Unit to make this assertion?

The key to using Test::Unit is to declare a class derived from
Test::Unit::TestCase.  In this derived class you define methods that
begin with "test_".  Inside the methods you can assert anything you
want.  There are several versions of assert that you can use.  The major
one I use all the time are:

  assert condition, optional_message
  assert_equal expected_value, actual_value

There are also assertions that work with regular expressions and
exceptions.  See the following URL for the gory details ...

   http://testunit.talbott.ws/doc/classes/Test/Unit/Assertions.html

Ok, here's an example using your assertion ...  
  
  require 'test/unit'
  require 'myclass'

  class TestMyClass < Test::Unit::TestCase
    def test_greater
      myclass = MyClass.new
      assert myclass.send(:add, 1,2) > 1, "Should be greater"
    end
  end

Note the funky use of "send" in the assertion.  If the "add" method were
not private, I would have written the assertion as ...

     assert myclass.add(1,2) > 1, "Should be greater"

To run the above snippet, put it in a file (e.g. testmyclass.rb) and run

    ruby testmyclass.rb

That's all.  Test::Unit will automatically figure out where the test
classes are and automatically run them.

Does that help?

-- 
-- Jim Weirich     jweirich / one.net    http://w3.one.net/~jweirich
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct, 
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)