LEVIATHAN vd52e79c4 · d52e79c486

Standard Library

class ContractViolation

A violated contract: a failed pre, post, assert or invariant.

since 0.1.0-alpha.1linuxwindowswasm

bases
IException, ILogicException

Overview

It implements IException and ILogicException but does not extend Exception, so ordinary error handling (catch (Exception e)) lets it pass, and a boundary that must survive any failure (a test runner, a request loop) catches IException. Its fields hold the report; message is the report as text, which is what an uncaught violation prints. The compiler builds and throws these; a library that checks a contract by hand may build one too.

Examples

use io::console;

try {
    throw ContractViolation(ContractKind::Assertion, "x > 0", "main", "demo.lev:3:5");
} catch (Exception e) {
    console.writeln("not here");
} catch (IException e) {
    console.writeln(e.message);
}
assertion failed: x > 0
  in main at demo.lev:3:5

Constructors

new

new(ContractKind kind, string predicate, string function, string location)

Creates a violation report with no operands.

Parameters

kind
Which kind of check failed.
predicate
The predicate's source text.
function
The function, class or enclosing function the check belongs to.
location
Where the predicate is written, or "".

Examples

use io::console;

ContractViolation v = ContractViolation(ContractKind::Precondition, "n > 0", "half", "a.lev:1:20");
console.writeln(v.message);
precondition violated: n > 0
  in half at a.lev:1:20

Fields

attached

Array<IException> attached

Exceptions that came with the violation: one thrown while evaluating the predicate, or by a reporter.

callerLocation

string callerLocation

Where the reporting call was written, as file:line:col; empty for an assert.

function

string function

The function that was entered, the class whose invariant failed, or the function holding the assert.

kind

Which kind of check failed.

location

string location

Where the predicate is written, as file:line:col.

message

string message

The report as text (see format).

operands

The predicate's top-level operands that were evaluated, with their values.

predicate

string predicate

The predicate's source text.

reported

bool reported

Whether a reporter has already seen this violation.

userMessage

string userMessage

The message of assert(condition, message); empty otherwise.

Methods

format

format() -> string

Builds the report text from the fields.

The first line is the kind and the predicate. Then come, when present: where the predicate is and which function it belongs to, the calling site, one text = value line per operand (an operand that is its own value, like 0, is left out, and so is a repeated line), the assert message, and one line per attached exception.

Returns

The report.

Examples

use io::console;

ContractViolation v = ContractViolation(ContractKind::Precondition, "n > 0", "half", "a.lev:1:20");
v.callerLocation = "a.lev:9:1";
v.operands.add(ContractOperand("n", "-2"));
v.operands.add(ContractOperand("0", "0"));
console.writeln(v.format());
precondition violated: n > 0
  in half at a.lev:1:20
  called from a.lev:9:1
  n = -2

kindText

static kindText(ContractKind k) -> string

The headline word for a kind of check.

Parameters

k
The kind.

Returns

precondition violated, postcondition violated, assertion failed or invariant violated.

Examples

use io::console;

console.writeln(ContractViolation::kindText(ContractKind::Invariant));
invariant violated

toString

toString() -> string

Returns the report text.

Returns

message.

Examples

use io::console;

ContractViolation v = ContractViolation(ContractKind::Assertion, "ok", "", "");
console.writeln(v.toString());
assertion failed: ok

See also