soma.cell

Basic Tests

Tests are declared with `cell test Name { rules { assert ... } }`.

Example 092 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.

How it is built

The cell Calculator has no face and no memory. Handlers: add (line 17) and multiply (line 21).

The file also holds 1 test cell (BasicMath) with 4 assertions, run by soma test.

Generated from soma describe on this file.

The source

092_tests_basic.cell

// ============================================================================
// 092 — Basic Tests
// ============================================================================
// Tests are declared with `cell test Name { rules { assert ... } }`.
//
// Run:   soma test 092_tests_basic.cell
// Expect:
//   test BasicMath ...
//     ok assert add(2, 3) == 5
//     ok assert add(0, 0) == 0
//     ok assert multiply(3, 4) == 12
//     ok assert multiply(0, 5) == 0
//   4 tests: 4 passed, 0 failed

// The code under test
cell Calculator {
    on add(a: Int, b: Int) {
        return a + b
    }

    on multiply(a: Int, b: Int) {
        return a * b
    }
}

// Declarative test cell
cell test BasicMath {
    rules {
        // assert expression == expected_value
        assert add(2, 3) == 5
        assert add(0, 0) == 0
        assert multiply(3, 4) == 12
        assert multiply(0, 5) == 0
    }
}

What the toolchain says

$ soma check 092_tests_basic.cell
✓ All checks passed.
$ soma test 092_tests_basic.cell
test BasicMath ...
  ✓ assert add(2, 3) == 5
  ✓ assert add(0, 0) == 0
  ✓ assert multiply(3, 4) == 12
  ✓ assert multiply(0, 5) == 0

4 tests: 4 passed, 0 failed