about summary refs log tree commit diff
path: root/lib/tests/misc.nix
diff options
context:
space:
mode:
authorhsjobeki <hsjobeki+github@gmail.com>2023-02-28 11:04:19 +0100
committerhsjobeki <hsjobeki+github@gmail.com>2023-03-11 10:42:00 +0100
commit15a8d05ba592355f37442743bb3ce9fe55b72911 (patch)
tree98a5da74ced314a9f40f49b4115a89fdd6b38dfc /lib/tests/misc.nix
parent624432c25b2044dc291608cd204a7dee5275089b (diff)
init: lib.foldlAttrs
- provide comprehensive example
- add unit test
Diffstat (limited to 'lib/tests/misc.nix')
-rw-r--r--lib/tests/misc.nix31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 07d04f5356c7b..baa382f3e589c 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -533,6 +533,37 @@ runTests {
     };
   };
 
+  # code from example
+  testFoldlAttrs = {
+    expr = {
+      example = foldlAttrs
+        (acc: name: value: {
+          sum = acc.sum + value;
+          names = acc.names ++ [ name ];
+        })
+        { sum = 0; names = [ ]; }
+        {
+          foo = 1;
+          bar = 10;
+        };
+      # should just return the initial value
+      emptySet = foldlAttrs (throw "function not needed") 123 { };
+      # should just evaluate to the last value
+      accNotNeeded = foldlAttrs (_acc: _name: v: v) (throw "accumulator not needed") { z = 3; a = 2; };
+      # the accumulator doesnt have to be an attrset it can be as trivial as being just a number or string
+      trivialAcc = foldlAttrs (acc: _name: v: acc * 10 + v) 1 { z = 1; a = 2; };
+    };
+    expected = {
+      example = {
+        sum = 11;
+        names = [ "bar" "foo" ];
+      };
+      emptySet = 123;
+      accNotNeeded = 3;
+      trivialAcc = 121;
+    };
+  };
+
   # code from the example
   testRecursiveUpdateUntil = {
     expr = recursiveUpdateUntil (path: l: r: path == ["foo"]) {