Tutorial

Testing in する

する has no separate test framework. You write tests in the language itself: short examples right under a definition, test blocks in the same file, and larger tests in a 試験/ (“test”) folder. One command runs them all. This page needs する 0.10.0 or later; read the tutorial first if the grammar is new to you.

例えば … は … です以下は「…」の試験です…試験するsuru --試験suruc --試験kumu 試験GitHub Actions

01Tests live with the code

Here is a small file, 計算.suru (“calculation”), with one public function, two examples and one test block. Nothing in it is a comment with special meaning: the examples and the test are ordinary sentences, so the interpreter, the compiler and the editor type-check them with the rest of the file.

計算.suru
# Returns x times two.
数 を 二倍する は 数 を 返す
x を 二倍する とは x * 2 です        # 二倍する = double
例えば (2 を 二倍する) は 4 です   # 例えば = for example
例えば (-3 を 二倍する) は -6 です

数 を 加一する は 数 を 返す
x を 加一する とは x + 1 です        # 加一する = add one

以下は「二倍」の試験です             # 試験 = test
  (0 を 加一して 二倍する) を 2 に 一致確認する  # 一致確認する = check that it equals
  1 < 2 を 確認する                # 確認する = check that it is true
  以上です

加一する は 非公開とする             # 非公開 = private

When you run the file as a program (suru 計算.suru), or another file reads it with 「計算」 を 読み込む, the examples and the test block are skipped, and a build leaves them out of the binary. They run only when you ask for the tests.

02Examples: 例えば

例えば means “for example.” A top-level line 例えば expression は expected です (“for example, expression is expected”) is an example of the definition just above it. It passes when the two sides are equal (=). The です at the end may be left out.

挨拶.suru
文字列 を 挨拶にする は 文字列 を 返す
名前 を 挨拶にする とは 「こんにちは、」 + 名前 + 「さん」 です  # 挨拶にする = make a greeting
例えば (「太郎」 を 挨拶にする) は 「こんにちは、太郎さん」 です

Each example is reported as “nameの例(line行)”, the example of that definition on that line. Because an example is a sentence, not a comment, a typo in it is an error before anything runs, and it can never drift silently out of date. In VS Code, hovering over the function’s name shows its examples next to its type.

03Test blocks

For checks that take more than one line, write a test block at the top level of the file: 以下は「二倍」の試験です (“the following is the test 「二倍」”), a procedure, then 以上です (“that is all”). The name in 「」 is the name of the test. A file may hold several blocks, each with its own name.

挨拶.suru
文字列 を 挨拶にする は 文字列 を 返す
名前 を 挨拶にする とは 「こんにちは、」 + 名前 + 「さん」 です

以下は「挨拶」の試験です
  文 とは 「花子」 を 挨拶にする  # 文 = sentence
  文 を 「こんにちは、花子さん」 に 一致確認する
  (文 の 長さ) > 0 を 確認する
  以上です

Inside a test block you can use the file’s private names (such as 加一する above, which is marked 非公開とする, “make private”) and the check actions of the 「試験」 library without reading it in:

CheckMeaningWhen it fails
actual を expected に 一致確認するcheck that the two are equal等しくありません: 期待は 4 / 実際は 5 (“not equal: expected 4 / actual 5”)
condition を 確認するcheck that the condition is 真 (true)真ではありません (“not true”)
reason を 不合格にするfail now, with your own reasonthe reason you gave

A failing check writes its line and its reason to standard error and ends that test with exit code 1. The other tests still run.

04Running the tests

suru --試験 takes files or folders (a folder means every .suru file in it) and runs their examples, test blocks and …試験する procedures with the interpreter. Each test runs in its own process, so a crash stops only that test.

terminal
$ suru --試験 計算.suru
✓ 計算.suru 二倍するの例(4行)
✓ 計算.suru 二倍するの例(5行)
✓ 計算.suru 二倍

合格 3・不合格 0

合格 is “passed” and 不合格 is “failed.” When a test fails, its reason and anything it printed appear indented under it, and the exit code is 1. Suppose the second example said は 6 です by mistake:

terminal
$ suru --試験 計算.suru
✓ 計算.suru 二倍するの例(4行)
✗ 計算.suru 二倍するの例(5行)
    計算.suru:5: 等しくありません: 期待は 6 / 実際は -6
✓ 計算.suru 二倍

合格 2・不合格 1

Any argument that is not a path picks tests by part of their name or path: suru --試験 計算.suru 二倍する runs only the examples of 二倍する. suruc --試験 does the same with the Go compiler: it builds one test binary per file (at -O0 unless you give another -O) and runs each test as its own process. The report looks the same.

terminal
$ suruc --試験 計算.suru 二倍

05The 試験/ folder

Tests that use a whole project, as its users would, go in files under 試験/. There, every top-level definition with no arguments whose name ends in 試験する (“to test”) is one test. (Names may not be all hiragana, so it is 二倍試験する, not “二倍を試験する”.) Read in the checks with 「試験」 を 読み込んで 使う (“read in and use 「試験」”), and read the project by the name in its 組.suru.

試験/二倍.suru
「試験」 を 読み込んで 使う
「計算」 を 読み込む                 # the project, by the name in 組.suru

二倍試験する は 真偽 を 返す 入出力動作  # 二倍試験する = test 二倍する
二倍試験する とは 以下の手順です
  (5 を 計算内の二倍する) を 10 に 一致確認する
  (0 を 計算内の二倍する) = 0 を 確認する
  以上です

Other definitions in the file, such as helper functions, are not tests. A file in 試験/ may have the same name as the project file it tests: 「計算」 を 読み込む in 試験/計算.suru skips the file itself and finds the project’s 計算.suru. kumu 新規 (“new”) creates 試験/例.suru with one test, so a new project has a test to run from the start.

06kumu 試験

In a kumu project, kumu 試験 (kumu test) runs everything: first the examples and test blocks in the project’s source files, then the …試験する procedures in 試験/. Each test file becomes one binary under 出力/試験/, linked with the project’s dependencies and C sources, and each test runs as its own process.

terminal
$ kumu 新規 計算        # new project; put 計算.suru above next to 本体.suru
$ cd 計算
$ kumu 試験
✓ 計算.suru 二倍するの例(4行)
✓ 計算.suru 二倍するの例(5行)
✓ 計算.suru 二倍
✓ 試験/二倍.suru 二倍試験する

合格 4・不合格 0

A failure points at the line of the check in the test file, even when the code under test is in a library:

terminal
$ kumu 試験 二倍試験
✗ 試験/二倍.suru 二倍試験する
    「二倍.suru」の6行目 25文字目: 等しくありません: 期待は 11 / 実際は 10

合格 0・不合格 1

As with suru --試験, words after kumu 試験 pick tests by part of their name or path. A file that cannot be read or does not type-check fails as a whole, with the error under it. Subfolders that have their own 組.suru (separate projects, such as 例/) are skipped. kumu 公開 (“publish”) runs all the tests and refuses to tag a release if any fail.

07In the editor

The VS Code extension checks examples and test blocks as you type, like the rest of the file. In files under 試験/, a “▷ 試験する” link appears above each …試験する とは line and runs just that test. The command palette has “プロジェクトを試験する(kumu 試験)” (“test the project”) for the whole project.

08On every pull request

Because kumu 試験 exits with 1 when a test fails, it is all a CI job needs. This GitHub Actions workflow installs kumu and clang and runs the tests on every push and pull request. The official libraries use the same steps, plus the C libraries they wrap.

.github/workflows/試験.yml
name: 試験
on:
  push:
    branches: [main]
  pull_request:
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: stable
      - run: sudo apt-get update && sudo apt-get install -y clang
      - run: go install github.com/suru-lang/suru/cmd/kumu@latest
      - run: ~/go/bin/kumu 試験

Without kumu, run pip install suru-lang and suru --試験 . instead.

09Going further

The full rules are in the language README: “Tests in the same file and examples” for 例えば and test blocks, and “Project management (kumu)” for 試験/ and kumu 試験. The standard library tests itself this way too: lib/算術.suru and lib/乱数.suru carry their own examples and test blocks.