Monday, February 28, 2011

Unit Testing Language Features

I was cleaning up some code I was working on and found a variable that was only used to hold the results of a function that returned a list for enumeration. I could make the code a little more readable by moving the function call directly into the loop. My only question was whether the function would be called each iteration.

We use NUnit at work for test-driven development and automated regression tests. If it works for testing scenarios in the code that we write, why not for language features also? Here is the test I came up with.
<Test(), Explicit()> Public Sub ForEachTest()
  Dim numRuns As Integer = 0

  For Each s As String In TestForEach(numRuns)

  Assert.AreEqual(1, numRuns)
End Sub

Private Function TestForEach(ByRef numberOfRuns As Integer) As IList(Of String)
  numberOfRuns += 1

  Dim result As New List(Of String)
  result.Add("Test 1")
  result.Add("Test 2")
  result.Add("Test 3")

  Return result
End Function
If the iterator calls the function multiple times then the test would fail. I marked the test to be Explicit() so that it would not run with our automated build in the case I accidentally checked it in.

Sunday, February 27, 2011

Intent: Part 2

I use a number of different programming languages and technologies depending on what I am currently working on. Some projects have requirements that lend themselves to one technology or language over another. You will be able to determine which language or technology I'm working with based on what the post contains in it. I will post code samples in the language I am using at the time I have a blog post idea.

Friday, February 25, 2011


Sometimes 140 characters just isn't enough space. Sometimes I want to post about things that don't really fit with my family blog. This is a place for such topics.

I am a software engineer by day. I am paid to solve problems on computers. A lot of problems that I run into have already been solved and Google helps me to not reinvent the wheel. Nevertheless, I do occasionally run into questions that do not have a readily available answer (namely the first few pages of a Google search).

My intent with this blog is to post primarily about computer programming topics. I reserve the right to occasionally differ. I also do not commit to a regular posting schedule.