PeaBox Lab › PeaLang › Learn › Chapter 10 › Lesson 1

Part II — Intermediate Development · Chapter 10 · Lesson 1 of 6 · 7 min read

Lesson 1: Native Test Syntax and Expectations

Level: Intermediate

Purpose. Turn the informal prediction tables used so far into repeatable, automated tests. This lesson introduces how a PeaLang test is structured, how expectations are written, and how to keep tests small, deterministic, and diagnosable.

Learning objectives

After this lesson you will be able to:

Release note: the test keywords, helper names, and commands in this chapter are illustrative. The language reserves the keyword assert, and the tooling layer includes testing, but verify the exact native syntax and commands against the installed v0.63 release before relying on them.

1.1 From prediction tables to automated tests

Earlier checkpoints asked you to predict a result, run the program, and compare. That process is valuable but manual: it depends on reading output carefully and repeating every case after every change. An automated test records the prediction in code, runs it on demand, and reports a pass or fail that a script or CI pipeline can act on.

1.2 Anatomy of a test

Every test has four parts, whatever syntax expresses them:

PartQuestion answeredExample
NameWhat behavior is being checked?gold order with bulk quantity gets combined rate
ArrangeWhat input and configuration are used?a gold-tier order with items at the bulk threshold
ActWhat single operation is run?call the exported decision function
AssertWhat must be true afterwards?reason code is GOLD_BULK

A test should have one reason to fail. If a test acts on several unrelated things, a failure does not say which one broke.

1.3 A first test (illustrative syntax)

test "gold bulk order gets combined reason" {
    let order = {"order_id": "A1", "customer_tier": "gold", "subtotal": 100, "item_count": 10}
    let result = decide_order(order)
    assert result["reason_code"] == "GOLD_BULK", "expected GOLD_BULK"
}

The block has a descriptive name, one arranged input, one action, and one expectation with a message. The structure matters more than the exact keywords.

1.4 Expectations

An expectation states what must be true. Choose the most specific form that reads clearly.

KindUse whenIllustration
EqualityA value must match exactlyassert result["discount"] == 10
BooleanA condition must holdassert result["allowed"]
NegationA condition must not holdassert not result["allowed"]
MembershipAn item must appear in a collectionassert "VALIDATION_ERROR" == result["reason_code"]
ErrorA call must fail in a defined wayexpect the call to raise a validation error
ToleranceA float result must be close to a targetassert abs(actual - expected) < 0.0001

Prefer equality on a single named field over comparing a whole map at once; the failure then points at the field that differs.

1.5 Failure messages

A failing test is only useful if its message helps. State the expectation and, where it helps, the observed value.

Never include secrets or personal data in a message; failure output is stored in logs.

1.6 Testing pure rules first

The pure rules module from Chapter 9 is the easiest and most valuable thing to test: no files, no network, no clock. Give it values and check results. Test adapters separately and sparingly, using injected substitutes for capabilities.

1.7 Naming and organizing tests

1.8 Boundary tests

Every threshold deserves three tests: just below, exactly at, and just above. For a bulk threshold of 10 items, test 9, 10, and 11.

test "nine items is not bulk" {
    let result = decide_order(order_with(item_count: 9))
    assert result["reason_code"] == "NONE", "9 items must not be bulk"
}

test "ten items is bulk" {
    let result = decide_order(order_with(item_count: 10))
    assert result["reason_code"] == "BULK", "10 items must be bulk"
}

The helper order_with is assumed to build a standard order and override one field; helpers like this keep tests short and make the changed field obvious.

1.9 Testing failure paths

Invalid input and error cases are part of the contract, so test them. Check both that the call is rejected and that the reason is the stable code, not merely that something went wrong.

1.10 Determinism

A test must give the same answer every time. Avoid reading the current time, using unseeded randomness, depending on file order, or relying on the network. Pass such values in as inputs so the test can fix them.

1.11 Running tests and reading results

pea test tests/
pea test tests/rules_tests.pea

Command form is illustrative; confirm it in the installed release. A run should report how many tests passed and failed, name each failure with its message, and return a nonzero exit status when anything fails, so a CI step stops on failure. In CI, run tests after the format check and the checker, as a separate step.

1.12 Common mistakes

MistakeConsequenceCorrection
One test checks many behaviorsA failure does not say what brokeSplit into focused tests
Vague failure messageSlow diagnosisState the expectation and the input
Testing only the happy pathValidation bugs reach usersAdd failure and boundary tests
Tests that depend on each other or on orderIntermittent failuresMake each test self-contained
Copying the implementation into the testThe test cannot catch a wrong ruleWrite expected values from the policy, not from the code
Real network, clock, or files in rule testsFlaky testsInject values and substitutes
Weakening a test to make it passHidden defectsFix the code or change the requirement deliberately

1.13 Diagnostic workflow for a failing test

1.14 Guided practice

1.15 Hands-on lab: Test the Chapter 9 rules module

Goal: Write a small automated suite for the pure rules module of the multi-module project.

Deliverable: The test file, the run output, the defect note, and a table mapping each policy rule to the tests that cover it. Use synthetic data only.

1.16 Review questions

1.17 Applied challenge

Choose a rules module from an earlier chapter and write a test plan: list each policy statement, the test names that cover it, the boundary cases, the failure cases, and the inputs you will inject to keep tests deterministic. Then implement the plan and report which statements, if any, remain uncovered and why.

1.18 Lesson summary

Native tests turn predictions into repeatable checks. Each test has a clear name, an arrangement, a single action, and a specific expectation with a helpful message. Boundary and failure tests protect the contract, determinism keeps results trustworthy, and a nonzero exit status lets automation stop on failure. Begin with the pure rules module, where tests are simple and valuable.

Transition to Lesson 2

The next lesson covers filtering, watch mode, and coverage baselines: running only the tests you need, re-running them as you edit, and measuring what the suite exercises.

Lesson status: Ready for review.

← Previous Next →