<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>testing</title>
        <link>http://blog.smoothfriction.nl/category/3.aspx</link>
        <description>testing</description>
        <language>nl-NL</language>
        <copyright>Erik van Brakel</copyright>
        <generator>Subtext Version 2.1.2.2</generator>
        <item>
            <title>Introducing the Smooth Friction testdata generator (v0.1)</title>
            <link>http://blog.smoothfriction.nl/archive/2009/11/12/introducing-the-smooth-friction-testdata-generator-v0.1.aspx</link>
            <description>&lt;p&gt;Between work, .Less and other things, I’ve been working a bit on a tool I would personally use. How many times did you just need a big set of test data for an application? How many times did you resort to simply dumping randomly generated data into your tables? Well, I’ve did that a few times, and one of the biggest annoyances is that the generated data is usually not really ‘real’, if you know what I mean.&lt;/p&gt;  &lt;p&gt;Say I have an application which has keeps customer records. I’d have a name, address, maybe some dates, something numeric and so on. With my new project, I aim to make getting some sample data easy(tm). It’s very, VERY prototype’ish right now, but I want to practice a bit of ‘release early, release often’ this time. So here it is! Take a look at the &lt;a href="http://testdata.smoothfriction.nl"&gt;Smooth Friction test-data generator&lt;/a&gt;, and tell me what you think. Is it something you’d use? Should I continue working on this?&lt;/p&gt;  &lt;p&gt;Disclaimer:   &lt;br /&gt;I am not a designer, so it looks crap. It’s a very early version, so expect bugs. Lots of them. Break it please, and tell me about it and how you did it ;-)&lt;/p&gt;&lt;img src="http://blog.smoothfriction.nl/aggbug/30.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Erik van Brakel</dc:creator>
            <guid>http://blog.smoothfriction.nl/archive/2009/11/12/introducing-the-smooth-friction-testdata-generator-v0.1.aspx</guid>
            <pubDate>Wed, 11 Nov 2009 23:27:19 GMT</pubDate>
            <wfw:comment>http://blog.smoothfriction.nl/comments/30.aspx</wfw:comment>
            <comments>http://blog.smoothfriction.nl/archive/2009/11/12/introducing-the-smooth-friction-testdata-generator-v0.1.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://blog.smoothfriction.nl/comments/commentRss/30.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Unit testing ANTLR generated code</title>
            <link>http://blog.smoothfriction.nl/archive/2009/08/25/unit-testing-antlr-generated-code.aspx</link>
            <description>&lt;p&gt;ANTLR generates code, which you have to assume works as it should. However, the output is a variable factor, determined only by your grammar files. Considering that the rest of the application, or at least the part which loads the AST into your object model, depends on a specific output from your parser, it’s smart to test your parser output.&lt;/p&gt;  &lt;p&gt;Consider the following grammar:&lt;/p&gt; &lt;pre name="code" class="plain"&gt;grammar expression;
options {
	output=AST;
	ASTLabelType=CommonTree;
}

expression : additiveExpression*;

additiveExpression : value ('+' value)* -&amp;gt; ^('+' value+);
value : NUMERIC;

NUMERIC : '0'..'9'+;
&lt;/pre&gt;

&lt;p&gt;This grammar accepts input in the form of x+y, where x is an integer value. Thanks to the rewrite rule we’ve defined, the output will be '(+ x y)’. This is quite handy for parsing/building expression trees, but that’s out of scope for today. The trick is that the code which deals with the generated AST directly is very dependant on the output. If you decide to change your grammar setup, which could be for any reason ranging from aesthetics to simply adding features, you risk breaking your code. This could result in compiler errors, but I found that more often than not it’s a silent bug. And you know, silent bugs are quite annoying to track down. Now, let me show you a quite simple approach on how to test the output of the parser. I’ll use NUnit for the test syntax, combined with the NUnit extension methods.&lt;/p&gt;

&lt;p&gt;Every parser eventually returns an object which extends ParserRuleReturnScope, which provides us with a way of getting the AST out in the form of an object which implements ITree. A very handy helper method on that interface is the ‘ToStringTree’ method. All it does is convert your tree into a string, in prefix notation. An example:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;html 
    &lt;ul&gt;
      &lt;li&gt;head 
        &lt;ul&gt;
          &lt;li&gt;title &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;

      &lt;li&gt;body 
        &lt;ul&gt;
          &lt;li&gt;div &lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Will be formatted as (html (head title) (body div)). The first element in the list is the root, the consecutive elements the children. We’ll use this method to our advantage in our tests.&lt;/p&gt;

&lt;p&gt;To get to the point of being able to get the string representation of an AST, you first have to do some plumbing. I’ll use the simplest form possible.&lt;/p&gt;

&lt;pre class="c#" name="code"&gt;public expressionParser CreateParser(string input)
{

	var stream = new ANTLRStringStream(input);

	var lexer = new expressionLexer(stream);
	return new expressionParser(new CommonTokenStream(lexer));
}&lt;/pre&gt;

&lt;p&gt;This will create a parser we can use, which in turn allows us to get the AST we want to use. The test fixture itself is pretty straightforward. We know which output we expect, all we have to do is assert it’s equal.&lt;/p&gt;
&lt;pre class="c#" name="code"&gt;
[TestFixture]
public class ParserTest
{
	[Test]
	public void Parser_TestAdditiveExpression()
	{
		string input = "10+2";
		expressionParser parser = CreateParser(input);
		additiveExpression_return result = parser.additiveExpression();
		ITree tree = result.Tree as ITree;
		
		tree.ToStringTree().Should().Be.EqualTo("(+ 10 2)");
		// Or: Assert.AreEqual("(+ 10 2)", tree.ToStringTree());
	}
}
&lt;/pre&gt;
&lt;p /&gt;

&lt;p&gt;That's basically it! Now, even though this test works, it has a lot of repetition when you add multiple tests with different input. For this, you can use a simple extension method, which operates on a string.&lt;/p&gt;

&lt;pre class="c#" name="code"&gt;
public static string GetStringTree(this string input, Func&amp;lt;expressionParser, ParserRuleReturnScope&amp;gt; func)
{
	var parser = CreateParser(input);
	return (func(parser).Tree as ITree).ToStringTree();
}
&lt;/pre&gt;
&lt;p&gt;Which you can use to simplify the test, as such:&lt;/p&gt;
&lt;pre name="code" class="c#"&gt;
[TestFixture]
public class ParserTest
{
	[Test]
	public void Parser_TestAdditiveExpression()
	{
		string result = "10+2".GetStringTree(p =&amp;gt; p.additiveExpression());
		
		result.Should().Be.EqualTo("(+ 10 2)");
		// Or: Assert.AreEqual("(+ 10 2)", result);
	}
}
&lt;/pre&gt;

&lt;p&gt;Happy testing!&lt;/p&gt;&lt;img src="http://blog.smoothfriction.nl/aggbug/24.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Erik van Brakel</dc:creator>
            <guid>http://blog.smoothfriction.nl/archive/2009/08/25/unit-testing-antlr-generated-code.aspx</guid>
            <pubDate>Tue, 25 Aug 2009 11:34:30 GMT</pubDate>
            <wfw:comment>http://blog.smoothfriction.nl/comments/24.aspx</wfw:comment>
            <comments>http://blog.smoothfriction.nl/archive/2009/08/25/unit-testing-antlr-generated-code.aspx#feedback</comments>
            <wfw:commentRss>http://blog.smoothfriction.nl/comments/commentRss/24.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>