TAP [1] output producing test runner for Kotlin mostly ported from @substack's tape module for Node.js [2]. This package is installable with NPM [3], IMS [4], clib [5], GHI [6], and of course, from [source][#install-from-source]
The datkt.tape
package an be installed with various package managers.
$ npm install @datkt/tape
Note: This will install tape into node_modules/@datkt/tape
$ clib install datkt/tape
Note: This will install tape into deps/tape
$ ghi install datkt/tape
$ git clone git@github.com:datkt/tape.git
$ cd tape
$ make build # or make klib
$ make install
$ konanc -r node_modules/@datkt -l tape/tape test.kt -o test
$ konanc deps/@datkt/tape/*.kt test.kt -o test
$ konanc -l tape test.kt -o test ## library should be installed in ~/.konan/klib
import datkt.tape.collect
import datkt.tape.test
import lerp.*
fun main(args: Array<String>) {
test("lerp(a, b, c)", fun(t: Test) {
val x = 1.0
val y = 2.0
val z = lerp(x, y, 1.0)
t.plan(1)
t.ok(2.0 == z, "lerp compute fail :shrug:")
t.end()
})
collect() // collects results and prints to stdout
}
The datkt.tape
package exports a public API documented in this section.
Creates and returns a new named scoped test. The test callback will not be invoked if null is given.
import datkt.tape.test
test("function test", fun(t: Test) {
t.plan(1)
t.ok(true, "we're okay")
t.end()
})
Creates and returns a new skipped scoped test. The test callback will not be invoked if null is given.
import datkt.tape.skip
skip("skipped test", fun(t: Test) {
t.end(Error("This should never run"))
})
Closes results container and writes results to underlying write stream.
import datkt.tape.collect
val results = datkt.tape.collect()
The Test
class represents a named test that is invoked in a
function callback. When a test is running, it will call various function hooks
and write TAP formatted output to a stream.
t = Test("thing", false, fun(t: Test) {
t.end
})
t.run()
Add a callback that will be invoked before the test is ran.
t.onBeforeRun({
// do something before the tests is ran
})
Add a callback that will be invoked after the test is ran.
t.onAferRun({
// do something after the tests is ran
})
Add a callback that will be invoked when there
is a test result. It could be a String
or AssertionResult
.
t.onResult(fun(_, result: Any?) {
if (result is String) {
// do something with result string
} else if (result is AssertionResult) {
// do something with assertion result
}
})
Add a callback that will be invoked when a plan has been set.
t.onPlan(fun(_, plan: Int?) {
if (null != plan) {
// do something with plan
}
})
Add a callback that will be invoked when the test has ended.
t.onEnd({
// do something when test ends
})
Runs the test runner invoking the runner callback given to the constructor if the test is not skipped.
t.run() // will call test callback, if test is not skipped
Ensure a planned assertion count for a test. Will throw Error
if
count
is 0
.
t.plan(4)
Emit a comment
t.comment("@TODO(jwerle): Fix this")
Ends the test runner with an optional error that will generate an error assertion.
t.end()
or with an Error
t.end(Error("oops"))
Asserts that input is truthy based on some optional assertion options.
t.assert(true)
t.assert(1234)
t.assert("okay")
t.assert(::println)
t.assert({ 1 + 1 })
Asserts that input is "ok" based on some optional assertion options.
t.ok(null == err)
t.ok(thing is Thing)
t.ok(string.length)
Asserts that input is "not ok" based on some optional assertion options.
t.notOk(err)
t.notOk(thing is That)
t.notOk(array.count)
Asserts that an error is falsy. If an error is given the message is used in the assertion.
t.error(Error("oops")) // failing assertion
t.error(null) // passing
Creates a failing assertion with an optional message.
t.fail("well, that didn't work")
Creates a passing assertion with an optional message.
t.pass("well done!")
Creates a skipping assertion with an optional message.
t.skip("we'll come back to shit")
Creates an equality assertion for two values with an optional assertion error message.
t.equal("hello", "hello", "hello == hello")
t.equal("good", "food", "good != food")
Creates an assertion that checks for an error to be thrown inside of a given function.
t.throws({ throw Error("oops") }) // passes
t.throws({ throw Exception("yikes") }, Error, "Expection != Error")
The tape
package can be built from source into various targets.
tape.klib
, a Kotlin library that can be linked with konanc
can be
built from source.
$ make klib
which will produce build/lib/tape.klib
. The library can be installed
with klib
by running make install
libtape.a
, a static library that can be linked with konanc
can be
built from source.
$ make static
which will produce build/lib/libtape.a
and C header files in
build/include
. The library can be installed into your system by
running make install
. The path prefix can be set by defining the
PREFIX
environment or make
variable. It defaults to
PREFIX=/usr/local
libtape.so
, a shared library that can be linked with konanc
can be
built from source.
$ make shared
which will produce build/lib/libtape.so
and C header files in
build/include
. The library can be installed into your system by
running make install
. The path prefix can be set by defining the
PREFIX
environment or make
variable. It defaults to
PREFIX=/usr/local
- ✔ assert [pass: 21, fail: 0, duration: 0ms]
- ✔ constants [pass: 13, fail: 0, duration: 0ms]
- ✔ context [pass: 6, fail: 0, duration: 0ms]
- ✔ options [pass: 6, fail: 0, duration: 0ms]
- ✔ result [pass: 5, fail: 0, duration: 0ms]
- ✔ results [pass: 0, fail: 0, duration: 1ms]
- ✔ simple [pass: 0, fail: 0, duration: 0ms]
- ✔ simple [pass: 2, fail: 0, duration: 0ms]
- ✔ stream [pass: 8, fail: 0, duration: 0ms]
- ✔ test [pass: 36, fail: 0, duration: 0ms]
- ✔ truthy [pass: 11, fail: 0, duration: 3ms]
- duration: 4ms
- planned: 108
- assertions: 108
- pass: 108
- fail: 0
- TAP - https://en.wikipedia.org/wiki/Test_Anything_Protocol
- Node.js - https://nodejs.org/en/
- NPM - https://www.npmjs.com/
- IMS - https://github.com/mafintosh/ims
- clib - https://github.com/clibs/clib
- GHI - https://github.com/stephenmathieson/ghi
MIT