Jan 15 2008

RSpec + JRuby

dastels @ 10:17 pm

My coworker, Paul Zabelin, posted here on some ideas that we’ve been experimenting with using RSpec stories and JRuby.

Tags: 

Jan 15 2008

New and Improved: RSpec 1.1.2

dastels @ 3:48 am

Tonight RSpec-1.1.2 was released. See most of the details on David Chelimsky’s blog.

I take a personal interest in this release as it includes my first active submission to the codebase in some time.

The functionality I added relates to the definition of steps in the new story component. Up until now, you used a string to define a step. For example:

Given "a student named '$name'" do |name|
  #...
end

When "the student is given a grade of $grade" do |grade|
  #...
end

Then "the student should $pass_fail" do |pass_fail|
  #...
end

This would result in stories like the following:

Given a student named 'Mike'
When the student is given a grade of 40
Then the student should fail

Continually talking about “the student” is grating and very un-natural sounding. Sounds downright, bloody legalistic, actually. And a lawyer is the last thing we want to be accused of sounding like… other than maybe Denny Crane.

One approach to this would be to go to something like:

When "he is given a grade of $grade" do |grade|
  #...
end

Then "he should $pass_fail" do |pass_fail|
  #...
end

which would give us:

Given a student named "Mike"
When he is given a grade of 40
Then he should fail

Now, speaking of lawyers, we probably want to make this a little more PC and be able to do this:

Given a student named "Michelle"
When she is given a grade of 60
Then she should pass

We could conceivable create another set of steps for the feminine forms, refactoring to remove the duplication. That might suffice in the simple case, but it’s still rather crude. I’d like to be able to use a regular expression and create steps something like:

When /(he|she) is given a grade of (.*?)/ do |pronoun, grade|
  #...
end

Then /(he|she) should (.*?)/ do |pronoun, pass_fail|
  #...
end

With release 1.1.2, that’s exactly what you can do.

There are a couple things to point out:

  1. Alternatives need to be in a group to limit their scope.
  2. Whatever matches groups such as that (and any others) will be sent into the supplied block as arguments. As such they need to be accommodated by having a block parameter for each of them.
  3. Since this is already a regexp, no internal processing is done to it. With string step names, variables (of the form $<identifier>) are rewritten as (.*?). When using a regexp as the step name where there are variables, we much do this rewriting ourselves.

This new feature provides a new level of flexibility in defining story steps. Have fun with it.

Tags: 

Sep 19 2007

Everyone’s a pirate

dastels @ 5:23 pm

Seen on #caboose:

courtenay: we uses arrrrrrrspec

Tags: 

Sep 11 2007

Describing Equivalence Classes in Ruby with RSpec

dastels @ 1:45 pm

Here’s an article I wrote a while ago but didn’t get around to releasing.  Enjoy. 

Describing Equivalence Classes in Ruby with RSpec

Since writing that article, I’ve made good use of the code. Here’s an example of it in action: grouping the cards in a deck into their types. A second method then uses those groups to reassemble the cards into an ordered list.

  CARD_TYPES = ['basic land', 'land', 'creature', 'artifact',
                'enchantment', 'sorcery', 'instant', '']

  def maindeck_cards_grouped
    maindeck_cards.equivalence_classes(*CARD_TYPES) do |card, the_type|
      card.magic_card.card_type.downcase.include?(the_type)
    end
  end

  def maindeck_cards_ordered
    cards_by_class = maindeck_cards_grouped
    (CARD_TYPES.inject([]) {|cards, type| cards << cards_by_class[type]}).flatten
  end

Tags: 

Mar 30 2007

RSpec Autotest now a Rails Plugin

admin @ 12:30 am

RSpec Autotest now a Rails Plugin:

“Posted by Nick Sieger Wed, 15 Nov 2006 15:46:00 GMT

Inspired by a posting on the RSpec list and recent comments stating that my Auto RSpec hack wasn’t working, I’ve bitten the bullet and upgraded to RSpec 0.7.2, and made rspec_autotest a plugin in the process. So, here are the necessary incantations to auto-rspec your project.”

OK, so I’m a bit behind on RSpec developments. But this plugin makes a world of difference when using rspec.

While Nick mentions rspec 0.7.2 in the post, I’m using it with the latest (pre-trunk) 0.9.0 without any issues.

If you use rspec (with rails or not.. see rspec-autotest-for-standalone-projects) you should be using this plugin!

Tags: 

Oct 29 2006

RSpec On Rails

admin @ 4:58 pm

Nice post by Defiler on RSpec:

RSpec On Rails:

RSpec is mere days away from a new release with greatly improved Rails support.

Since people are currently paying me to write Rails code, rather than plain old standalone Ruby (hint hint), I’ve been waiting for these features before making serious use of RSpec.

As an exercise, I ‘ported’ the acts_as_authenticated controller tests to RSpec. The results were fairly interesting. Subjectively, I find it more readable than the test/unit version. Objectively, one of the test/unit test cases doesn’t get run because there are two methods with the same name.

So far, so good. Anyone who thinks that RSpec is ‘only’ about a different set of terminology should give it a serious try first.

(Via ~:caboose.)

Tags: 

Oct 24 2006

Encapsulation in Action

admin @ 5:35 pm

I’m an OO bigot, plain & simple. One of my soapboxes/sacred-cows/hot-buttons/whatever is encapsulation… or more to the point, the lack of it.

I was writing some code recently and thought it might make a nice example.

I was just starting with a rough idea: I have a Form object that has FormPages. Pages can be moved around, added, deleted, etc. But they need to be ordered… pages of a form get filled out in some logical sequence.

So here’s my initial spec:

context "A form" do

  setup do
    @form = Form.new
    @form.form_pages << FormPage.new(:name => ‘one’, :number => 3)
    @form.form_pages << FormPage.new(:name => ‘three’, :number => 2)
    @form.form_pages << FormPage.new(:name => ‘two’, :number => 1)
  end

  specify “should be able to order its pages” do
    @form.ordered_pages.collect {|page| page.number}.should_eql [1, 2, 3]
  end
end

Some code to make this work:

class Form < ActiveRecord::Base
  has_many :form_pages

  def ordered_pages
    form_pages.sort
  end
end

class FormPage < ActiveRecord::Base
  belongs_to :form
  has_many :form_fields

  def <=>(other_page)
    number <=> other_page.number
  end
end

And that works just fine. BUT I’m totally violating any semblance of encapsulation. Look at FormPage for starters. We’re reaching into other_page and pulling out its number. Not so bad maybe.. it’s an instance of FormPage too. But it’s still breaking encapsulation. It can be done cleaner with a bit of double dispatch fun. Try this:

class FormPage < ActiveRecord::Base
  belongs_to :form
  has_many :form_fields

  def <=>(other_page)
    other_page.compare_number_to(number)
  end

  def compare_number_to(other_page_number)
    other_page_number <=> number
  end
end

That still works, and things are kept nicely encapsulated.

Now let’s look at the spec:

specify "should be able to order its pages" do
  @form.ordered_pages.collect {|page| page.number}.should_eql [1, 2, 3]
end

That collect is leaving a bloody trail in its wake as it hacks its way through the pages, ripping out numbers like the organ collectors in Monty Python’s “The Meaning of Life”. What are the alternatives? Well.. to support sorting we just wrote FormPage#compare_number_to(other_page_number). Let’s see if we can’t use that:

specify "should be able to order its pages" do
  @form.ordered_pages.each_with_index do |page, index|
    page.compare_number_to(index + 1).should_eql 0
  end
end

That does it. Notice that this works because we had that method to use, and the FormPages in the fixture were number sequentially.. we could clean it up even more by numbering the pages starting with 0 (which is much more ruby-esque anyway):

context "A form" do

  setup do
    @form = Form.new
    @form.form_pages << FormPage.new(:name => ‘one’, :number => 2)
    @form.form_pages << FormPage.new(:name => ‘three’, :number => 1)
    @form.form_pages << FormPage.new(:name => ‘two’, :number => 0)
  end

  specify “should be able to order its pages” do
    @form.ordered_pages.each_with_index do |page, index|
      page.compare_number_to(index).should_eql 0
    end
  end
end

Nice. We can go further… I don’t really care for the exposure of the array of pages. I’d feel better with an addPage method:

class Form < ActiveRecord::Base
  has_many :form_pages

  def add_page(aPage)
    form_pages << aPage
  end

  def ordered_pages
    form_pages.sort
  end
end

That lets us have our context setup be like this:

setup do
  @form = Form.new
  @form.add_page(FormPage.new(:name => 'one', :number => 2))
  @form.add_page(FormPage.new(:name => 'three', :number => 1))
  @form.add_page(FormPage.new(:name => 'two', :number => 0))
end

That’s much nicer from a pure OO perspective. “But,” you say, “that’s not overly Rails-like!” So… it IS very OO. It’s up to you where you draw the line. But remember… just because we can ignore encapsulation doesn’t mean we should. Doing so couples our code to the schema, and the goal of OO is to manage and minimize the coupling in our code. For a one-off, throw-away, 15 minute hack.. who cares.. but a mission critical application needs more thought & care. And it’s amazing how many one-off, throw-away, 15 minute hacks turn into mission critical apps.

David Chelimsky suggested making one more step and removing the reliance on the comparison method. After all, that is an implementation detail and relying on it from the spec makes it hard to change… just like any dependancy. So:

context "A form" do

  setup do
    @form = Form.new
    @pages_in_order = [
      FormPage.new(:id => 10, :name => 'two',  :number => 0),
      FormPage.new(:id => 5,  :name => 'one',  :number => 1),
      FormPage.new(:id => 13, :name => 'zero', :number => 2)
    ]
    @form.add_page(@pages_in_order[2])
    @form.add_page(@pages_in_order[1])
    @form.add_page(@pages_in_order[0])
  end

  specify "should be able to order its pages" do
    @form.ordered_pages.each_with_index do |page, index|
     page.should_equal @pages_in_order[index]
   end
  end
end

This final version is much better. I’ll blame my not seeing it on being sick at the time.

Tags: 

Oct 22 2006

Heard on IRC

admin @ 1:12 am

Mike Pence: “BDD is the shizzit… all the goodness of TDD with less religiosity and more practicality”

Tags: 

Sep 13 2006

rSpec Bundle for TextMate

admin @ 11:07 pm

I’ve whipped up a simple TextMate bundle for making rSpec a bit nicer to work with. You can download it here.

You might need to unzip the file (Safari does automatically), and put it in ~/Library/ApplicationSupport/TextMate/Bundles. Note you might have to create the Bundles folder.

At the moment it tweaks the ruby rules for coloring to colour context, specify, setup, and teardown as keywords. It also adds snippets for the same four: context<tab>, specify<tab>, setup<tab>, and teardown<tab>.

Tags: 

Sep 09 2006

rSpec Cheat Sheet

admin @ 5:20 pm

I’ve made up a cheat sheet for rSpec. It’s checked into the rSpec repository on rubyforge. You can grab it directly here.

Tweaks will be made as rSpec moves forward.

Update: I should mention, it’s 2 pages with a cmd line reference reference on the second page. Works great printed two sided.

SafariScreenSnapz002.png

Tags: 

Next Page »