summary refs log tree commit diff
path: root/lib/tests/misc.nix
diff options
context:
space:
mode:
authorJacob Abel <jacobabel@nullpo.dev>2022-07-08 19:37:45 -0400
committerJacob Abel <jacobabel@nullpo.dev>2022-10-23 17:50:24 -0400
commit39a4ab78a1245eb45d333fc14ec56f3a8f045986 (patch)
treefd264453068cd9be8e1d9445d3105954ac0d5173 /lib/tests/misc.nix
parent3d196a5f2a72595b14c439a9b4aba7737c0f1ebe (diff)
downloadnixpkgs-39a4ab78a1245eb45d333fc14ec56f3a8f045986.tar
nixpkgs-39a4ab78a1245eb45d333fc14ec56f3a8f045986.tar.gz
nixpkgs-39a4ab78a1245eb45d333fc14ec56f3a8f045986.tar.bz2
nixpkgs-39a4ab78a1245eb45d333fc14ec56f3a8f045986.tar.lz
nixpkgs-39a4ab78a1245eb45d333fc14ec56f3a8f045986.tar.xz
nixpkgs-39a4ab78a1245eb45d333fc14ec56f3a8f045986.tar.zst
nixpkgs-39a4ab78a1245eb45d333fc14ec56f3a8f045986.zip
lib/strings: Refactor toInt into toInt and toIntBase10
Diffstat (limited to 'lib/tests/misc.nix')
-rw-r--r--lib/tests/misc.nix60
1 files changed, 44 insertions, 16 deletions
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 97d53026c64..4bfc8bb8769 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -339,20 +339,6 @@ runTests {
     (0 == toInt " 0")
     (0 == toInt "0 ")
     (0 == toInt " 0 ")
-    # Zero Padding
-    (123 == toInt "0123")
-    (123 == toInt "0000123")
-    (0 == toInt "000000")
-    # Whitespace and Zero Padding
-    (123 == toInt " 0123")
-    (123 == toInt "0123 ")
-    (123 == toInt " 0123 ")
-    (123 == toInt " 0000123")
-    (123 == toInt "0000123 ")
-    (123 == toInt " 0000123 ")
-    (0 == toInt " 000000")
-    (0 == toInt "000000 ")
-    (0 == toInt " 000000 ")
   ];
 
   testToIntFails = testAllTrue [
@@ -360,10 +346,52 @@ runTests {
     ( builtins.tryEval (toInt "123 123") == { success = false; value = false; } )
     ( builtins.tryEval (toInt "0 123") == { success = false; value = false; } )
     ( builtins.tryEval (toInt " 0d ") == { success = false; value = false; } )
+    ( builtins.tryEval (toInt "00") == { success = false; value = false; } )
+    ( builtins.tryEval (toInt "01") == { success = false; value = false; } )
+    ( builtins.tryEval (toInt "002") == { success = false; value = false; } )
+    ( builtins.tryEval (toInt " 002 ") == { success = false; value = false; } )
     ( builtins.tryEval (toInt " foo ") == { success = false; value = false; } )
     ( builtins.tryEval (toInt " foo 123 ") == { success = false; value = false; } )
-    ( builtins.tryEval (toInt " foo 00123 ") == { success = false; value = false; } )
-    ( builtins.tryEval (toInt " foo00123 ") == { success = false; value = false; } )
+    ( builtins.tryEval (toInt " foo123 ") == { success = false; value = false; } )
+  ];
+
+  testToIntBase10 = testAllTrue [
+    # Naive
+    (123 == toIntBase10 "123")
+    (0 == toIntBase10 "0")
+    # Whitespace Padding
+    (123 == toIntBase10 " 123")
+    (123 == toIntBase10 "123 ")
+    (123 == toIntBase10 " 123 ")
+    (123 == toIntBase10 "   123   ")
+    (0 == toIntBase10 " 0")
+    (0 == toIntBase10 "0 ")
+    (0 == toIntBase10 " 0 ")
+    # Zero Padding
+    (123 == toIntBase10 "0123")
+    (123 == toIntBase10 "0000123")
+    (0 == toIntBase10 "000000")
+    # Whitespace and Zero Padding
+    (123 == toIntBase10 " 0123")
+    (123 == toIntBase10 "0123 ")
+    (123 == toIntBase10 " 0123 ")
+    (123 == toIntBase10 " 0000123")
+    (123 == toIntBase10 "0000123 ")
+    (123 == toIntBase10 " 0000123 ")
+    (0 == toIntBase10 " 000000")
+    (0 == toIntBase10 "000000 ")
+    (0 == toIntBase10 " 000000 ")
+  ];
+
+  testToIntBase10Fails = testAllTrue [
+    ( builtins.tryEval (toIntBase10 "") == { success = false; value = false; } )
+    ( builtins.tryEval (toIntBase10 "123 123") == { success = false; value = false; } )
+    ( builtins.tryEval (toIntBase10 "0 123") == { success = false; value = false; } )
+    ( builtins.tryEval (toIntBase10 " 0d ") == { success = false; value = false; } )
+    ( builtins.tryEval (toIntBase10 " foo ") == { success = false; value = false; } )
+    ( builtins.tryEval (toIntBase10 " foo 123 ") == { success = false; value = false; } )
+    ( builtins.tryEval (toIntBase10 " foo 00123 ") == { success = false; value = false; } )
+    ( builtins.tryEval (toIntBase10 " foo00123 ") == { success = false; value = false; } )
   ];
 
 # LISTS