Wednesday, September 12, 2007

One of the disadvantages of using non-mainstream .NET languages, like Chrome, F# and IronPython is that the most of the popular and most useful VS.NET add-ins tend not to support them. I can live with this for most things (although its getting harder as these add-ins get better), however, one thing that I can't live without is not being able to run my unit tests without leaving Visual Studio.

I'm using Chrome in my current project, I also use JetBrains ReSharper. ReSharper currently supports C# and VB.NET (JetBrains say that they have no plans to support Chrome :-( ). Its a marvalous refactoring tool and it also comes with a built in unit test runner*. This built in test runner will allow you run or debug a test by clicking on an icon in the left-hand gutter of the editor. Or, you can run your tests via a dockable test browser window that lists all of your test fixtures and tests in the current solution. As you can imagine, this is really convenient; so much so, that with a little imagination you can almost use VS.NET like an interactive IDE similar to a Smalltalk workspace.

Unfortunately, ReSharper's unit test runner doesn't understand Chrome and this put me on a search to try and find a unit test runner that did. At the very least I was hoping for an add-in that would find the tests in a compiled assembly, like NUnit does, and let me run them all the tests by a key-stroke or mouse click, if not individually. 

I could only find two other in-IDE unit test runners for Visual Studio. Of the two I found I tried TestDriven.NET - nada; then I tried ExactMagic’s TestMatrix - zilch. I wasn't surprise by this and in a way I was sort of relieved, I was reluctant to install yet another VS.NET add-in just to run some tests when I already had a perfectly good add-in that could do just that.

At this point I had a brain wave – perhaps I could run my tests via C#.  To try this I added a C# project to my Solution and created a class in the new C# project. I then added the necessary NUnit assembly and using references to the new project and then marked the new C# class as a TestFixture.  Then I inherited the C# class from by Chrome test fixture class (see below).


using NUnit.Framework; 
namespace GeoMEM.NETandCF.Maths.Tests { 
   [TestFixture] 
   public class GMatrixTestWrapper : GMatrixTests { 
   }
}

The Chrome test fixture is declared as follows:


uses
  GeoMEM.NETandCF.Maths,
  Nunit.framework,
  NUnit.Framework.SyntaxHelpers;
  
type
  [TestFixture]
  GMatrixTests = public class( AssertionHelper )
  ...
  public
  ... Lots of tests ....
  end;

I built the new test wrapper project and then ran the tests via the gutter icon. Sure enough, all my Chrome tests duly appeared in ReSharpers unit test browser (and passed, of course). I can even run tests individually and debug them via the test browser. The only thing I don't get is the ability to run tests straight from the editor, but I can live without that. The other thing that I’ve noticed is that if make changes to either your test code or the code under test you have to make sure that you compile the code via C# test wrapper project. Otherwise the wrapper project doesn’t get recompiled and you end up running “stale” tests.

So, to summarise; if you practice TDD and work in a language other than C# or VB.NET then chances are you can use an in-IDE unit test runner if you create a C# project and derive a C# test fixture from each of other languages test fixtures. You will get virtually all the advantages of a in-IDE test runner with perhaps one or two minor niggles.

*JetBrains also do a freebie unit test runner called UnitRun, you can get from here: http://www.jetbrains.com/unitrun/ and what I’ve described here should also work with UnitRun too.

posted on Wednesday, September 12, 2007 5:25:15 PM (GMT Standard Time, UTC+00:00)  #    Comments [2] Trackback
 Wednesday, March 14, 2007

Problem

You need to find the numeric code for a character (either ASCII or Unicode).

Solution

namespace Cookbook1_2;

interface

type
  ConsoleApp = class
  public
    class method Main;
  end;

implementation

class method ConsoleApp.Main;
begin
  var ch      : char    := 'A';
  var chAsInt : Integer := Integer(ch);
  Console.WriteLine( chAsInt );
end;

end.

The ASCII or Unicode value of character is obtained by casting the character to an Integer. Also note that I have forgo Chromes type inference and explicitly declared the type for each variable.

 

posted on Wednesday, March 14, 2007 8:58:53 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Monday, March 05, 2007

Problem

You want to break up a string into its individual tokens or words

Solution

I’ll present the solution in two forms; the first will use “traditional” Object Pascal idiom, the second will make the most of Chrome’s features where appropriate. Both solutions will use the FCL’s string.Split() method.

Object Pascal Version

namespace Cookbook1_1a;

interface

type
  ConsoleApp = class
  public
    class method Main;
  end;

implementation

class method ConsoleApp.Main;
const
  Space : char = ' ';
var
  userRec    : String;
  attributes : array of String;
begin
  userRec    := '01 Barry Carr 17/04/1964
barry@notarealdomain.com';
  attributes := userRec.Split( Space );
 
  for each token : String in attributes do
      Console.WriteLine( token );
end;

end.

Chrome Version

namespace Cookbook1_1b;

interface

type
  ConsoleApp = class
  public
    class method Main;
  end;

implementation

class method ConsoleApp.Main;
const
  Space : char = ' ';
begin
  var userRec    := '01 Barry Carr 17/04/1964
barry@notarealdomain.com';
  var attributes := userRec.Split( Space );
 
  for each token in attributes do
      Console.WriteLine( token );
end;

end.

Notice how the Chrome version doesn’t have any type declarations, Chrome’s type inference is doing all the work of figuring out what the types are at compile time. One other point to note is that Chrome allows the developer to declare their variables in-line, where they are needed and does not require all local variable to be declared at the top of the method, procedure or function as classic object pascal compilers do.

Finally, the loop at the bottom is there so that I can see that the string has been split as I’d expect. A unit test would be better, however.

posted on Monday, March 05, 2007 8:50:25 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Sunday, March 04, 2007

Taking a leaf out of Gary Short’s (cook)book and blatantly stealing his idea, I’m going to follow his example and start some programming cookbooks of my own. Like Gary, my cookbooks will be based upon the items featured in the Python Cookbook. Gary is currently doing a cookbook for Smalltalk, one of my favourite programming languages, so I’ll do cookbooks for D, Chrome and possibly F# and OCaml. I’m interested in learning all of these languages so undertaking these cookbooks should help me to gain a wider understanding of them and also enable me to share my experiences with a wider audience.

The Languages

D is the creation of Walter Bright, the man that developed the worlds first C++ compiler (before Walter, all C++ “compilers” were C preprocessors). D’s aim is to overcome some of the short-coming of C++. D offers virtually all the features you’d expect from a modern language, including: Templates (generics), Delegates, Interfaces, Mixins, (optional) Garbage Collection, Type Inference, Design by Contract, Tail Recursion and much, much more. The D compiler is free to down load and use and is available for Win32 and Linux.

Chrome is an Object Pascal implementation targeted at the .NET and Mono platforms. It’s being developed by RemObjects. Chrome offers all the features of C# 2.0 as well as Design by Contract, Type Inference and Virtual Constructors. It also improves on “classic” Object Pascal implementations by improving the with statement and by adding the ability to declare variables in-line, like the C family of languages. Chrome is a commercial product but RemObjects do provide a free, command line compiler.

OCaml is an Object-oriented functional/imperative language hybrid developed and maintained by INRIA, France's national research institute for computer science. OCaml can be interpreted or compiled very efficiently into native code. Like all the languages mentioned so far, OCaml supports Type Inference, Garbage Collection and Generics. Also, as you would expect from a functional language, OCaml supports: Tail Recursion, Pattern-matching and Currying.

F# is an experimental functional language developed by Microsoft and, as you’d expect, F# is targeted at the .NET platform. F# is based on OCaml.

Tags: , , ,
posted on Sunday, March 04, 2007 8:06:44 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Thursday, February 22, 2007

I can't believe that the people that brought us Delphi, JBuilder and C++Builder have gone and done this: http://www.codegear.com/Products/Delphi/DelphiforPHP/tabid/237/Default.aspx

A PHP IDE!

It's no wonder that Delphi and the BDS is lagging behind the likes of Chrome and Visual Studio when they are frittering their time away on nonsense like this. Why didn't they expend their effort on something up-and-coming like Rails and not on a spent force like PHP?  A decent Rails IDE would have flown off the shelves.

It seems that my evil plan to eradicate PHP has taken a step back - curses

posted on Thursday, February 22, 2007 4:25:04 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback