Testing String Functions
Test string manipulation with declarative assertions.
Example 093 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell StringUtils has no face and no memory. Handlers: shout (line 10), whisper (line 14), greet (line 18) and initials (line 22).
The file also holds 1 test cell (StringTests) with 6 assertions, run by soma test.
The source
093_tests_strings.cell
// ============================================================================
// 093 — Testing String Functions
// ============================================================================
// Test string manipulation with declarative assertions.
//
// Run: soma test 093_tests_strings.cell
// Expect: All assertions pass
cell StringUtils {
on shout(s: String) {
return uppercase(s)
}
on whisper(s: String) {
return lowercase(s)
}
on greet(name: String) {
return concat("Hello, ", concat(name, "!"))
}
on initials(first: String, last: String) {
let f = substring(first, 0, 1)
let l = substring(last, 0, 1)
return concat(f, l)
}
}
cell test StringTests {
rules {
assert shout("hello") == "HELLO"
assert whisper("HELLO") == "hello"
assert greet("Alice") == "Hello, Alice!"
assert greet("World") == "Hello, World!"
assert initials("Alice", "Smith") == "AS"
assert initials("Bob", "Jones") == "BJ"
}
}What the toolchain says
$ soma check 093_tests_strings.cell
✓ All checks passed.$ soma test 093_tests_strings.cell
test StringTests ...
✓ assert shout("hello") == "HELLO"
✓ assert whisper("HELLO") == "hello"
✓ assert greet("Alice") == "Hello, Alice!"
✓ assert greet("World") == "Hello, World!"
✓ assert initials("Alice", "Smith") == "AS"
✓ assert initials("Bob", "Jones") == "BJ"
6 tests: 6 passed, 0 failed