2017-03-18 20:41:02 +01:00
|
|
|
# to run these tests:
|
2017-04-25 02:12:43 +02:00
|
|
|
# nix-instantiate --eval --strict nixpkgs/lib/tests/misc.nix
|
2017-03-18 20:41:02 +01:00
|
|
|
# if the resulting list is empty, all tests passed
|
2017-04-25 02:12:43 +02:00
|
|
|
with import ../default.nix;
|
2009-03-07 00:21:17 +01:00
|
|
|
|
2009-03-31 15:03:50 +02:00
|
|
|
runTests {
|
2009-03-07 00:21:17 +01:00
|
|
|
|
2017-04-12 00:43:52 +02:00
|
|
|
|
|
|
|
# TRIVIAL
|
|
|
|
|
2009-11-22 22:28:41 +01:00
|
|
|
testId = {
|
2009-03-31 15:03:50 +02:00
|
|
|
expr = id 1;
|
|
|
|
expected = 1;
|
|
|
|
};
|
2015-11-24 10:00:44 +01:00
|
|
|
|
2009-11-22 22:28:41 +01:00
|
|
|
testConst = {
|
2009-03-31 15:03:50 +02:00
|
|
|
expr = const 2 3;
|
|
|
|
expected = 2;
|
|
|
|
};
|
2012-05-13 21:10:57 +02:00
|
|
|
|
|
|
|
/*
|
2009-11-22 22:28:41 +01:00
|
|
|
testOr = {
|
2009-03-31 15:03:50 +02:00
|
|
|
expr = or true false;
|
|
|
|
expected = true;
|
|
|
|
};
|
2012-05-13 21:10:57 +02:00
|
|
|
*/
|
2015-11-24 10:00:44 +01:00
|
|
|
|
2009-11-22 22:28:41 +01:00
|
|
|
testAnd = {
|
2009-03-31 15:03:50 +02:00
|
|
|
expr = and true false;
|
|
|
|
expected = false;
|
|
|
|
};
|
2015-11-24 10:00:44 +01:00
|
|
|
|
2009-11-22 22:28:41 +01:00
|
|
|
testFix = {
|
2009-03-31 15:03:50 +02:00
|
|
|
expr = fix (x: {a = if x ? a then "a" else "b";});
|
|
|
|
expected = {a = "a";};
|
|
|
|
};
|
|
|
|
|
2017-04-12 00:43:52 +02:00
|
|
|
testComposeExtensions = {
|
|
|
|
expr = let obj = makeExtensible (self: { foo = self.bar; });
|
|
|
|
f = self: super: { bar = false; baz = true; };
|
|
|
|
g = self: super: { bar = super.baz or false; };
|
|
|
|
f_o_g = composeExtensions f g;
|
|
|
|
composed = obj.extend f_o_g;
|
|
|
|
in composed.foo;
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
# STRINGS
|
|
|
|
|
2009-11-22 22:28:41 +01:00
|
|
|
testConcatMapStrings = {
|
2009-03-31 15:03:50 +02:00
|
|
|
expr = concatMapStrings (x: x + ";") ["a" "b" "c"];
|
|
|
|
expected = "a;b;c;";
|
|
|
|
};
|
|
|
|
|
2009-11-22 22:28:41 +01:00
|
|
|
testConcatStringsSep = {
|
2009-03-31 15:03:50 +02:00
|
|
|
expr = concatStringsSep "," ["a" "b" "c"];
|
|
|
|
expected = "a,b,c";
|
|
|
|
};
|
|
|
|
|
2017-04-12 00:43:52 +02:00
|
|
|
testSplitStringsSimple = {
|
|
|
|
expr = strings.splitString "." "a.b.c.d";
|
|
|
|
expected = [ "a" "b" "c" "d" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitStringsEmpty = {
|
|
|
|
expr = strings.splitString "." "a..b";
|
|
|
|
expected = [ "a" "" "b" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitStringsOne = {
|
|
|
|
expr = strings.splitString ":" "a.b";
|
|
|
|
expected = [ "a.b" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitStringsNone = {
|
|
|
|
expr = strings.splitString "." "";
|
|
|
|
expected = [ "" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitStringsFirstEmpty = {
|
|
|
|
expr = strings.splitString "/" "/a/b/c";
|
|
|
|
expected = [ "" "a" "b" "c" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
testSplitStringsLastEmpty = {
|
|
|
|
expr = strings.splitString ":" "2001:db8:0:0042::8a2e:370:";
|
|
|
|
expected = [ "2001" "db8" "0" "0042" "" "8a2e" "370" "" ];
|
|
|
|
};
|
|
|
|
|
2017-05-30 21:48:32 +02:00
|
|
|
testIsStorePath = {
|
|
|
|
expr =
|
|
|
|
let goodPath =
|
2017-06-09 23:30:10 +02:00
|
|
|
"${builtins.storeDir}/d945ibfx9x185xf04b890y4f9g3cbb63-python-2.7.11";
|
2017-05-30 21:48:32 +02:00
|
|
|
in {
|
|
|
|
storePath = isStorePath goodPath;
|
|
|
|
storePathAppendix = isStorePath
|
|
|
|
"${goodPath}/bin/python";
|
|
|
|
nonAbsolute = isStorePath (concatStrings (tail (stringToCharacters goodPath)));
|
|
|
|
asPath = isStorePath (builtins.toPath goodPath);
|
|
|
|
otherPath = isStorePath "/something/else";
|
|
|
|
otherVals = {
|
|
|
|
attrset = isStorePath {};
|
|
|
|
list = isStorePath [];
|
|
|
|
int = isStorePath 42;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
storePath = true;
|
|
|
|
storePathAppendix = false;
|
|
|
|
nonAbsolute = false;
|
|
|
|
asPath = true;
|
|
|
|
otherPath = false;
|
|
|
|
otherVals = {
|
|
|
|
attrset = false;
|
|
|
|
list = false;
|
|
|
|
int = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-04-12 00:43:52 +02:00
|
|
|
# LISTS
|
|
|
|
|
2009-11-22 22:28:41 +01:00
|
|
|
testFilter = {
|
2009-03-31 15:03:50 +02:00
|
|
|
expr = filter (x: x != "a") ["a" "b" "c" "a"];
|
|
|
|
expected = ["b" "c"];
|
|
|
|
};
|
|
|
|
|
2017-03-18 20:41:02 +01:00
|
|
|
testFold =
|
|
|
|
let
|
|
|
|
f = op: fold: fold op 0 (range 0 100);
|
|
|
|
# fold with associative operator
|
|
|
|
assoc = f builtins.add;
|
|
|
|
# fold with non-associative operator
|
|
|
|
nonAssoc = f builtins.sub;
|
|
|
|
in {
|
|
|
|
expr = {
|
|
|
|
assocRight = assoc foldr;
|
|
|
|
# right fold with assoc operator is same as left fold
|
|
|
|
assocRightIsLeft = assoc foldr == assoc foldl;
|
|
|
|
nonAssocRight = nonAssoc foldr;
|
|
|
|
nonAssocLeft = nonAssoc foldl;
|
|
|
|
# with non-assoc operator the fold results are not the same
|
|
|
|
nonAssocRightIsNotLeft = nonAssoc foldl != nonAssoc foldr;
|
|
|
|
# fold is an alias for foldr
|
|
|
|
foldIsRight = nonAssoc fold == nonAssoc foldr;
|
|
|
|
};
|
|
|
|
expected = {
|
|
|
|
assocRight = 5050;
|
|
|
|
assocRightIsLeft = true;
|
|
|
|
nonAssocRight = 50;
|
|
|
|
nonAssocLeft = (-5050);
|
|
|
|
nonAssocRightIsNotLeft = true;
|
|
|
|
foldIsRight = true;
|
|
|
|
};
|
|
|
|
};
|
2009-03-31 15:03:50 +02:00
|
|
|
|
2009-12-08 22:47:14 +01:00
|
|
|
testTake = testAllTrue [
|
|
|
|
([] == (take 0 [ 1 2 3 ]))
|
|
|
|
([1] == (take 1 [ 1 2 3 ]))
|
|
|
|
([ 1 2 ] == (take 2 [ 1 2 3 ]))
|
|
|
|
([ 1 2 3 ] == (take 3 [ 1 2 3 ]))
|
|
|
|
([ 1 2 3 ] == (take 4 [ 1 2 3 ]))
|
|
|
|
];
|
|
|
|
|
2012-08-28 14:40:24 +02:00
|
|
|
testFoldAttrs = {
|
|
|
|
expr = foldAttrs (n: a: [n] ++ a) [] [
|
|
|
|
{ a = 2; b = 7; }
|
|
|
|
{ a = 3; c = 8; }
|
|
|
|
];
|
|
|
|
expected = { a = [ 2 3 ]; b = [7]; c = [8];};
|
|
|
|
};
|
2009-12-08 22:47:14 +01:00
|
|
|
|
2012-11-20 13:54:38 +01:00
|
|
|
testSort = {
|
|
|
|
expr = sort builtins.lessThan [ 40 2 30 42 ];
|
|
|
|
expected = [2 30 40 42];
|
|
|
|
};
|
2015-11-24 10:00:44 +01:00
|
|
|
|
|
|
|
testToIntShouldConvertStringToInt = {
|
|
|
|
expr = toInt "27";
|
|
|
|
expected = 27;
|
|
|
|
};
|
|
|
|
|
|
|
|
testToIntShouldThrowErrorIfItCouldNotConvertToInt = {
|
|
|
|
expr = builtins.tryEval (toInt "\"foo\"");
|
|
|
|
expected = { success = false; value = false; };
|
|
|
|
};
|
|
|
|
|
2015-12-04 16:17:45 +01:00
|
|
|
testHasAttrByPathTrue = {
|
|
|
|
expr = hasAttrByPath ["a" "b"] { a = { b = "yey"; }; };
|
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
testHasAttrByPathFalse = {
|
|
|
|
expr = hasAttrByPath ["a" "b"] { a = { c = "yey"; }; };
|
|
|
|
expected = false;
|
|
|
|
};
|
|
|
|
|
2016-11-06 01:51:13 +01:00
|
|
|
|
2017-04-12 00:43:52 +02:00
|
|
|
# GENERATORS
|
|
|
|
# these tests assume attributes are converted to lists
|
|
|
|
# in alphabetical order
|
2016-11-06 01:51:13 +01:00
|
|
|
|
2016-12-06 23:19:34 +01:00
|
|
|
testMkKeyValueDefault = {
|
2017-11-09 15:58:14 +01:00
|
|
|
expr = generators.mkKeyValueDefault {} ":" "f:oo" "bar";
|
2016-12-04 22:11:24 +01:00
|
|
|
expected = ''f\:oo:bar'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToKeyValue = {
|
|
|
|
expr = generators.toKeyValue {} {
|
|
|
|
key = "value";
|
|
|
|
"other=key" = "baz";
|
|
|
|
};
|
|
|
|
expected = ''
|
|
|
|
key=value
|
|
|
|
other\=key=baz
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-11-06 01:51:13 +01:00
|
|
|
testToINIEmpty = {
|
|
|
|
expr = generators.toINI {} {};
|
|
|
|
expected = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
testToINIEmptySection = {
|
|
|
|
expr = generators.toINI {} { foo = {}; bar = {}; };
|
|
|
|
expected = ''
|
|
|
|
[bar]
|
|
|
|
|
|
|
|
[foo]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToINIDefaultEscapes = {
|
|
|
|
expr = generators.toINI {} {
|
|
|
|
"no [ and ] allowed unescaped" = {
|
|
|
|
"and also no = in keys" = 42;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
expected = ''
|
|
|
|
[no \[ and \] allowed unescaped]
|
|
|
|
and also no \= in keys=42
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
testToINIDefaultFull = {
|
|
|
|
expr = generators.toINI {} {
|
|
|
|
"section 1" = {
|
|
|
|
attribute1 = 5;
|
|
|
|
x = "Me-se JarJar Binx";
|
|
|
|
};
|
|
|
|
"foo[]" = {
|
|
|
|
"he\\h=he" = "this is okay";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
expected = ''
|
|
|
|
[foo\[\]]
|
|
|
|
he\h\=he=this is okay
|
|
|
|
|
|
|
|
[section 1]
|
|
|
|
attribute1=5
|
|
|
|
x=Me-se JarJar Binx
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-11-06 14:14:24 +01:00
|
|
|
/* right now only invocation check */
|
|
|
|
testToJSONSimple =
|
|
|
|
let val = {
|
|
|
|
foobar = [ "baz" 1 2 3 ];
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
expr = generators.toJSON {} val;
|
2017-04-19 21:41:28 +02:00
|
|
|
# trivial implementation
|
2016-11-06 14:14:24 +01:00
|
|
|
expected = builtins.toJSON val;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* right now only invocation check */
|
|
|
|
testToYAMLSimple =
|
|
|
|
let val = {
|
|
|
|
list = [ { one = 1; } { two = 2; } ];
|
|
|
|
all = 42;
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
expr = generators.toYAML {} val;
|
2017-04-19 21:41:28 +02:00
|
|
|
# trivial implementation
|
2016-11-06 14:14:24 +01:00
|
|
|
expected = builtins.toJSON val;
|
|
|
|
};
|
|
|
|
|
2017-06-06 22:41:22 +02:00
|
|
|
testToPretty = {
|
|
|
|
expr = mapAttrs (const (generators.toPretty {})) rec {
|
|
|
|
int = 42;
|
|
|
|
bool = true;
|
|
|
|
string = "fnord";
|
|
|
|
null_ = null;
|
|
|
|
function = x: x;
|
2017-06-12 07:07:59 +02:00
|
|
|
functionArgs = { arg ? 4, foo }: arg;
|
2017-06-06 22:41:22 +02:00
|
|
|
list = [ 3 4 function [ false ] ];
|
|
|
|
attrs = { foo = null; "foo bar" = "baz"; };
|
|
|
|
drv = derivation { name = "test"; system = builtins.currentSystem; };
|
|
|
|
};
|
|
|
|
expected = rec {
|
|
|
|
int = "42";
|
|
|
|
bool = "true";
|
|
|
|
string = "\"fnord\"";
|
|
|
|
null_ = "null";
|
|
|
|
function = "<λ>";
|
2017-06-12 07:07:59 +02:00
|
|
|
functionArgs = "<λ:{(arg),foo}>";
|
2017-06-06 22:41:22 +02:00
|
|
|
list = "[ 3 4 ${function} [ false ] ]";
|
|
|
|
attrs = "{ \"foo\" = null; \"foo bar\" = \"baz\"; }";
|
|
|
|
drv = "<δ>";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testToPrettyAllowPrettyValues = {
|
|
|
|
expr = generators.toPretty { allowPrettyValues = true; }
|
|
|
|
{ __pretty = v: "«" + v + "»"; val = "foo"; };
|
|
|
|
expected = "«foo»";
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-04-12 00:43:52 +02:00
|
|
|
# MISC
|
2017-03-15 22:16:04 +01:00
|
|
|
|
2017-04-12 00:43:52 +02:00
|
|
|
testOverridableDelayableArgsTest = {
|
|
|
|
expr =
|
|
|
|
let res1 = defaultOverridableDelayableArgs id {};
|
|
|
|
res2 = defaultOverridableDelayableArgs id { a = 7; };
|
|
|
|
res3 = let x = defaultOverridableDelayableArgs id { a = 7; };
|
|
|
|
in (x.merge) { b = 10; };
|
|
|
|
res4 = let x = defaultOverridableDelayableArgs id { a = 7; };
|
|
|
|
in (x.merge) ( x: { b = 10; });
|
|
|
|
res5 = let x = defaultOverridableDelayableArgs id { a = 7; };
|
2017-05-30 21:48:32 +02:00
|
|
|
in (x.merge) ( x: { a = builtins.add x.a 3; });
|
|
|
|
res6 = let x = defaultOverridableDelayableArgs id { a = 7; mergeAttrBy = { a = builtins.add; }; };
|
2017-04-12 00:43:52 +02:00
|
|
|
y = x.merge {};
|
|
|
|
in (y.merge) { a = 10; };
|
2017-03-15 22:16:04 +01:00
|
|
|
|
2017-04-12 00:43:52 +02:00
|
|
|
resRem7 = res6.replace (a: removeAttrs a ["a"]);
|
2017-03-15 22:16:04 +01:00
|
|
|
|
2017-05-30 21:48:32 +02:00
|
|
|
resReplace6 = let x = defaultOverridableDelayableArgs id { a = 7; mergeAttrBy = { a = builtins.add; }; };
|
2017-04-12 00:43:52 +02:00
|
|
|
x2 = x.merge { a = 20; }; # now we have 27
|
|
|
|
in (x2.replace) { a = 10; }; # and override the value by 10
|
2017-03-15 22:16:04 +01:00
|
|
|
|
2017-04-12 00:43:52 +02:00
|
|
|
# fixed tests (delayed args): (when using them add some comments, please)
|
|
|
|
resFixed1 =
|
|
|
|
let x = defaultOverridableDelayableArgs id ( x: { a = 7; c = x.fixed.b; });
|
|
|
|
y = x.merge (x: { name = "name-${builtins.toString x.fixed.c}"; });
|
|
|
|
in (y.merge) { b = 10; };
|
|
|
|
strip = attrs: removeAttrs attrs ["merge" "replace"];
|
|
|
|
in all id
|
|
|
|
[ ((strip res1) == { })
|
|
|
|
((strip res2) == { a = 7; })
|
|
|
|
((strip res3) == { a = 7; b = 10; })
|
|
|
|
((strip res4) == { a = 7; b = 10; })
|
|
|
|
((strip res5) == { a = 10; })
|
|
|
|
((strip res6) == { a = 17; })
|
|
|
|
((strip resRem7) == {})
|
|
|
|
((strip resFixed1) == { a = 7; b = 10; c =10; name = "name-10"; })
|
|
|
|
];
|
2017-04-12 18:40:24 +02:00
|
|
|
expected = true;
|
|
|
|
};
|
|
|
|
|
2009-03-31 15:03:50 +02:00
|
|
|
}
|