Skip to content

calculator.aug ​

A small tested application · Source and specification

Files in this project

Code ​

aug
// aug-spec: "calculator.aug.md" explains this file. Read it before changes; refresh with aug spec.
import Console and SystemConsole from august.io
import Logger from logging
/** Adds two integers. */
interface Arithmetic:
    add(resolve Console console, int left, int right) returns int uses Console.write
/** Uses the selected logger to describe each addition. */
Calculator(resolve Logger logger to _logger) implements Arithmetic:
    /**
    * Adds left and right, logging the operation.
    * @param left First integer.
    * @param right Second integer.
    * @return Sum of the two integers.
    */
    add(resolve Console console, int left, int right):
        _logger.log(message="adding integers")
        return left + right
/**
* Demonstrates a checked failure instead of a successful result.
* @param fail Whether to simulate a failed load.
* @throws FileError When fail is true.
*/
load(bool fail):
    if fail:
        throw FileError()
    return "loaded"
/** Test adapter: keeps calculator tests independent of console output. */
_SilentLogger() implements Logger:
    log(resolve Console console, string message) uses Console.write:
        pass
test Calculator calculator:
    when "addition":
        implement Console with SystemConsole
        implement Logger with _SilentLogger
        calculator = Calculator()
        List<int> values = [1, 2]
        it "adds labeled inputs":
            assert(calculator.add(right=2, left=1) == 3)
            borrow values:
                values.append(value=3)
            assert(values.length() == 3)
        it "starts with fresh setup":
            assert(values.length() == 2)
            assert(
                calculator.add(left=values.get(index=0), right=values.get(index=1)) == 3
            )
aug
// aug-spec: "calculator.aug.md" explains this file. Read it before changes; refresh with aug spec.
import Console and SystemConsole from august.io
import Logger from logging
/** Adds two integers. */
interface Arithmetic {
    add(resolve Console console, int left, int right) returns int uses Console.write
}
/** Uses the selected logger to describe each addition. */
Calculator(resolve Logger logger to _logger) implements Arithmetic {
    /**
    * Adds left and right, logging the operation.
    * @param left First integer.
    * @param right Second integer.
    * @return Sum of the two integers.
    */
    add(resolve Console console, int left, int right) {
        _logger.log(message="adding integers")
        return left + right
    }
}
/**
* Demonstrates a checked failure instead of a successful result.
* @param fail Whether to simulate a failed load.
* @throws FileError When fail is true.
*/
load(bool fail) {
    if fail {
        throw FileError()
    }
    return "loaded"
}
/** Test adapter: keeps calculator tests independent of console output. */
_SilentLogger() implements Logger {
    log(resolve Console console, string message) uses Console.write {
        pass
    }
}
test Calculator calculator {
    when "addition" {
        implement Console with SystemConsole
        implement Logger with _SilentLogger
        calculator = Calculator()
        List<int> values = [1, 2]
        it "adds labeled inputs" {
            assert(calculator.add(right=2, left=1) == 3)
            borrow values {
                values.append(value=3)
            }
            assert(values.length() == 3)
        }
        it "starts with fresh setup" {
            assert(values.length() == 2)
            assert(
                calculator.add(left=values.get(index=0), right=values.get(index=1)) == 3
            )
        }
    }
}

Compiled specification ​

Interactions and sequences

Arithmetic · interface · source ​

Adds two integers.

Arithmetic.add · source ​

It takes left and right as integers. It gets console (Console) from dependency injection. It returns int. It can call Console.write.

Calculator · class · source ​

Uses the selected logger to describe each addition. It implements Arithmetic. The _logger dependency is injected as Logger and stored read-only and privately.

Calculator.add · source ​

Adds left and right, logging the operation. It takes left and right as integers. It gets console (Console) from dependency injection.

It returns int — Sum of the two integers. It can call Console.write. It passes "adding integers" to _logger.log, using injected console. It returns left plus right. source

Checked interface
text
add(resolve Console console, int left, int right) returns int uses Console.write

It takes left as an integer (First integer) and right as an integer (Second integer). It gets console (Console) from dependency injection. It returns int — Sum of the two integers. It can call Console.write.

load · source ​

Demonstrates a checked failure instead of a successful result. It takes fail as a boolean. Failures can raise FileError (when fail is true).

It checks that fail is false. It raises a FileError at the first failed check. It returns "loaded". source

Checked interface
text
load(bool fail) returns string unless FileError

It takes fail as a boolean (Whether to simulate a failed load). Failures can raise FileError (when fail is true).

_SilentLogger · class · source ​

Test adapter: keeps calculator tests independent of console output. It implements Logger. It is private to this file.

_SilentLogger.log · source ​

It takes message as a string. It gets console (Console) from dependency injection. It can call Console.write. It continues without an operation. source

Checked interface
text
log(resolve Console console, string message) returns void uses Console.write

It takes message as a string (Text to write). It gets console (Console) from dependency injection. It can call Console.write.

test Calculator calculator · source ​

Tests Calculator. Each case gets fresh setup and dependencies.

addition ​

Setup for each case: Console is provided by SystemConsole. Stateless instances are reused; stateful instances are created for each resolve. Logger is provided by _SilentLogger. Stateless instances are reused; stateful instances are created for each resolve.

It sets calculator to a Calculator using injected Logger for _logger. It sets values of type List<int> to a list containing 1, 2. source

adds labeled inputs · source ​

The test requires calculator.add with right 2 and left 1 using injected Console for console equals 3. With temporary permission to change values, it appends 3 to values. The test requires the number of elements in values equals 3. source

starts with fresh setup · source ​

The test requires the number of elements in values equals 2. The test requires calculator.add with left from the item at index 0 in values and right from the item at index 1 in values using injected Console for console equals 3. source

Dependencies ​

It uses Console (write) and SystemConsole from august.io. It uses Logger (log) from logging.

Built-in operations follow the language reference.

Last updated:

The world runs on language