mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-24 22:50:49 +00:00
Merge pull request #24835 from Profpatsch/lib-doc-improvements
Lib doc improvements
This commit is contained in:
commit
948488343b
159
lib/tests.nix
159
lib/tests.nix
|
@ -6,6 +6,9 @@ with import ./default.nix;
|
||||||
|
|
||||||
runTests {
|
runTests {
|
||||||
|
|
||||||
|
|
||||||
|
# TRIVIAL
|
||||||
|
|
||||||
testId = {
|
testId = {
|
||||||
expr = id 1;
|
expr = id 1;
|
||||||
expected = 1;
|
expected = 1;
|
||||||
|
@ -33,6 +36,18 @@ runTests {
|
||||||
expected = {a = "a";};
|
expected = {a = "a";};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
testConcatMapStrings = {
|
testConcatMapStrings = {
|
||||||
expr = concatMapStrings (x: x + ";") ["a" "b" "c"];
|
expr = concatMapStrings (x: x + ";") ["a" "b" "c"];
|
||||||
expected = "a;b;c;";
|
expected = "a;b;c;";
|
||||||
|
@ -43,6 +58,38 @@ runTests {
|
||||||
expected = "a,b,c";
|
expected = "a,b,c";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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" "" ];
|
||||||
|
};
|
||||||
|
|
||||||
|
# LISTS
|
||||||
|
|
||||||
testFilter = {
|
testFilter = {
|
||||||
expr = filter (x: x != "a") ["a" "b" "c" "a"];
|
expr = filter (x: x != "a") ["a" "b" "c" "a"];
|
||||||
expected = ["b" "c"];
|
expected = ["b" "c"];
|
||||||
|
@ -93,45 +140,6 @@ runTests {
|
||||||
expected = { a = [ 2 3 ]; b = [7]; c = [8];};
|
expected = { a = [ 2 3 ]; b = [7]; c = [8];};
|
||||||
};
|
};
|
||||||
|
|
||||||
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; };
|
|
||||||
in (x.merge) ( x: { a = add x.a 3; });
|
|
||||||
res6 = let x = defaultOverridableDelayableArgs id { a = 7; mergeAttrBy = { a = add; }; };
|
|
||||||
y = x.merge {};
|
|
||||||
in (y.merge) { a = 10; };
|
|
||||||
|
|
||||||
resRem7 = res6.replace (a: removeAttrs a ["a"]);
|
|
||||||
|
|
||||||
resReplace6 = let x = defaultOverridableDelayableArgs id { a = 7; mergeAttrBy = { a = add; }; };
|
|
||||||
x2 = x.merge { a = 20; }; # now we have 27
|
|
||||||
in (x2.replace) { a = 10; }; # and override the value by 10
|
|
||||||
|
|
||||||
# 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"; })
|
|
||||||
];
|
|
||||||
expected = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
testSort = {
|
testSort = {
|
||||||
expr = sort builtins.lessThan [ 40 2 30 42 ];
|
expr = sort builtins.lessThan [ 40 2 30 42 ];
|
||||||
expected = [2 30 40 42];
|
expected = [2 30 40 42];
|
||||||
|
@ -158,9 +166,9 @@ runTests {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/* Generator tests */
|
# GENERATORS
|
||||||
# these tests assume attributes are converted to lists
|
# these tests assume attributes are converted to lists
|
||||||
# in alphabetical order
|
# in alphabetical order
|
||||||
|
|
||||||
testMkKeyValueDefault = {
|
testMkKeyValueDefault = {
|
||||||
expr = generators.mkKeyValueDefault ":" "f:oo" "bar";
|
expr = generators.mkKeyValueDefault ":" "f:oo" "bar";
|
||||||
|
@ -247,43 +255,44 @@ runTests {
|
||||||
expected = builtins.toJSON val;
|
expected = builtins.toJSON val;
|
||||||
};
|
};
|
||||||
|
|
||||||
testSplitStringsSimple = {
|
# MISC
|
||||||
expr = strings.splitString "." "a.b.c.d";
|
|
||||||
expected = [ "a" "b" "c" "d" ];
|
|
||||||
};
|
|
||||||
|
|
||||||
testSplitStringsEmpty = {
|
testOverridableDelayableArgsTest = {
|
||||||
expr = strings.splitString "." "a..b";
|
expr =
|
||||||
expected = [ "a" "" "b" ];
|
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; };
|
||||||
|
in (x.merge) ( x: { a = add x.a 3; });
|
||||||
|
res6 = let x = defaultOverridableDelayableArgs id { a = 7; mergeAttrBy = { a = add; }; };
|
||||||
|
y = x.merge {};
|
||||||
|
in (y.merge) { a = 10; };
|
||||||
|
|
||||||
testSplitStringsOne = {
|
resRem7 = res6.replace (a: removeAttrs a ["a"]);
|
||||||
expr = strings.splitString ":" "a.b";
|
|
||||||
expected = [ "a.b" ];
|
|
||||||
};
|
|
||||||
|
|
||||||
testSplitStringsNone = {
|
resReplace6 = let x = defaultOverridableDelayableArgs id { a = 7; mergeAttrBy = { a = add; }; };
|
||||||
expr = strings.splitString "." "";
|
x2 = x.merge { a = 20; }; # now we have 27
|
||||||
expected = [ "" ];
|
in (x2.replace) { a = 10; }; # and override the value by 10
|
||||||
};
|
|
||||||
|
|
||||||
testSplitStringsFirstEmpty = {
|
# fixed tests (delayed args): (when using them add some comments, please)
|
||||||
expr = strings.splitString "/" "/a/b/c";
|
resFixed1 =
|
||||||
expected = [ "" "a" "b" "c" ];
|
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; };
|
||||||
testSplitStringsLastEmpty = {
|
strip = attrs: removeAttrs attrs ["merge" "replace"];
|
||||||
expr = strings.splitString ":" "2001:db8:0:0042::8a2e:370:";
|
in all id
|
||||||
expected = [ "2001" "db8" "0" "0042" "" "8a2e" "370" "" ];
|
[ ((strip res1) == { })
|
||||||
};
|
((strip res2) == { a = 7; })
|
||||||
|
((strip res3) == { a = 7; b = 10; })
|
||||||
testComposeExtensions = {
|
((strip res4) == { a = 7; b = 10; })
|
||||||
expr = let obj = makeExtensible (self: { foo = self.bar; });
|
((strip res5) == { a = 10; })
|
||||||
f = self: super: { bar = false; baz = true; };
|
((strip res6) == { a = 17; })
|
||||||
g = self: super: { bar = super.baz or false; };
|
((strip resRem7) == {})
|
||||||
f_o_g = composeExtensions f g;
|
((strip resFixed1) == { a = 7; b = 10; c =10; name = "name-10"; })
|
||||||
composed = obj.extend f_o_g;
|
];
|
||||||
in composed.foo;
|
|
||||||
expected = true;
|
expected = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -56,6 +56,8 @@ rec {
|
||||||
# nix-repl> fix f
|
# nix-repl> fix f
|
||||||
# { bar = "bar"; foo = "foo"; foobar = "foobar"; }
|
# { bar = "bar"; foo = "foo"; foobar = "foobar"; }
|
||||||
#
|
#
|
||||||
|
# Type: fix :: (a -> a) -> a
|
||||||
|
#
|
||||||
# See https://en.wikipedia.org/wiki/Fixed-point_combinator for further
|
# See https://en.wikipedia.org/wiki/Fixed-point_combinator for further
|
||||||
# details.
|
# details.
|
||||||
fix = f: let x = f x; in x;
|
fix = f: let x = f x; in x;
|
||||||
|
|
Loading…
Reference in a new issue