Showing posts with label helpers. Show all posts
Showing posts with label helpers. Show all posts

Tuesday, April 7, 2009

More Helper Specs

‹prev | My Chain | next›

A long softball game (8 walks an inning was typical), precludes me from making much progress tonight. Nevertheless I must offer up some work to the gods of #mychain, lest they take offense and smite me.

As fate would have it, I left off last night with some fairly simple helper specs pending implementation. So let's implement...

First up is that the wiki helper should convert textile to HTML. To specify this, I use an example of some very simple textile, which should be converted to HTML. The RedCloth library can take care of the full specification, I am only interested in specifying that it quacks like a textile converter:
  it "should convert textile to HTML" do
textile = <<_TEXTILE
paragraph 1 *bold text*

paragraph 2
_TEXTILE

wiki(textile).
should have_selector("p", :content => "paragraph 1 bold text") do |p|
p.should have_selector("strong", :content => "bold text")
end
end
Implementation of that is as easy as wrapping the wiki method around a RedCloth call:
    def wiki(text)
RedCloth.new(text).to_html
end
Next up is that the wiki call should wikify temperatures, which can be specified as:
  it "should wikify temperatures" do
wiki("250F").should contain("250° F")
end
This can be implemented as:
    def wiki(original)
text = original.dup
text.gsub!(/\b(\d+)F/, "\\1° F")
RedCloth.new(text).to_html
end
Seeing this capricious use of gsub immediately makes me think of the Eigenclass class article on markdown conversion.

I am doing bad things.
I am doing bad things.
I am doing bad things.

But, I must not prematurely optimize. If this does cost me in performance, I will deal with it later.

Staying focused on implementation rather than optimization, I move on to writing the example for wikifying kid's nicknames:
  it "should wikify kids names" do
self.stub!(:kid_nicknames).
and_return({"marsha" => "the oldest Brady girl"})

wiki("[kid:marsha]").should contain("the oldest Brady girl")
end
Implementing the code to make this example pass is a simple matter of another gsub:
    def wiki(original)
text = original.dup
text.gsub!(/\b(\d+)F/, "\\1° F")
text.gsub!(/\[kid:(\w+)\]/m) { |kid| kid_nicknames[$1] }
RedCloth.new(text).to_html
end

def kid_nicknames
{ }
end
There is still the matter of how to populate the kid_nicknames method. Hard-coding it will not do—there is not much point in obfuscating names if I make them publicly accessible elsewhere. I either need to make it a YAML configuration file or store it in CouchDB. I think I will ultimately choose to store it in CouchDB since that is easier to update (most days I think I have enough kids already, but you never know).

Since implementation will have to wait for tomorrow, I leave myself a note:
  it "should lookup kid nicknames in CouchDB"
and call it a day.
(commit)

Monday, April 6, 2009

Wiki Specifcations

‹prev | My Chain | next›

Realizing that I had overlooked recipe summary and instructions, I get started on them tonight. The feature scenario:
    Scenario: Viewing summary and recipe instructions

Given a recipe for Curried Shrimp
When I view the recipe
Then I should a nice summary of the dish
And I should see detailed, easy-to-read instructions
The inside view specs are straight-forward:
  it "should display the recipe's summary" do
self.stub!(:wiki).and_return("wiki #{@summary}")

render("/views/recipe.haml")
response.should have_selector("#eee-summary",
:content => "wiki #{@summary}")
end

it "should display the recipe's instructions" do
self.stub!(:wiki).and_return("wiki #{@instructions}")

render("/views/recipe.haml")
response.should have_selector("#eee-instructions",
:content => "wiki #{@instructions}")
end
The not-so-trivial part of working inside the guts of the application is that wiki helper method. In addition to textilizing text, it performs various domain-specific clean-up such as converting 250F to 250° F, replacing recipe URIs with linked titles and replacing our kids names with nicknames.

Happily, I spent quite a bit of time getting helpers into controller specs. Even better, that work makes direct helper specifications trivial:
require File.expand_path(File.dirname(__FILE__) + '/spec_helper' )

describe "wiki" do
it "should return simple text as unaltered text" do
wiki("bob").should == "bob"
end
it "should convert textile to HTML"
it "should wikify temperatures"
it "should wikify kids names"
it "should wikify recipe URIs"
end
A trivial implementation of the wiki makes this spec pass:
    def wiki(text)
text
end
(commit)

Next up: making those other 4 examples pass.