Home / Docs / Diagnostics

U3015: std.testing used outside a handler or example

Release v0.4.0

A function of std.testing is used outside handler operation bodies and the lines of a test.

Why Una rejects it

The functions of std.testing, such as fake_hash, produce fake values for tests. They are usable only inside handler operation bodies and the lines of a test, an example in editions 0.1 and 0.2 and a test in edition 0.3 (spec/14 §4, ADR 0105), so production code never depends on them.

How to fix it

Move the fake into a handler, and install the handler in the example.

The repair at a glance

diff
  module demo

  import std.testing (fake_hash)

- pub digest(seed: Int) -> internal Hashed = fake_hash(seed)
+ capability Hasher
+   op hash(seed: Int) -> internal Hashed
+
+ handler fake() for Hasher
+   op hash(seed) = fake_hash(seed)
+
+ pub digest(seed: Int) -> internal Hashed
+   uses @hasher: Hasher[hash]
+ =
+   @hasher.hash(seed)
+
+ example "a digest"
+   given @hasher = fake()
+   do digest(1)
+   expect result == fake_hash(1)

Failing example

una
module demo

import std.testing (fake_hash)

pub digest(seed: Int) -> internal Hashed = fake_hash(seed)

Fixed example

una
module demo

import std.testing (fake_hash)

capability Hasher
  op hash(seed: Int) -> internal Hashed

handler fake() for Hasher
  op hash(seed) = fake_hash(seed)

pub digest(seed: Int) -> internal Hashed
  uses @hasher: Hasher[hash]
=
  @hasher.hash(seed)

example "a digest"
  given @hasher = fake()
  do digest(1)
  expect result == fake_hash(1)