add tests

This commit is contained in:
tristan 2024-01-02 12:44:07 +00:00
parent aeb174ac65
commit 0b7f5ad721
12 changed files with 73 additions and 16 deletions

6
tix/default.nix Normal file
View file

@ -0,0 +1,6 @@
{pkgs ? import <nixpkgs> {}}: {
run = import ./run.nix pkgs;
it = import ./it.nix;
test = import ./test.nix;
watch = import ./watch.nix pkgs;
}

51
tix/it.nix Normal file
View file

@ -0,0 +1,51 @@
msg: {
actual,
expected ? {},
asString ? false,
asJSON ? false,
removeDunders ? false,
safeToPrint ? true,
throws ? false,
}: let
preProcess = v:
if removeDunders
then undunder v
else if asString
then toString v
else if asJSON
then builtins.toJSON v
else v;
a = preProcess actual;
e = preProcess expected;
undunder = v:
if builtins.isAttrs v
then builtins.removeAttrs v ["__toString" "__functor"]
else v;
out =
if safeToPrint
then
builtins.toJSON (undunder (
if throws
then (builtins.tryEval actual).value
else {
inherit actual expected;
}
))
else ''{"msg": "cannot be stringified ):"}'';
success =
if throws
then (builtins.tryEval actual).success == false
else (a == e);
in
{
inherit success msg;
}
// (
if success
then {}
else builtins.trace "FAILED ${msg}" {inherit actual expected;}
)

15
tix/run.nix Normal file
View file

@ -0,0 +1,15 @@
pkgs: files: let
test = import ./test.nix;
res = map test files;
resFile = builtins.toFile "results" (builtins.toJSON res);
filter = ''
.[] | {
(.path): {
"": [.results | map(select(.success==false))[] | {(.msg): {actual, expected}}],
"": .results | map(select(.success) | .msg) | length
}
}
'';
in (pkgs.writeShellScriptBin "test" ''
cat '${resFile}' | ${pkgs.jq}/bin/jq '${filter}'
'')

14
tix/test.nix Normal file
View file

@ -0,0 +1,14 @@
path: let
it = import ./it.nix;
suite = import path;
in
builtins.trace ("testing " + builtins.baseNameOf path)
(
if !builtins.isFunction suite
then throw "A test suite should be in the form { it }: [ <tests> ]."
else
{
inherit path;
results = suite {inherit it;};
}
)

9
tix/watch.nix Normal file
View file

@ -0,0 +1,9 @@
pkgs: cmd: pname:
pkgs.writeShellScriptBin "watch" ''
while true
do
${cmd} &
${pkgs.inotify-tools}/bin/inotifywait -e modify -r .
pkill ${pname}
done
''