<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog &#124; Rubyyot &#187; starting a project</title>
	<atom:link href="http://blog.rubyyot.com/tag/starting-a-project/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rubyyot.com</link>
	<description>Programming, Pragmatism and Getting By in the World</description>
	<lastBuildDate>Wed, 24 Mar 2010 00:14:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting a project off the ground with TDD and Cucumber</title>
		<link>http://blog.rubyyot.com/2009/06/getting-a-project-off-the-ground-with-tdd-and-cucumber/</link>
		<comments>http://blog.rubyyot.com/2009/06/getting-a-project-off-the-ground-with-tdd-and-cucumber/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 05:17:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[starting a project]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://blog.rubyyot.com/?p=659</guid>
		<description><![CDATA[RSimpy
I am currently wrapping up an initial release of a new gem that wraps the Simpy.com API.  I like social bookmarking and used delicious for years before switching to Simpy as my primary service.  Simpy is not the biggest online bookmarking service, but I&#8217;ve decided to use it for reasons I will outline [...]]]></description>
			<content:encoded><![CDATA[<h3>RSimpy</h3>
<p>I am currently wrapping up an initial release of a new gem that wraps the <a href="http://simpy.com/" title="" target="_blank" rel="user">Simpy.com</a> API.  I like social bookmarking and used delicious for years before switching to Simpy as my primary service.  Simpy is not the biggest online bookmarking service, but I&#8217;ve decided to use it for reasons I will outline in another post.  In any case I&#8217;ve put together a gem that wraps the API with the help of Httparty.  It actually turned out much simpler than I expected it to be, httparty did all of the heavy lifting, but again that is for another post.</p>
<p>So, I have a functioning gem for Simpy, now I want to make something useful with it.  The reason I made the gem was to simplify my bookmarking which is overall a mess on multiple workstations in multiple locations.  Since I&#8217;ve recently been getting familia with cron, I&#8217;ve decided to make a script to store urls locally and use <a href="http://en.wikipedia.org/wiki/Cron" title="cron" target="_blank" rel="definition">cron</a> to run a script that pushes a file of pages to Simpy.</p>
<p>As I touched on briefly, I&#8217;ve been unhappy with all my previous bookmarking solutions thus far.  This time I want to make it, dead simple.  I don&#8217;t want to have to enter any additional information if I don&#8217;t want to, just a url and have it go on it&#8217;s merry way.  However to post the link, I will need to at least have the page title.  Pulling a page title from a url is really outside of the scope of RSimpy.  Ruby makes it fairly simple to do, but I don&#8217;t want to isolate the functionality to a script.  Ruminate to the rescue, but what is Ruminate?</p>
<h3>Ruminate</h3>
<p>Well the answer to that question is that I&#8217;m not really sure.  A few months back, I made a gem with jeweler called ruminate with the description  &#8220;Extracts statistics from html documents&#8221;.  Actually, I&#8217;m not really sure what I originally had in mind when I setup this gem, but it will be useful now since I want to extract at least the page title.  Once I get the ability to extract a page title, I can make a script that is simply glue code to put Ruminate together with RSimpy and voila!</p>
<h3>Bootstrapping the project</h3>
<p>I have a vague idea of something that I want Ruminate to do, that is I want to be able to query a url to return the title of the page.  It&#8217;s obviously not the entire purpose of the gem, but I&#8217;m sure that will be lots of other bits that will come together in time. </p>
<p>The question is, How do I kickstart this project with TDD to get it off the ground?</p>
<h3>Programming by Intention</h3>
<p><a title="programming by intention" target="blank" href="http://www.hans-eric.com/2007/09/05/tools-of-the-effective-developer-personal-planning/">Programming by intention</a> is the practice of writing code, while pretending that any methods or classes you might reference already exist and operate in the way that you reference them.  It&#8217;s a great way of uncovering a simple intuative API that you might otherwise would have missed.</p>
<p>I&#8217;ve seen it described in two books, first in <a href="http://www.pragprog.com/titles/bmsft/everyday-scripting-with-ruby"  target="_blank" title="Everyday scripting with Ruby" rel="book I've read"><em>Everyday Scripting with Ruby</em></a>, from The Pragmatic Bookshelf. Secondly, it&#8217;s used in an excellent book I&#8217;m currently reading called  <a href="http://www.javalobby.org/java/forums/t102176.html" target="_blank" title="Test Driven" rel="book I've read"><em>Test Driven</em></a> from Manning.</p>
<p>  There are <a href="http://bradwilson.typepad.com/blog/2009/04/its-not-tdd-its-design-by-example.html" target="_blank">a number</a> <a href="http://www.hanselman.com/blog/HanselminutesPodcast146TestDrivenDevelopmentIsDesignTheLastWordOnTDD.aspx" target="_blank">of sources</a> <a href="http://blog.jpboodhoo.com/DesignVsCodingHowMuchIsTooMuch.aspx" target="_blank">stating that</a> <a href="http://weblogs.asp.net/rosherove/archive/2007/10/08/the-various-meanings-of-tdd.aspx" target="_blank">TDD</a> <a href="http://blog.codiceplastico.com/?p=181" target="_blank">is about design first</a> <a href="http://davybrion.com/blog/2008/08/introducing-tdd-how-would-you-do-it/" target="_blank">and automated testing</a> <a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/04/22/should-you-tdd-when-flying-solo.aspx" target="_blank">is a nice by-product</a>.  In my limited experience with TDD, programming by intention is the crux of this design, and by extension the primary purpose of TDD.</p>
<h3>Cucumber</h3>
<p>One of the things that I really like about cucumber is that you can plainly state your intent and important bits of your feature focused domain in plain English and match that up to executable code. To describe the ability of Ruminate to grab the title of a url, I made the following test:</p>

<div class="wp_syntax"><div class="code"><pre class="cucumber" style="font-family:monospace;">Feature: Get Page Title
  In to get the page title
  A user of ruminate
  Will request a page title
&nbsp;
  Scenario: Requesting the title of a page from the page
    Given the url &quot;http://www.google.com&quot;
    When I execute the request
    Then the &quot;title&quot; should be &quot;Google&quot;</pre></div></div>

<p>I let cucumber auto-generate the regular expression blocks and came up with the following steps:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
Given <span style="color:#006600; font-weight:bold;">/</span>^the url <span style="color:#996600;">&quot;([^<span style="color:#000099;">\&quot;</span>]*)&quot;</span>$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>url<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#0066ff; font-weight:bold;">@url</span> = url
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">When</span> <span style="color:#006600; font-weight:bold;">/</span>^I execute the request$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  query = <span style="color:#996600;">&quot;Select title from #{@url};&quot;</span>
  <span style="color:#0066ff; font-weight:bold;">@result</span> = chew query
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">Then</span> <span style="color:#006600; font-weight:bold;">/</span>^the <span style="color:#996600;">&quot;([^<span style="color:#000099;">\&quot;</span>]*)&quot;</span> should be <span style="color:#996600;">&quot;([^<span style="color:#000099;">\&quot;</span>]*)&quot;</span>$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>msg, value<span style="color:#006600; font-weight:bold;">|</span>
  assert_equal value, <span style="color:#0066ff; font-weight:bold;">@result</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>As you can see I&#8217;ve decided to try out a sql like syntax for the tool.  I thought it sounded interesting, we&#8217;ll see if I keep it. In any case, I now need to make this test pass with the help of a mixin, that I&#8217;m including in features/support/env.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># features/support/env.rb</span>
<span style="color:#ff6633; font-weight:bold;">$LOAD_PATH</span>.<span style="color:#9900CC;">unshift</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/../../lib'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'ruminate'</span>
<span style="color:#9966CC; font-weight:bold;">include</span> Ruminate
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test/unit/assertions'</span>
&nbsp;
World<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Test::Unit::Assertions</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># lib/ruminate.rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'query_parser'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'engine'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> Ruminate
  <span style="color:#9966CC; font-weight:bold;">def</span> chew query
    parser = <span style="color:#6666ff; font-weight:bold;">Ruminate::QueryParser</span>.<span style="color:#9900CC;">new</span>
    query_object = parser.<span style="color:#9900CC;">parse</span> query
    engine = <span style="color:#6666ff; font-weight:bold;">Ruminate::Engine</span>.<span style="color:#9900CC;">new</span>
&nbsp;
    engine.<span style="color:#9900CC;">execute</span> query_object
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Here I&#8217;ve used the mixin to split the method into to operations and encapsulated each of these into a class.  Programming by intention has now generated a mixin and two classes that have a clean separation of responsibilities.  Here is the simplest thing that could possibly work to make the feature pass.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># lib/query_parser.rb</span>
<span style="color:#9966CC; font-weight:bold;">module</span> Ruminate
  <span style="color:#9966CC; font-weight:bold;">class</span> QueryParser
    <span style="color:#9966CC; font-weight:bold;">def</span> parse query
      <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># lib/engine.rb</span>
<span style="color:#9966CC; font-weight:bold;">module</span> Ruminate
  <span style="color:#9966CC; font-weight:bold;">class</span> Engine
    <span style="color:#9966CC; font-weight:bold;">def</span> execute query
      <span style="color:#996600;">&quot;Google&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Obviously this isn&#8217;t the final code, but it gives us the minimal implementation to pass the feature.  Now I can turn to Test::Unit and shoulda to flesh out tests and implementations to make it work in a more useful manner.</p>
<p>What I really wanted to show here is how to get a TDD project started.  I know that I&#8217;ve struggled with this in the past.  What to test first can be a big decision, one that can create a type of code writers block. When this moment happened where everything just came together and I was able to generate a mixin and two orthagonal classes with a simple feature, I wanted to write it up.</p>
<h3>The Code</h3>
<p>The full code for this example can be found <a href="http://github.com/rubyyot/ruminate/tree/simplest-thing-that-could-possibly-work">on github with the tag simplest-thing-that-could-possibly-work</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rubyyot.com/2009/06/getting-a-project-off-the-ground-with-tdd-and-cucumber/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
