summaryrefslogtreecommitdiff
path: root/pkgs/development/ruby-modules/testing/testing.nix
blob: ef19506e7c3c3a125b398bcd582e3734d7d50804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
with builtins;
let
  /*
    underTest = {
      x = {
        a = 1;
        b = "2";
      };
    };

    tests = [
      (root: false)
      {
        x = [
          (set: true)
          {
            a = (a: a > 1);
            b = (b: b == "3");
          }
        ];
      }
    ];

    results = run "Examples" underTest tests;
  */

  passed = desc: {
    result = "pass";
    description = desc;
  };

  failed = desc: {
    result = "failed";
    description = desc;
  };

  prefixName = name: res: {
    inherit (res) result;
    description = "${name}: ${res.description}";
  };

  run =
    name: under: tests:
    if isList tests then
      (concatLists (map (run name under) tests))
    else if isAttrs tests then
      (concatLists (
        map (
          subName:
          run (name + "." + subName) (if hasAttr subName under then getAttr subName under else "<MISSING!>") (
            getAttr subName tests
          )
        ) (attrNames tests)
      ))
    else if isFunction tests then
      let
        res = tests under;
      in
      if isBool res then
        [
          (prefixName name (if tests under then passed "passed" else failed "failed"))
        ]
      else
        [ (prefixName name res) ]
    else
      [
        failed
        (name ": not a function, list or set")
      ];
in
{
  inherit run passed failed;
}