about summary refs log tree commit diff
path: root/lib/tests
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-07-19 16:44:11 +0200
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-07-19 17:35:28 +0200
commit53dcfd24ad71bb1a26c9d17cbd2af1f83a2da7a3 (patch)
tree0b1bf8d1b2c9ec8163409aa4b4c9f569e877a8b8 /lib/tests
parenta4f7840e1813bc389995083fac2b3bcd0ca6823d (diff)
downloadnixlib-53dcfd24ad71bb1a26c9d17cbd2af1f83a2da7a3.tar
nixlib-53dcfd24ad71bb1a26c9d17cbd2af1f83a2da7a3.tar.gz
nixlib-53dcfd24ad71bb1a26c9d17cbd2af1f83a2da7a3.tar.bz2
nixlib-53dcfd24ad71bb1a26c9d17cbd2af1f83a2da7a3.tar.lz
nixlib-53dcfd24ad71bb1a26c9d17cbd2af1f83a2da7a3.tar.xz
nixlib-53dcfd24ad71bb1a26c9d17cbd2af1f83a2da7a3.tar.zst
nixlib-53dcfd24ad71bb1a26c9d17cbd2af1f83a2da7a3.zip
lib.lists.findFirstIndex: init
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Diffstat (limited to 'lib/tests')
-rw-r--r--lib/tests/misc.nix50
1 files changed, 30 insertions, 20 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index ce980436c1bc..dfa6a94ee48e 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -518,45 +518,55 @@ runTests {
     expected = false;
   };
 
-  testFindFirstExample1 = {
-    expr = findFirst (x: x > 3) 7 [ 1 6 4 ];
-    expected = 6;
+  testFindFirstIndexExample1 = {
+    expr = lists.findFirstIndex (x: x > 3) (abort "index found, so a default must not be evaluated") [ 1 6 4 ];
+    expected = 1;
   };
 
-  testFindFirstExample2 = {
-    expr = findFirst (x: x > 9) 7 [ 1 6 4 ];
-    expected = 7;
+  testFindFirstIndexExample2 = {
+    expr = lists.findFirstIndex (x: x > 9) "a very specific default" [ 1 6 4 ];
+    expected = "a very specific default";
   };
 
-  testFindFirstEmpty = {
-    expr = findFirst (abort "when the list is empty, the predicate is not needed") null [];
+  testFindFirstIndexEmpty = {
+    expr = lists.findFirstIndex (abort "when the list is empty, the predicate is not needed") null [];
     expected = null;
   };
 
-  testFindFirstSingleMatch = {
-    expr = findFirst (x: x == 5) null [ 5 ];
-    expected = 5;
+  testFindFirstIndexSingleMatch = {
+    expr = lists.findFirstIndex (x: x == 5) null [ 5 ];
+    expected = 0;
   };
 
-  testFindFirstSingleDefault = {
-    expr = findFirst (x: false) null [ (abort "if the predicate doesn't access the value, it must not be evaluated") ];
+  testFindFirstIndexSingleDefault = {
+    expr = lists.findFirstIndex (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") ]);
+  testFindFirstIndexNone = {
+    expr = builtins.tryEval (lists.findFirstIndex (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);
+  testFindFirstIndexBig = {
+    expr = lists.findFirstIndex (x: x == 1000000) null (range 0 1000000);
     expected = 1000000;
   };
 
-  testFindFirstLazy = {
-    expr = findFirst (x: x == 1) 7 [ 1 (abort "list elements after the match must not be evaluated") ];
-    expected = 1;
+  testFindFirstIndexLazy = {
+    expr = lists.findFirstIndex (x: x == 1) null [ 1 (abort "list elements after the match must not be evaluated") ];
+    expected = 0;
+  };
+
+  testFindFirstExample1 = {
+    expr = lists.findFirst (x: x > 3) 7 [ 1 6 4 ];
+    expected = 6;
+  };
+
+  testFindFirstExample2 = {
+    expr = lists.findFirst (x: x > 9) 7 [ 1 6 4 ];
+    expected = 7;
   };
 
 # ATTRSETS