1let inherit (builtins) add; in
2with import ./default.nix;
3
4runTests {
5
6 testId = {
7 expr = id 1;
8 expected = 1;
9 };
10
11 testConst = {
12 expr = const 2 3;
13 expected = 2;
14 };
15
16 /*
17 testOr = {
18 expr = or true false;
19 expected = true;
20 };
21 */
22
23 testAnd = {
24 expr = and true false;
25 expected = false;
26 };
27
28 testFix = {
29 expr = fix (x: {a = if x ? a then "a" else "b";});
30 expected = {a = "a";};
31 };
32
33 testConcatMapStrings = {
34 expr = concatMapStrings (x: x + ";") ["a" "b" "c"];
35 expected = "a;b;c;";
36 };
37
38 testConcatStringsSep = {
39 expr = concatStringsSep "," ["a" "b" "c"];
40 expected = "a,b,c";
41 };
42
43 testFilter = {
44 expr = filter (x: x != "a") ["a" "b" "c" "a"];
45 expected = ["b" "c"];
46 };
47
48 testFold = {
49 expr = fold (builtins.add) 0 (range 0 100);
50 expected = 5050;
51 };
52
53 testTake = testAllTrue [
54 ([] == (take 0 [ 1 2 3 ]))
55 ([1] == (take 1 [ 1 2 3 ]))
56 ([ 1 2 ] == (take 2 [ 1 2 3 ]))
57 ([ 1 2 3 ] == (take 3 [ 1 2 3 ]))
58 ([ 1 2 3 ] == (take 4 [ 1 2 3 ]))
59 ];
60
61 testFoldAttrs = {
62 expr = foldAttrs (n: a: [n] ++ a) [] [
63 { a = 2; b = 7; }
64 { a = 3; c = 8; }
65 ];
66 expected = { a = [ 2 3 ]; b = [7]; c = [8];};
67 };
68
69 testOverridableDelayableArgsTest = {
70 expr =
71 let res1 = defaultOverridableDelayableArgs id {};
72 res2 = defaultOverridableDelayableArgs id { a = 7; };
73 res3 = let x = defaultOverridableDelayableArgs id { a = 7; };
74 in (x.merge) { b = 10; };
75 res4 = let x = defaultOverridableDelayableArgs id { a = 7; };
76 in (x.merge) ( x: { b = 10; });
77 res5 = let x = defaultOverridableDelayableArgs id { a = 7; };
78 in (x.merge) ( x: { a = add x.a 3; });
79 res6 = let x = defaultOverridableDelayableArgs id { a = 7; mergeAttrBy = { a = add; }; };
80 y = x.merge {};
81 in (y.merge) { a = 10; };
82
83 resRem7 = res6.replace (a : removeAttrs a ["a"]);
84
85 resReplace6 = let x = defaultOverridableDelayableArgs id { a = 7; mergeAttrBy = { a = add; }; };
86 x2 = x.merge { a = 20; }; # now we have 27
87 in (x2.replace) { a = 10; }; # and override the value by 10
88
89 # fixed tests (delayed args): (when using them add some comments, please)
90 resFixed1 =
91 let x = defaultOverridableDelayableArgs id ( x : { a = 7; c = x.fixed.b; });
92 y = x.merge (x : { name = "name-${builtins.toString x.fixed.c}"; });
93 in (y.merge) { b = 10; };
94 strip = attrs : removeAttrs attrs ["merge" "replace"];
95 in all id
96 [ ((strip res1) == { })
97 ((strip res2) == { a = 7; })
98 ((strip res3) == { a = 7; b = 10; })
99 ((strip res4) == { a = 7; b = 10; })
100 ((strip res5) == { a = 10; })
101 ((strip res6) == { a = 17; })
102 ((strip resRem7) == {})
103 ((strip resFixed1) == { a = 7; b = 10; c =10; name = "name-10"; })
104 ];
105 expected = true;
106 };
107
108 testSort = {
109 expr = sort builtins.lessThan [ 40 2 30 42 ];
110 expected = [2 30 40 42];
111 };
112
113 testToIntShouldConvertStringToInt = {
114 expr = toInt "27";
115 expected = 27;
116 };
117
118 testToIntShouldThrowErrorIfItCouldNotConvertToInt = {
119 expr = builtins.tryEval (toInt "\"foo\"");
120 expected = { success = false; value = false; };
121 };
122
123 testHasAttrByPathTrue = {
124 expr = hasAttrByPath ["a" "b"] { a = { b = "yey"; }; };
125 expected = true;
126 };
127
128 testHasAttrByPathFalse = {
129 expr = hasAttrByPath ["a" "b"] { a = { c = "yey"; }; };
130 expected = false;
131 };
132
133}