r/ProgrammingLanguages • u/Inconstant_Moo 🧿 Pipefish • Jun 05 '26
Testing your code in Pipefish
After months of consolidation and polishing and testing I finally got to add a new feature! Yay!
My thinking was this. We spend a lot of time writing tests, so it should be as ergonomic as possible. If that means making testing first-class, you should do it. It also means that you should be able to put tests in with the code you're hacking on, and move them into separate files when your code is more stable. These considerations should of course be combined with the usual design principle that everything Java does is a godless abomination.
So, here's what I did. First of all, I introduced a test control structure. E.g:
test :
2 + 2 == 4
3 + 3 == 7This will return OK if the conditions are true, or an error if, as in this case, one of them isn't. The error-generating mechanism gives nice helpful errors --- if I put the above code into the REPL, I get:
[0] Error: failed test 3 + 3 == 7 :
â–ª lhs was : 6
â–ª rhs was : 7
Test failed at at line 3:8-10 of REPL input. In Pipefish, imperative code returns OK or an error, and we can test this in a test block too, along with the boolean conditions:
test :
2 <= 3
post "Hello world!"You will notice that a test block itself returns an error or OK, and so is itself imperative.
The point of having test as a control structure is that we can embed it in other imperative code:
const
TEST_VALUES = [-99, -1, 0, 1, 42, 1000000]
cmd
testArithmeticStillWorks :
for _::x = range TEST_VALUES :
for _::y = range TEST_VALUES :
test :
x * y == y * x
x + y - y == xAs we've seen, you can use a test block in any command, or in the REPL. However, we can also specify that the purpose of a command is testing by putting test as the first word of its name. (Pipefish functions and commands can have fancy syntax with all the infixes and mixfixes you could ask for).
def
double(x int/float) :
2 * x
test double :
for _::x = range [-99, -1, 0, 1, 42, 3.2, 0.0, 99.9] :
test :
double x == x + xThings defined in this way have the same semantics as ordinary commands, except that (a) none of them can have parameters (b) test on its own will call everything in a module defined in this way. (Hence if we import a module into namespace foo, then foo.test will run all the tests for foo from the importing module.) Tests can be put anywhere in the code. (They are run in the order of their declaration: you can temporarily move a test to the top of your code to ensure it's run first; or you can have the first one set up state for all the others and the last one tear it down.)
So we can write e.g:
import
"foo.pf"
"bar.pf"
test dependencies :
foo.test
bar.test
newtype
Person = struct(name string, age int) :
age >= 0
test validation :
test :
valid Person("Joseph", 22)
not valid Person("Joseph", -99)
def
inc(i int) :
i + 1
dec(i int) :
i - 1
test inc is inverse of dec :
for _::x = range [-3, -1, 0, 1, 86, 47] :
test :
inc dec x == x
dec inc x == x
cmd
init :
testAs in Go, init is a parameterless command run immediately after a module compiles. Hence by putting test at the end of whatever else we put in init, we guarantee that the tests will all be run at compile-time, useful if you're actively hacking away at your code.
Once your code is mature you can remove that and/or put your tests into another file which you include in the root file of your project --- or vice-versa depending on what exactly you're trying to achieve.
Eventually I'll have to do something about measuring test coverage and so on, but that's mere hacking. Designing the API is the important bit, and this seems to do everything I want from it.
Because Pipefish has functional-core/imperative-shell semantics, you don't really need much else. All the business logic is in pure functions that don't need any state to be initialized/mocked. For the rest, when setup and teardown isn't enough for us, it's even easier to mock a type you don't own in Pipefish than it is in Go: you can make an interface that the original object and mock object both satisfy; but you could also just make a mock object ad hoc that can have the same overloaded functions called on it.
So it seems like these new additions, plus the existing resources of the language, should be sufficient to write all the tests any reasonable person would need.
Unreasonable people can of course go on using Java.
View all comments
Show parent comment
2
u/Inconstant_Moo 🧿 Pipefish Jun 08 '26
A block consisting of one line can always be written on the same line, whether in tests or anything else.
The code can be structured in various ways according to need. You can write the tests in among the code as shown --- that's useful when that's the bit of code you're developing.
When it's more stable you could break them off and put them into another file which is included in the file to be tested; the
initcommand will go on calling them on compilation.When it's stabler still you could do it the other way round and have the test file include the file to be tested. That way that tests would only be run if you ran the whole thing by telling it to run the test file.
The difference between novel syntax and a macro isn't all that much from the user's point of view. And it's just another whitespaced block, they'll be used to it.
However, the difference between me making features first-class and me having a macro system to arbitrarily extend the language is that in the first case I'm the only person allowed to do that. Pipefish is meant to be a small simple boring language like Go where all you do all day is use
forloops to range of the same couple of container types and everyone can read everyone's code. If I have a macro system then any bozo can say "Actually, no it isn't."And some dev tools are worth making first-class. Testing is one of them; I also have "logging statements", a cross between everything you like about debuggers with everything you like about just sticking
printfstatements in your code.