Saturday, December 26, 2009

Method reflection and unit tests

I just finished v2 of the simple unit test framework. The v1 version simply looked for global functions beginning with "test" and called them. The v2 version now supports JUnit-like test fixtures, where. Unit tests derive from tart.testing.Test.

Here's a short example of a test class:

import tart.collections.ArrayList;
import tart.reflect.ComplexType;
import tart.testing.Test;

@EntryPoint
def main(args:String[]) -> int {
  return ArrayListTest().runTests(ArrayListTest);
}

class ArrayListTest : Test {
  def testConstruct() {
    let a = ArrayList[int](1, 2, 3);
    assertEq(3, a.length);
    assertEq(1, a[0]);
    assertEq(2, a[1]);
    assertEq(3, a[2]);
  }
  def testImplicitType() {
    let a = ArrayList(1, 2, 3);
    assertEq(3, a.length);
    assertEq(1, a[0]);
    assertEq(2, a[1]);
    assertEq(3, a[2]);
  }
}

This demonstrates a number of interesting language features:
  • Reflection of class methods.
  • "import namespace". The 'assertEq' methods are defined in class "Asserts". Class Test does an "import namespace Asserts", which means that all of the assert methods are available without needing a qualifier (i.e. Asserts.assertEq).
The test output looks like this:

[Running] ArrayListTest.testConstruct
[Running] ArrayListTest.testImplicitType
[OK     ]

No comments:

Post a Comment