about summary refs log tree commit diff
path: root/lib/tests/misc.nix
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-05-31 22:50:00 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-06-06 17:06:18 +0200
commit6996f76885d81fbc1b066fe346713630e6ac9e1b (patch)
treed748749e9b3a01d0381f1195251068e9d959efbf /lib/tests/misc.nix
parent28ecd1752334b467929ef3f565389fdf94c03f1e (diff)
downloadnixlib-6996f76885d81fbc1b066fe346713630e6ac9e1b.tar
nixlib-6996f76885d81fbc1b066fe346713630e6ac9e1b.tar.gz
nixlib-6996f76885d81fbc1b066fe346713630e6ac9e1b.tar.bz2
nixlib-6996f76885d81fbc1b066fe346713630e6ac9e1b.tar.lz
nixlib-6996f76885d81fbc1b066fe346713630e6ac9e1b.tar.xz
nixlib-6996f76885d81fbc1b066fe346713630e6ac9e1b.tar.zst
nixlib-6996f76885d81fbc1b066fe346713630e6ac9e1b.zip
lib/tests: Add findFirst tests
Diffstat (limited to 'lib/tests/misc.nix')
-rw-r--r--lib/tests/misc.nix35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 231f19c513eb..dcb8f1102612 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -518,6 +518,41 @@ runTests {
     expected = false;
   };
 
+  testFindFirstExample1 = {
+    expr = findFirst (x: x > 3) 7 [ 1 6 4 ];
+    expected = 6;
+  };
+
+  testFindFirstExample2 = {
+    expr = findFirst (x: x > 9) 7 [ 1 6 4 ];
+    expected = 7;
+  };
+
+  testFindFirstEmpty = {
+    expr = findFirst (abort "when the list is empty, the predicate is not needed") null [];
+    expected = null;
+  };
+
+  testFindFirstSingleMatch = {
+    expr = findFirst (x: x == 5) null [ 5 ];
+    expected = 5;
+  };
+
+  testFindFirstSingleDefault = {
+    expr = findFirst (x: false) null [ (abort "if the predicate doesn't access the value, it must not be evaluated") ];
+    expected = null;
+  };
+
+  testFindFirstNone = {
+    expr = builtins.tryEval (findFirst (x: x == 2) null [ 1 (throw "the last element must be evaluated when there's no match") ]);
+    expected = { success = false; value = false; };
+  };
+
+  # Makes sure that the implementation doesn't cause a stack overflow
+  testFindFirstBig = {
+    expr = findFirst (x: x == 1000000) null (range 0 1000000);
+    expected = 1000000;
+  };
 
 # ATTRSETS