By Example

Other than showcasing how monetary quantities can be formatted and showing off the data available for different currencies, one of the objectives of NMoneys.Web is showing code samples on how to accomplish common tasks.

Deviate from Reality

Code examples, like code comments, are always at risk to deviate from reality offered by code, given a forgetful developer fails to change samples and code when software evolves.

Unfortunately, there is no samples (or comments) compiler or tester so far… Or is there?

The Real Thing

What if we could serve .cs source files that get compiled (and, optionally, tested) so that we have totally living documentation?

Serving files with the .cs extension is disabled by default by ASP.NET, but it can be easily enabled and constrained to a path of the application:

So, whenever a source file is requested (for example, from the quickstart), a real .cs file is served.

Real Things Get Compiled

What’s more, those source files that are served through IIS, can be compiled to verify their validity as C# code.

That can be achieved in an automated test and it is simple, although it involves more than one step:

  1. Add the .cs files that need to be compiled to the test project as links (so that there is no file duplication)
  2. Set the Build Action to Content
  3. Set the Copy to Output Directory to Copy Always

And with those steps, every time the test project is compiled, there will be a bunch of .cs files inside a folder hierarchy below the bin folder.

In order to compile them, I chose to use the compilation and code generation services of

CSharpCodeProvider, that can be wrapped in a function in order to call it from multiple tests:

Tests make Wonderful Examples

That I am quite a fan of automated testing, quite a few people can confirm.
I am also a believer that well-written unit tests make one of the best documentations available and I put my money where my mouth was by writing the code samples for my two Codeproject articles I wrote some time ago as unit tests.

So we can compile our code samples, how difficult could it be to run the tests they contain? Not much, to be honest.

  1. Add references to nunit.core.dll and nunit.core.interfaces.dll (that can be obtained from the NUnit.Runners packages (or from my version of it NUnit.Runners.lite)
  2. Call this method from multiple tests:
  3. Assert that are not failures in the result (the .IsFailure property)

Kategorier: Udvikling

Tagged as: ,

2 Comments »

  1. I’ve been surfing online mre than three hours lately, yet
    I never found any attention-grabbing article like yours.
    It’s pretty value enough for me. In mmy opinion, if all site owners and bloggers made good content material as you probably did, the web might be a lot more
    useful than ever before.

Skriv et svar

Udfyld dine oplysninger nedenfor eller klik på et ikon for at logge ind:

WordPress.com Logo

Du kommenterer med din WordPress.com konto. Log Out / Skift )

Twitter picture

Du kommenterer med din Twitter konto. Log Out / Skift )

Facebook photo

Du kommenterer med din Facebook konto. Log Out / Skift )

Google+ photo

Du kommenterer med din Google+ konto. Log Out / Skift )

Connecting to %s