hd-lang

Tour · 13 of 18

Tests

The program on the right prices parcels and tests the price in three ways.

#The tests: block

tests:
    it("a parcel up to 1 kg pays the base rate"):
        assert_equal(shipping_cents(800), 499)

A file keeps its unit tests in one tests: block. Test code can call private helpers, and it does not ship in the program.

#Examples, tables, and properties

it_prop("shipping never drops below the base", prop=fn!(grams: u16):
    assert(shipping_cents(i32(grams)) >= 499)
)
  • it checks one example.
  • it_each runs one body over a table of rows.
  • it_prop checks a rule on generated inputs.

it_each and it_prop come from std.testing, so the block starts with a use.

Test it. Press Test. hd test does not run a script body, so this page has no top-level println. One example, two table rows and one property give 4 tests passed.

Try it: change if weight_grams <= 1000: 499 else: 499 + (weight_grams - 1000) / 500 * 150 to if weight_grams <= 1000: 450 else: 499 + (weight_grams - 1000) / 500 * 150 and press Test.

Test: Ctrl+Enter · Pages: Alt+← Alt+→