.\" Made by website/plain/build.mjs from website/words/test.md; change that file. .TH SG-TEST 7 2026-11-28 "SG" "SG reference" .SH NAME test \- a block of checks written next to the code, which only sg test compiles and runs .SH SYNOPSIS .TP \fBtest "name" { ... }\fR a test, written at the top level of a file; the name is any text on one line .TP \fBsg test file.sg\fR runs the tests of the file and of the modules it uses .TP \fBsg test folder\fR runs the tests of the folder and of every module under it .TP \fBsg test file.sg \-\-compiled\fR the same tests, built as a debug build and run .TP \fBtry in a test\fR an error of any type ends the test, which fails and writes the error .SH DESCRIPTION A test is a block with a name, written at the top level of a file, beside the functions it checks. Only \fBsg test\fR compiles and runs it: \fBsg file.sg\fR and \fBsg build\fR leave the tests out, so they cost the program nothing. A file that holds only tests needs no \fBmain\fR\&. \fBtest\fR is a keyword only at the top level and in front of a text, so it can still be a name elsewhere. .PP Inside the block, \fBassert\fR checks what should hold. A test fails when an \fBassert\fR is false, when it panics, when a \fBtry\fR meets an error, or when it ends with \fBshared\fR values still alive. The other tests still run, each with variables of its own, and \fBsg test\fR ends with exit code 1 if any failed. Integer overflow is checked in tests, as in a debug build. .PP In a function, \fBtry\fR hands an error to the caller. A test has no caller, so \fBtry\fR takes an error of any type, writes it and fails the test; the error type must be \fBFormattable\fR\&. \fBtry\fR on a \fBT?\fR is refused there: there is no caller to give \fBnull\fR to, so \fB?? panic("...")\fR says what was expected. There is no test that expects a panic; a failure that is to be tested is returned as a \fBResult\fR, and the test looks at the error. .PP A test belongs to the module of its folder. It sees every name of that module, \fBpublic\fR or not, and only the \fBpublic\fR names of other modules, as a real user would. \fBsg test\fR writes one line for each test, \fBok\fR or \fBFAIL\fR and the name, with the name of the module in front for a test of a module below. What a test prints comes before its line. Two tests of one module cannot have the same name, and a name has no control character (\fB\en\fR, \fB\et\fR, \fB\e0\fR), since it is one line of the report. Compiled with \fB\-\-compiled\fR, each test runs in a process of its own, and the report is the same, letter for letter. .SH EXAMPLES .SS "Tests next to the code" .RS 4 .nf int shippingCents(int grams) { if grams <= 500 { return 450 } return 450 + (grams - 500) / 100 * 50 } test "a small parcel costs the base price" { assert(shippingCents(120) == 450) assert(shippingCents(500) == 450) } test "each full 100 grams over 500 adds 50 cents" { assert(shippingCents(600) == 500) assert(shippingCents(1250) == 800) } void main() { print(shippingCents(2000)) } .fi .RE .PP .RS 4 .nf ok a small parcel costs the base price ok each full 100 grams over 500 adds 50 cents 2 test(s) passed .fi .RE .PP This is what \fBsg test example.sg\fR prints, and \fBsg test example.sg \-\-compiled\fR prints the same. \fBsg example.sg\fR leaves the tests out and prints \fB1200\fR\&. .SS "Setup is a function" .RS 4 .nf struct Cart { List items long totalCents Cart create() { return Cart { items = List(), totalCents = 0 } } void add(ref this, string item, long cents) { this.items.add(item) this.totalCents += cents } void clear(ref this) { this.items.clear() this.totalCents = 0 } } Cart cartWithTea() { var cart = Cart() cart.add("tea", 450) cart.add("cups", 1200) return cart } test "adding counts the price" { var cart = cartWithTea() assert(cart.items.length() == 2) assert(cart.totalCents == 1650) } test "clearing empties the cart" { var cart = cartWithTea() cart.clear() assert(cart.items.length() == 0) assert(cart.totalCents == 0) } test "each test starts from its own cart" { var cart = cartWithTea() cart.add("pot", 2500) print($"{cart.items.length()} items") assert(cart.totalCents == 4150) } .fi .RE .PP .RS 4 .nf ok adding counts the price ok clearing empties the cart 3 items ok each test starts from its own cart 3 test(s) passed .fi .RE .PP There is no setup block: \fBcartWithTea\fR is an ordinary function, and each test calls it for a cart of its own. The file has no \fBmain\fR, since it holds only tests. The line the third test prints comes before its \fBok\fR\&. .SS "Testing an error" .RS 4 .nf enum PortError : Formattable, Equatable { Empty NotANumber(string text) OutOfRange(int value) } Result parsePort(read string text) { if text.length() == 0 { return Error(PortError.Empty) } var value = int.parse(text) ?? return Error(PortError.NotANumber(text.clone())) if value < 1 || value > 65535 { return Error(PortError.OutOfRange(value)) } return Ok(value) } test "a port is read" { var port = try parsePort("8080") assert(port == 8080) } test "each wrong text gives its own error" { assert(parsePort("") is Error(Empty)) assert(parsePort("http") is Error(NotANumber(_))) match parsePort("70000") { Error(e) => assert(e == PortError.OutOfRange(70000)) Ok(port) => panic($"read {port} from 70000") } } void main() { print(parsePort("443") ?? 80) } .fi .RE .PP .RS 4 .nf ok a port is read ok each wrong text gives its own error 2 test(s) passed .fi .RE .PP \fBtry\fR keeps the path that works short: an error there would fail the test. Where the test is about the error, \fBis\fR checks its variant and \fBmatch\fR takes it out to compare it whole. .SS "What a failing test prints" The file ports.sg: .PP .RS 4 .nf enum PortError : Formattable { Empty NotANumber(string text) } Result parsePort(read string text) { if text.length() == 0 { return Error(PortError.Empty) } var value = int.parse(text) ?? return Error(PortError.NotANumber(text.clone())) return Ok(value) } test "a port is read" { var port = try parsePort("8080") assert(port == 8080) } test "the usual ports" { var http = try parsePort("80") var https = try parsePort("443") var ports = List { http, https } assert(ports == List { 80, 8443 }) } test "a port with a scheme" { var port = try parsePort("http:80") print(port) } test "the first of no ports" { var ports = List() print("looking") print(ports[0]) } test "the tests after a failure still run" { var port = try parsePort("22") assert(port == 22) } .fi .RE .PP .RS 4 .nf $ sg test ports.sg ok a port is read FAIL the usual ports panic: assert failed: ports == List { 80, 8443 } left: [80, 443] right: [80, 8443] --> ports.sg:23:5 23 | assert(ports == List { 80, 8443 }) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FAIL a port with a scheme error: NotANumber("http:80") looking FAIL the first of no ports panic: index 0 is out of range: the length is 0 --> ports.sg:34:11 34 | print(ports[0]) | ^^^^^^^^ hint: `.get(i)` reads with `null` instead; before a write, compare the index with `.length()` ok the tests after a failure still run 3 of 5 test(s) failed .fi .RE .PP A failed \fBassert\fR writes its condition and both sides; a \fBtry\fR that meets an error writes the error; a panic writes its message and where it happened. The command ends with exit code 1, so a script or a CI job sees it. .SS "Tests in a folder of modules" The file library/loans/fines.sg: .PP .RS 4 .nf int daysLate(int dueDay, int returnedDay) { if returnedDay <= dueDay { return 0 } return returnedDay - dueDay } public int fineCents(int dueDay, int returnedDay) { return daysLate(dueDay, returnedDay) * 25 } .fi .RE .PP The file library/loans/tests.sg: .PP .RS 4 .nf test "a book back early is not late" { assert(daysLate(10, 8) == 0) } test "each late day costs 25 cents" { assert(daysLate(10, 14) == 4) assert(fineCents(10, 14) == 100) } .fi .RE .PP The file library/main.sg: .PP .RS 4 .nf use loans void main() { print(fineCents(3, 10)) } test "a fine seen from outside the module" { assert(fineCents(1, 2) == 25) } .fi .RE .PP .RS 4 .nf $ sg test library ok a fine seen from outside the module ok loans: a book back early is not late ok loans: each late day costs 25 cents 3 test(s) passed .fi .RE .PP The tests in \fBloans\fR reach \fBdaysLate\fR, which is not \fBpublic\fR, since they are in its module. The test in \fBmain.sg\fR sees only \fBfineCents\fR; \fBdaysLate\fR there is an error. \fBsg test library/loans\fR runs the tests of that module alone. .SH "WHAT THE COMPILER REFUSES" .SS "A test inside a function" .RS 4 .nf int shippingCents(int grams) { return grams <= 500 ? 450 : 450 + (grams - 500) / 100 * 50 } void main() { test "a small parcel" { assert(shippingCents(120) == 450) } print(shippingCents(120)) } .fi .RE .PP .RS 4 .nf error: a test is written at the top level of the file, not inside a function --> example.sg:6:5 6 | test "a small parcel" { | ^^^^ .fi .RE .SS "Tests run as a program" .RS 4 .nf int shippingCents(int grams) { return grams <= 500 ? 450 : 450 + (grams - 500) / 100 * 50 } test "a small parcel costs the base price" { assert(shippingCents(120) == 450) } .fi .RE .PP .RS 4 .nf error: there is no `main` function --> example.sg:7:2 7 | } | ^ hint: this file holds tests; they are run by the test command: `sg test ` .fi .RE .PP \fBsg example.sg\fR runs a program, which starts at \fBmain\fR\&. \fBsg test example.sg\fR runs this file as it is. .SS "A name that is not a text" .RS 4 .nf int shippingCents(int grams) { return grams <= 500 ? 450 : 450 + (grams - 500) / 100 * 50 } test smallParcel { assert(shippingCents(120) == 450) } void main() { print(shippingCents(120)) } .fi .RE .PP .RS 4 .nf error: expected `(`, found `{` --> example.sg:5:18 5 | test smallParcel { | ^ .fi .RE .PP Without a text after it, \fBtest\fR is not a keyword: the line reads as a function named \fBsmallParcel\fR that returns a type \fBtest\fR, and a function needs its parameters. The name of a test is written in quotes: \fBtest "a small parcel" { }\fR\&. .PP \fBsg test\fR refuses three more things, which \fBsg example.sg\fR does not see, since it leaves the tests out: two tests of one module with the same name (\fBerror: there are two tests named "..."\fR), a name with a control character (\fBerror: the name of a test is one line of the report, with no control character; this one has `\en`\fR, with the hint of the name with a space in its place), and \fBtry\fR on a \fBT?\fR in a test (\fBerror: `try` on `int?` returns `null` to the caller, and a test has no caller\fR). .SH "SEE ALSO" .BR sg-assert (7), .BR sg-test (1), .BR sg-try (7), .BR sg-public (7), .BR sg-use (7) .PP https://sg\-lang.org/reference/test.html