Asserts your function invariants
annotate allows you to ... guess what ... annotate your functions. For
instance you could document invariants of your function. Or attach a
description to it. It is possible to access this data later on.
This metadata can be used by tools such as annofuzz in order to generate tests. In addition you can access the metadata via REPL.
The usage is quite simple as the following example illustrates:
// let's define some function to annotatereturn a + b;// type checkers from annois ()var addNumbers = annotate'addNumbers' 'Adds numbers'onisnumber isnumber add;var addStrings = annotate'addStrings' 'Adds strings'onisstring isstring add;// you can assert invariants toovar addPositive = annotate'addPositive' 'Adds positive'onisPositive isPositive addsatisfiesisPositive; // postcondition// it is possible to chain guardsvar fib = annotate'fib' 'Calculates Fibonacci numbers'on0 0on1 1onisnumberreturn fibn - 1 + fibn - 2;;// invariants may depend on each othervar clamp = annotate'clamp' 'Clamps given number between given bounds'onisnumber isnumberreturn isnumbera && args1 <= a;return MathmaxMathmina max min;;// furthermore it is possible to pass a variable amount of argsvar min = annotate'min' 'Returns minimum of the given numbers'onisnumber Mathmin;return a >= 0;
annotate function will create a new function that contains the metadata as
_postconditions. In case
some pre- or postcondition doesn't pass it won't return and gives a warning
- suite.js - Constructs tests based on invariant data (fuzzing)
- funkit - Collection of utilities tested using
annotate is available under MIT. See LICENSE for more details.