My line of work involves software requirements and testing. I find test driven design better than test-after. I understand it's not everyone's cup of tea.
I enjoy Nim for the clarity, minimalism and readability of the test code when using the unittest module. An idea I explored tonight was, "Can we write software requirements in Nim as readable as unit tests?"
Here is my sample rqmts.nim
# Write no production code except to make a failing test pass.
# Write no more of a test than is sufficient to fail.
# Write no more production code than is sufficient to pass the one failing test.
# Write no test constraint except to support a requirement.
import requirio
rqmt "p14p SHALL have a heap":
rqmt "The heap SHALL provide a function to accept a contiguous block of RAM as the heap."
rqmt "The heap SHALL provide a function to allocate a chunk of a specified size from heap.":
rqmt "The allocate function SHALL result in an error if the requested size is less than or equal to zero."
rqmt "The allocate function SHALL return a chunk of at least the requested size and at least the minimum size when the requested size is greater than zero."
and to make this compile, I wrote the minimum necessary module, requirio.nim:
template rqmt*(text: string) =
discard
template rqmt*(text: string, subrqmts: untyped) =
discard
My sample rqmts file compiles. Even though it doesn't do anything, I'm happy with the result because it is very close to doing something. The only other feature I want out of this tool is some way to connect a rqmt to my test cases, which are constraints designed to support the requirement.
test_heap.nim:
# To run these tests, execute `nimble tdd` from the project root directory.
import unittest
import heap
import pm
test "heap module SHOULD have an init function":
var heapbuf: array[12, uint8]
check compiles(heapInit(heapbuf))
test "heap init SHOULD return OK after a successful init":
var heapbuf: array[32, uint8]
check heapInit(heapbuf) == PmRetOk
test "heap init SHOULD return error after a failed init":
var heap1: array[0, uint8]
check heapInit(heap1) == PmRetNo
# this test is assumed to run on a 64-bit desktop platform
var heap2: array[23, uint8]
check heapInit(heap2) == PmRetNo