<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom">

  <title>Barry Mitchelson Blog</title>
  <subtitle />
  
  <link href="http://barrymitchelson.com/" />
  <updated>2010-05-10T08:53:31+01:00</updated>
  <author>
    <name>Barry Mitchelson</name>
    <email>barry.mitchelson@gmail.com</email>
  </author>
  <id>http://barrymitchelson.com/</id>
  
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/barrymitchelson" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="barrymitchelson" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <title>Coloured output from Erlang EUnit tests using Rake</title>
    <link href="/blog/2009/06/15/coloured-output-from-erlang-eunit-tests-using-rake.html" />
    <id>tag:barrymitchelson.com,2009-06-15:1245102077</id>
    <updated>2009-06-15T22:41:17+01:00</updated>
    <content type="html">&lt;p&gt;
  I’m spending more and more time fiddling around with Erlang lately, and I’ve recently started using
  &lt;a href='http://svn.process-one.net/contribs/trunk/eunit/doc/overview-summary.html' title='EUnit'&gt;EUnit&lt;/a&gt;
  to write unit tests for my code. As I spend my working days writing Ruby code, I’ve found using Rake files to compile and run my code a lot easier to deal with than scary Makefiles.
&lt;/p&gt;
&lt;p&gt;
  The Rakefile I’ve been using to compile my code is from
  &lt;a href='http://seangeo.blogspot.com/2007/09/building-erlang-with-rake.html' title='Building Erlang with Rake'&gt;Building Erlang with Rake&lt;/a&gt;
  , and whilst working on a Rake task to run my unit tests I came across
  &lt;a href='http://21ccw.blogspot.com/2008/04/using-rake-for-erlang-unit-testing.html' title='Using Rake for Erlang Unit Testing'&gt;Using Rake for Erlang Unit Testing&lt;/a&gt;
  . Based on this, I’ve written a rake task to run my EUnit tests and give me the coloured output I’m used to from Ruby unit testing frameworks such a Rspec.
&lt;/p&gt;
&lt;p&gt;
  Here's the Rakefile:
&lt;/p&gt;
&lt;pre class="eiffel"&gt;
&lt;span class="Keyword"&gt;require&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;rake/clean&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;

&lt;span class="Variable"&gt;INCLUDE&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;include&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="Variable"&gt;ERLC_FLAGS&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;-I&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;&lt;span class="Variable"&gt;INCLUDE&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt; +warn_unused_vars +warn_unused_import&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;

&lt;span class="Variable"&gt;SRC&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="LibraryObject"&gt;FileList&lt;/span&gt;[&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;src/*.erl&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;]
&lt;span class="Variable"&gt;OBJ&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="Variable"&gt;SRC&lt;/span&gt;.&lt;span class="FunctionName"&gt;pathmap&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;%{src,ebin}X.beam&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
&lt;span class="Variable"&gt;CLEAN&lt;/span&gt;.&lt;span class="FunctionName"&gt;include&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;ebin/*.beam&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)

directory &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;ebin&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;

rule &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;.beam&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; =&amp;gt; [&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;%{ebin,src}X.erl&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;] &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;t&lt;/span&gt;|
  sh &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;erlc -pa ebin -W &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;&lt;span class="Variable"&gt;ERLC_FLAGS&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt; -o ebin &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;t&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;.&lt;/span&gt;&lt;span class="FunctionName"&gt;source&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;

desc &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Compiles all .erl files in src to .beam files in ebin&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
task &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;compile&lt;/span&gt; =&amp;gt; [&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;ebin&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;] &lt;span class="Keyword"&gt;+&lt;/span&gt; &lt;span class="Variable"&gt;OBJ&lt;/span&gt;
task &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;default&lt;/span&gt; =&amp;gt; &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;compile&lt;/span&gt;

desc &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Compiles all .erl files in src to .beam files in ebin&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
task &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;compile_with_tests&lt;/span&gt; &lt;span class="Keyword"&gt;do&lt;/span&gt;
  &lt;span class="LibraryObject"&gt;FileList&lt;/span&gt;[&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;src/**/*.erl&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;].&lt;span class="FunctionName"&gt;each&lt;/span&gt; &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;src&lt;/span&gt;|
    sh &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;erlc -D EUNIT -pa ebin -W &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;&lt;span class="Variable"&gt;ERLC_FLAGS&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt; -o ebin &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;src&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
  &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;

desc &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Run all tests&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
task &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;test&lt;/span&gt; =&amp;gt; &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;compile_with_tests&lt;/span&gt; &lt;span class="Keyword"&gt;do&lt;/span&gt;
  modules &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="Variable"&gt;OBJ&lt;/span&gt;.&lt;span class="FunctionName"&gt;map&lt;/span&gt; {|&lt;span class="Variable"&gt;o&lt;/span&gt;| &lt;span class="LibraryObject"&gt;File&lt;/span&gt;.&lt;span class="FunctionName"&gt;basename&lt;/span&gt;(o, &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;.beam&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) }
  output &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;`&lt;/span&gt;erl \&lt;/span&gt;
&lt;span class="String"&gt;  -noshell \&lt;/span&gt;
&lt;span class="String"&gt;  -pa ebin \&lt;/span&gt;
&lt;span class="String"&gt;  -eval 'eunit:test([&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;modules&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;.&lt;/span&gt;&lt;span class="FunctionName"&gt;join&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;(&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;,&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;)&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;], [verbose])' \&lt;/span&gt;
&lt;span class="String"&gt;  -s init stop&lt;span class="String"&gt;`&lt;/span&gt;&lt;/span&gt;

  output.&lt;span class="FunctionName"&gt;each_line&lt;/span&gt; &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;line&lt;/span&gt;|
    &lt;span class="Keyword"&gt;case&lt;/span&gt; line
    &lt;span class="Keyword"&gt;when&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;= &lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;EUnit&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt; =&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;
      print line.&lt;span class="FunctionName"&gt;gsub&lt;/span&gt;(&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;1&lt;/span&gt;, &lt;span class="FunctionName"&gt;green&lt;/span&gt;(&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;1&lt;/span&gt;))
    &lt;span class="Keyword"&gt;when&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="StringInterpolation"&gt;\*&lt;/span&gt;failed&lt;span class="StringInterpolation"&gt;\*&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;
      print &lt;span class="FunctionName"&gt;red&lt;/span&gt;(line)
    &lt;span class="Keyword"&gt;when&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\.&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\.&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\.&lt;/span&gt;.*ok&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;
      print line.&lt;span class="FunctionName"&gt;gsub&lt;/span&gt;(&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;1&lt;/span&gt;,&lt;span class="FunctionName"&gt;green&lt;/span&gt;(&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;1&lt;/span&gt;))
    &lt;span class="Keyword"&gt;when&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;Failed:&lt;span class="StringInterpolation"&gt;\s&lt;/span&gt;+&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\d&lt;/span&gt;+&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\.&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\s&lt;/span&gt;+Skipped:&lt;span class="StringInterpolation"&gt;\s&lt;/span&gt;+&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\d&lt;/span&gt;+&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\.&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\s&lt;/span&gt;+Passed:&lt;span class="StringInterpolation"&gt;\s&lt;/span&gt;+&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\d&lt;/span&gt;+&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\.&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;
      puts &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;&lt;span class="StringInterpolation"&gt;&lt;span class="FunctionName"&gt;red&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;(&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Failed: &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;1&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;)&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt; Skipped: &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;2&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;&lt;span class="StringInterpolation"&gt;&lt;span class="FunctionName"&gt;green&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;(&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Passed: &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;3&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;)&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
    &lt;span class="Keyword"&gt;when&lt;/span&gt;&lt;span class="Keyword"&gt;/&lt;/span&gt;(&lt;span class="Variable"&gt;All&lt;/span&gt; \d&lt;span class="Keyword"&gt;+&lt;/span&gt; tests passed.)&lt;span class="Keyword"&gt;/&lt;/span&gt;
      print &lt;span class="FunctionName"&gt;green&lt;/span&gt;(line)
    &lt;span class="Keyword"&gt;else&lt;/span&gt;
      print line
    &lt;span class="Keyword"&gt;end&lt;/span&gt;
  &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;

&lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="FunctionName"&gt;green&lt;/span&gt;(&lt;span class="FunctionParameter"&gt;text&lt;/span&gt;)
  &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\e&lt;/span&gt;[32m&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;text&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\e&lt;/span&gt;[0m&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;

&lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="FunctionName"&gt;red&lt;/span&gt;(&lt;span class="FunctionParameter"&gt;text&lt;/span&gt;)
  &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\e&lt;/span&gt;[31m&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;text&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\e&lt;/span&gt;[0m&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
  The :compile_with_tests task will, as expected, compile all the *.erl source files in the src/ directory with the -D EUNIT flag which will compile the tests in. The :test task will run the tests and give simple coloured output. We can test the task using the following simple Erlang module:
&lt;/p&gt;
&lt;pre class="eiffel"&gt;
-&lt;span class="Keyword"&gt;module&lt;/span&gt;(&lt;span class="TypeName"&gt;utils&lt;/span&gt;).
-&lt;span class="Keyword"&gt;compile&lt;/span&gt;(&lt;span class="UserDefinedConstant"&gt;export_all&lt;/span&gt;).

-&lt;span class="Keyword"&gt;ifdef&lt;/span&gt;(&lt;span class="FunctionName"&gt;EUNIT&lt;/span&gt;).
-&lt;span class="Keyword"&gt;include_lib&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;eunit/include/eunit.hrl&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;).
-&lt;span class="Keyword"&gt;endif&lt;/span&gt;.

&lt;span class="FunctionName"&gt;double&lt;/span&gt;(&lt;span class="Variable"&gt;X&lt;/span&gt;) -&amp;gt;
  &lt;span class="Variable"&gt;X&lt;/span&gt; &lt;span class="Keyword"&gt;*&lt;/span&gt; &lt;span class="Number"&gt;2&lt;/span&gt;.

&lt;span class="FunctionName"&gt;triple&lt;/span&gt;(&lt;span class="Variable"&gt;X&lt;/span&gt;) -&amp;gt;
  &lt;span class="Variable"&gt;X&lt;/span&gt; &lt;span class="Keyword"&gt;*&lt;/span&gt; &lt;span class="Number"&gt;3&lt;/span&gt;.

&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;%&lt;/span&gt; Tests&lt;/span&gt;

-&lt;span class="Keyword"&gt;ifdef&lt;/span&gt;(&lt;span class="FunctionName"&gt;EUNIT&lt;/span&gt;).

&lt;span class="FunctionName"&gt;double_test_&lt;/span&gt;() -&amp;gt;
  {&lt;span class="UserDefinedConstant"&gt;inparallel&lt;/span&gt;,
    [
      &lt;span class="Keyword"&gt;?&lt;/span&gt;&lt;span class="FunctionName"&gt;_assertEqual&lt;/span&gt;( &lt;span class="Number"&gt;100&lt;/span&gt;, &lt;span class="TypeName"&gt;utils&lt;/span&gt;:&lt;span class="FunctionName"&gt;double&lt;/span&gt;(&lt;span class="Number"&gt;50&lt;/span&gt;)),
      &lt;span class="Keyword"&gt;?&lt;/span&gt;&lt;span class="FunctionName"&gt;_assertEqual&lt;/span&gt;( &lt;span class="Number"&gt;50&lt;/span&gt;, &lt;span class="TypeName"&gt;utils&lt;/span&gt;:&lt;span class="FunctionName"&gt;double&lt;/span&gt;(&lt;span class="Number"&gt;20&lt;/span&gt;))
    ]
  }.

&lt;span class="FunctionName"&gt;triple_test_&lt;/span&gt;() -&amp;gt;
  {&lt;span class="UserDefinedConstant"&gt;inparallel&lt;/span&gt;,
    [
      &lt;span class="Keyword"&gt;?&lt;/span&gt;&lt;span class="FunctionName"&gt;_assertEqual&lt;/span&gt;( &lt;span class="Number"&gt;9&lt;/span&gt;, &lt;span class="TypeName"&gt;utils&lt;/span&gt;:&lt;span class="FunctionName"&gt;triple&lt;/span&gt;(&lt;span class="Number"&gt;3&lt;/span&gt;)),
      &lt;span class="Keyword"&gt;?&lt;/span&gt;&lt;span class="FunctionName"&gt;_assertEqual&lt;/span&gt;( &lt;span class="Number"&gt;75&lt;/span&gt;, &lt;span class="TypeName"&gt;utils&lt;/span&gt;:&lt;span class="FunctionName"&gt;triple&lt;/span&gt;(&lt;span class="Number"&gt;25&lt;/span&gt;))
    ]
  }.

-&lt;span class="Keyword"&gt;endif&lt;/span&gt;.
&lt;/pre&gt;
&lt;p&gt;
  Running rake test produces the following:
  &lt;pre class="console"&gt;barry@pinga:~/code/erlang$rake test&amp;#x000A;(in /Users/barry/code/erlang)&amp;#x000A;erlc -D EUNIT -pa ebin -W -Iinclude +warn_unused_vars +warn_unused_import -o ebin src/utils.erl&amp;#x000A;======================== &lt;span class="green"&gt;EUnit&lt;/span&gt; ========================&amp;#x000A;module 'utils'&amp;#x000A;  utils:21: double_test_...ok&amp;#x000A;  &lt;span class="red"&gt;utils:22: double_test_...*failed*&lt;/span&gt;&amp;#x000A;::error:{assertEqual_failed,[{module,utils},&amp;#x000A;                           {line,22},&amp;#x000A;                           {expression,"utils : double ( 20 )"},&amp;#x000A;                           {expected,50},&amp;#x000A;                           {value,40}]}&amp;#x000A;  in function utils:'-double_test_/0-fun-2-'/1&amp;#x000A;&amp;#x000A;  utils:29: triple_test_&lt;span class="green"&gt;...ok&lt;/span&gt;&amp;#x000A;  utils:30: triple_test_&lt;span class="green"&gt;...ok&lt;/span&gt;&amp;#x000A;  [done in 0.005 s]&amp;#x000A;=======================================================&amp;#x000A;&lt;span class="red"&gt;Failed: 1&lt;/span&gt; Skipped: 0 &lt;span class="green"&gt;Passed: 3&lt;/span&gt;&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;
  Fixing this error and rerunning the rake task outputs:
  &lt;pre class="console"&gt;barry@pinga:~/code/erlang$rake test&amp;#x000A;(in /Users/barry/code/erlang)&amp;#x000A;erlc -D EUNIT -pa ebin -W -Iinclude +warn_unused_vars +warn_unused_import -o ebin src/utils.erl&amp;#x000A;======================== &lt;span class="green"&gt;EUnit&lt;/span&gt; ========================&amp;#x000A;module 'utils'&amp;#x000A;  utils:21: double_test_&lt;span class="green"&gt;...ok&lt;/span&gt;&amp;#x000A;  utils:22: double_test_&lt;span class="green"&gt;...ok&lt;/span&gt;&amp;#x000A;  utils:29: triple_test_&lt;span class="green"&gt;...ok&lt;/span&gt;&amp;#x000A;  utils:30: triple_test_&lt;span class="green"&gt;...ok&lt;/span&gt;&amp;#x000A;  [done in 0.005 s]&amp;#x000A;=======================================================&amp;#x000A;  &lt;span class="green"&gt;All 4 tests passed.&lt;/span&gt;&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;
  One issue with this is that the :compile_with_tests task will always recompile source files regardless of whether they need recompiling or not, the standard :compile task only compiles when the source files have been modified. Other than that, this should work as intended. I'm sure someone will now tell me that this functionality is available in EUnit as it is, but I did Google for this, I promise.
&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>iTunes smart playlists</title>
    <link href="/blog/2009/04/26/itunes-smart-playlists.html" />
    <id>tag:barrymitchelson.com,2009-04-26:1240747517</id>
    <updated>2009-04-26T13:05:17+01:00</updated>
    <content type="html">&lt;style&gt;
  #content img {
    border: 1px solid #dddddd;
    padding: 0.8em;
    background-color: #ffffff; }
&lt;/style&gt;
&lt;p&gt;
  Despite exhibiting a fair amount of autistic behaviour towards most aspects of my music listening (I’ve devoted numerous evenings to tagging, renaming, and artworking pretty much all my mp3s), I am pretty slack in rating songs in iTunes. Which is a shame, because having all my songs rated would help me massively when making playlists. Luckily, due both to a nerdy desire to automate most things and lots of laziness, I’ve got a few smart playlists which try and do that job for me.
&lt;/p&gt;
&lt;h3&gt;Songs I love, but haven’t listened to for a month.&lt;/h3&gt;
&lt;img src='/images/blog/itunes-1.jpg' /&gt;
&lt;p&gt;Loved songs being those I’ve listened to more 5 times. Over time I’ll increase the number, but at the moment 5 is working out pretty well for me.&lt;/p&gt;
&lt;h3&gt;Songs I like, but haven’t listened to for a month.&lt;/h3&gt;
&lt;img src='/images/blog/itunes-2.jpg' /&gt;
&lt;p&gt;Same as the loved list, but only 2 plays are required to be included. Also, excluding the loved list so there are no duplicates.&lt;/p&gt;
&lt;h3&gt;Songs I really love&lt;/h3&gt;
&lt;img src='/images/blog/itunes-3.jpg' /&gt;
&lt;p&gt;So I’ve listened to them loads, and never skipped them - I must really love these ones. By the way, as far as I know, Apple’s definition of skipping a song is when you press next any time between the 2nd and 20th second of a song. I wish it didn’t have the 2 second buffer at the start, I usually skip songs within a second of hearing one I can’t be bothered with.&lt;/p&gt;
&lt;h3&gt;10 songs from last week&lt;/h3&gt;
&lt;img src='/images/blog/itunes-4.jpg' /&gt;
&lt;p&gt;Not sure why I like this playlist, but I do. It’s quite nice to hear a song again after a few days.&lt;/p&gt;
&lt;p&gt;OK, so those are the main playlists I use on my iPod. I don’t sync directly with them though, I have a folder which contains the lists I sync with.&lt;/p&gt;
&lt;img src='/images/blog/itunes-5.jpg' /&gt;
&lt;p&gt;10 from last week is the one I mentioned above, Genius is the Apple Genius playlist I currently have, Liked/Loved 3gb is&lt;/p&gt;
&lt;img src='/images/blog/itunes-6.jpg' /&gt;
&lt;p&gt;just a mix of the liked/loved playlists limited to 3GB.&lt;/p&gt;
&lt;p&gt;
  Played, random is random songs I’ve listened to at least once, but not in the past week
&lt;/p&gt;
&lt;img src='/images/blog/itunes-7.jpg' /&gt;
&lt;p&gt;The unplayed list, is the same as the played, but with a play count of 0.&lt;/p&gt;
&lt;h3&gt;The final playlist&lt;/h3&gt;
&lt;p&gt;
  It’s only now I’ve written it all down that I realise how convoluted and complicated this all is, but from this folder comes the final playlist. I only sync with this one.
&lt;/p&gt;
&lt;img src='/images/blog/itunes-8.jpg' /&gt;
&lt;p&gt;It’s the iPod Smart folder, excluding all the songs I skipped last week, because chances are I still don’t want to listen to them. It’s limited to 10GB, because this is for my iPhone, I have this same playlist for my 4GB Nano, but limited to 4GB (obv). It’s quite nice being able to do it this way, it means I don’t have shuffle around the sizes of all the playlists I use.&lt;/p&gt;
&lt;p&gt;
  And that’s pretty much it. There are some limitations with the smart playlists - I’d love to be able to have a playlist based on songs I’d listened to the most times in the last week/month/year, and I wish I could make it pick a random album for playlists. Unless I missed something. These things are possible with all my
  &lt;a href='http://www.last.fm/user/flicky'&gt;last.fm data&lt;/a&gt;
  , I just need a way to get that data into iTunes. That can be another side project for me.
&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Scenes - a Rails plugin</title>
    <link href="/blog/2009/04/19/scenes-a-rails-plugin.html" />
    <id>tag:barrymitchelson.com,2009-04-19:1240157537</id>
    <updated>2009-04-19T17:12:17+01:00</updated>
    <content type="html">&lt;p&gt;
  We're big fans of &lt;a title="Cucumber" href="http://cukes.info"&gt;Cucumber&lt;/a&gt; at work - we're using it on all new projects we start, and it's quickly becoming as essential to us as writing tests using Rspec. We first started using it last year on a reasonably complicated Rails app, and we had a lot of features which required the system to be in a certain state before we could test what the feature was actually about. There are different ways to do this now - e.g. &lt;a title="Cucumber backgrounds steps" href="http://wiki.github.com/aslakhellesoy/cucumber/background" target="_blank"&gt;background&lt;/a&gt; steps - but as a solution to this problem I wrote what eventually became &lt;a title="scenes github repo" href="http://github.com/barrym/scenes/tree/master" target="_blank"&gt;scenes&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
  It's probably easiest to demonstrate with a simple example.
&lt;/p&gt;
&lt;pre class="eiffel"&gt;
&lt;span class="LibraryObject"&gt;Scenes&lt;/span&gt;::&lt;span class="FunctionName"&gt;Character&lt;/span&gt;.&lt;span class="FunctionName"&gt;named&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;David&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) {
  &lt;span class="LibraryObject"&gt;User&lt;/span&gt;.&lt;span class="FunctionName"&gt;create&lt;/span&gt;(&lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;name&lt;/span&gt; =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;David&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
}

&lt;span class="LibraryObject"&gt;Scenes&lt;/span&gt;::&lt;span class="FunctionName"&gt;Character&lt;/span&gt;.&lt;span class="FunctionName"&gt;named&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Joe&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) {
  &lt;span class="LibraryObject"&gt;User&lt;/span&gt;.&lt;span class="FunctionName"&gt;create&lt;/span&gt;(&lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;name&lt;/span&gt; =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Joe&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
}

&lt;span class="LibraryObject"&gt;Scenes&lt;/span&gt;::&lt;span class="FunctionName"&gt;Scene&lt;/span&gt;.&lt;span class="FunctionName"&gt;named&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;David and Joe&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) {
  &lt;span class="LibraryObject"&gt;Scenes&lt;/span&gt;::&lt;span class="FunctionName"&gt;Character&lt;/span&gt;[&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;David&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;]
  &lt;span class="LibraryObject"&gt;Scenes&lt;/span&gt;::&lt;span class="FunctionName"&gt;Character&lt;/span&gt;[&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Joe&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;]
}
&lt;/pre&gt;
&lt;p&gt;This will set up a scene containing two characters - David and Joe. In the Rails console, we can now load this scene and then play it back to create these users.&lt;/p&gt;
&lt;pre class="irb"&gt;Loading development environment (Rails 2.3.2)&amp;#x000A;&amp;gt;Scenes::load&amp;#x000A;=&amp;gt; ["scenes/scenes.rb"]&amp;#x000A;&amp;gt;Scenes::Scene.list&amp;#x000A;=&amp;gt; ["David and Joe"]&lt;/pre&gt;
&lt;p&gt;Scenes::load will read all ruby files in the scenes/ directory. Scenes::Scene.list lists the scenes that have been loaded.&lt;/p&gt;
&lt;pre class="irb"&gt;&amp;gt;User.count&amp;#x000A;=&amp;gt; 0&amp;#x000A;&amp;gt;Scenes::Scene["David and Joe"].play&amp;#x000A;=&amp;gt; true&amp;#x000A;&amp;gt;User.all.map(&amp;amp;:name)&amp;#x000A;=&amp;gt; ["David", "Joe"]&lt;/pre&gt;
&lt;p&gt;Playing this scene back created David and Joe. The first time a character is referenced, the object is created in the database. Subsequent references will load this object rather than duplicating it. Characters can contain any additional code for setting up the character (eg, activating a user account), but must return the object they are creating.&lt;/p&gt;
&lt;pre class="eiffel"&gt;
&lt;span class="LibraryObject"&gt;Scenes&lt;/span&gt;::&lt;span class="FunctionName"&gt;Character&lt;/span&gt;.&lt;span class="FunctionName"&gt;named&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;David&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) {
  u &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="LibraryObject"&gt;User&lt;/span&gt;.&lt;span class="FunctionName"&gt;create&lt;/span&gt;(&lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;name&lt;/span&gt; =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;David&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
  u.&lt;span class="FunctionName"&gt;activate!&lt;/span&gt;
  u
}
&lt;/pre&gt;
&lt;h3&gt;Usage with Cucumber&lt;/h3&gt;
&lt;p&gt;To play scenes in a cucumber scenario, you first have to load the scenes, and then create a step which will load them.&lt;/p&gt;
&lt;pre class="eiffel"&gt;
&lt;span class="LibraryObject"&gt;Scenes&lt;/span&gt;::&lt;span class="FunctionName"&gt;load&lt;/span&gt;

&lt;span class="Variable"&gt;Given&lt;/span&gt; &lt;span class="Keyword"&gt;/&lt;/span&gt;&lt;span class="Keyword"&gt;^&lt;/span&gt;the scene &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;(.+)&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;/&lt;/span&gt;i &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;scene_name&lt;/span&gt;|
  &lt;span class="LibraryObject"&gt;Scenes&lt;/span&gt;::&lt;span class="FunctionName"&gt;Scene&lt;/span&gt;[scene_name].&lt;span class="FunctionName"&gt;play&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;I put this code in my features/support/env.rb, although the step definition should probably live in its own definition file. Once this code is in place you can use a scene in a scenario as follows&lt;/p&gt;
&lt;pre class="irb"&gt;Given the scene "David and Joe"&amp;#x000A;When I am on the users page&amp;#x000A;I should see "2" users&lt;/pre&gt;
&lt;p&gt;As mentioned at the beginning of this post, this is a very simple example, but it should show how to use scenes.&lt;/p&gt;
&lt;h3&gt;Rake tasks&lt;/h3&gt;
&lt;p&gt;There are also two rake tasks for scenes, the first will list all the scenes you have set up and allow you to load them in to your development environment.&lt;/p&gt;
&lt;pre class="irb"&gt;$ rake scenes&amp;#x000A;(in /Users/barry/code/ruby/example)&amp;#x000A;&amp;#x000A;Available Scenes&amp;#x000A;----------------&amp;#x000A;&amp;#x000A;   1 - David and Joe&amp;#x000A;&amp;#x000A;This will REBUILD the database first&amp;#x000A;&amp;#x000A;Or 0/q to quit&amp;#x000A;&amp;gt; 1&amp;#x000A;==  CreateUsers: migrating ================================&amp;#x000A;-- create_table(:users)&amp;#x000A;   -&amp;gt; 0.0019s&amp;#x000A;==  CreateUsers: migrated (0.0027s) =======================&lt;/pre&gt;
&lt;p&gt;
  This task will run rake db:migrate:reset to clear your development database, then play the selected scene. This is handy for checking that you have set your scene up correctly, and it's been a useful way of setting apps up in a predetermined state for weekly demos.
  The other task allows you to save your current development environment as a scene for future playback.
&lt;/p&gt;
&lt;pre class="irb"&gt;$ rake scenes:save&amp;#x000A;(in /Users/barry/code/ruby/example)&amp;#x000A;&amp;#x000A;Enter a name for this scene:&amp;#x000A;&amp;gt; my awesome scene&amp;#x000A;Saved&amp;#x000A;$ ls scenes/saved/&amp;#x000A;my_awesome_scene.rb	my_awesome_scene.yml&lt;/pre&gt;
&lt;p&gt;This saves your data in a yaml file along with a ruby file which sets the scene. This task is quite simple, and if you have any database constraints in place it probably won't work as expected.&lt;/p&gt;
&lt;p&gt;Hopefully, that's given a basic example of using this plugin. It's still a work in progress, and the code is up on &lt;a title="scenes github repo" href="http://github.com/barrym/scenes/tree/master"&gt;github&lt;/a&gt;.&lt;/p&gt;
</content>
  </entry>
  
</feed>
