Skip to content
Kavachकवच

Standard Library: std.test

● Intermediate5 min

Every type, method and function of std.test: what the spec lists, then each declaration in its source, with its examples and the line that declares it.

Spec 14.7.

A declaration below shows what a program writes against: a type's fields, a function's parameters, what it gives, its contracts (require, ensure) and its examples. kavach test runs every example, so each one is true of the code.

What the spec lists

MethodGives
gen.int(from, to)An Int from from to to, both included
gen.bool()true or false
gen.pick(items)One of the items; the list must not be empty
gen.word()A word of 1 to 8 lowercase letters

std.test · test.kav

use std.test: 2 types and 0 functions. stdlib/test/test.kav

Sample interface

A type that makes its own values for generated tests: whenever kavach test needs a value of the type, it calls sample.

pub interface Sample {
    fn sample(gen: Gen) -> Self
}

Gen type

Random choices for sample. They are the test run's, seeded, never the real world's: real random numbers need the Random capability.

pub type Gen {

}

int method

A whole number from from to to, both included.

pub fn int(self, from: Int, to: Int) -> Int
    require from <= to
    require from >= 0 or to <= 9_223_372_036_854_775_807 + from
    ensure result >= from and result <= to
    example Gen().int(3, 3) == 3

bool method

true or false.

pub fn bool(self) -> Bool

pick method

One of items.

pub fn pick<T>(self, items: [T]) -> T
    require not items.is_empty()
    example Gen().pick([7]) == 7

word method

A word of 1 to 8 lowercase letters.

pub fn word(self) -> String
    ensure result.len() >= 1 and result.len() <= 8