.\" Made by website/plain/build.mjs from website/words/sg-test.md; change that file. .TH SG-TEST 1 2026-11-28 "SG" "SG commands" .SH NAME sg test \- runs the tests of a file, or of every module in a folder, in the interpreter or compiled .SH SYNOPSIS .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 path \-\-compiled\fR the same tests, built through C as a debug build and run, each in a process of its own .TP \fBsg test path \-L folder, \-Lfolder\fR looks for the libraries of extern blocks in that folder first; given once for each folder .TP \fBsg test \-\-help\fR prints this page .SH DESCRIPTION \fBsg test\fR reads a program the way \fBsg\fR does, a file or a folder, but with its \fBtest\fR blocks, which \fBsg\fR and \fBsg build\fR leave out. It checks all of it, \fBmain\fR included, and then runs each test, one after the other, each with variables of its own. \fBmain\fR is not run, and a file that holds only tests needs none. With a file, the tests of the modules it uses run too; with a folder, the tests of every module under it, used or not. A file of a module given alone is a program of its own: the other files of its folder are not part of it, so a module's tests are run through its folder. .PP The report goes to the standard output: one line for each test, \fBok\fR or \fBFAIL\fR and the name, then a count. The tests of the root come first, then those of each module, with the name of the module in front (\fBstock: \fR, \fBstock.count: \fR). What a test prints comes before its line, and under a \fBFAIL\fR line comes what stopped it: a false \fBassert\fR with both sides, a panic with its place, the error a \fBtry\fR met, or the report of \fBshared\fR values the test left alive. Raw memory a test keeps is its own business and is not counted. Integer overflow is checked, as in a debug build. A test cannot end the program: \fBexit\fR in a test fails that test. .PP Without \fB\-\-compiled\fR the tests run in the interpreter, which needs no C compiler unless the program calls C. With it, \fBsg test\fR builds the program of tests as a debug build, in a folder of the temporary folder made new for it and removed at the end, with the C compiler \fBCC\fR names (\fBcc\fR when it names none), and runs it. Each test then runs in a process of its own, so a test that crashes cannot take the others with it. The report is the same, except for a stack overflow, which compiled code tells without the call it was making. \fBsg build path \-\-test\fR keeps that program (sg build). .PP \fB\-L folder\fR is for a program whose \fBextern\fR blocks name a library that is not where the C compiler looks; the interpreter and the compiled tests both look there first. It may come before or after the path. .PP The exit code is 0 when every test passed, a program with no tests too (\fB0 test(s) passed\fR); 1 when a test failed, or when the checker refused the program, whose error goes to the standard error; 2 when the command line is not one of the forms, or a path or a folder of \fB\-L\fR is not there. .SH EXAMPLES .SS "The tests of a file" The file prices/prices.sg: .PP .RS 4 .nf long withTax(long cents, int percent) { return cents + cents * percent / 100 } test "no tax leaves the price" { assert(withTax(1000, 0) == 1000) } test "twenty percent" { assert(withTax(1000, 20) == 1200) } void main() { print(withTax(2500, 20)) } .fi .RE .PP .RS 4 .nf $ cd prices $ sg test prices.sg ok no tax leaves the price ok twenty percent 2 test(s) passed $ sg prices.sg 3000 .fi .RE .PP \fBsg test\fR runs the two tests and not \fBmain\fR; \fBsg\fR runs \fBmain\fR and not the tests. .SS "A failing test, and the exit code" The file tax/tax.sg: .PP .RS 4 .nf long withTax(long cents, int percent) { return cents + cents * percent / 100 } test "twenty percent" { assert(withTax(1000, 20) == 1200) } test "a third" { print("checking a third") assert(withTax(999, 33) == 1329) } test "the largest price" { assert(withTax(long.MaxValue, 1) > 0) } .fi .RE .PP .RS 4 .nf $ cd tax $ sg test tax.sg ok twenty percent checking a third FAIL a third panic: assert failed: withTax(999, 33) == 1329 left: 1328 right: 1329 --> tax.sg:11:5 11 | assert(withTax(999, 33) == 1329) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FAIL the largest price panic: overflow: 9223372036854775807 + 92233720368547758 does not fit in long --> tax.sg:2:12 2 | return cents + cents * percent / 100 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ hint: `tryAdd` gives `null` instead, and `wrappingAdd` wraps around 2 of 3 test(s) failed $ sg test tax.sg > report.txt; echo $? 1 $ grep -c FAIL report.txt 2 .fi .RE .PP Integer division rounds down, so a third of 999 adds 329, not 330. The third test overflows, which fails it as a debug build would. The whole report, failures too, is on the standard output, and the exit code 1 is what a script or a CI job looks at. .SS "A folder of modules" The file shop/stock/shelf.sg: .PP .RS 4 .nf public struct Shelf { List counts public Shelf create(int places) { return Shelf { counts = List.repeat(0, places) } } public void put(ref this, int place, int count) { this.counts[place] += count } public int total(read this) { return sum(read this.counts) } } int sum(read List xs) { var total = 0 for x in xs { total += x } return total } .fi .RE .PP The file shop/stock/tests.sg: .PP .RS 4 .nf test "the sum of nothing is 0" { assert(sum(read List()) == 0) } test "a new shelf is empty" { var shelf = Shelf(3) assert(shelf.counts == List { 0, 0, 0 }) } .fi .RE .PP The file shop/main.sg: .PP .RS 4 .nf use stock void main() { var shelf = Shelf(2) shelf.put(0, 5) shelf.put(1, 7) print(shelf.total()) } test "what is put is counted" { var shelf = Shelf(2) shelf.put(1, 4) shelf.put(1, 4) assert(shelf.total() == 8) } .fi .RE .PP .RS 4 .nf $ sg test shop ok what is put is counted ok stock: the sum of nothing is 0 ok stock: a new shelf is empty 3 test(s) passed $ sg test shop/stock ok the sum of nothing is 0 ok a new shelf is empty 2 test(s) passed $ sg test shop/stock/tests.sg error: there is no function named `sum` --> shop/stock/tests.sg:2:12 2 | assert(sum(read List()) == 0) | ^^^ hint: `sum` is in `shop/stock/shelf.sg`; the files of a folder are one program: name the folder, `shop/stock`, in place of the file .fi .RE .PP The tests in \fBstock\fR reach \fBsum\fR and the field \fBcounts\fR, which are not public, since they are in its module; the test in \fBmain.sg\fR sees what is public. \fBsg test shop/main.sg\fR runs the same three tests, the module's because \fBmain.sg\fR uses it. \fBtests.sg\fR given alone is a program of one file, without \fBshelf.sg\fR beside it. .SS "Compiled, and a leak" The file friends/friends.sg: .PP .RS 4 .nf struct Person { string name List friends } shared checked Person person(string name) { return new shared checked Person { name = name, friends = List() } } test "friends one way" { var ada = person("ada".clone()) var grace = person("grace".clone()) ada.friends.add(share grace) assert(ada.friends.length() == 1) } test "friends both ways" { var ada = person("ada".clone()) var grace = person("grace".clone()) ada.friends.add(share grace) grace.friends.add(share ada) assert(grace.friends[0].name == "ada") } test "raw memory is the test's own" { var cells = Memory.allocate(4) unsafe { cells[0] = 7 } assert(unsafe cells[0] == 7) } .fi .RE .PP .RS 4 .nf $ cd friends $ sg test friends.sg --compiled ok friends one way FAIL friends both ways leak: the test ended with 2 `shared` values still alive, which hold each other through `Person.friends` 2 x `Person`, made here: --> friends.sg:7:12 7 | return new shared checked Person { name = name, friends = List() } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ hint: the side that does not own is `weak`: `List` ok raw memory is the test's own 1 of 3 test(s) failed $ sg test friends.sg > interpreted.txt; sg test friends.sg --compiled > compiled.txt; cmp interpreted.txt compiled.txt && echo same same .fi .RE .PP The second test passes its \fBassert\fR and still fails: Ada and Grace own each other, so neither is ever freed. The third keeps four integers of raw memory, which is not counted. The interpreter and the compiled tests write the same report, byte for byte. .SS "A library of C, with -L" The file scale/grams.c: .PP .RS 4 .nf #include int64_t ounces_to_centigrams(int64_t ounces) { return ounces * 2835; } .fi .RE .PP The file scale/main.sg: .PP .RS 4 .nf extern "grams" { long ounces_to_centigrams(long ounces) } long grams(long ounces) { return unsafe ounces_to_centigrams(ounces) / 100 } test "a pound is 453 grams" { assert(grams(16) == 453) } test "no ounces weigh nothing" { assert(grams(0) == 0) } void main() { print(grams(8)) } .fi .RE .PP .RS 4 .nf $ cd scale $ mkdir -p vendor && cc -c -fPIC -o grams.o grams.c && ar rcs vendor/libgrams.a grams.o $ sg test main.sg -L vendor ok a pound is 453 grams ok no ounces weigh nothing 2 test(s) passed $ sg test main.sg --compiled -L vendor ok a pound is 453 grams ok no ounces weigh nothing 2 test(s) passed .fi .RE .PP \fBextern "grams"\fR is linked as \fB\-lgrams\fR\&. Without \fB\-L vendor\fR the library is not found, and the error names the \fBextern\fR block, with the hint of \fB\-L folder\fR\&. .SH "MISTAKES IT TELLS" .SS "A path that is not there" .RS 4 .nf $ sg test nothere.sg; echo $? error: cannot read nothere.sg: No such file or directory (os error 2) 2 .fi .RE .SS "A folder of -L that is not there" The file lone/lone.sg: .PP .RS 4 .nf test "one" { assert(1 + 1 == 2) } .fi .RE .PP .RS 4 .nf $ sg test lone/lone.sg -L lone/vendor; echo $? error: -L lone/vendor: no such folder 2 .fi .RE .PP \fB\-L\fR with nothing after it is told too: \fBerror: \-L names a folder: `\-L folder`\fR\&. A second path, or a word the command does not know, is not one of the forms: \fBsg test\fR then writes its usage and ends with 2. .SH "SEE ALSO" .BR sg-test (7), .BR sg-assert (7), .BR sg-build (1), .BR sg (1), .BR sg-public (7), .BR sg-extern (7) .PP https://sg\-lang.org/reference/sg-test.html