This Time Self-Hosted
dark mode light mode Search

Ruby, tests and specs

While I started working on ruby-bombe, I also wanted to improve the tests of Ruby-Elf, possibly making them more explicit, comment them out so that they are more meaningful and not just to me, and so on. But when I started looking at the code, I found myself trying to start with some dynamic programming, trying to write methods that could generate the test methods, since otherwise I’ve been adding code over code that did just the same thing with very slight differences in names and similar.

So I decided to look into alternative testing frameworks, to see if there was something providing the features I felt I needed. While the basic Test::Unit support in Ruby suits pretty well ruby-bombe, which is mostly a functional library (I’m interested in behaviour rather than data), it does not seem to apply very well to Ruby-Elf where I’m instead testing returned values to ensure they conform to what I’m expecting from them.

I asked Jason from Ohloh if he had any suggestion, and pointed me at RSpec (which I knew by name but never tried myself) and ZenTest (which seems to be lacking any sort of decent documentation). I tried RSpec first and it seems to be almost what I need. Almost.

It might be that I haven’t found how to do it yet, so I’m here asking the ruby-knowing lazyweb some help. Basically, I’m fine with the description idea at the base of RSpec, and indeed it’s useful to see it this way:

describe "linux_amd64_dynamic_executable" do
  it_should_behave_like "dynamic ELF executables"
end

so that it actually runs the tests like I want them to be run:

linux_amd64_dynamic_executable
- should be an ELF file
- should be version 1
- should be an executable file
- should have a .dynamic section
- should have a .dynstr section

But I need to go much deeper. For instance, I’d like that I could for instance describe further the behaviour of the dynamic section, having code similar to this:

  it "should have a .dynamic section" do
    @elf.should have_section(".dynamic")
    @elf[".dynamic"].describe do
      it_should_behave_like "all dynamic sections"
    end
  end

and then having something like this as results:

linux_amd64_dynamic_executable
- should be an ELF file
- should be version 1
- should be an executable file
- should have a .dynamic section
  - should be a dynamic section
  - should be of dynamic type
  - should be called .dynamic
  - should have a final NULL entry
  - ...
- should have a .dynstr section
  - should be a string table
  - should be of string table type
  - ...

The problem is that, as far as I can see, I cannot use the describe method there to do what I need. Or if I can, the documentation does not tell me how and why… but as far as I can see, this only creates a new top-level example, which I don’t need in this case..

Does anybody know anything like this or do I have to hack my own rspec-alike?

Comments 1
  1. This is easy, you can nest describe blocks. You’d probably end up with something like this:<typo:code>shared_examples_for “a .dynamic section” do … tests here … it “should be called .dynamic” do @dynamic_section.name.should == “.dynamic” endendshared_examples_for “a dynamic ELF executable” do it “should have a .dynamic section” do @elf.should have_dynamic_section end describe “its .dynamic section” do it_should_behave_like “a dynamic section” endenddescribe “linux_amd64_dynamic_executable” do before :each do @elf = Elf.new(“executable_file”) @dynamic_section = elf.dynamic_section end it_should_behave_like “a dynamic ELF executable”end</typo:code>Mix and match any way you want. RSpec gives you a lot of freedom.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.