Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: Unit Testing Guide
---

import { FileTree, Steps, Tabs, TabItem } from "@astrojs/starlight/components";

Unit tests are written in C++ with the aid of macros from
[Catch2 2.x](https://github.com/catchorg/Catch2/tree/v2.x). These tests are suited for larger scale
testing than `sktest`, as a large number of tests can be quickly run and the output easily checked.
This makes them suited for automation, because any failing test will be flagged so that any issues
can be resolved before merging the changes into the main repository of SplashKit.

When writing unit tests, you should aim to test the expected behaviour of a specific function or
procedure. In addition to positive test cases (where the function or procedure takes valid input and
should produce the expected output), consider negative cases (cases where the function or procedure
has invalid input), as well as edge cases.

## File structure

Test files are located at:

<FileTree>

- coresdk
- src
- test
- unit_test_main.cpp
- `unit_test_<name>.cpp`

</FileTree>

`unit_test_main.cpp` is the entry point for all unit tests. You do not need to modify this to write
your own tests or update existing ones.

The `unit_test_<name>.cpp` files contain tests for related parts of SplashKit. For example,
`unit_test_utilities.cpp` has tests for SplashKit's utility functions. A test file must include the
Catch2 header file along with any other includes required:

```cpp
#include "catch.hpp"
```

## Writing a Unit Test

At a minimum, a unit test consists of a `TEST_CASE` and an assertion (usually `REQUIRE`):

```cpp
TEST_CASE("gets the number of milliseconds that have passed since the program was started", "[current_ticks]")
{
unsigned int result = current_ticks();
REQUIRE(result >= 0);
}
```

`TEST_CASE(name, [,tags])` defines a test case with the given name and, optionally, one or more
tags.

`REQUIRE` evaluates an expression and aborts the test as a failure if the result is false.
`REQUIRE_FALSE` is similar but fails if the expression evaluates true. There are
[other assertion macros](https://github.com/catchorg/Catch2/blob/v2.x/docs/assertions.md#top) but
these are the most common.

A test may contain multiple assertions:

```cpp
TEST_CASE("random number float between 0 and 1 is generated", "[rnd]")
{
float result = rnd();
REQUIRE(result >= 0);
REQUIRE(result <= 1);
}
```

You may write tests that have some common steps, such as defining a variable. You can define one or
more `SECTION(name)` inside a `TEST_CASE`. The `TEST_CASE` is run from the start for each `SECTION`.

```cpp
TEST_CASE("return a SplashKit resource of resource_kind with name filename as a string", "[file_as_string]")
{
const resource_kind RESOURCE = resource_kind::BUNDLE_RESOURCE;
const string RESOURCE_PATH = "blah.txt";

SECTION("filename is a valid file")
{
string result = file_as_string(RESOURCE_PATH, RESOURCE);
string expected = "BITMAP,ufo,ufo.png\n";
REQUIRE(result == expected);
}
SECTION("filename is an empty string")
{
string result = file_as_string("", RESOURCE);
string expected = "";
REQUIRE(result == expected);
}
SECTION("filename is an invalid file")
{
string result = file_as_string("invalid.txt", RESOURCE);
string expected = "";
REQUIRE(result == expected);
}
}
```

This test has three `SECTION`s, so the `TEST_CASE` will run three times. Each time, the `RESOURCE`
and `RESOURCE_PATH` variables will be defined.

## See Also

- [Catch2 tutorial](https://github.com/catchorg/Catch2/blob/v2.x/docs/tutorial.md)
- [Catch2 reference](https://github.com/catchorg/Catch2/blob/v2.x/docs/Readme.md)
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -44,74 +44,13 @@ git checkout {new branch name}

Now that a new branch is created and active, development can begin.

### Building the Test Programs

You cannot create new programs with splashkit-core as you do when using the traditional SplashKit
library. Instead, two programs are generated which can be configured to test its functionality:
`sktest` and `skunit_tests`. They are built with CMake using a preconfigured `CMakeLists.txt` file.
Open a WSL terminal and enter:

```shell
cd
cd splashkit-core/projects/cmake
cmake -G "Unix Makefiles" .
make
```

The associated macOS and Linux commands can be found here:
[CONTRIBUTING](https://github.com/thoth-tech/splashkit-core/blob/develop/CONTRIBUTING.md)

### Running the Test Programs

To run the test programs, open a WSL terminal and enter:

```shell
cd
cd splashkit-core/bin
```

Then for sktest:

```shell
./sktest
```

Or for skunit_tests:

```shell
./skunit_tests
```

### Making Changes

`sktest` is built with the .cpp files from `~/splashkit-core/coreskd/src/test/`. To add your own
tests, modify one or more of the files such as `test_animation.cpp`.

`skunit_tests` is built with the .cpp files from `~/splashkit-core/coreskd/src/test/unit_tests/`.
When it runs, all unit tests from all files in this folder are executed. Additional files can be
added to this folder if necessary. If adding a new file, copy the structure from one of the existing
unit test files. Critically, `#include "catch.hpp"` must be present in the file for it to be
compiled into `skunit_tests`. Beyond that, the hierarchy of, `TEST_CASE > SECTION > ASSERTION`
should be followed to improve readability and tracing of errors.

### Testing Changes

If a change is made to the code, the test programs need to be rebuilt. In a WSL terminal enter:

```shell
cd
cd splashkit-core/projects/cmake
make
```

If any files were created or deleted, the CMake files need to be regenerated. In that case use:

```shell
cd
cd splashkit-core/projects/cmake
cmake -G "Unix Makefiles" .
make
```
It is critical that changes made to splashkit-core are thoroughly tested, so that issues can be
found and resolved early. When making changes to code, whether adding new functions or altering
existing functions, be sure to run the existing tests and think about adding new ones to test your
changes. See [the testing guide](/products/splashkit/documentation/splashkit-expansion/01-testing-guide/) to get
started with testing.

### Documenting Changes

Expand Down Expand Up @@ -155,3 +94,7 @@ following guide details the procedure and etiquette which is expected while usin
Solutions for common issues can be found below. Be sure to also check the following page for help
troubleshooting:
[Guide to resolving Common Issues](/products/splashkit/03-github-guide/#troubleshooting)

### See Also

[splashkit-core CONTRIBUTING.md](https://github.com/thoth-tech/splashkit-core/blob/develop/CONTRIBUTING.md)
Loading