nunit + f# code
13 Mar 2009 03:33 PM Uncategorizedbeen messing around with f# still. started porting the django templating language to f# (under the auspices of hill30). one of the things that's a must have on a project like that is a good unit-test suite. so what happens when you want to unit test something small? like a string handling function? well...
using NUnit.Framework; using Microsoft.FSharp.Collections; using FSStringList = Microsoft.FSharp.Collections.List<string>; namespace UnitTests { [TestFixture] public class Tests { [Test] public void TestStringFunctions() { Func<string[], FSStringList> of_array = (array) => ListModule.of_array<string>(array); Func<FSStringList, string> to_string = (string_list) => { var tl = string_list; var res = String.Empty; while (ListModule.length<string>(tl) > 0) { res += "\\" + ListModule.hd<string>(tl); tl = ListModule.tl<string>(tl); } return res; }; Assert.AreEqual(to_string(of_array(new string[] { "This", "is", "\"a person's\"", "test." })), to_string(OutputHandling.smart_split("This is \"a person's\" test."))); Assert.AreEqual(to_string(of_array(new string[] { "Another", "'person\'s'", "test." })), to_string(OutputHandling.smart_split("Another 'person\'s' test."))); Assert.AreEqual(to_string(of_array(new string[] { "A", "\"\\\"funky\\\" style\"", "test." })), to_string(OutputHandling.smart_split("A \"\\\"funky\\\" style\" test."))); Assert.AreEqual(to_string(of_array(new string[] { "This", "is", "\"a person's\" test)." })), to_string(OutputHandling.split_token_contents("This is _(\"a person's\" test)."))); Assert.AreEqual(to_string(of_array(new string[] { "Another", "_('person\'s')", "test." })), to_string(OutputHandling.split_token_contents("Another '_(person\'s)' test."))); Assert.AreEqual(to_string(of_array(new string[] { "A", "_(\"\\\"funky\\\" style\" test.)" })), to_string(OutputHandling.split_token_contents("A _(\"\\\"funky\\\" style\" test.)"))); } } }
not too horrible, but one thing is annoying for sure - lambdas aren't compatible with f# FastFunc<>s. Makes sense, but annoying nonetheless. if it was compatible, the second lambda (to_string) would have been a simple fold_left
0 Replies to “nunit + f# code”