The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
RSpec/Textmate Pro-Tip

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Rick DeNatale

Posts: 269
Nickname: rdenatale
Registered: Sep, 2007

Rick DeNatale is a consultant with over three decades of experience in OO technology.
RSpec/Textmate Pro-Tip Posted: Sep 23, 2009 8:06 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Rick DeNatale.
Original Post: RSpec/Textmate Pro-Tip
Feed Title: Talk Like A Duck
Feed URL: http://talklikeaduck.denhaven2.com/articles.atom
Feed Description: Musings on Ruby, Rails, and other topics by an experienced object technologist.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Rick DeNatale
Latest Posts From Talk Like A Duck

Advertisement

I often need to output debugging info in specs when something just isn't working. No sweat, just put in a puts or two, and run the spec.

Of course I'm usually doing this in Textmate using the RSpec bundle, which displays the results in a nice window formatted with html.

RSpecFormattedResults

But that causes problems with my debugging output

Bad Puts Format

The problem is that the puts output is just raw text so the HTML formatter ignores line breaks.

At one of the Radiant hack fests, either John Long or Adam Williams taught this old dog a simple new trick. The suggestion was to define a new Kernel#rputs method as a wrapper for puts which 'wraps' the arguments in an html

 tag.  Then just call rputs (the r is for rspec of course) in the spec and or code under test.

Good Rputs Format

This works great as long as you run the specs under RSpec. But if you run them in the shell you see something like this:

$ spec spec/ri_cal/bugreports_spec.rb
  Booga booga
  
  this should be another line
  
....... Finished in 0.525324 seconds 7 examples, 0 failures

I've been putting up with this minor annoyance for a while, but this morning it struck me that I could define rputs conditionally depending on whether the spec was being run in Textmate or not. The trick is that Textmate defines one or more environment variables whose name starts with 'TM_'. So, without further ado, here's the new fragment from my spec_helper.rb:

  module Kernel
    if ENV.keys.find {|env_var| env_var.start_with?("TM_")}
      def rputs(*args)
        puts( *["
", args.collect {|a| CGI.escapeHTML(a.to_s)}, "
"
]) end else alias_method :rputs, :puts end end

With this change, running in the shell now looks like this:

$ spec spec/ri_cal/bugreports_spec.rb Booga booga this should be another line ....... Finished in 0.348086 seconds 7 examples, 0 failures

A nice refinement to my bag of tricks!

Read: RSpec/Textmate Pro-Tip

Topic: Learn Ruby Programming on YouTube Previous Topic   Next Topic Topic: Ruby Brainfuck golf

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use