<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" 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:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Peli's Farm - Pex, Stubs, Moles, QuickGraph, MbUnit, Reflector Addins - CCI</title>
    <link>http://blog.dotnetwiki.org/</link>
    <description>TouchDevelop, Pex4Fun, Rise4Fun, Pex, Moles, QuickGraph, MbUnit, Reflector Addins</description>
    <language>en-us</language>
    <copyright>Jonathan 'Peli' de Halleux</copyright>
    <lastBuildDate>Thu, 31 Dec 2009 04:45:27 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>jonathan.dehalleux@gmail.com</managingEditor>
    <webMaster>jonathan.dehalleux@gmail.com</webMaster>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=9dce05c9-217b-45a7-a8d1-2dce2792116d</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=9dce05c9-217b-45a7-a8d1-2dce2792116d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It is holiday time so I could spend some time to play with <a href="http://ccimetadata.codeplex.com/">CCI</a>.
The result is a bunch of interesting assembly mutators. Let’s start with the first
one: automatic rich assertion messages.
</p>
        <p>
          <strong>The problem with Assertions</strong>
        </p>
        <p>
When an assertion fails, the error message is usually insufficient to understand failure.
The problem is that we are usually lacking the values in the expression to immediately
diagnose the problem. Consider this example,
</p>
        <blockquote>
          <p>
Assert.True(x == 123);
</p>
        </blockquote>
        <p>
When this assertion fails, one would like to know what was the value of ‘x’. Of course,
that value of x is not serialized in the test output since the assertion method only
sees the ‘false’ value. What we would really like to get is something like ‘x (64)
!= 123’. A number of techniques have been developed to work around this issue.
</p>
        <p>
          <strong>Solution #1: Specialized Assertions</strong>
        </p>
        <p>
A common approach is to provide specialized assertion methods with enhanced logging.
For example, a special method to test equality:
</p>
        <blockquote>
          <p>
Assert.Equal(x , 123);
</p>
        </blockquote>
        <p>
When this assertion fails, the Equal method has the value of both sides of the equality
and can render it in the message: ‘expected 123, actual 64’. Unfortunately, expressions
are more readable when you write them, and specialized assertions cannot cover all
scenarios. This takes us to the next solution.
</p>
        <p>
          <strong>Solution #2: Expression Trees at Run time</strong>
        </p>
        <p>
          <a href="http://themechanicalbride.blogspot.com/">Jafar Husain</a> wrote <a href="http://themechanicalbride.blogspot.com/2009/06/better-unit-tests-with-testassert-for.html">a
very interresting post</a> on how to use Expression Trees to generate rich assertion
messages (this is <a href="http://www.gallio.org/api/html/M_MbUnit_Framework_AssertEx_That.htm">also
supported in MbUnit</a>).
</p>
        <blockquote>
          <p>
Assert.True(<a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx">() =&gt;</a> x
== 123);
</p>
        </blockquote>
        <p>
Instead of taking a bool, the assert method takes an expression tree (Expression&lt;Func&lt;bool&gt;&gt;).
Thus, when the expression evaluates to false, the expression tree can be traversed
to extract interesting values (such as x) and generate a rich log of the failure. 
</p>
        <p>
Unfortunately, there is a major drawback to this technique: what used to be 3 MSIL
instructions (ldloc.1, ldc.i4 123, ceq) to evaluate the condition becomes hundreds
of methods calls, millions of instructions executed through the System.Linq namespaces.
This performance is a overhead on the <a href="http://research.microsoft.com/pex/">Pex</a> whitebox
analysis.
</p>
        <p>
Jafar considers unit test frameworks as an extension of the compiler and uses expression
trees to access what the compiler knows: expression and statements. Following his
idea takes us to the CCI based solution.
</p>
        <p>
          <strong>Solution #3: Assembly Rewritter at Compile time</strong>
        </p>
        <p>
In the previous approach, expression trees were used to build a little compiler <strong>at
runtime</strong>. A better solution would be to rewrite the expressions <strong>at
compile time. </strong>In other words, we want to build an assembly mutator that takes
the original method call and appends code that generates the logging:
</p>
        <blockquote>
          <p>
Assert.True(x == 123<strong>, String.Format(“x == 123 where x = ‘{0}’”, x)</strong>);
</p>
        </blockquote>
        <p>
The assembly mutator extracts the expression sources from the pdb (‘x == 123’), collect
the local/variable/field references by traversing the expression tree, generate a
String.Format friendly message and replace the assertion method call to the assertion
method that takes the additional string.
</p>
        <p>
          <strong>Hello </strong>
          <a href="http://ccimetadata.codeplex.com/">
            <strong>Common Compiler
Infrastructure</strong>
          </a>
          <strong> (CCI) and CciSharp</strong>
        </p>
        <p>
Of course, to rewrite an assembly at compile time, we need a framework that can read
and write MSIL, decompile expressions etc… This is where CCI (from <a href="http://ccimetadata.codeplex.com">http://ccimetadata.codeplex.com</a> )
comes. It allows to manipulate .NET assemblies using an object model and save them
back to disk. The <a href="http://ccisamples.codeplex.com/wikipage?title=AssertMessage&amp;referringTitle=CciSharp">assertion
message mutator</a> is just one of other mutations that have been or will be implemented
as part of <a href="http://ccisamples.codeplex.com/wikipage?title=CciSharp"><strong>CciSharp</strong></a><strong>,
a post compiler for .NET that is built on top of CCI</strong>.
</p>
        <p>
(There’s already a bunch of useful operators in CciSharp that i’ve been coding over
the holidays: assigning the value of auto-properties, making auto-properties readonly
or lazy (or weakly lazy), or even implementing the DependencyProperty stuff automatically.
We’ll talk about this later.)
</p>
        <p>
          <strong>Where can I find it?</strong>
        </p>
        <p>
          <strong>CciSharp</strong> binaries and sources are avaiable on <a title="http://ccisamples.codeplex.com/wikipage?title=CciSharp" href="http://ccisamples.codeplex.com/wikipage?title=CciSharp">http://ccisamples.codeplex.com/wikipage?title=CciSharp</a>.
Grab it!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=9dce05c9-217b-45a7-a8d1-2dce2792116d" />
      </body>
      <title>Rich Assertion Messages using CCI (and without expression trees)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/12/31/RichAssertionMessagesUsingCCIAndWithoutExpressionTrees.aspx</link>
      <pubDate>Thu, 31 Dec 2009 04:45:27 GMT</pubDate>
      <description>&lt;p&gt;
It is holiday time so I could spend some time to play with &lt;a href="http://ccimetadata.codeplex.com/"&gt;CCI&lt;/a&gt;.
The result is a bunch of interesting assembly mutators. Let’s start with the first
one: automatic rich assertion messages.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The problem with Assertions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
When an assertion fails, the error message is usually insufficient to understand failure.
The problem is that we are usually lacking the values in the expression to immediately
diagnose the problem. Consider this example,
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.True(x == 123);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
When this assertion fails, one would like to know what was the value of ‘x’. Of course,
that value of x is not serialized in the test output since the assertion method only
sees the ‘false’ value. What we would really like to get is something like ‘x (64)
!= 123’. A number of techniques have been developed to work around this issue.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution #1: Specialized Assertions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
A common approach is to provide specialized assertion methods with enhanced logging.
For example, a special method to test equality:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.Equal(x , 123);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
When this assertion fails, the Equal method has the value of both sides of the equality
and can render it in the message: ‘expected 123, actual 64’. Unfortunately, expressions
are more readable when you write them, and specialized assertions cannot cover all
scenarios. This takes us to the next solution.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution #2: Expression Trees at Run time&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://themechanicalbride.blogspot.com/"&gt;Jafar Husain&lt;/a&gt; wrote &lt;a href="http://themechanicalbride.blogspot.com/2009/06/better-unit-tests-with-testassert-for.html"&gt;a
very interresting post&lt;/a&gt; on how to use Expression Trees to generate rich assertion
messages (this is &lt;a href="http://www.gallio.org/api/html/M_MbUnit_Framework_AssertEx_That.htm"&gt;also
supported in MbUnit&lt;/a&gt;).
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.True(&lt;a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx"&gt;() =&amp;gt;&lt;/a&gt; x
== 123);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Instead of taking a bool, the assert method takes an expression tree (Expression&amp;lt;Func&amp;lt;bool&amp;gt;&amp;gt;).
Thus, when the expression evaluates to false, the expression tree can be traversed
to extract interesting values (such as x) and generate a rich log of the failure. 
&lt;/p&gt;
&lt;p&gt;
Unfortunately, there is a major drawback to this technique: what used to be 3 MSIL
instructions (ldloc.1, ldc.i4 123, ceq) to evaluate the condition becomes hundreds
of methods calls, millions of instructions executed through the System.Linq namespaces.
This performance is a overhead on the &lt;a href="http://research.microsoft.com/pex/"&gt;Pex&lt;/a&gt; whitebox
analysis.
&lt;/p&gt;
&lt;p&gt;
Jafar considers unit test frameworks as an extension of the compiler and uses expression
trees to access what the compiler knows: expression and statements. Following his
idea takes us to the CCI based solution.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution #3: Assembly Rewritter at Compile time&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
In the previous approach, expression trees were used to build a little compiler &lt;strong&gt;at
runtime&lt;/strong&gt;. A better solution would be to rewrite the expressions &lt;strong&gt;at
compile time. &lt;/strong&gt;In other words, we want to build an assembly mutator that takes
the original method call and appends code that generates the logging:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.True(x == 123&lt;strong&gt;, String.Format(“x == 123 where x = ‘{0}’”, x)&lt;/strong&gt;);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The assembly mutator extracts the expression sources from the pdb (‘x == 123’), collect
the local/variable/field references by traversing the expression tree, generate a
String.Format friendly message and replace the assertion method call to the assertion
method that takes the additional string.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Hello &lt;/strong&gt;&lt;a href="http://ccimetadata.codeplex.com/"&gt;&lt;strong&gt;Common Compiler
Infrastructure&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; (CCI) and CciSharp&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Of course, to rewrite an assembly at compile time, we need a framework that can read
and write MSIL, decompile expressions etc… This is where CCI (from &lt;a href="http://ccimetadata.codeplex.com"&gt;http://ccimetadata.codeplex.com&lt;/a&gt; )
comes. It allows to manipulate .NET assemblies using an object model and save them
back to disk. The &lt;a href="http://ccisamples.codeplex.com/wikipage?title=AssertMessage&amp;amp;referringTitle=CciSharp"&gt;assertion
message mutator&lt;/a&gt; is just one of other mutations that have been or will be implemented
as part of &lt;a href="http://ccisamples.codeplex.com/wikipage?title=CciSharp"&gt;&lt;strong&gt;CciSharp&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;,
a post compiler for .NET that is built on top of CCI&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
(There’s already a bunch of useful operators in CciSharp that i’ve been coding over
the holidays: assigning the value of auto-properties, making auto-properties readonly
or lazy (or weakly lazy), or even implementing the DependencyProperty stuff automatically.
We’ll talk about this later.)
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Where can I find it?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;CciSharp&lt;/strong&gt; binaries and sources are avaiable on &lt;a title="http://ccisamples.codeplex.com/wikipage?title=CciSharp" href="http://ccisamples.codeplex.com/wikipage?title=CciSharp"&gt;http://ccisamples.codeplex.com/wikipage?title=CciSharp&lt;/a&gt;.
Grab it!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=9dce05c9-217b-45a7-a8d1-2dce2792116d" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</comments>
      <category>CCI</category>
      <category>CciSharp</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=613f853f-19ed-4482-af6f-685ed1876096</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,613f853f-19ed-4482-af6f-685ed1876096.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,613f853f-19ed-4482-af6f-685ed1876096.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=613f853f-19ed-4482-af6f-685ed1876096</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
For the last couple of weeks, I given on helping hand to Herman Venter to set up the
open-source-MS-PL-super-cool-Common Compiler Infrastructure (<a href="http://ccimetadata.codeplex.com/" target="_blank">CCI</a>)
project. 
</p>
        <p>
You’ve may have heard and probably used different incarnations of the CCI in the past:
the FxCop introspection engine, <a href="http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx" target="_blank">ILMerge</a>, <a href="http://research.microsoft.com/en-us/projects/specsharp/" target="_blank">Spec#</a> or
Code <a href="http://research.microsoft.com/contracts" target="_blank">Contracts</a> use
CCI in same ways. CCI is a set of tools and components that are useful to build compilers:
readers and writers for MSIL and symbol files, and more. With CCI, you can now <em>easily</em> write
your own crazy aspect oriented programming framework, inspect assemblies without reflection
etc…  Just sync the sources, build it, tweak it, etc… It’s all there on <a href="http://ccimetadata.codeplex.com/" target="_blank">codeplex</a> at <a href="http://ccimetadata.codeplex.com">http://ccimetadata.codeplex.com</a>.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=613f853f-19ed-4482-af6f-685ed1876096" />
      </body>
      <title>Read, morph, tweak, write MSIL with CCI (open source)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,613f853f-19ed-4482-af6f-685ed1876096.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/04/16/ReadMorphTweakWriteMSILWithCCIOpenSource.aspx</link>
      <pubDate>Thu, 16 Apr 2009 03:51:23 GMT</pubDate>
      <description>&lt;p&gt;
For the last couple of weeks, I given on helping hand to Herman Venter to set up the
open-source-MS-PL-super-cool-Common Compiler Infrastructure (&lt;a href="http://ccimetadata.codeplex.com/" target="_blank"&gt;CCI&lt;/a&gt;)
project. 
&lt;/p&gt;
&lt;p&gt;
You’ve may have heard and probably used different incarnations of the CCI in the past:
the FxCop introspection engine, &lt;a href="http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx" target="_blank"&gt;ILMerge&lt;/a&gt;, &lt;a href="http://research.microsoft.com/en-us/projects/specsharp/" target="_blank"&gt;Spec#&lt;/a&gt; or
Code &lt;a href="http://research.microsoft.com/contracts" target="_blank"&gt;Contracts&lt;/a&gt; use
CCI in same ways. CCI is a set of tools and components that are useful to build compilers:
readers and writers for MSIL and symbol files, and more. With CCI, you can now &lt;em&gt;easily&lt;/em&gt; write
your own crazy aspect oriented programming framework, inspect assemblies without reflection
etc…&amp;#160; Just sync the sources, build it, tweak it, etc… It’s all there on &lt;a href="http://ccimetadata.codeplex.com/" target="_blank"&gt;codeplex&lt;/a&gt; at &lt;a href="http://ccimetadata.codeplex.com"&gt;http://ccimetadata.codeplex.com&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=613f853f-19ed-4482-af6f-685ed1876096" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,613f853f-19ed-4482-af6f-685ed1876096.aspx</comments>
      <category>CCI</category>
      <category>Code Contracts</category>
      <category>RiSE</category>
    </item>
  </channel>
</rss>