<?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>www.hans-eric.com &#187; DSLs</title>
	<atom:link href="http://www.hans-eric.com/category/dsls/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hans-eric.com</link>
	<description>Hans-Eric Grönlund on software development</description>
	<lastBuildDate>Wed, 18 Aug 2010 08:09:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Does unit-testing deserve its own DSL?</title>
		<link>http://www.hans-eric.com/2007/12/10/does-unit-testing-deserve-its-own-dsl/</link>
		<comments>http://www.hans-eric.com/2007/12/10/does-unit-testing-deserve-its-own-dsl/#comments</comments>
		<pubDate>Mon, 10 Dec 2007 15:51:11 +0000</pubDate>
		<dc:creator>Hans-Eric</dc:creator>
				<category><![CDATA[DSLs]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.hans-eric.com/2007/12/10/does-unit-testing-deserve-its-own-dsl/</guid>
		<description><![CDATA[We’ve done a lot with testing frameworks over the years, but does the testing concern deserve its own standalone DSL?
This intriguing question was asked by Michael Feathers in his Mock Objects: Leaping out of the Language post. My spontaneous  answer is: Absolutely!
I&#8217;m a big fan of xUnit frameworks, but when I imagine an alternative [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>We’ve done a lot with testing frameworks over the years, but does the testing concern deserve its own standalone DSL?</p></blockquote>
<p>This intriguing question was asked by Michael Feathers in his <a href="http://beautifulcode.oreillynet.com/2007/12/mock_objects_leaping_out_of_th.php">Mock Objects: Leaping out of the Language</a> post. My spontaneous  answer is: Absolutely!</p>
<p>I&#8217;m a big fan of <a href="http://en.wikipedia.org/wiki/XUnit">xUnit frameworks</a>, but when I imagine an alternative unit-testing specific language one special property comes to mind, a feature that would really make a difference. I&#8217;d call it unconditional mocking. With a DSL based unit-testing framework one could test really complex objects, even legacy code, since mocking internal objects would require no change to the original programming interface.</p>
<p>For example, this (nonsense) code</p>
<pre><code>class A {
  private B _b;

  // constructor
  this A() {
    _b = new B()
  }
}

unittest {
  // B can not be mocked
  A a = new A();
}
</code></pre>
<p>would require refactoring in order for _b to be mockable.</p>
<pre><code>class A {
  private B _b;

  // constructor
  this A(B b) {
    _b = b;
  }
}

unittest {
  // B could be mocked
  B b = new BMock(...);
  A a = new A(b);
}
</code></pre>
<p>But in a unit-testing DSL, one should be able to mock any object, in this case B, without changing the source code first. This is handy for dealing with the unit-testing paradox: Refactoring requires a unit-testing harness to make sure no functionality gets broken, but unit-testing requires testable code; So what to do when the code isn&#8217;t testable? A unit-testing DSL would make it easier to put up the initial testing harness.</p>
<p>Also, as Michael points out, a unit-testing DSL could be used to mock any kind of construction, not just objects: Functions and methods for instance. Oh man, could I have use for such a feature?</p>
<p>To give us an image of a DSL for unit-testing in a non-object-oriented language like C, Michael provides this example:</p>
<pre><code>function send_port_command with 90, “CMD: 12”
            calls io_mode which returns M_READ
            calls set_mode with M_WRITE
            calls write_byte with 90
            calls write_bytes with “12”
            returns E_OKAY</code></pre>
<p>That would be testing a function like this:</p>
<pre><code>status send_port_command(byte port, const char *message)
{
  if(io_mode() == M_READ)
    set_mode(M_WRITE);
  write_byte(port)
  write_bytes(translate_command(message));
  return E_OKAY;
}</code></pre>
<p>I have a problem with his example though. In my opinion the test-code resembles the target code a little too much, like a bad manager performing low-level supervision. Too detailed testing beats the purpose since it makes changes more difficult. My philosophy is that test-code should test WHAT the code does, and not bother too much on the HOW.</p>
<p>So, my not so thought through proposal, using Michaels example, would be something like this:</p>
<pre><code>TEST send_port_command

MOCK write_byte(port)
EXPECT port == 90

MOCK write_bytes(bytes)
EXPECT bytes == "12"

CALL send_port_command with 90, "CMD: 12"
EXPECT E_OKAY</code></pre>
<p>Of course there should be support for more advanced mock features like call counting:</p>
<pre><code>MOCK  write_bytes(bytes)
EXPECT   "12", "13"

CALL send_port_command with 90, "CMD: 12"
EXPECT E_OKAY
CALL send_port_command with 90, "CMD: 13"
EXPECT E_OKAY</code></pre>
<p>or</p>
<pre><code>MOCK  write_bytes(bytes)
EXPECT  2 CALLS</code></pre>
<p>or sequential values</p>
<pre><code>MOCK  io_mode
RETURN  M_READ, M_WRITE</code></pre>
<p>Implementing the DSL would be a hefty task though. But, the problems aside, how would your unit-testing DSL be like? I&#8217;d be very interested to hear your opinions.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hans-eric.com/2007/12/10/does-unit-testing-deserve-its-own-dsl/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.367 seconds -->
