summary refs log tree commit diff
path: root/lib/tests
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2021-11-22 20:34:01 +0100
committerSilvan Mosberger <contact@infinisil.com>2022-03-18 00:08:29 +0100
commit85003ecdbb11cabd48b9ac7137720d764219a286 (patch)
tree5881396cabc6ee577f16431427508153a8cdd0c1 /lib/tests
parent1ad7812c4ac038b653dd57b887c7c07d02bf1bec (diff)
downloadnixpkgs-85003ecdbb11cabd48b9ac7137720d764219a286.tar
nixpkgs-85003ecdbb11cabd48b9ac7137720d764219a286.tar.gz
nixpkgs-85003ecdbb11cabd48b9ac7137720d764219a286.tar.bz2
nixpkgs-85003ecdbb11cabd48b9ac7137720d764219a286.tar.lz
nixpkgs-85003ecdbb11cabd48b9ac7137720d764219a286.tar.xz
nixpkgs-85003ecdbb11cabd48b9ac7137720d764219a286.tar.zst
nixpkgs-85003ecdbb11cabd48b9ac7137720d764219a286.zip
lib.attrsets: Introduce updateManyAttrsByPath
Diffstat (limited to 'lib/tests')
-rw-r--r--lib/tests/misc.nix114
1 files changed, 114 insertions, 0 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index c2e76f18d95..27111903139 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -799,4 +799,118 @@ runTests {
     expr = groupBy' builtins.add 0 (x: boolToString (x > 2)) [ 5 1 2 3 4 ];
     expected = { false = 3; true = 12; };
   };
+
+  # The example from the updateManyAttrsByPath documentation
+  testUpdateManyAttrsByPathExample = {
+    expr = updateManyAttrsByPath [
+      {
+        path = [ "a" "b" ];
+        update = old: { d = old.c; };
+      }
+      {
+        path = [ "a" "b" "c" ];
+        update = old: old + 1;
+      }
+      {
+        path = [ "x" "y" ];
+        update = old: "xy";
+      }
+    ] { a.b.c = 0; };
+    expected = { a = { b = { d = 1; }; }; x = { y = "xy"; }; };
+  };
+
+  # If there are no updates, the value is passed through
+  testUpdateManyAttrsByPathNone = {
+    expr = updateManyAttrsByPath [] "something";
+    expected = "something";
+  };
+
+  # A single update to the root path is just like applying the function directly
+  testUpdateManyAttrsByPathSingleIncrement = {
+    expr = updateManyAttrsByPath [
+      {
+        path = [ ];
+        update = old: old + 1;
+      }
+    ] 0;
+    expected = 1;
+  };
+
+  # Multiple updates can be applied are done in order
+  testUpdateManyAttrsByPathMultipleIncrements = {
+    expr = updateManyAttrsByPath [
+      {
+        path = [ ];
+        update = old: old + "a";
+      }
+      {
+        path = [ ];
+        update = old: old + "b";
+      }
+      {
+        path = [ ];
+        update = old: old + "c";
+      }
+    ] "";
+    expected = "abc";
+  };
+
+  # If an update doesn't use the value, all previous updates are not evaluated
+  testUpdateManyAttrsByPathLazy = {
+    expr = updateManyAttrsByPath [
+      {
+        path = [ ];
+        update = old: old + throw "nope";
+      }
+      {
+        path = [ ];
+        update = old: "untainted";
+      }
+    ] (throw "start");
+    expected = "untainted";
+  };
+
+  # Deeply nested attributes can be updated without affecting others
+  testUpdateManyAttrsByPathDeep = {
+    expr = updateManyAttrsByPath [
+      {
+        path = [ "a" "b" "c" ];
+        update = old: old + 1;
+      }
+    ] {
+      a.b.c = 0;
+
+      a.b.z = 0;
+      a.y.z = 0;
+      x.y.z = 0;
+    };
+    expected = {
+      a.b.c = 1;
+
+      a.b.z = 0;
+      a.y.z = 0;
+      x.y.z = 0;
+    };
+  };
+
+  # Nested attributes are updated first
+  testUpdateManyAttrsByPathNestedBeforehand = {
+    expr = updateManyAttrsByPath [
+      {
+        path = [ "a" ];
+        update = old: old // { x = old.b; };
+      }
+      {
+        path = [ "a" "b" ];
+        update = old: old + 1;
+      }
+    ] {
+      a.b = 0;
+    };
+    expected = {
+      a.b = 1;
+      a.x = 1;
+    };
+  };
+
 }