IronRuby revision 77 in SVN

March 14th, 2008 Aaron Junod Posted in Source, UnitTests, IronRuby No Comments »

The IronRuby team committed revision 77 into RubyForge. Here is the commit comment :

Big set of changes from our Silverlight push. Things that are working here should be rudimentary debugging in VS under Desktop CLR.

Also the beginnings of our integration work with the latest build of Ruby specs (hence the explosion in # of files under tests). The older stuff will be removed once we get the new specs working.

This revision still won’t let you do a Silverlight build yet- that will come later.

The spec results for this revision can be found here, but don’t appear to have changed since R76.

AddThis Social Bookmark Button

Running all of IronRuby’s specs

February 25th, 2008 Aaron Junod Posted in UnitTests, IronRuby No Comments »

It’s easy to run all of the specs that have been included thus far. From the command line, change to the root of your IronRuby project. Then type

rake spec - - dox

That will run all of the specs thus far. If you’d like to see the output from the current revision, click here.

AddThis Social Bookmark Button

Testing .net with IronRuby’s mini_rspec.rb

February 21st, 2008 Aaron Junod Posted in UnitTests, Samples, IronRuby 4 Comments »

The IronRuby team has uses a couple facilities to test their own implementation of the Ruby language. simple_test.rb which resembles a Test::Unit type of framework with less baked in, and minispec by Brian Ford of the RubySpec project. (Thanks Charles for pointing out my error, sorry Brian for the wrong attribution). Minispec and RubySpec allow ruby implementations to test their compatibility among a common set of of specs produced by the RubySpec team. This allows the IronRuby team to run the same specs against Ruby, as well as run them directly through rbx.exe, the IronRuby interpreter. Once you get and compile IronRuby, you can see they have also created rake tasks to run the specs in a few scenarios. Here is the output of rake -T | grep spec.

rake rspec # run specs using Ruby - params are: class [method(…

rake spec # run specs - params are: class [method(s)] [reporter]

rake spec2 # run specs using IronRuby and Ruby - params are: c…

rake specs # run Ruby spec suite

So running rake spec from the command line will run all of the IronRuby specs through rbx. If you would like to take a look at all of these, the mini_spec framework is in %IronRubyRoot%\tests\ironruby\specs and all of the specs are in %IronRubyRoot%\tests\ironruby\specs\core.

One of the use cases I see IronRuby excelling at is writing unit tests. I’m a huge fan of RhinoMocks and NUnit, and am able to get a ton done with these tools, but after looking at testing in Ruby I’m always jealous of the tools the test centric Ruby community has at their disposal. There is no lack of elegant testing frameworks, tools, and practices on this side of the fence. Hopefully a good portion of them, if not all of them, will eventually make it over to IronRuby, but for now you can use these mini frameworks to test your .net code.

Disclaimer time, IronRuby is not even an alpha yet. Relying on these frameworks to do anything more then oooo look at me I’m testing .net with IronRuby demonstrations and practice is not suggested.

So on to the code. Let’s assume you are building a system that requires a person, and this person has a first and a last name. Imagine you have an class that represents that person that looks something like, or exactly like :

Code (c)
  1.  
  2. public class Person
  3.   {
  4.     public string FirstName { get; set; }
  5.     public string LastName { get; set; }
  6.     public string FullName
  7.     {
  8.       get { return string.Format("{0} {1}", FirstName, LastName); }
  9.     }
  10.     public int Age { get; set;}
  11.     public int AgeInDays
  12.     {
  13.       get { return Age*365; }
  14.     }
  15.   }

This complicated code MUST be tested. Below is some IronRuby code that works today that will test the class above. It looks like :

Code (ruby)
  1.  
  2. require ‘C:\projects\IronRuby\trunk\tests\ironruby\Specs\spec_helper’
  3. require ‘c:\PathToMyDll\Person.dll
  4. Nsp = NameSpaceThatTheAboveClassIsIn
  5.  
  6. describe ‘Nsp::Person’ do
  7.  
  8.   before :each do
  9.     @person = Nsp::Person.new
  10.   end
  11.  
  12.   it "should concatenate name" do
  13.     @person.first_name = "Bill"
  14.     @person.last_name = "Smith"
  15.     @person.full_name.to_str.should == "Bill Smith"
  16.   end
  17.  
  18.   it "should be able to calculate age in days" do
  19.     @person.age = 1
  20.     @person.age_in_days.should == 365
  21.   end
  22. end

That works today. Being a big fan of the spec syntax, I think this test reads very well, much less clutter then in a typical xUnit test.

Walking through the test, notice that we first bring in and alias the external resources we will need. Then we dive into a describe block that sets the initial context of the test. The describe method will either take a type or a string, but did not seem to like taking a .net type yet. Then we have a before filter that will be run before each test, setting initial state for the contexts below. Then we have a number of it blocks where specific tests are performed. Notice that once we have a Person instance, we can address it’s variables via Ruby conventions. My C# class has a FirstName property, but I can use the first_name convention in IronRuby, smooth.

Also of note, notice I had to call .to_str on @person.full_name. This is due to the fact that IronRuby string != CLR Strings as of yet. A recent thread on the mailing list reminds us all that the team will be fixing this later, but calling to_str will work.

Will I be doing this at work? Not yet, but the idea of using Ruby test tools to test my .net things is very appealing to me. Less cruft, mocking and stubbing should be a lot easier, and tools like AutoTest will change how I do TDD in .net if things come together as I am imagining.

AddThis Social Bookmark Button

Unit test results for second drop

September 1st, 2007 Aaron Junod Posted in UnitTests, IronRuby No Comments »

The results of the unit tests in the latest drop are here. The file is more the double the size, it’s obvious the team has been busy. If you want to compare these to the last run the results are here, as well as the tiny script to run them yourself

AddThis Social Bookmark Button

Using ruby to test IronRuby

July 27th, 2007 Aaron Junod Posted in UnitTests, IronRuby 1 Comment »

This is not pretty, but it’s a start. It started as a nobel goal of publishing an easy to read output of all the ruby tests here, but I’m not there yet, and really want to try to get a post up every weekday at least, so here’s what I’ve got so far.

I’m no ruby expert yet, but here is the script I wrote to run all the tests. It basically recursively searches all the paths below the “root” path for files named test_*, and then changes to that folder, and runs rbx filename, and appends the output to a text file. I’ve also thrown the text file up on the server here, although it desperately needs some formatting.

Code (ruby)
  1.  
  2. start_folder = ‘C:projectsIronRuby-Pre-Alpha1TestsRuby’
  3.  
  4. class TestRunner
  5.  
  6.         def initialize
  7.                 output_file_name = ‘c:IronRubyTests.log
  8.                 @output_file = File.open(output_file_name, ‘w’)
  9.         end
  10.  
  11.         def run_tests(folder, file)
  12.           putstest ‘ + file
  13.           @output_file << ‘Running tests in ‘ + file
  14.           Dir.chdir(folder)
  15.           @output_file << `rbx #{file}`
  16.         end
  17.  
  18.         def scan_folder(folder)
  19.           Dir.chdir(folder)
  20.           Dir.foreach(folder) do |f|
  21.                 if f != "." and f != ".."
  22.                   fullpath = folder + ” + f
  23.               run_tests(folder, f) if f =~ /test_*/
  24.               scan_folder(fullpath) if File.directory?(fullpath)
  25.             end
  26.           end
  27.         end
  28. end
  29.  
  30. tester = TestRunner.new
  31. tester.scan_folder(start_folder)

Please feel free to comment, I’m no ruby expert by any means yet, just hope to get there, and see IronRuby as a means to sneak it through IT, and hence learn it far more in depth. More on those devious little thoughts later :) Although most of the tests in IronRuby are still erroring, or I’m not running them right, I’m still very optimistic.

Edit, thanks Dan!

Dan, who will quickly get sick of me asking him ruby questions, commented on Dir.glob. When you use the right tools, it’s amazing what you can get done.

Code (ruby)
  1.  
  2. start_folder = ‘C:/projects/IronRuby-Pre-Alpha1/Tests/Ruby’
  3. @output_file = File.open(‘c:\tests.log‘, ‘w’)
  4.  
  5. def run_tests(file)
  6.   Dir.chdir(File.dirname(file))
  7.   puts ‘rbx #{File.basename(file)}’
  8.   @output_file << ‘Running tests in ‘ + file + "\r\n" + `rbx #{File.basename(file)}` + "\r\n"
  9. end
  10.        
  11. Dir.glob("#{start_folder}/**/test_*.rb").each do |f|
  12.         run_tests(f)
  13. end
  14.  
AddThis Social Bookmark Button

Starting to dig through the tests

July 25th, 2007 Aaron Junod Posted in UnitTests, IronRuby 3 Comments »

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
AddThis Social Bookmark Button