lib/trivial: add a few examples of usage of assertMsg/assertOneOf

This commit is contained in:
Profpatsch 2018-08-06 01:36:09 +02:00
parent 320cdecd16
commit 3e45b61a99
5 changed files with 20 additions and 10 deletions

View file

@ -509,7 +509,8 @@ rec {
=> 3 => 3
*/ */
last = list: 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 /* Return all elements but the last
@ -517,7 +518,9 @@ rec {
init [ 1 2 3 ] init [ 1 2 3 ]
=> [ 1 2 ] => [ 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 /* return the image of the cross product of some lists by a function

View file

@ -468,7 +468,10 @@ rec {
strw = lib.stringLength str; strw = lib.stringLength str;
reqWidth = width - (lib.stringLength filler); reqWidth = width - (lib.stringLength filler);
in 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; if strw == width then str else filler + fixedWidthString reqWidth filler str;
/* Format a number adding leading zeroes up to fixed width. /* Format a number adding leading zeroes up to fixed width.

View file

@ -119,7 +119,9 @@ rec {
let let
betweenDesc = lowest: highest: betweenDesc = lowest: highest:
"${toString lowest} and ${toString highest} (both inclusive)"; "${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) // { addCheck int (x: x >= lowest && x <= highest) // {
name = "intBetween"; name = "intBetween";
description = "integer between ${betweenDesc lowest highest}"; description = "integer between ${betweenDesc lowest highest}";
@ -439,7 +441,9 @@ rec {
# Either value of type `finalType` or `coercedType`, the latter is # Either value of type `finalType` or `coercedType`, the latter is
# converted to `finalType` using `coerceFunc`. # converted to `finalType` using `coerceFunc`.
coercedTo = coercedType: coerceFunc: finalType: coercedTo = coercedType: coerceFunc: finalType:
assert coercedType.getSubModules == null; assert assertMsg (coercedType.getSubModules == null)
"coercedTo: coercedType must not have submodules (its a ${
coercedType.description})";
mkOptionType rec { mkOptionType rec {
name = "coercedTo"; name = "coercedTo";
description = "${finalType.description} or ${coercedType.description} convertible to it"; description = "${finalType.description} or ${coercedType.description} convertible to it";

View file

@ -1,13 +1,12 @@
{ stdenv, fetchurl, fetchFromGitHub { stdenv, lib, fetchurl, fetchFromGitHub
, pkgconfig , pkgconfig
, autoconf, automake, intltool, gettext , autoconf, automake, intltool, gettext
, gtk, vte , gtk, vte
# "stable" or "git"
, flavour ? "stable" , flavour ? "stable"
}: }:
assert flavour == "stable" || flavour == "git"; assert lib.assertOneOf "flavour" flavour [ "stable" "git" ];
let let
stuff = stuff =

View file

@ -8,7 +8,8 @@
with stdenv.lib; with stdenv.lib;
assert elem fileFormat ["lowerTriangularCsv" "upperTriangularCsv" "dipha"]; assert assertOneOf "fileFormat" fileFormat
["lowerTriangularCsv" "upperTriangularCsv" "dipha"];
assert useGoogleHashmap -> sparsehash != null; assert useGoogleHashmap -> sparsehash != null;
let let