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> attachedExceptions that came with the violation: one thrown while evaluating the predicate, or by a reporter.
callerLocation
string callerLocationWhere the reporting call was written, as file:line:col; empty for an assert.
function
string functionThe function that was entered, the class whose invariant failed, or the function holding the assert.
kind
ContractKind kindWhich kind of check failed.
location
string locationWhere the predicate is written, as file:line:col.
message
string messageThe report as text (see format).
operands
Array<ContractOperand> operandsThe predicate's top-level operands that were evaluated, with their values.
predicate
string predicateThe predicate's source text.
reported
bool reportedWhether a reporter has already seen this violation.
userMessage
string userMessageThe message of assert(condition, message); empty otherwise.
Methods
format
format() -> stringBuilds 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) -> stringThe 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() -> stringReturns the report text.
Returns
message.
Examples
use io::console;
ContractViolation v = ContractViolation(ContractKind::Assertion, "ok", "", "");
console.writeln(v.toString());
assertion failed: ok
See also
- IContractReporter — Receives every contract violation before it is thrown.
- ContractKind — What kind of check a
ContractViolationreports.