document throws, run alejandra

This commit is contained in:
tristan 2024-01-02 11:13:47 +00:00
parent b800bb4695
commit aeb174ac65
9 changed files with 105 additions and 48 deletions

View file

@ -18,6 +18,10 @@ Make a test file:
actual = 2 + 2; actual = 2 + 2;
expected = 5; expected = 5;
}) })
(it "throws a party" {
actual = throw "a party";
throws = true;
})
] ]
``` ```

24
flake.lock Normal file
View file

@ -0,0 +1,24 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1703013332,
"narHash": "sha256-+tFNwMvlXLbJZXiMHqYq77z/RfmpfpiI3yjL6o/Zo9M=",
"path": "/nix/store/50bgi74d890mpkp90w1jwc5g0dw4dccr-source",
"rev": "54aac082a4d9bb5bbc5c4e899603abfb76a3f6d6",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

View file

@ -1,10 +1,16 @@
{ {
description = "A testing library for nix"; description = "A testing library for nix";
outputs = { self, nixpkgs }: outputs = {
let self,
nixpkgs,
}: let
system = "x86_64-linux"; system = "x86_64-linux";
pkgs = import nixpkgs {inherit system;}; pkgs = import nixpkgs {inherit system;};
testing = import ./testing/. { inherit pkgs; }; tix = import ./testing/. {inherit pkgs;};
in testing; in
tix
// {
packages.x86_64-linux.test = tix.run [./mytest.nix];
};
} }

14
mytest.nix Normal file
View file

@ -0,0 +1,14 @@
{it, ...}: [
(it "can add numbers" {
actual = 2 + 2;
expected = 4;
})
(it "loves big brother" {
actual = 2 + 2;
expected = 5;
})
(it "won't throw a party" {
actual = "a party";
throws = true;
})
]

View file

@ -1,6 +1,7 @@
path: path: let
let it = import ./it.nix; it = import ./it.nix;
in builtins.trace ("testing " + builtins.baseNameOf path) { in
builtins.trace ("testing " + builtins.baseNameOf path) {
inherit path; inherit path;
results = (import path { inherit it; }); results = import path {inherit it;};
} }

View file

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

View file

@ -1,5 +1,4 @@
pkgs: files: pkgs: files: let
let
test = import ./import.nix; test = import ./import.nix;
res = map test files; res = map test files;
resFile = builtins.toFile "results" (builtins.toJSON res); resFile = builtins.toFile "results" (builtins.toJSON res);

View file

@ -7,4 +7,3 @@ pkgs.writeShellScriptBin "watch" ''
pkill ${pname} pkill ${pname}
done done
'' ''