soma.cell

Multiple Cells in One File

A file can contain multiple cells. Each is an independent unit. Tests can reference handlers from non-test cells.

Example 098 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 Arithmetic has no face and no memory. Handlers: add (line 19), subtract (line 23), multiply (line 27), absolute (line 31) and run (line 35).

The cell Demo has no face and no memory. Handlers: run (line 44).

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

Generated from soma describe on this file.

The source

098_multi_cell.cell

// ============================================================================
// 098 — Multiple Cells in One File
// ============================================================================
// A file can contain multiple cells. Each is an independent unit.
// Tests can reference handlers from non-test cells.
//
// Run:   soma test 098_multi_cell.cell
// Expect: All tests pass
//
// Run:   soma run 098_multi_cell.cell run
// Expect:
//   5 + 3 = 8
//   5 - 3 = 2
//   5 * 3 = 15
//   abs(-7) = 7

// Cell 1: Arithmetic operations
cell Arithmetic {
    on add(a: Int, b: Int) {
        return a + b
    }

    on subtract(a: Int, b: Int) {
        return a - b
    }

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

    on absolute(n: Int) {
        return abs(n)
    }

    on run() {
        let r = add(5, 3)
        print("{r}")
        return "done"
    }
}

// Cell 2: Demo that uses the arithmetic cell
cell Demo {
    on run() {
        let a = 5
        let b = 3
        let sum = a + b
        let diff = a - b
        let prod = a * b
        let av = abs(-7)
        print("{a} + {b} = {sum}")
        print("{a} - {b} = {diff}")
        print("{a} * {b} = {prod}")
        print("abs(-7) = {av}")
        return "done"
    }
}

// Cell 3: Tests for the arithmetic cell
cell test ArithmeticTests {
    rules {
        assert add(10, 20) == 30
        assert subtract(10, 3) == 7
        assert multiply(6, 7) == 42
        assert absolute(-42) == 42
        assert absolute(42) == 42
    }
}

What the toolchain says

$ soma check 098_multi_cell.cell
✓ All checks passed.
$ soma test 098_multi_cell.cell
test ArithmeticTests ...
  ✓ assert add(10, 20) == 30
  ✓ assert subtract(10, 3) == 7
  ✓ assert multiply(6, 7) == 42
  ✓ assert absolute(-42) == 42
  ✓ assert absolute(42) == 42

5 tests: 5 passed, 0 failed
$ soma run 098_multi_cell.cell run
8
done