Skip to content

Commit 148bd62

Browse files
MattSturgeonnix-infra-bot
authored andcommitted
modules/test: provide access to expect function
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
1 parent 1666276 commit 148bd62

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

modules/top-level/test.nix

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ let
7676
};
7777
}
7878
);
79+
80+
expectationListType =
81+
let
82+
fnType = lib.mkOptionType {
83+
name = "function";
84+
description = "function";
85+
descriptionClass = "noun";
86+
check = builtins.isFunction;
87+
};
88+
# Supply the function with an `expect` function
89+
coerceFn = fn: fn (expect: value: { inherit expect value; });
90+
in
91+
lib.types.coercedTo fnType coerceFn (lib.types.listOf expectationType);
7992
in
8093
{
8194
options.test = {
@@ -116,9 +129,22 @@ in
116129
};
117130

118131
warnings = lib.mkOption {
119-
type = lib.types.listOf expectationType;
120-
description = "A list of expectations for `warnings`.";
121-
defaultText = lib.literalMD "Expect `count == 0`";
132+
type = expectationListType;
133+
description = ''
134+
A list of expectations for `warnings`.
135+
136+
Function definitions will be supplied a `mkExpectation` function that
137+
enables defining simple expectations less verbosely.
138+
'';
139+
example = lib.literalExpression ''
140+
expect: [
141+
(expect "count" 1)
142+
(expect "any" "Hello, world!")
143+
]
144+
'';
145+
defaultText = lib.literalExpression ''
146+
expect: [ (expect "count" 0) ]
147+
'';
122148
default = [
123149
{
124150
expect = "count";
@@ -128,9 +154,22 @@ in
128154
};
129155

130156
assertions = lib.mkOption {
131-
type = lib.types.listOf expectationType;
132-
description = "A list of expectations for `assertions`.";
133-
defaultText = lib.literalMD "Expect `count == 0`";
157+
type = expectationListType;
158+
description = ''
159+
A list of expectations for `warnings`.
160+
161+
Function definitions will be supplied a `mkExpectation` function that
162+
enables defining simple expectations less verbosely.
163+
'';
164+
example = lib.literalExpression ''
165+
expect: [
166+
(expect "count" 1)
167+
(expect "any" "Hello, world!")
168+
]
169+
'';
170+
defaultText = lib.literalExpression ''
171+
expect: [ (expect "count" 0) ]
172+
'';
134173
default = [
135174
{
136175
expect = "count";

tests/test-sources/plugins/by-name/nvim-osc52/default.nix

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
let
2-
expect = expect: value: { inherit expect value; };
3-
42
# This plugin is deprecated
5-
warnings = [
3+
warnings = expect: [
64
(expect "count" 1)
75
(expect "any" "this plugin is obsolete and will be removed after 24.11.")
86
];

tests/test-sources/plugins/by-name/rust-tools/default.nix

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
let
2-
expect = expect: value: { inherit expect value; };
3-
42
# This plugin is deprecated
5-
warnings = [
3+
warnings = expect: [
64
(expect "count" 1)
75
(expect "any" "The `rust-tools` project has been abandoned.")
86
];

0 commit comments

Comments
 (0)