Wiki

Tests

Support for provision of (unit) Test code is built into the cobra language.

Tests are specified as clauses on Class members (methods and properties) or on Class types(?).

They are given as an indented block of standalone code prefixed by the test keyword suffix with an optional test name  forum discussion.
The indented code block does the setup for a test (object instantiation and state initialisation), executes the test case (or cases), verifies ( usually by assertions) that the test passes followed by any necessary teardown.

Tests are executed whenever the program or code is run (unless tests are suppressed at build by the -include-tests:no compiler option).

Failures show up as an assert failure and a stack trace.

Example

class Incrementer

    var counter =0
    var val = 0

    test
      incr = Incrementer()
      assert incr.counter == 0
      assert incr.val == 0 
      i = incr.bump(2)
      assert i == 2
      assert incr.counter == 1
      assert incr.val == 2
      i = incr.bump(3)
      assert i == 5
      assert incr.counter == 2
      assert incr.val == 5


    def bump(inc as int) as int
        test
          incr = Incrementer()
          incr.bump(1)
          assert incr.counter == 1
          assert incr.val == 1
          incr.bump(2)
          assert incr.counter == 2
          assert incr.val == 3
        body
           .counter += 1
           .val += inc
           return .val 

class X

    test fill
        ml = MultiList<of int>(4, 3, 2).fill(5, [1, 2, 3, 4, 5, 6, 7, 8])
        assert ml.toString == ...
        ml1 = MultiList<of Pair<of int>>([2, 2]).fill([Pair<of int>(0, 2), Pair<of int>(1, 3)])
        ml2 = MultiList<of Pair<of int>>([2, 2], [Pair<of int>(0, 2), Pair<of int>(1, 3)])
        assert ml1.equals(ml2)

    test cloneAndTranspose
        ml = MultiList<of int>([3, 4, 2], for i in 24 get i+3)
        assert ml.clone.transpose.equals(ml.permute([2, 1, 0]))