···223223 # Check whether a value is a store path.
224224 isStorePath = x: builtins.substring 0 1 (toString x) == "/" && dirOf (builtins.toPath x) == builtins.storeDir;
225225226226+ # Convert string to int
227227+ # Obviously, it is a bit hacky to use fromJSON that way.
228228+ toInt = str:
229229+ let may_be_int = builtins.fromJSON str; in
230230+ if builtins.isInt may_be_int
231231+ then may_be_int
232232+ else throw "Could not convert ${str} to int.";
233233+226234}
+16-6
lib/tests.nix
···77 expr = id 1;
88 expected = 1;
99 };
1010-1010+1111 testConst = {
1212 expr = const 2 3;
1313 expected = 2;
···1919 expected = true;
2020 };
2121 */
2222-2222+2323 testAnd = {
2424 expr = and true false;
2525 expected = false;
2626 };
2727-2727+2828 testFix = {
2929 expr = fix (x: {a = if x ? a then "a" else "b";});
3030 expected = {a = "a";};
···6767 };
68686969 testOverridableDelayableArgsTest = {
7070- expr =
7070+ expr =
7171 let res1 = defaultOverridableDelayableArgs id {};
7272 res2 = defaultOverridableDelayableArgs id { a = 7; };
7373 res3 = let x = defaultOverridableDelayableArgs id { a = 7; };
···8787 in (x2.replace) { a = 10; }; # and override the value by 10
88888989 # fixed tests (delayed args): (when using them add some comments, please)
9090- resFixed1 =
9090+ resFixed1 =
9191 let x = defaultOverridableDelayableArgs id ( x : { a = 7; c = x.fixed.b; });
9292 y = x.merge (x : { name = "name-${builtins.toString x.fixed.c}"; });
9393 in (y.merge) { b = 10; };
···109109 expr = sort builtins.lessThan [ 40 2 30 42 ];
110110 expected = [2 30 40 42];
111111 };
112112-112112+113113+ testToIntShouldConvertStringToInt = {
114114+ expr = toInt "27";
115115+ expected = 27;
116116+ };
117117+118118+ testToIntShouldThrowErrorIfItCouldNotConvertToInt = {
119119+ expr = builtins.tryEval (toInt "\"foo\"");
120120+ expected = { success = false; value = false; };
121121+ };
122122+113123}