Poke brings a small notebook

A test has a fairly modest request. It would like to know what happened, and whether that was supposed to happen.

Poke is my new testing library for Pooka. Version 1.0.0 is out. It brings assertions, property tests, snapshots, and a few other ways to ask a program questions without having to believe its first answer.

Start with something small

Add it as a development dependency:

pooka add poke --dev

Here is a complete test/parcel.spec.pooka:

module parcel_test

import poke/should
import poke/generator
import poke/property
import poke/snapshot

total(items: Int, price: Int) -> Int {
  items * price
}

test "three parcels at 250 cost 750" {
  should.equal(total(3, 250), 750)
}

test "combining orders preserves their total" {
  property.for_all(
    generator.pair(generator.integers(0, 100), generator.integers(0, 100)),
    fn(orders) {
      should.equal(
        total(orders.0 + orders.1, 250),
        total(orders.0, 250) + total(orders.1, 250)
      )
    }
  )
}

test "the parcel label stays familiar" {
  snapshot.snap("To: Owl\n", "parcel-label")
}

should.equal takes the actual value first and the expected value second. A failure keeps both around for the report. Nobody has to reconstruct them from a particularly disappointed False.

Ask more than once

The first test checks an example. The property asks whether combining orders preserves their total across generated pairs of counts.

Poke uses a seed, so the run can be repeated. When a case fails, it tries smaller inputs and reports a counterexample. A small mistake is usually easier to understand than a large one wearing several unrelated numbers.

Properties still need a useful claim. A hundred random questions cannot rescue a question that says nothing.

A snapshot asks permission

On the first run, the snapshot test fails and writes:

test/snapshots/parcel-label.new

Open the reviewer:

pooka run -m poke review

Check the candidate and accept it. Poke saves parcel-label.accepted in the same directory. Commit that file with the test.

Later, suppose the label becomes To: Little Owl. The reviewer puts the old and new text together:

- To: Owl
+ To: Little Owl

You can accept, reject, or leave the candidate for later. Running tests never silently replaces the accepted version. This is useful because a snapshot is evidence of a decision, and occasionally yesterday's decision was better.

The part I keep looking at

After accepting the first snapshot, pooka test lists the tests by module:

  Running tests...

  • test module: parcel_test

  ✓ three parcels at 250 cost 750
  ✓ combining orders preserves their total
  ✓ the parcel label stays familiar

  3 tests, 3 passed

  Finished in 0.27s

The elapsed time will vary. The little list is the part I care about.

Pooka owns test discovery and this report. Poke supplies the checks inside the tests. That arrangement lets a library grow without asking the compiler to recognise its particular furniture.

There is more in the Poke guide, including fixtures, regression cases, and process checks. The cookbook has another complete example.

For now, three questions have answers. The notebook can close for a moment.

— Peregrine Finch

· pooka · testing · poke