nupp.test

Assertions, for Nupp's own test runner and for project suites.

const test = nupp.test

test.equal(2 + 2, 4)
test.deepEqual({"ready", 200}, {"ready", 200})
test.matches(greeting, "^hello")
test.raises(function(): nil error("nope") end, "nope")

Every failure names the values that differed. An assertion returns the input it was given where that is useful, so one can narrow or validate a value in place rather than spelling it twice:

const name = test.assert(lookup(id), "no row for " .. id)

Module contents

Functions

FunctionKindDescription
assertfunctionRequires a truthy value and returns its complete input pack.
casesfunctionBuilds ordinary named cases from a sequence of input rows.
deepEqualfunctionRequires two values to be structurally equal and returns the actual value.
equalfunctionRequires two values to be equal and returns the actual value.
factfunctionAttaches a structured fact to the current case result.
fixturefunctionResolves a content-addressed immutable fixture for the current case.
isNotExecutedfunctionReturns whether a caught error marks a case as not executed.
isSkipfunctionReturns whether a caught error is a skip raised by this module.
matchesfunctionRequires a string value containing a Lua pattern and returns the string.
metricfunctionAdds a named measurement to the current case result.
notEqualfunctionRequires two values to differ and returns the actual value.
notExecutedfunctionMarks the current case as unavailable on this runtime.
notExecutedReasonfunctionReturns the reason carried by a not-executed error.
raisesfunctionRequires a callback to raise and returns its error value.
requireCapabilityfunctionRequires a capability and records the evidence used to decide it.
skipfunctionSkips the current test case with a reason.
skipReasonfunctionReturns the reason carried by a skip error.
workfunctionAdds a deterministic integer work count to the current case result.

Functions#

assertfunction#

function assert(value: any, ...: any): any

Requires a truthy value and returns its complete input pack.

Arguments

NameTypeDescription
valueany
...any

Returns

TypeDescription
any

Raises

TypeCondition
string

when value is false or nil

casesfunction#

function cases(rows: {any}, name: function(any, integer): string, run: function(any, integer): any): any

Builds ordinary named cases from a sequence of input rows.

The returned table can be returned directly from a test suite or merged into a suite containing handwritten cases. Names must be stable and unique.

Arguments

NameTypeDescription
rows{any}

the rows to expand

namefunction(any, integer): string

returns the case name for a row

runfunction(any, integer): any

runs a row

Returns

TypeDescription
any

the named test cases

deepEqualfunction#

function deepEqual(actual: any, expected: any, message: string?): any

Requires two values to be structurally equal and returns the actual value.

Tables compare their own fields recursively. Keys must be booleans, numbers, or strings. Shared references do not have to use the same aliasing, and cycles compare by bisimulation. A shared __eq metamethod decides equality for its instances. Other leaves use native equality, so NaN differs from NaN and reference cdata compares by identity.

Arguments

NameTypeDescription
actualany
expectedany
messagestring?

Returns

TypeDescription
any

Raises

TypeCondition
string

at the first differing path

equalfunction#

function equal(actual: any, expected: any, message: string?): any

Requires two values to be equal and returns the actual value.

Arguments

NameTypeDescription
actualany
expectedany
messagestring?

Returns

TypeDescription
any

Raises

TypeCondition
string

when actual and expected differ

factfunction#

function fact(name: string, value: any): nil

Attaches a structured fact to the current case result.

Arguments

NameTypeDescription
namestring

the fact kind

valueany

the JSON-compatible fact value

Returns

TypeDescription
nil

fixturefunction#

function fixture(key: string, produce: function(string): any): string, any, boolean

Resolves a content-addressed immutable fixture for the current case.

The producer receives a private directory. Its JSON-compatible return value is published with that directory only when production succeeds.

Arguments

NameTypeDescription
keystring

the complete content key

producefunction(string): any

creates the fixture in a private directory

Returns

TypeDescription
string

the published directory

any

the producer's persisted value

boolean

whether this call reused a previously published fixture

isNotExecutedfunction#

function isNotExecuted(value: any): boolean

Returns whether a caught error marks a case as not executed.

Arguments

NameTypeDescription
valueany

Returns

TypeDescription
boolean

isSkipfunction#

function isSkip(value: any): boolean

Returns whether a caught error is a skip raised by this module.

Arguments

NameTypeDescription
valueany

Returns

TypeDescription
boolean

matchesfunction#

function matches(value: any, pattern: string, message: string?): string

Requires a string value containing a Lua pattern and returns the string.

Arguments

NameTypeDescription
valueany
patternstring
messagestring?

Returns

TypeDescription
string

Raises

TypeCondition
string

when value is not a string or does not contain pattern

metricfunction#

function metric(name: string, value: number, unit: string?): nil

Adds a named measurement to the current case result.

Arguments

NameTypeDescription
namestring

the metric name

valuenumber

the additive measurement

unitstring?

the measurement unit

Returns

TypeDescription
nil

notEqualfunction#

function notEqual(actual: any, unwanted: any, message: string?): any

Requires two values to differ and returns the actual value.

Arguments

NameTypeDescription
actualany
unwantedany
messagestring?

Returns

TypeDescription
any

Raises

TypeCondition
string

when actual and unwanted are equal

notExecutedfunction#

function notExecuted(reason: string): never

Marks the current case as unavailable on this runtime.

Arguments

NameTypeDescription
reasonstring

why the case could not execute

Returns

TypeDescription
never

notExecutedReasonfunction#

function notExecutedReason(value: any): string?

Returns the reason carried by a not-executed error.

Arguments

NameTypeDescription
valueany

Returns

TypeDescription
string?

raisesfunction#

function raises(fn: function(...: any): any, pattern: string?): any

Requires a callback to raise and returns its error value.

Arguments

NameTypeDescription
fnfunction(...: any): any
patternstring?

Returns

TypeDescription
any

Raises

TypeCondition
string

when the callback returns, or its error does not match pattern

requireCapabilityfunction#

function requireCapability(name: string, available: boolean, evidence: any?): nil

Requires a capability and records the evidence used to decide it.

A false requirement reports the case as not executed rather than passed, skipped, or failed.

Arguments

NameTypeDescription
namestring

the stable capability name

availableboolean

whether the runtime proved the capability

evidenceany?

structured evidence supporting the decision

Returns

TypeDescription
nil

skipfunction#

function skip(reason: string): never

Skips the current test case with a reason.

Arguments

NameTypeDescription
reasonstring

Returns

TypeDescription
never

skipReasonfunction#

function skipReason(value: any): string?

Returns the reason carried by a skip error.

Arguments

NameTypeDescription
valueany

Returns

TypeDescription
string?

workfunction#

function work(name: string, count: integer): nil

Adds a deterministic integer work count to the current case result.

Arguments

NameTypeDescription
namestring

the counter name

countinteger

the additive count

Returns

TypeDescription
nil