Skip to content

Commit

Permalink
lib.attrsWith: init eval tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjobeki committed Oct 2, 2024
1 parent dc45ca2 commit 0a2f174
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
38 changes: 38 additions & 0 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,44 @@ runTests {
expected = [ [ "_module" "args" ] [ "foo" ] [ "foo" "<name>" "bar" ] [ "foo" "bar" ] ];
};

testAttrsWithName = {
expr = let
eval = evalModules {
modules = [
{
options = {
foo = lib.mkOption {
type = lib.types.attrsWith {
name = "MyCustomPlaceholder";
elemType = lib.types.submodule {
options.bar = lib.mkOption {
type = lib.types.int;
default = 42;
};
};
};
};
};
}
];
};
opt = eval.options.foo;
in
(opt.type.getSubOptions opt.loc).bar.loc;
expected = [
"foo"
"<MyCustomPlaceholder>"
"bar"
];
};

testShowOptionWithPlaceholder = {
# <name>, *, should now be escaped. It is used as a placeholder by convention.
# Other symbols should be escaped. `{}`
expr = lib.showOption ["<name>" "<myName>" "*" "{foo}"];
expected = "<name>.<myName>.*.\"{foo}\"";
};

testCartesianProductOfEmptySet = {
expr = cartesianProduct {};
expected = [ {} ];
Expand Down
6 changes: 3 additions & 3 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ rec {
lazy ? false,
}:
let
typeName = "attrsOf";
typeName = if lazy then "lazyAttrsOf" else "attrsOf";
lazyMergeFn = loc: defs:
zipAttrsWith (name: defs:
let merged = mergeDefinitions (loc ++ [name]) elemType defs;
Expand All @@ -596,7 +596,7 @@ rec {

mergeFn = loc: defs:
mapAttrs (n: v: v.value) (filterAttrs (n: v: v ? value) (zipAttrsWith (name: defs:
(mergeDefinitions (loc ++ [name]) elemType defs).optionalValue
(mergeDefinitions (loc ++ [name]) elemType (defs)).optionalValue
)
# Push down position info.
(pushPositions defs)));
Expand All @@ -613,7 +613,7 @@ rec {
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<${name}>"]);
getSubModules = elemType.getSubModules;
substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit name lazy; };
functor = (defaultFunctor typeName) // { wrapped = elemType; };
functor = (defaultFunctor typeName) // { wrapped = elemType; type = t: attrsWith { elemType = t; inherit name lazy; }; };
nestedTypes.elemType = elemType;
};

Expand Down

0 comments on commit 0a2f174

Please sign in to comment.