Skip to content

Commit

Permalink
modules/test: provide access to expect function
Browse files Browse the repository at this point in the history
Allow `test.warnings` and `test.assertions` to be defined as either a
list, or a function coerced to a list.

When defined as a function, it is supplied an `expect` function which
provides some syntactic-sugar for defining simple expectations.

This is an alternative to the current approach of defining that `expect`
function on an ad-hoc basis.

I prefer this to adding `expect` to nixvim's lib because:
1. That would require having access to `lib`
2. IDK where in `lib` such a specialized function should live
  • Loading branch information
MattSturgeon committed Jan 17, 2025
1 parent 1666276 commit ff29c97
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
51 changes: 45 additions & 6 deletions modules/top-level/test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ let
};
}
);

expectationListType =
let
fnType = lib.mkOptionType {
name = "function";
description = "function";
descriptionClass = "noun";
check = builtins.isFunction;
};
# Supply the function with an `expect` function
coerceFn = fn: fn (expect: value: { inherit expect value; });
in
lib.types.coercedTo fnType coerceFn (lib.types.listOf expectationType);
in
{
options.test = {
Expand Down Expand Up @@ -116,9 +129,22 @@ in
};

warnings = lib.mkOption {
type = lib.types.listOf expectationType;
description = "A list of expectations for `warnings`.";
defaultText = lib.literalMD "Expect `count == 0`";
type = expectationListType;
description = ''
A list of expectations for `warnings`.
Function definitions will be supplied a `mkExpectation` function that
enables defining simple expectations less verbosely.
'';
example = lib.literalExpression ''
expect: [
(expect "count" 1)
(expect "any" "Hello, world!")
]
'';
defaultText = lib.literalExpression ''
expect: [ (expect "count" 0) ]
'';
default = [
{
expect = "count";
Expand All @@ -128,9 +154,22 @@ in
};

assertions = lib.mkOption {
type = lib.types.listOf expectationType;
description = "A list of expectations for `assertions`.";
defaultText = lib.literalMD "Expect `count == 0`";
type = expectationListType;
description = ''
A list of expectations for `warnings`.
Function definitions will be supplied a `mkExpectation` function that
enables defining simple expectations less verbosely.
'';
example = lib.literalExpression ''
expect: [
(expect "count" 1)
(expect "any" "Hello, world!")
]
'';
defaultText = lib.literalExpression ''
expect: [ (expect "count" 0) ]
'';
default = [
{
expect = "count";
Expand Down
4 changes: 1 addition & 3 deletions tests/test-sources/plugins/by-name/nvim-osc52/default.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
let
expect = expect: value: { inherit expect value; };

# This plugin is deprecated
warnings = [
warnings = expect: [
(expect "count" 1)
(expect "any" "this plugin is obsolete and will be removed after 24.11.")
];
Expand Down
4 changes: 1 addition & 3 deletions tests/test-sources/plugins/by-name/rust-tools/default.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
let
expect = expect: value: { inherit expect value; };

# This plugin is deprecated
warnings = [
warnings = expect: [
(expect "count" 1)
(expect "any" "The `rust-tools` project has been abandoned.")
];
Expand Down

0 comments on commit ff29c97

Please sign in to comment.