Wiki
Version 2 (modified by nerdzero, 5 years ago)

correct typo

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.
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