DatumHueEnterprise v0.0.24datumhue.com
Contents

12345678Build a ball pit · Step 7 of 8

Test it

You'll learn15 min
  • Write tests with describe, it and expect
  • Run them with pkg test
  • Press buttons from a test
Time
15 min
Builds on
Step 6: Remember things
File
ballpit/tests/pit_test.lua

So far you have tried each change to the pit by hand. In this step you write tests that try it for you: one file checks the pit's logic, and another presses the toolbar's buttons as a person would.

Test the pit

A package's tests live in its tests/ folder, in files whose names end in _test.lua. pkg new wrote one already, tests/01_example_test.lua, which checks that 1 + 1 is 2. A test file loads your modules with require, as init.lua does. init.lua itself does not run, so each test sets up what it checks.

Save this as ballpit/tests/pit_test.lua:

ballpit/tests/pit_test.lualua
local pit = require("pit")

describe("pit", { before_each = pit.clear })

it("counts the balls it adds", function()
    pit.add()
    pit.add()
    expect(pit.count()).to_be(2)
end)

it("is empty once cleared", function()
    pit.add()
    pit.clear()
    expect(pit.count()).to_be(0)
end)

it("tells its listeners each new count", function()
    local counts = {}
    pit.on_change(function(n)
        table.insert(counts, n)
    end)
    pit.add()
    pit.add()
    pit.clear()
    expect(counts).to_equal({ 1, 2, 0 })
end)
  • describe names a group of tests. Each it adds a test to the group: a name, then a function that runs the test.
  • expect takes a value to check. .to_be(other) passes when the two are equal and fails the test when they are not. to_equal compares tables, entry by entry.
  • All the test files run in one app, so require("pit") gives every test the same pit. before_each = pit.clear empties it before each test, so no test sees the balls of the one before.

Run the package's tests with pkg test:

sh
$ datumhue pkg test ballpit

A window opens while the tests run, and closes when they end. Each test prints a line, ok when it passed, and the last line counts them:

text
1..4
# run seed: 1282112497
ok 1 - example > runs
ok 2 - pit > counts the balls it adds
ok 3 - pit > is empty once cleared
ok 4 - pit > tells its listeners each new count
# 4 passed, 0 failed, 0 skipped in 0.1s

A test that fails prints not ok, then the file and line of the check, with the value it expected and the one it got.

Press a button from a test

A test can also use your app as a person does. datumhue.debug.input moves the pointer, clicks and presses keys, and your app hears them as it hears real input. Save this as ballpit/tests/toolbar_test.lua:

ballpit/tests/toolbar_test.lualua
local pit = require("pit")
local toolbar = require("toolbar")

local input = datumhue.debug.input

describe("toolbar", { before_each = pit.clear })

it("adds a ball on a click and on the A key", function()
    local bar = toolbar.new(datumhue.ui.root)
    wait_until(function()
        return bar.add.bounds ~= nil
    end)

    local center = bar.add.bounds.center
    input.cursor(center.x, center.y)
    input.click()
    wait_until(function()
        return pit.count() == 1
    end)

    input.tap("a")
    wait_until(function()
        return pit.count() == 2
    end)
    expect(bar.count.text).to_be("Balls: 2")
end)

In this example: datumhue.debug.input datumhue.ui.root datumhue.debug.input.cursor datumhue.debug.input.click datumhue.debug.input.tap

  • The test builds the toolbar on datumhue.ui.root, the whole window.
  • A widget has no place on screen until it is laid out, in the frame after it is made. wait_until waits a frame at a time until its function returns true: here, until the Add ball button has its UiElement.bounds. wait_frames waits a set number of frames, for a test that needs time to pass rather than a condition to hold.
  • debug.input.cursor moves the pointer to the button's center, in the units bounds uses, and debug.input.click clicks there. debug.input.tap presses a key and lets it go.
  • Your app hears the input in the next frame, as it hears real input, so the test waits for each new ball before it checks.

Run datumhue pkg test ballpit again. The last line now counts 5 passed, 0 failed.

Test a package shows how to run only some of the tests, measure how much of your code they cover and keep a report. Package testing lists every check expect offers.

Try this

  • Make a test fail on purpose: expect 3 balls where there are 2, and read what the run prints.
  • Test the Clear button: at the end of the test, click it, then check the pit's count and the counter's text.
  • In a second test in toolbar_test.lua, add three balls, then build a toolbar. Test that its counter starts at Balls: 3.

Recap

  • Tests live in tests/, in files that end in _test.lua, and datumhue pkg test runs them.
  • describe names a group, it adds a test, and expect checks a value.
  • Tests load your modules with require; init.lua does not run.
  • datumhue.debug.input clicks and presses keys from a test. Wait a frame before you check what it did.

New in this step

datumhue.com

(c) 2026 Shuusei AB. Quote excerpts of this manual, with attribution to DatumHue, in your own documentation, tutorials, course materials, reviews, and other writing or video. Republishing it in whole or in substantial part needs written permission: support@datumhue.com.

Victor Mono is licensed under the SIL Open Font License 1.1
Copyright (c) 2024, Rune Bjørnerås (https://github.com/rubjo)

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

Try

    ↑↓ selectEnter openTab filter

    Settings

    Theme
    Text
    Print

    Keyboard shortcuts

    / or Ctrl KSearch
    ↑ ↓ EnterPick a result
    TabNext result filter
    j kNext or previous entry
    [ ]Previous or next page
    yCopy a link to the current entry
    ?These shortcuts
    EscClose

    On this page