labs/lib/default.test.nix

101 lines
2.8 KiB
Nix
Raw Permalink Normal View History

2024-06-03 09:57:13 +00:00
let
lib = import ./default.nix;
root = ./.;
files = [
./src/default.test.nix
2024-06-03 09:57:13 +00:00
./src/attrs/default.test.nix
./src/bools/default.test.nix
./src/dag/default.test.nix
2024-06-03 09:57:13 +00:00
./src/errors/default.test.nix
./src/fp/default.test.nix
./src/generators/default.test.nix
./src/importers/default.test.nix
./src/lists/default.test.nix
./src/math/default.test.nix
./src/modules/default.test.nix
./src/numbers/default.test.nix
./src/options/default.test.nix
./src/packages/default.test.nix
./src/paths/default.test.nix
./src/points/default.test.nix
./src/strings/default.test.nix
./src/types/default.test.nix
./src/versions/default.test.nix
];
2024-06-22 17:56:18 +00:00
resolve =
file:
let
imported = import file;
value = if builtins.isFunction imported then imported { inherit lib; } else imported;
relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString file);
in
{
inherit file value;
relative = if lib.strings.hasPrefix "/" relative then "." + relative else relative;
namespace = getNamespace file;
};
2024-06-03 09:57:13 +00:00
resolved = builtins.map resolve files;
2024-06-22 17:56:18 +00:00
getNamespace =
path:
let
relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString path);
parts = lib.strings.split "/" relative;
2024-06-03 09:57:13 +00:00
in
2024-06-22 17:56:18 +00:00
if builtins.length parts > 2 then builtins.elemAt parts 2 else relative;
2024-06-03 09:57:13 +00:00
2024-06-22 17:56:18 +00:00
results =
let
getTests =
file: prefix: suite:
let
nested = lib.attrs.mapToList (name: value: getTests file (prefix ++ [ name ]) value) suite;
relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString file);
in
if builtins.isAttrs suite then
builtins.concatLists nested
else
[
{
inherit prefix file;
name = builtins.concatStringsSep " > " prefix;
value = suite;
relative = if lib.strings.hasPrefix "/" relative then "." + relative else relative;
}
];
base = builtins.map (entry: getTests entry.file [ entry.namespace ] entry.value) resolved;
in
2024-06-03 09:57:13 +00:00
builtins.concatLists base;
successes = builtins.filter (test: test.value) results;
failures = builtins.filter (test: !test.value) results;
total = "${builtins.toString (builtins.length successes)} / ${builtins.toString (builtins.length results)}";
in
2024-06-22 17:56:18 +00:00
if failures == [ ] then
let
message = lib.strings.concatMapSep "\n" (test: " ${test.name}") successes;
in
''
2024-06-03 09:57:13 +00:00
SUCCESS (${total})
${message}
''
2024-06-22 17:56:18 +00:00
else
let
successMessage = lib.strings.concatMapSep "\n" (test: " ${test.name}") successes;
failureMessage = lib.strings.concatMapSep "\n\n" (
test: " ${test.name}\n" + " -> ${test.relative}"
) failures;
in
''
2024-06-03 09:57:13 +00:00
FAILURE (${total})
${failureMessage}
''