Starting to dig through the tests

One thing that has always amazed me about Ruby is how much you can get done with so little code. I was talking at lunch with our architect about the Ruby language tests (in %YourIronRubyFolder%\Tests\Ruby\Builtin) and I asked “Think they ported over test::unit first?”. Boy was I wrong.

I’ve only dug through a couple tests, but their elegance is already quite apparent, and they got there very simply. Here is an example test

Code (ruby)
  1.  
  2. describe “Array#new“ do
  3.   it “creates an empty array via language syntactical sugar“ do
  4.     a = []
  5.     a.length.should == 0
  6.   end

It reads very well, and then the output to the screen looks like this.

Array#new

it creates an empty array via language syntactical sugar: .

The IronRuby team achieved this feat with a very small amount of code in simple_test.rb (\Tests\Ruby\Util\simple_test.rb). They simply added a few methods to object that output the string to the screen, and yield the block for execution. Below are Describe, it and should, although there is more in simple_test.rb that I would encourage you to look at.

Code (ruby)
  1.  
  2. class Object
  3.   def describe(message)
  4.     puts “nn#{message}“
  5.     yield
  6.   end
  7.  
  8.   def it(name)
  9.     print “n  it #{name}: “
  10.     $name = name
  11.     yield
  12.   end
  13.  
  14.   def should
  15.     PositiveExpectation.new(self)
  16.   end

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Responses to “Starting to dig through the tests”

  1. Mike Woodhouse Says:

    Looks rather like rspec, possibly a stripped-down subset thereof.

    http://rspec.rubyforge.org/

    Doesn’t make it a bad thing, mind.

  2. Not at all.. The little I’ve seen of rspec has been impressive. Of course we’re all wondering what it will take to bring packages like rspec over, but not sure it’s time to ask that yet :)

  3. Please let me point out that the IronRuby specs are based on the ones from Rubinius.

    Rubinius has mini-spec which allows it to use rspec-notation without actually being able to run RSpec.

    Those specs are designed to be useful for *all* Ruby implementations.

Leave a Reply