[rspec-users] driving rspec from a Ruby script

Pat Maddox pergesu at gmail.com
Sat Jan 17 19:40:09 EST 2009

On Sat, Jan 17, 2009 at 6:48 AM, Ed Keith <lists at ruby-forum.com> wrote:
> Ashley Moran wrote:
>> On 16 Jan 2009, at 17:44, Ed Keith wrote:
>> You don't provide enough information for me to be sure, but what you
>> describe sounds sufficiently high-level enough to make Cucumber[1]
>> worth looking into.
>> If you search the archives of this list I posted a very bad example of
>> using Cucumber to spec binaries written in other languages (I used
>> sort, I think).
>> Ashley
>> [1] http://wiki.github.com/aslakhellesoy/cucumber
>> --
>> http://www.patchspace.co.uk/
>> http://aviewfromafar.net/
>> http://twitter.com/ashleymoran
> I looked at Cucumber, I'm not clear on what it does, but I do not think
> it is what I need.
> Going into the details: I am testing a C++ library. There are many test
> classes to test different aspects of the library. Some of the test cases
> as supposed to fail to compile when the library is misused. At first I
> used ruby to run the compilers and check the return value against 0 for
> success. For the tested that were supposed to fail to compile, that was
> all I needed. For the tests that were supposed to work I then ran the
> generated executable and tested the return value. So far everything is
> great.
> The library is supposed to be portable. So I am testing it with several
> different compilers on several different operating systems. I do not
> want to put platform specific information into the RSpec files, so I
> wrote shell scripts of Unix and batch files on windows to set up the
> environment for each compiler and then call RSpec with each
> configuration environment. Now I have two problems: 1) Batch language is
> real pain to do anything nontrivial in; 2) I have two sets of driver
> scripts to maintain.
> I should be able to solve both problems by using a portable scripting
> language for the driver scripts. My first thought was to use Python

What about...Ruby?

I think it would make a lot of sense to define a couple hashes/objects
that represent each compiler.  If you're just using different strings,
you can use a hash.

gcc = {:name => 'gcc', :command => 'gcc', :flags => '...'}
pcc = {:name => 'pcc', :command => 'pcc', :flags => '...'}
watcom = {:name => 'watcom', :command => 'wcm', :flags => '...'}

[gcc, pcc, watcom].each do |compiler|
  describe "#{compiler[:name] compiler" do
    it "should build the binary" do
      Dir.chdir(project_dir) { exec "make
COMPILER=#{compiler[:command]} FLAGS='#{compiler[:flags]}" }
      build_binary.exit_code.should == 0

You probably wouldn't put the compiler definitions right in there, but
you could if you wanted to.  But putting them in another file is easy
and good.

If you need more complex setup, create helper classes.

class GccCompiler
  def setup
    # create some files...

  def name; "gcc" end
  def command; "gcc" end

Same thing then, you create a new instance of each of these classes,
iterate through, call their setup method, etc.


More information about the rspec-users mailing list