domain/numbers.aug
Modules and composition · Source and specification
Files in this project
Code
// aug-spec: "numbers.aug.md" explains this file. Read it before changes; refresh with aug spec.
/** Raised when an input is outside the operation's domain. */
RangeError(int value) implements Error:
pass
/** A pure validation layer, shared by any compatible callable. */
interceptor Positive<T>():
around(int amount) returns T:
if amount < 0:
throw RangeError(value=amount)
return next()
/**
* Double a nonnegative amount.
* @param amount Integer to double.
* @return Twice the amount, with defined integer wrapping.
* @throws RangeError A validation layer rejected a negative input.
*/
[Positive]
double(int amount):
return amount * 2
test double:
when "positive":
it "doubles" for (input, expected) in [(0, 0), (3, 6), (7, 14)]:
assert(double(amount=input) == expected)
it "rejects_negative":
bool rejected to false
try:
double(amount=-1)
catch RangeError error:
rejected to error.value == -1
assert(rejected)// aug-spec: "numbers.aug.md" explains this file. Read it before changes; refresh with aug spec.
/** Raised when an input is outside the operation's domain. */
RangeError(int value) implements Error {
pass
}
/** A pure validation layer, shared by any compatible callable. */
interceptor Positive<T>() {
around(int amount) returns T {
if amount < 0 {
throw RangeError(value=amount)
}
return next()
}
}
/**
* Double a nonnegative amount.
* @param amount Integer to double.
* @return Twice the amount, with defined integer wrapping.
* @throws RangeError A validation layer rejected a negative input.
*/
[Positive]
double(int amount) {
return amount * 2
}
test double {
when "positive" {
it "doubles" for (input, expected) in [(0, 0), (3, 6), (7, 14)] {
assert(double(amount=input) == expected)
}
it "rejects_negative" {
bool rejected to false
try {
double(amount=-1)
}
catch RangeError error {
rejected to error.value == -1
}
assert(rejected)
}
}
}Compiled specification
RangeError · class · source
Raised when an input is outside the operation's domain. It implements Error. It takes value as an integer, kept read-only.
Positive · interceptor · source
A pure validation layer, shared by any compatible callable. The type parameters are T. Creates one interceptor per invocation. Its around operation may delegate once or finish early.
Positive.around · source
It takes amount as an integer. Failures can raise RangeError. If amount is negative, it raises a RangeError with value from amount. It returns next. source
Checked interface
around(int amount) returns T unless RangeErrorIt takes amount as an integer. Failures can raise RangeError.
double · source
Double a nonnegative amount. It takes amount as an integer. It returns int — Twice the amount, with defined integer wrapping. Failures can raise RangeError (A validation layer rejected a negative input).
Layers run in the declared order. Call Positive.around. It returns amount times 2. source
Checked interface
double(int amount) returns int unless RangeErrorIt takes amount as an integer (Integer to double). It returns int — Twice the amount, with defined integer wrapping. Failures can raise RangeError (A validation layer rejected a negative input).
test double · source
Tests double. Each case gets fresh setup and dependencies.
positive
Run once for each row of a tuple containing 0, 0; a tuple containing 3, 6; a tuple containing 7, 14. Bind row positions to input, expected. The test requires double with amount from input equals expected. source
It sets rejected to false. source
It tries to call double with amount -1. If this work raises RangeError as error, it sets rejected to error.value equals -1. The test requires rejected is true. source
Built-in operations follow the language reference.