summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2018-08-06 01:36:09 +0200
committerProfpatsch <mail@profpatsch.de>2018-09-06 18:14:27 +0200
commit3e45b61a9920466a8ea06b8ad9350d56ade435bc (patch)
tree1913cb4951336a37ed9921a538db5a50c7b62008 /lib
parent320cdecd1697020cb367adc1f8408dbf689ca254 (diff)
downloadnixlib-3e45b61a9920466a8ea06b8ad9350d56ade435bc.tar
nixlib-3e45b61a9920466a8ea06b8ad9350d56ade435bc.tar.gz
nixlib-3e45b61a9920466a8ea06b8ad9350d56ade435bc.tar.bz2
nixlib-3e45b61a9920466a8ea06b8ad9350d56ade435bc.tar.lz
nixlib-3e45b61a9920466a8ea06b8ad9350d56ade435bc.tar.xz
nixlib-3e45b61a9920466a8ea06b8ad9350d56ade435bc.tar.zst
nixlib-3e45b61a9920466a8ea06b8ad9350d56ade435bc.zip
lib/trivial: add a few examples of usage of assertMsg/assertOneOf
Diffstat (limited to 'lib')
-rw-r--r--lib/lists.nix7
-rw-r--r--lib/strings.nix7
-rw-r--r--lib/types.nix8
3 files changed, 16 insertions, 6 deletions
diff --git a/lib/lists.nix b/lib/lists.nix
index 288882924fff..9a75f179e964 100644
--- a/lib/lists.nix
+++ b/lib/lists.nix
@@ -509,7 +509,8 @@ rec {
        => 3
   */
   last = list:
-    assert list != []; elemAt list (length list - 1);
+    assert assertMsg (list != []) "lists.last: list must not be empty!";
+    elemAt list (length list - 1);
 
   /* Return all elements but the last
 
@@ -517,7 +518,9 @@ rec {
        init [ 1 2 3 ]
        => [ 1 2 ]
   */
-  init = list: assert list != []; take (length list - 1) list;
+  init = list:
+    assert assertMsg (list != []) "lists.init: list must not be empty!";
+    take (length list - 1) list;
 
 
   /* return the image of the cross product of some lists by a function
diff --git a/lib/strings.nix b/lib/strings.nix
index 833f69d2fbfe..0c4095bb55cd 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -410,7 +410,7 @@ rec {
       components = splitString "/" url;
       filename = lib.last components;
       name = builtins.head (splitString sep filename);
-    in assert name !=  filename; name;
+    in assert name != filename; name;
 
   /* Create an --{enable,disable}-<feat> string that can be passed to
      standard GNU Autoconf scripts.
@@ -468,7 +468,10 @@ rec {
       strw = lib.stringLength str;
       reqWidth = width - (lib.stringLength filler);
     in
-      assert strw <= width;
+      assert lib.assertMsg (strw <= width)
+        "fixedWidthString: requested string length (${
+          toString width}) must not be shorter than actual length (${
+            toString strw})";
       if strw == width then str else filler + fixedWidthString reqWidth filler str;
 
   /* Format a number adding leading zeroes up to fixed width.
diff --git a/lib/types.nix b/lib/types.nix
index 4d6ac51c8988..441af98cb9b6 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -119,7 +119,9 @@ rec {
       let
         betweenDesc = lowest: highest:
           "${toString lowest} and ${toString highest} (both inclusive)";
-        between = lowest: highest: assert lowest <= highest;
+        between = lowest: highest:
+          assert lib.assertMsg (lowest <= highest)
+            "ints.between: lowest must be smaller than highest";
           addCheck int (x: x >= lowest && x <= highest) // {
             name = "intBetween";
             description = "integer between ${betweenDesc lowest highest}";
@@ -439,7 +441,9 @@ rec {
     # Either value of type `finalType` or `coercedType`, the latter is
     # converted to `finalType` using `coerceFunc`.
     coercedTo = coercedType: coerceFunc: finalType:
-      assert coercedType.getSubModules == null;
+      assert assertMsg (coercedType.getSubModules == null)
+        "coercedTo: coercedType must not have submodules (it’s a ${
+          coercedType.description})";
       mkOptionType rec {
         name = "coercedTo";
         description = "${finalType.description} or ${coercedType.description} convertible to it";