modules/test: allow specifying expectation value type

This commit is contained in:
Matt Sturgeon 2024-12-27 15:23:16 +00:00
parent c2e172b0d3
commit 24e3b11b23
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299

View file

@ -39,7 +39,14 @@ let
default = null; default = null;
}; };
value = lib.mkOption { value = lib.mkOption {
type = lib.types.anything; type =
if config.expect == null then
lib.types.unspecified
// {
description = "defined by `expect`";
}
else
namedPredicate.valueType;
description = '' description = ''
If defined, will be used together with `expect` to define `predicate`. If defined, will be used together with `expect` to define `predicate`.
''; '';
@ -137,6 +144,12 @@ in
Expectation message supplier, matching `(value) -> String` or `(value) -> [(message)] -> String`. Expectation message supplier, matching `(value) -> String` or `(value) -> [(message)] -> String`.
''; '';
}; };
valueType = lib.mkOption {
type = lib.types.optionType;
description = ''
The type to use for `value` when this expectation is used.
'';
};
}; };
}); });
description = '' description = ''
@ -196,14 +209,17 @@ in
count = { count = {
predicate = v: l: builtins.length l == v; predicate = v: l: builtins.length l == v;
message = v: l: "Expected length to be ${toString v} but found ${toString (builtins.length l)}."; message = v: l: "Expected length to be ${toString v} but found ${toString (builtins.length l)}.";
valueType = lib.types.ints.unsigned;
}; };
any = { any = {
predicate = v: builtins.any (lib.hasInfix v); predicate = v: builtins.any (lib.hasInfix v);
message = v: "Expected ${lib.toJSON v} infix to be present."; message = v: "Expected ${lib.toJSON v} infix to be present.";
valueType = lib.types.str;
}; };
anyExact = { anyExact = {
predicate = builtins.elem; predicate = builtins.elem;
message = v: "Expected ${lib.toJSON v} to be present."; message = v: "Expected ${lib.toJSON v} to be present.";
valueType = lib.types.str;
}; };
}; };
}; };