Skip to content

Examples

examples

The checked examples of a literate module.

A fence written ```qvr example holds the steps of an example: let bindings and expect lines that state an expected result of the module's code. Each such fence is a test of the module the document tangles to (quivers.testing), named after the document and the fence's line, and its steps keep their document lines, so a failure names the line of the expectation that failed.

document_examples

document_examples(text: str, label: str) -> tuple[TestDecl, ...]

The examples a literate document's qvr example fences declare.

PARAMETER DESCRIPTION
text

The document.

TYPE: str

label

The document's path, which names each example and its diagnostics.

TYPE: str

RETURNS DESCRIPTION
tuple[TestDecl, ...]

One example per fence, in document order, named <label>:<line> after the fence's line.

RAISES DESCRIPTION
ParseError

If a fence's body is not a sequence of example steps, located at the fence.

Source code in src/quivers/literate/examples.py
def document_examples(text: str, label: str) -> tuple[TestDecl, ...]:
    """The examples a literate document's ``qvr example`` fences declare.

    Parameters
    ----------
    text : str
        The document.
    label : str
        The document's path, which names each example and its diagnostics.

    Returns
    -------
    tuple[TestDecl, ...]
        One ``example`` per fence, in document order, named
        ``<label>:<line>`` after the fence's line.

    Raises
    ------
    ParseError
        If a fence's body is not a sequence of example steps, located at the
        fence.
    """
    examples: list[TestDecl] = []
    for block in scan_blocks(text, literate=True):
        if block.mode != "example":
            continue
        name = f"{label}:{block.line}"
        source = (
            "\n" * (block.line - 1)
            + f'example "{name}" =\n'
            + indent_body(_STEP_INDENT, block.body)
        )
        try:
            parsed = parse(source, label)
        except ParseError as error:
            raise ParseError(
                f"{name}: an example fence holds example steps: {error}"
            ) from error
        if len(parsed.tests) != 1 or parsed.statements:
            raise ParseError(f"{name}: an example fence holds example steps")
        examples.append(parsed.tests[0])
    return tuple(examples)

literate_source

literate_source(path: Path) -> tuple[bytes, tuple[TestDecl, ...]]

Read a literate module as the test runner reads a module file.

PARAMETER DESCRIPTION
path

A .qvr.md file.

TYPE: Path

RETURNS DESCRIPTION
tuple[bytes, tuple[TestDecl, ...]]

The source it tangles to, and the examples its example fences declare.

RAISES DESCRIPTION
OSError

If the file cannot be read.

ParseError

If an example fence does not hold example steps.

Source code in src/quivers/literate/examples.py
def literate_source(path: Path) -> tuple[bytes, tuple[TestDecl, ...]]:
    """Read a literate module as the test runner reads a module file.

    Parameters
    ----------
    path : Path
        A ``.qvr.md`` file.

    Returns
    -------
    tuple[bytes, tuple[TestDecl, ...]]
        The source it tangles to, and the examples its example fences
        declare.

    Raises
    ------
    OSError
        If the file cannot be read.
    ParseError
        If an example fence does not hold example steps.
    """
    text = path.read_text(encoding="utf-8")
    return tangle(text).encode("utf-8"), document_examples(text, str(path))