#<after_each>after_each
Holds statements.Holds statements in { }. Scripts only.
<after_each>
<set name="cart" value="[]" />
</after_each>after_each {
$cart = []
}Tests run by kscript test: <test>, <describe>, <expect>, <expect_error>, <capture> and @assert().
Import it at the top of a script:
<import package="KScript.Test" />import KScript.TestKScript.Test runs with kscript test, so its examples can't run inside the docs. Copy one into a .ks.xml file and run it with kscript.
<after_each>after_eachHolds statements.Holds statements in { }. Scripts only.
<after_each>
<set name="cart" value="[]" />
</after_each>after_each {
$cart = []
}<before_each>before_eachHolds statements.Holds statements in { }. Scripts only.
<let name="cart" value="[]" />
<before_each>
<set name="cart" value="[]" />
</before_each>$cart = []
before_each {
$cart = []
}<capture>captureinput gives it lines to read.Holds statements.Holds statements in { }. Scripts only.
| AttributeOption | Value | Description |
|---|---|---|
into required | a new variable's name | The variable that receives the output (line endings are \n, without the last one). |
input | expression | What <input> and other prompts read: text, or a list of lines.What input and other prompts read: text, or a list of lines. |
<capture into="out">
<call fn="greet" args="'Joan'" />
</capture>
<expect value="$out" equals="'Hello, Joan!'" />capture(into: out) {
@greet('Joan')
}
expect($out, equals: 'Hello, Joan!')<describe>describeHolds statements.Holds statements in { }. Scripts only.
| AttributeOption | Value | Description |
|---|---|---|
name required | a string with { } | What the group is about. |
skip | a string with { } | Skip it: skip="true", or a reason such as skip="needs a network". |
only | true or false | true runs only the tests marked only in this file, while you work on them. |
<describe name="lists">
<test name="push adds to the end">
<expect value="[1]->push(2)" equals="[1, 2]" />
</test>
</describe>describe(name: 'lists') {
test(name: 'push adds to the end') {
expect([1]->push(2), equals: [1, 2])
}
}<expect>expectSelf-closing.A statement on its own. Scripts only.
| AttributeOption | Value | Description |
|---|---|---|
value required | expression | The value to check. |
equals | expression | It equals this (== for text and numbers, item by item for lists and maps). |
not_equals | expression | It doesn't equal this. |
same | expression | It is strictly equal (===): same type and value. |
contains | expression | A list has this item, text has this text, or a map has this key. |
not_contains | expression | A list doesn't have this item, text doesn't have this text, or a map doesn't have this key. |
matches | expression | Text matches this regular expression, such as '^\d+$'. |
type | 'string'| | Its type is this. |
count | expression | A list, map or text has this many items or characters. |
count_at_most | expression | A list, map or text has at most this many items or characters: expect(@queries(), count_at_most: 4). |
close_to | expression | A number is within within of this, for maths that floats can't hold exactly: expect(0.1 + 0.2, close_to: 0.3). |
within | expression | How far from close_to the number may be (0.000000001 by default): expect($average, close_to: 4.33, within: 0.01). |
has | expression | A map has these keys with these values, and may have others; for a list, every item does: expect($order, has: { status: 'paid' }). Nested maps are checked the same way. |
throws | an error type | Working out the value stops with an error of this type, or one that extends it: throws="NotFound". |
message | a string with { } | What to say when it fails, instead of the usual message. |
<expect value="2 + 3" equals="5" />
<expect value="$names" contains="'Joan'" />
<expect value="$id" matches="'^[a-z0-9-]+$'" />
<expect value="'nope'->convert(from: json)" throws="Json" />expect(2 + 3, equals: 5)
expect($names, contains: 'Joan')
expect($id, matches: '^[a-z0-9-]+$')
expect('nope'->convert(from: json), throws: Json)<expect_error>expect_errortype and contains check which error; as keeps it for more checks.Holds statements.Holds statements in { }. Scripts only.
| AttributeOption | Value | Description |
|---|---|---|
type | an error type | The error type it should be (or a type that extends it), such as Validation, FileNotFound or an <error>.The error type it should be (or a type that extends it), such as Validation, FileNotFound or an error. |
contains | a string with { } | Text the error message should contain. |
as | a new variable's name | A variable for the error (type, message, line, and the fields of <throw>), declared after the element.A variable for the error (type, message, line, and the fields of throw), declared after the element. |
<expect_error type="Validation" contains="positive">
<call fn="set_age" args="-1" />
</expect_error>expect_error(type: Validation, contains: 'positive') {
@set_age(-1)
}<test>testHolds statements.Holds statements in { }. Scripts only.
| AttributeOption | Value | Description |
|---|---|---|
name required | a string with { } | What the test checks, shown in the results. In a <for> loop, { } can use the loop's variables.What the test checks, shown in the results. In a for item in list loop, { } can use the loop's variables. |
skip | a string with { } | Skip it: skip="true", or a reason such as skip="needs a network". |
only | true or false | true runs only the tests marked only in this file, while you work on them. |
timeout | expression | Fail the test if it runs longer than this: 30s, 2m (the default is 10s, or test.timeout in kscript.json). A plain number is seconds. |
<fn name="add" params="a, b" value="$a + $b" />
<test name="adds two numbers">
<expect value="@add(2, 3)" equals="5" />
</test>
<for each="n" in="[1, 2, 3]">
<test name="doubles {$n}">
<expect value="@add($n, $n)" equals="$n * 2" />
</test>
</for>fn add(a, b) => $a + $b
test(name: 'adds two numbers') {
expect(@add(2, 3), equals: 5)
}
for n in [1, 2, 3] {
test(name: "doubles {$n}") {
expect(@add($n, $n), equals: $n * 2)
}
}Commands are called with @, in any expression: @name(arguments).
@assert(condition, message?) → boolArguments can be named: @assert(…, message: …).
<do>@assert(2 + 3 eq 5)</do>
<do>@assert([1, 2]->count() gt 0, 'The cart should not be empty')</do>@assert(2 + 3 == 5)
@assert([1, 2]->count() > 0, 'The cart should not be empty')@assert_response(response, status?: number, headers?: map, redirect?: string, json?, as?: type)| Parameter | Takes |
|---|---|
response required | any value |
status optional | number |
headers optional | map |
redirect optional | string |
json optional | any value |
as optional | type |
Arguments can be named: @assert_response(…, status: …, headers: …).
<test name="the API lists people">
<do>@assert_response(@api('http://localhost:8080/api/people'), status: 200, headers: { 'Content-Type': 'application/json' })</do>
</test>test(name: 'the API lists people') {
@assert_response(@api('http://localhost:8080/api/people'), status: 200, headers: { 'Content-Type': 'application/json' })
}@check_script(path: string) → list| Parameter | Takes |
|---|---|
path required | string |
<let name="problems" value="@check_script('mistakes/flags.kscript')" />
<expect value="$problems[0].message" contains="'needs true or false'" />$problems = @check_script('mistakes/flags.kscript')
expect($problems[0].message, contains: 'needs true or false')@fail(message?)<test name="every price is positive">
<for each="price" in="[3, 5, 8]">
<if $price le 0>
<do>@fail('Found a price of {$price}')</do>
</if>
</for>
</test>test(name: 'every price is positive') {
for price in [3, 5, 8] {
if $price <= 0 {
@fail('Found a price of {$price}')
}
}
}@run_script(path, options?) → mapoutput is what it printed, code its exit code (3 when it stopped with an error) and error the error, or null.Arguments can be named: @run_script(…, options: …).
<let name="result" value="@run_script('../scripts/hello.ks.xml', { args: ['Joan'], input: ['yes'] })" />
<expect value="$result.output" contains="'Hello, Joan'" />
<expect value="$result.code" equals="0" />$result = @run_script('../scripts/hello.ks.xml', { args: ['Joan'], input: ['yes'] })
expect($result.output, contains: 'Hello, Joan')
expect($result.code, equals: 0)@with_clock(time: date, work: fn) → anyArguments can be named: @with_clock(…, work: …).
<test name="a new year starts on 1 January">
<let name="year" value="@with_clock('2027-01-01'->to_date(), fn() => @today().year)" />
<expect value="$year" equals="2027" />
</test>test(name: 'a new year starts on 1 January') {
$year = @with_clock('2027-01-01'->to_date(), fn() => @today().year)
expect($year, equals: 2027)
}@with_environment(values: map, work: fn) → anyArguments can be named: @with_environment(…, work: …).
<test name="reads the region from the environment">
<let name="region" value="@with_environment({ REGION: 'eu-west' }, fn() => @env('REGION'))" />
<expect value="$region" equals="'eu-west'" />
</test>test(name: 'reads the region from the environment') {
$region = @with_environment({ REGION: 'eu-west' }, fn() => @env('REGION'))
expect($region, equals: 'eu-west')
}