AtlatestRepositorysigil-web-styles
sigil-web-styles / tree / build / dev / lib / _pkg / sigil-testtesting.md
1
# Testing3
> Test framework with groups, assertions, and structured results.5
```scheme6
(import (sigil test))7
```9
## Defining Tests11
Use `test` to define a test case and `test-group` to organize related tests.13
```scheme14
(test "addition works"15
(assert-equal 4 (+ 2 2)))17
(test-group "String operations"18
(test "concatenation"19
(assert-equal "hello world" (string-append "hello" " " "world")))20
(test "length"21
(assert-equal 5 (string-length "hello"))))23
;; Groups can be nested24
(test-group "Collections"25
(test-group "Lists"26
(test "map doubles"27
(assert-equal '(2 4 6) (map (lambda (x) (* x 2)) '(1 2 3))))))28
```30
## Skipping and Pending Tests32
```scheme33
;; Skip a test (temporarily disabled, body not executed)34
(test-skip "broken feature"35
(assert-equal 42 (broken-function)))37
;; Pending test (placeholder for future work, body not executed)38
(test-pending "not yet implemented"39
(assert-true (new-feature-works)))40
```42
Both are counted separately in the test summary.44
## Assertions46
### Value Equality48
```scheme49
(assert-equal expected actual) ; deep equality (equal?)50
(assert-eqv expected actual) ; value equivalence (eqv?)51
(assert-eq expected actual) ; object identity (eq?)52
```54
```scheme55
(assert-equal '(1 2 3) (iota 3 1)) ; pass — deep list comparison56
(assert-eqv 3.14 3.14) ; pass — numeric equivalence57
(assert-eq 'foo 'foo) ; pass — symbols are interned58
```60
### Boolean62
```scheme63
(assert-true val) ; passes if val is not #f64
(assert-false val) ; passes if val is #f65
```67
```scheme68
(assert-true (> 5 3))69
(assert-false (member 'x '(a b c)))70
```72
### Null Checks74
```scheme75
(assert-null val) ; passes if val is '()76
(assert-not-null val) ; passes if val is not '()77
```79
```scheme80
(assert-null (cdr '(1)))81
(assert-not-null (filter odd? '(1 2 3)))82
```84
### Error Checking86
```scheme87
(assert-error expr) ; passes if expr raises an error88
```90
```scheme91
(assert-error (car '()))92
(assert-error (error "expected failure"))93
```95
### Unconditional Failure97
```scheme98
(assert-fail message) ; always fails with message99
```101
```scheme102
(test "should not reach else branch"103
(if (valid? input)104
(assert-true (process input))105
(assert-fail "input was invalid")))106
```108
## Running Tests110
Every test file should end with `(run-tests)`.112
```scheme113
(import (sigil test))115
(test "example" (assert-true #t))116
(run-tests)117
```119
The CLI runs all test files in the workspace:121
```122
sigil test # Run all tests (native + Sigil)123
sigil test --sgl # Run only Sigil tests124
sigil test --native # Run only native (C) tests125
```127
## Test Results129
`run-tests` produces a `test-summary` with aggregate results.131
- `test-summary-total` — total tests run132
- `test-summary-passed` — tests that passed133
- `test-summary-failed` — tests that failed134
- `test-summary-skipped` — tests skipped or pending135
- `test-summary-duration-ms` — total time in milliseconds136
- `test-summary-results` — list of `test-result` records138
Each `test-result` has:140
- `test-result-name` — test name string141
- `test-result-group` — group path or `#f`142
- `test-result-passed?` — boolean143
- `test-result-message` — failure message or `#f`144
- `test-result-expected` / `test-result-actual` — values on failure146
## Common Patterns148
### Testing a Module150
```scheme151
(import (sigil test)152
(sigil json))154
(test-group "json-encode"155
(test "encodes string"156
(assert-equal "\"hello\"" (json-encode "hello")))157
(test "encodes number"158
(assert-equal "42" (json-encode 42)))159
(test "encodes dict"160
(let ((result (json-decode (json-encode #{ a: 1 }))))161
(assert-equal 1 (dict-ref result a:)))))163
(run-tests)164
```166
### Testing Error Conditions168
```scheme169
(test-group "input validation"170
(test "rejects empty input"171
(assert-error (parse-config "")))172
(test "rejects missing required field"173
(assert-error (parse-config "{}"))))174
```176
### Test File Structure178
```179
packages/my-package/180
test/181
test-core.sgl # Core functionality tests182
test-parsing.sgl # Parser tests183
test-output.sgl # Output formatting tests184
```186
Each file is self-contained with its own imports and `(run-tests)` call.