Documentation contents
Language guide

Inputs, scenarios and tests

Turn a calculation into something you can run under different conditions and check: will the town's pumps keep up with the forecast rain?

On this page

Myceta 2.0. These pages describe the language as planned for 2.0, the first public release, which is still in development. Details may change before release. The examples were type-checked and tested with the development compiler.

Nodes

A node is a named result. It depends on the other nodes it mentions, and the order of declarations does not matter. A function is a helper: calling one does not create a node.

fn highest(levels: array<f64<mm>>) -> f64<mm> =
    fold peak = 0.0<mm> with level in levels -> if level > peak then level else peak

node Forecast = [4.0<mm>, 12.5<mm>, 31.0<mm>]
node Peak = highest(Forecast)

Make something a node when it deserves a name of its own: a figure someone will ask about, test or trace back.

Inputs

An inputs block declares values that come from outside the calculation, each with a type and a default.

inputs Plan {
    pumpCapacity: f64<mm> = 40.0<mm>
    reserve: f64<mm> = 5.0<mm>
}

A node reads an input with @in, the block name and the field.

node Usable = @in.Plan.pumpCapacity - @in.Plan.reserve
node Margin = Usable - Peak

Scenarios

A scenario is a named set of input values. A second scenario can start from the first and change only what differs.

scenario Normal {
    Plan.pumpCapacity = 40.0<mm>
}

scenario PumpFault = Normal with {
    Plan.pumpCapacity = 30.0<mm>
}

The same nodes now answer two questions. With every pump working the margin is 4 mm. With one pump out it is 6 mm short.

A check that reports

A node can hold a rule in place of a value. If the condition is false, a rule with severity error makes the node finish as FAIL and attaches the message.

node DrainageCheck {
    validate {
        rule PumpsKeepUp: Margin >= 0.0<mm>
            severity error
            message "Forecast rainfall exceeds usable pump capacity"
    }
}

Tests

A test block lives in the same file as the nodes it checks. It names the scenario to run under and states what each node should produce. Use within to give a tolerance for floating-point values.

test PumpsCope under scenario Normal {
    expect Peak : OK with value = 31.0<mm> within 0.001<mm>
    expect MarginMagnitude : OK with value = 4.0 within 1e-9
    expect DrainageCheck : OK
}

A test can also define a value of its own, which stays out of the main calculation. This one takes the unit off the margin so it can be compared as a plain number.

test MarginMagnitude = strip_unit(Margin)

Under the fault scenario, DrainageCheck finishes as FAIL. A test that expects a failure has to name the diagnostic it expects, in the form expect DrainageCheck : FAIL with diagnostic MYC####. The reference gives the grammar for all five outcomes.

test FaultIsReported under scenario PumpFault {
    expect MarginMagnitude : OK with value = -6.0 within 1e-9
}

Running the tests

A test manifest lists the files to check and, for tests that need one, the scenario.

[[cases]]
id = "station.graph.normal"
kind = "test"
source = "station-graph.myc"
scenario = "Normal"

[[cases]]
id = "station.graph.pump-fault"
kind = "test"
source = "station-graph.myc"
scenario = "PumpFault"

In the 2.0 toolchain the command is myc test station-tests.toml. It reports each case as passed or failed and exits with an error if any case fails. The command-line reference describes the planned commands.

Run the example

The complete module is station-graph.myc, with the test manifest that covers every example in this guide. Continue with the language reference, or go back to the language tour.