@gtkx/testing 1.0.0-rc.2 → 1.0.0-rc.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/README.md +4 -5
  2. package/dist/act.d.ts +2 -0
  3. package/dist/act.d.ts.map +1 -1
  4. package/dist/act.js.map +1 -1
  5. package/dist/bound-queries.d.ts +2 -3
  6. package/dist/bound-queries.d.ts.map +1 -1
  7. package/dist/bound-queries.js.map +1 -1
  8. package/dist/build-queries.d.ts.map +1 -1
  9. package/dist/build-queries.js +18 -7
  10. package/dist/build-queries.js.map +1 -1
  11. package/dist/config.d.ts +1 -0
  12. package/dist/config.d.ts.map +1 -1
  13. package/dist/config.js +3 -3
  14. package/dist/config.js.map +1 -1
  15. package/dist/errors.js +2 -2
  16. package/dist/errors.js.map +1 -1
  17. package/dist/index.d.ts +7 -4
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +4 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/matchers.d.ts +125 -21
  22. package/dist/matchers.d.ts.map +1 -1
  23. package/dist/matchers.js +258 -21
  24. package/dist/matchers.js.map +1 -1
  25. package/dist/normalize.d.ts +10 -0
  26. package/dist/normalize.d.ts.map +1 -0
  27. package/dist/normalize.js +20 -0
  28. package/dist/normalize.js.map +1 -0
  29. package/dist/pretty-widget.d.ts +2 -1
  30. package/dist/pretty-widget.d.ts.map +1 -1
  31. package/dist/pretty-widget.js +3 -3
  32. package/dist/pretty-widget.js.map +1 -1
  33. package/dist/queries.d.ts +4 -21
  34. package/dist/queries.d.ts.map +1 -1
  35. package/dist/queries.js +46 -59
  36. package/dist/queries.js.map +1 -1
  37. package/dist/render.js +3 -3
  38. package/dist/render.js.map +1 -1
  39. package/dist/suggestions.d.ts +3 -0
  40. package/dist/suggestions.d.ts.map +1 -1
  41. package/dist/suggestions.js.map +1 -1
  42. package/dist/traversal.d.ts +5 -0
  43. package/dist/traversal.d.ts.map +1 -1
  44. package/dist/traversal.js +1 -0
  45. package/dist/traversal.js.map +1 -1
  46. package/dist/types.d.ts +69 -5
  47. package/dist/types.d.ts.map +1 -1
  48. package/dist/types.js.map +1 -1
  49. package/dist/user-event/adjustment.d.ts +13 -0
  50. package/dist/user-event/adjustment.d.ts.map +1 -1
  51. package/dist/user-event/adjustment.js +11 -0
  52. package/dist/user-event/adjustment.js.map +1 -1
  53. package/dist/user-event/click.d.ts +7 -0
  54. package/dist/user-event/click.d.ts.map +1 -1
  55. package/dist/user-event/click.js +7 -0
  56. package/dist/user-event/click.js.map +1 -1
  57. package/dist/user-event/controller.d.ts +31 -1
  58. package/dist/user-event/controller.d.ts.map +1 -1
  59. package/dist/user-event/controller.js +36 -1
  60. package/dist/user-event/controller.js.map +1 -1
  61. package/dist/user-event/gesture.d.ts +51 -0
  62. package/dist/user-event/gesture.d.ts.map +1 -1
  63. package/dist/user-event/gesture.js +47 -4
  64. package/dist/user-event/gesture.js.map +1 -1
  65. package/dist/user-event/index.d.ts +27 -0
  66. package/dist/user-event/index.d.ts.map +1 -1
  67. package/dist/user-event/index.js.map +1 -1
  68. package/dist/user-event/keyboard.d.ts +4 -2
  69. package/dist/user-event/keyboard.d.ts.map +1 -1
  70. package/dist/user-event/keyboard.js +13 -12
  71. package/dist/user-event/keyboard.js.map +1 -1
  72. package/dist/user-event/pointer.js +5 -5
  73. package/dist/user-event/pointer.js.map +1 -1
  74. package/dist/user-event/selection.d.ts +15 -0
  75. package/dist/user-event/selection.d.ts.map +1 -1
  76. package/dist/user-event/selection.js +15 -0
  77. package/dist/user-event/selection.js.map +1 -1
  78. package/dist/user-event/state.d.ts +1 -1
  79. package/dist/user-event/state.d.ts.map +1 -1
  80. package/dist/user-event/state.js +1 -1
  81. package/dist/user-event/state.js.map +1 -1
  82. package/dist/user-event/text.d.ts +30 -3
  83. package/dist/user-event/text.d.ts.map +1 -1
  84. package/dist/user-event/text.js +28 -1
  85. package/dist/user-event/text.js.map +1 -1
  86. package/dist/wait-for.d.ts +2 -0
  87. package/dist/wait-for.d.ts.map +1 -1
  88. package/dist/wait-for.js.map +1 -1
  89. package/dist/widget-accessible-properties.d.ts +28 -2
  90. package/dist/widget-accessible-properties.d.ts.map +1 -1
  91. package/dist/widget-accessible-properties.js +129 -10
  92. package/dist/widget-accessible-properties.js.map +1 -1
  93. package/dist/within.js.map +1 -1
  94. package/package.json +7 -7
  95. package/src/act.ts +2 -0
  96. package/src/bound-queries.ts +2 -3
  97. package/src/build-queries.ts +20 -8
  98. package/src/config.ts +4 -3
  99. package/src/errors.ts +2 -2
  100. package/src/index.ts +11 -2
  101. package/src/matchers.ts +500 -53
  102. package/src/normalize.ts +28 -0
  103. package/src/pretty-widget.ts +5 -4
  104. package/src/queries.ts +79 -105
  105. package/src/render.tsx +3 -3
  106. package/src/suggestions.ts +3 -0
  107. package/src/traversal.ts +5 -0
  108. package/src/types.ts +88 -4
  109. package/src/user-event/adjustment.ts +13 -0
  110. package/src/user-event/click.ts +7 -0
  111. package/src/user-event/controller.ts +50 -1
  112. package/src/user-event/gesture.ts +57 -6
  113. package/src/user-event/index.ts +27 -0
  114. package/src/user-event/keyboard.ts +18 -16
  115. package/src/user-event/pointer.ts +5 -5
  116. package/src/user-event/selection.ts +15 -0
  117. package/src/user-event/state.ts +2 -2
  118. package/src/user-event/text.ts +31 -4
  119. package/src/wait-for.ts +2 -0
  120. package/src/widget-accessible-properties.ts +188 -10
  121. package/src/within.ts +1 -1
package/src/matchers.ts CHANGED
@@ -1,75 +1,185 @@
1
1
  /// <reference types="@vitest/expect" />
2
+ import * as GObject from "@gtkx/gi/gobject";
2
3
  import * as Gtk from "@gtkx/gi/gtk";
4
+ import { getDefaultNormalizer } from "./normalize.js";
5
+ import { formatRole } from "./role-helpers.js";
3
6
  import {
7
+ type CheckedState,
4
8
  getWidgetAccessibleName,
5
9
  getWidgetCheckedState,
10
+ getWidgetDescribedByText,
6
11
  getWidgetDisplayValue,
12
+ getWidgetErrorMessage,
7
13
  getWidgetExpandedState,
8
- getWidgetNodeText,
14
+ getWidgetInvalidState,
15
+ getWidgetLabelText,
9
16
  getWidgetPlaceholderText,
10
17
  getWidgetPressedState,
18
+ getWidgetRequiredState,
11
19
  getWidgetSelectedState,
20
+ getWidgetSelection,
21
+ getWidgetTextContent,
12
22
  getWidgetValue,
23
+ hasDisplayValue,
24
+ isWidgetDisabled,
25
+ isWidgetVisible,
13
26
  } from "./widget-accessible-properties.js";
14
27
 
15
28
  /** The expected value for a text matcher: an exact string or a regular expression. */
16
29
  type TextExpectation = string | RegExp;
30
+
31
+ /** Options controlling how `toHaveTextContent` normalizes the text it reads. */
32
+ type TextContentOptions = {
33
+ /**
34
+ * When true (the default), trim the text and collapse runs of whitespace into single spaces
35
+ * before comparing. When false, only replace non-breaking spaces with regular ones.
36
+ */
37
+ normalizeWhitespace?: boolean | undefined;
38
+ };
39
+
40
+ /** The expected value for a style class: an exact class name or a regular expression. */
41
+ type ClassExpectation = string | RegExp;
42
+ /** The outcome of a matcher: whether it passed, and the failure text built on demand. */
17
43
  type MatcherResult = { pass: boolean; message: () => string };
44
+ /** The matcher state bound as `this`, supplying the test runner's deep equality check. */
45
+ type MatcherContext = { equals: (actual: unknown, expected: unknown) => boolean };
46
+ /** Compares text read from the received widget; with no expected value, asserts the text is non-empty. */
18
47
  type TextMatcher = (received: unknown, expected?: TextExpectation) => MatcherResult;
48
+ /** Asserts a single state of the received widget, taking no expected value. */
19
49
  type StateMatcher = (received: unknown) => MatcherResult;
20
- type ValueMatcher = (received: unknown, expected: number) => MatcherResult;
21
50
  type TextMatcherContext = { matcherName: string; widget: Gtk.Widget; actual: string | null };
51
+ type ClassArguments = { expected: ClassExpectation[]; isExact: boolean };
52
+
53
+ /** A text matcher that also takes normalization options for the text it reads. */
54
+ type TextContentMatcher = (
55
+ received: unknown,
56
+ expected?: TextExpectation,
57
+ options?: TextContentOptions,
58
+ ) => MatcherResult;
22
59
 
60
+ /** Signatures of the widget assertion matchers, keyed by the name each is registered under. */
23
61
  type MatcherImplementations = {
62
+ /** Asserts the text an editable widget or combo box shows; with no argument, that it is not empty. */
24
63
  toHaveDisplayValue: TextMatcher;
25
- toHaveTextContent: TextMatcher;
64
+ /** Asserts the widget's own text, or its descendants' when it has none, contains or matches the expectation. */
65
+ toHaveTextContent: TextContentMatcher;
66
+ /** Asserts the widget's accessible name; with no argument, that it has one. */
26
67
  toHaveAccessibleName: TextMatcher;
68
+ /**
69
+ * Asserts the joined accessible names of the widget's `described-by` targets, falling back to its
70
+ * `description` and then to its tooltip.
71
+ */
72
+ toHaveAccessibleDescription: TextMatcher;
73
+ /** Asserts the joined accessible names of the widget's `error-message` targets, read only while it is invalid. */
74
+ toHaveAccessibleErrorMessage: TextMatcher;
75
+ /** Asserts the widget's placeholder text; with no argument, that it has some. */
27
76
  toHavePlaceholderText: TextMatcher;
77
+ /** Asserts the text currently selected in an editable widget. */
78
+ toHaveSelection: TextMatcher;
79
+ /** Asserts the widget's checked state is checked rather than unchecked or mixed. */
28
80
  toBeChecked: StateMatcher;
81
+ /** Asserts the widget's checked state is mixed. */
82
+ toBePartiallyChecked: StateMatcher;
83
+ /** Asserts a `Gtk.ToggleButton` is active, and throws for any other widget. */
29
84
  toBePressed: StateMatcher;
85
+ /** Asserts a `Gtk.Expander` or the row behind a `Gtk.TreeExpander` is expanded, and throws for anything else. */
30
86
  toBeExpanded: StateMatcher;
87
+ /**
88
+ * Asserts a `Gtk.ListBoxRow` or `Gtk.FlowBoxChild` is selected, or that a widget in a selectable role
89
+ * carries the selected state flag, and throws for anything else.
90
+ */
31
91
  toBeSelected: StateMatcher;
32
- toHaveValue: ValueMatcher;
92
+ /** Asserts the widget is insensitive, or sits inside an insensitive ancestor. */
93
+ toBeDisabled: StateMatcher;
94
+ /** Asserts the widget and all of its ancestors are sensitive. */
95
+ toBeEnabled: StateMatcher;
96
+ /** Asserts the widget and its ancestors are all visible, and that none of them is fully transparent. */
97
+ toBeVisible: StateMatcher;
98
+ /** Asserts the widget's root is a window that is still in the toplevel list. */
99
+ toBeRooted: StateMatcher;
100
+ /** Asserts the widget has neither a child nor label text. */
101
+ toBeEmpty: StateMatcher;
102
+ /** Asserts the widget's accessible invalid state is set to anything other than false. */
103
+ toBeInvalid: StateMatcher;
104
+ /** Asserts the widget's accessible invalid state is unset or false. */
105
+ toBeValid: StateMatcher;
106
+ /** Asserts the widget's accessible required state. */
107
+ toBeRequired: StateMatcher;
108
+ /** Asserts the widget holds the platform focus state. */
109
+ toHaveFocus: StateMatcher;
110
+ /** Asserts a widget's numeric value, or its display value when a string is given. */
111
+ toHaveValue: (received: unknown, expected?: number | string) => MatcherResult;
112
+ /** Asserts the widget's accessible role. */
113
+ toHaveRole: (received: unknown, expected: Gtk.AccessibleRole) => MatcherResult;
114
+ /** Asserts the widget is, or is an ancestor of, the given widget. */
115
+ toContainElement: (received: unknown, descendant: Gtk.Widget | null) => MatcherResult;
116
+ /**
117
+ * Asserts the widget carries every given style class, each a name, a space-separated list, or a pattern.
118
+ * Pass `{ exact: true }` last to require exactly that set, which rules out patterns.
119
+ */
120
+ toHaveClass: (received: unknown, ...args: unknown[]) => MatcherResult;
121
+ /**
122
+ * Asserts a GObject property, named as in GObject and looked up by its camel-cased accessor, equals the
123
+ * expected value. With no expected value, asserts the property is set. GObject values compare by identity.
124
+ */
125
+ toHaveObjectProperty: (this: MatcherContext, received: unknown, ...args: unknown[]) => MatcherResult;
33
126
  };
34
127
 
35
128
  type ExpectExtend = { extend: (m: MatcherImplementations) => void };
36
129
 
37
- const toHaveDisplayValue: TextMatcher = textMatcher("toHaveDisplayValue", getWidgetDisplayValue, "exact");
130
+ const registration = { isRegistered: false };
131
+ const displayValueMatcher: TextMatcher = textMatcher("toHaveDisplayValue", getWidgetDisplayValue, "exact");
132
+ const toHaveAccessibleName: TextMatcher = textMatcher("toHaveAccessibleName", getWidgetAccessibleName, "exact");
38
133
 
39
- const toHaveTextContent: TextMatcher = textMatcher(
40
- "toHaveTextContent",
41
- (widget) => getWidgetNodeText(widget) ?? getWidgetAccessibleName(widget),
42
- "substring",
134
+ const toHaveAccessibleDescription: TextMatcher = textMatcher(
135
+ "toHaveAccessibleDescription",
136
+ getWidgetDescribedByText,
137
+ "exact",
43
138
  );
44
139
 
45
- const toHaveAccessibleName: TextMatcher = textMatcher("toHaveAccessibleName", getWidgetAccessibleName, "exact");
46
-
47
- const toHavePlaceholderText: TextMatcher = textMatcher(
48
- "toHavePlaceholderText",
49
- getWidgetPlaceholderText,
140
+ const toHaveAccessibleErrorMessage: TextMatcher = textMatcher(
141
+ "toHaveAccessibleErrorMessage",
142
+ readErrorMessageText,
50
143
  "exact",
51
144
  );
52
145
 
53
- const toBeChecked: StateMatcher = booleanStateMatcher("toBeChecked", "checked", getWidgetCheckedState);
146
+ const toHavePlaceholderText: TextMatcher = textMatcher("toHavePlaceholderText", getWidgetPlaceholderText, "exact");
147
+ const toHaveSelection: TextMatcher = textMatcher("toHaveSelection", getWidgetSelection, "exact");
54
148
  const toBePressed: StateMatcher = booleanStateMatcher("toBePressed", "pressed", getWidgetPressedState);
55
149
  const toBeExpanded: StateMatcher = booleanStateMatcher("toBeExpanded", "expanded", getWidgetExpandedState);
56
150
  const toBeSelected: StateMatcher = booleanStateMatcher("toBeSelected", "selected", getWidgetSelectedState);
151
+ const toBeRequired: StateMatcher = booleanStateMatcher("toBeRequired", "required", getWidgetRequiredState);
57
152
 
58
153
  /** The widget assertion matchers keyed by name, suitable for passing to `expect.extend`. */
59
154
  const matchers: MatcherImplementations = {
60
155
  toHaveDisplayValue,
61
156
  toHaveTextContent,
62
157
  toHaveAccessibleName,
158
+ toHaveAccessibleDescription,
159
+ toHaveAccessibleErrorMessage,
63
160
  toHavePlaceholderText,
161
+ toHaveSelection,
64
162
  toBeChecked,
163
+ toBePartiallyChecked,
65
164
  toBePressed,
66
165
  toBeExpanded,
67
166
  toBeSelected,
167
+ toBeDisabled,
168
+ toBeEnabled,
169
+ toBeVisible,
170
+ toBeRooted,
171
+ toBeEmpty,
172
+ toBeInvalid,
173
+ toBeValid,
174
+ toBeRequired,
175
+ toHaveFocus,
68
176
  toHaveValue,
177
+ toHaveRole,
178
+ toContainElement,
179
+ toHaveClass,
180
+ toHaveObjectProperty,
69
181
  };
70
182
 
71
- const registration = { isRegistered: false };
72
-
73
183
  const asWidget = (received: unknown, matcherName: string): Gtk.Widget => {
74
184
  if (!(received instanceof Gtk.Widget)) {
75
185
  throw new TypeError(`${matcherName}: received value must be a Gtk.Widget, got ${typeof received}`);
@@ -78,6 +188,14 @@ const asWidget = (received: unknown, matcherName: string): Gtk.Widget => {
78
188
  return received;
79
189
  };
80
190
 
191
+ const asObject = (received: unknown, matcherName: string): GObject.Object => {
192
+ if (!(received instanceof GObject.Object)) {
193
+ throw new TypeError(`${matcherName}: received value must be a GObject, got ${typeof received}`);
194
+ }
195
+
196
+ return received;
197
+ };
198
+
81
199
  const describeWidget = (widget: Gtk.Widget): string => {
82
200
  const role = Gtk.AccessibleRole[widget.getAccessibleRole()];
83
201
  const name = getWidgetAccessibleName(widget);
@@ -85,6 +203,9 @@ const describeWidget = (widget: Gtk.Widget): string => {
85
203
  return name === null ? `<${role}>` : `<${role} name=${JSON.stringify(name)}>`;
86
204
  };
87
205
 
206
+ const describeObject = (object: GObject.Object): string =>
207
+ object instanceof Gtk.Widget ? describeWidget(object) : `<${object.constructor.name}>`;
208
+
88
209
  const isTextMatch = (actual: string, expected: TextExpectation, mode: "exact" | "substring"): boolean => {
89
210
  if (expected instanceof RegExp) {
90
211
  expected.lastIndex = 0;
@@ -118,6 +239,127 @@ const matchedResult = (context: TextMatcherContext, expected: TextExpectation, i
118
239
  `but received ${JSON.stringify(context.actual)}\n${describeWidget(context.widget)}`,
119
240
  });
120
241
 
242
+ const stateResult = (widget: Gtk.Widget, stateName: string, isPass: boolean): MatcherResult => ({
243
+ pass: isPass,
244
+ message: () => `expected widget ${negationPrefix(isPass)}to be ${stateName}\n${describeWidget(widget)}`,
245
+ });
246
+
247
+ const camelCase = (name: string): string =>
248
+ name.replaceAll(/-([a-z])/g, (_match, letter: string) => letter.toUpperCase());
249
+
250
+ const isAsymmetricMatcher = (value: unknown): boolean =>
251
+ typeof value === "object" && value !== null && typeof Reflect.get(value, "asymmetricMatch") === "function";
252
+
253
+ const isEqualValue = (context: MatcherContext, actual: unknown, expected: unknown): boolean => {
254
+ if (isAsymmetricMatcher(expected)) {
255
+ return context.equals(actual, expected);
256
+ }
257
+
258
+ if (actual instanceof GObject.Object || expected instanceof GObject.Object) {
259
+ return Object.is(actual, expected);
260
+ }
261
+
262
+ return context.equals(actual, expected);
263
+ };
264
+
265
+ const isClassMatch = (actual: string[], expected: ClassExpectation): boolean =>
266
+ expected instanceof RegExp ? actual.some((name) => expected.test(name)) : actual.includes(expected);
267
+
268
+ const isWidgetRooted = (widget: Gtk.Widget): boolean => {
269
+ const root = widget.getRoot();
270
+
271
+ if (root === null) {
272
+ return false;
273
+ }
274
+
275
+ if (root instanceof Gtk.Window) {
276
+ return Gtk.Window.listToplevels().includes(root);
277
+ }
278
+
279
+ return true;
280
+ };
281
+
282
+ const globalExpect = (): ExpectExtend | null => {
283
+ const candidate: unknown = Reflect.get(globalThis, "expect");
284
+
285
+ if (candidate && typeof (candidate as ExpectExtend).extend === "function") {
286
+ return candidate as ExpectExtend;
287
+ }
288
+
289
+ return null;
290
+ };
291
+
292
+ function readErrorMessageText(widget: Gtk.Widget): string | null {
293
+ const state = getWidgetInvalidState(widget);
294
+
295
+ if (state === null || state === Gtk.AccessibleInvalidState.FALSE) {
296
+ return null;
297
+ }
298
+
299
+ const targets = getWidgetErrorMessage(widget);
300
+
301
+ if (targets === null) {
302
+ return null;
303
+ }
304
+
305
+ const texts = targets.map((target) => getWidgetAccessibleName(target)).filter((text) => text !== null);
306
+
307
+ return texts.length > 0 ? texts.join(" ") : null;
308
+ }
309
+
310
+ function assertReadableProperty(object: GObject.Object, name: string, property: string, value: unknown): void {
311
+ if (typeof value === "function") {
312
+ throw new TypeError(
313
+ `toHaveObjectProperty: "${name}" is shadowed by a method of the same name; ` +
314
+ `call ${property}() and assert on its result instead`,
315
+ );
316
+ }
317
+
318
+ if (value === undefined && !Reflect.has(object, property)) {
319
+ throw new TypeError(
320
+ `toHaveObjectProperty: no readable property "${name}" on ${describeObject(object)}; ` +
321
+ "construct-only and write-only properties cannot be read back",
322
+ );
323
+ }
324
+ }
325
+
326
+ function readObjectProperty(object: GObject.Object, name: string): unknown {
327
+ const property = camelCase(name);
328
+ const value: unknown = Reflect.get(object, property);
329
+ assertReadableProperty(object, name, property, value);
330
+
331
+ return value;
332
+ }
333
+
334
+ function classEntries(entry: unknown): ClassExpectation[] {
335
+ return entry instanceof RegExp ? [entry] : String(entry).split(/\s+/).filter(Boolean);
336
+ }
337
+
338
+ function parseClassArguments(args: unknown[]): ClassArguments {
339
+ const last = args.at(-1);
340
+ const isOptions = typeof last === "object" && last !== null && !(last instanceof RegExp);
341
+ const isExact = isOptions && Reflect.get(last, "exact") === true;
342
+ const entries = isOptions ? args.slice(0, -1) : args;
343
+
344
+ return { expected: entries.flatMap((entry) => classEntries(entry)), isExact };
345
+ }
346
+
347
+ function exactClassResult(widget: Gtk.Widget, actual: string[], expected: ClassExpectation[]): MatcherResult {
348
+ if (expected.some((entry) => entry instanceof RegExp)) {
349
+ throw new TypeError("toHaveClass: the exact option cannot be combined with a regular expression");
350
+ }
351
+
352
+ const names = expected.map(String);
353
+ const isPass = names.length === actual.length && names.every((name) => actual.includes(name));
354
+
355
+ return {
356
+ pass: isPass,
357
+ message: () =>
358
+ `expected widget ${negationPrefix(isPass)}to have exactly the classes ${JSON.stringify(names)}, ` +
359
+ `but received ${JSON.stringify(actual)}\n${describeWidget(widget)}`,
360
+ };
361
+ }
362
+
121
363
  function textMatcher(
122
364
  matcherName: string,
123
365
  read: (widget: Gtk.Widget) => string | null,
@@ -136,6 +378,37 @@ function textMatcher(
136
378
  };
137
379
  }
138
380
 
381
+ function normalizeTextContent(text: string, options?: TextContentOptions): string {
382
+ if (options?.normalizeWhitespace === false) {
383
+ return text.replaceAll("\u{A0}", " ");
384
+ }
385
+
386
+ return getDefaultNormalizer()(text);
387
+ }
388
+
389
+ function readTextContent(widget: Gtk.Widget, options?: TextContentOptions): string | null {
390
+ const text = getWidgetTextContent(widget);
391
+
392
+ return text === null ? null : normalizeTextContent(text, options);
393
+ }
394
+
395
+ function toHaveTextContent(
396
+ received: unknown,
397
+ expected?: TextExpectation,
398
+ options?: TextContentOptions,
399
+ ): MatcherResult {
400
+ const read = (widget: Gtk.Widget): string | null => readTextContent(widget, options);
401
+
402
+ return textMatcher("toHaveTextContent", read, "substring")(received, expected);
403
+ }
404
+
405
+ function notApplicable(matcherName: string, stateName: string, widget: Gtk.Widget): Error {
406
+ return new Error(
407
+ `${matcherName}: widget does not expose a ${stateName} ` +
408
+ `(role ${Gtk.AccessibleRole[widget.getAccessibleRole()]})\n${describeWidget(widget)}`,
409
+ );
410
+ }
411
+
139
412
  function booleanStateMatcher(
140
413
  matcherName: string,
141
414
  stateName: string,
@@ -146,32 +419,59 @@ function booleanStateMatcher(
146
419
  const state = read(widget);
147
420
 
148
421
  if (state === null) {
149
- throw new Error(
150
- `${matcherName}: widget does not expose a ${stateName} state ` +
151
- `(role ${Gtk.AccessibleRole[widget.getAccessibleRole()]})\n${describeWidget(widget)}`,
152
- );
422
+ throw notApplicable(matcherName, `${stateName} state`, widget);
153
423
  }
154
424
 
155
- return {
156
- pass: state,
157
- message: () =>
158
- `expected widget ${negationPrefix(state)}to be ${stateName}\n${describeWidget(widget)}`,
159
- };
425
+ return stateResult(widget, stateName, state);
160
426
  };
161
427
  }
162
428
 
163
- function toHaveValue(received: unknown, expected: number): MatcherResult {
429
+ function readCheckedState(widget: Gtk.Widget, matcherName: string): CheckedState {
430
+ const state = getWidgetCheckedState(widget);
431
+
432
+ if (state === null) {
433
+ throw notApplicable(matcherName, "checked state", widget);
434
+ }
435
+
436
+ return state;
437
+ }
438
+
439
+ function toBeChecked(received: unknown): MatcherResult {
440
+ const widget = asWidget(received, "toBeChecked");
441
+
442
+ return stateResult(widget, "checked", readCheckedState(widget, "toBeChecked") === "checked");
443
+ }
444
+
445
+ function toBePartiallyChecked(received: unknown): MatcherResult {
446
+ const widget = asWidget(received, "toBePartiallyChecked");
447
+
448
+ return stateResult(widget, "partially checked", readCheckedState(widget, "toBePartiallyChecked") === "mixed");
449
+ }
450
+
451
+ function toHaveDisplayValue(received: unknown, expected?: TextExpectation): MatcherResult {
452
+ const widget = asWidget(received, "toHaveDisplayValue");
453
+
454
+ if (!hasDisplayValue(widget)) {
455
+ throw notApplicable("toHaveDisplayValue", "display value", widget);
456
+ }
457
+
458
+ return displayValueMatcher(received, expected);
459
+ }
460
+
461
+ function toHaveValue(received: unknown, expected?: number | string): MatcherResult {
164
462
  const widget = asWidget(received, "toHaveValue");
463
+
464
+ if (typeof expected === "string") {
465
+ return displayValueMatcher(received, expected);
466
+ }
467
+
165
468
  const actual = getWidgetValue(widget).now;
166
469
 
167
470
  if (actual === null) {
168
- throw new Error(
169
- "toHaveValue: widget does not expose a numeric value " +
170
- `(role ${Gtk.AccessibleRole[widget.getAccessibleRole()]})\n${describeWidget(widget)}`,
171
- );
471
+ throw notApplicable("toHaveValue", "numeric value", widget);
172
472
  }
173
473
 
174
- const isPass = actual === expected;
474
+ const isPass = expected === undefined || actual === expected;
175
475
 
176
476
  return {
177
477
  pass: isPass,
@@ -181,15 +481,135 @@ function toHaveValue(received: unknown, expected: number): MatcherResult {
181
481
  };
182
482
  }
183
483
 
184
- const globalExpect = (): ExpectExtend | null => {
185
- const candidate: unknown = Reflect.get(globalThis, "expect");
484
+ function toBeDisabled(received: unknown): MatcherResult {
485
+ const widget = asWidget(received, "toBeDisabled");
186
486
 
187
- if (candidate && typeof (candidate as ExpectExtend).extend === "function") {
188
- return candidate as ExpectExtend;
487
+ return stateResult(widget, "disabled", isWidgetDisabled(widget));
488
+ }
489
+
490
+ function toBeEnabled(received: unknown): MatcherResult {
491
+ const widget = asWidget(received, "toBeEnabled");
492
+
493
+ return stateResult(widget, "enabled", !isWidgetDisabled(widget));
494
+ }
495
+
496
+ function toBeVisible(received: unknown): MatcherResult {
497
+ const widget = asWidget(received, "toBeVisible");
498
+
499
+ return stateResult(widget, "visible", isWidgetVisible(widget));
500
+ }
501
+
502
+ function toBeRooted(received: unknown): MatcherResult {
503
+ if (received === null) {
504
+ return { pass: false, message: () => "expected a widget to be rooted in a window, but received null" };
189
505
  }
190
506
 
191
- return null;
192
- };
507
+ const widget = asWidget(received, "toBeRooted");
508
+
509
+ return stateResult(widget, "rooted in a window", isWidgetRooted(widget));
510
+ }
511
+
512
+ function toBeEmpty(received: unknown): MatcherResult {
513
+ const widget = asWidget(received, "toBeEmpty");
514
+
515
+ return stateResult(widget, "empty", widget.getFirstChild() === null && getWidgetLabelText(widget) === null);
516
+ }
517
+
518
+ function toBeInvalid(received: unknown): MatcherResult {
519
+ const widget = asWidget(received, "toBeInvalid");
520
+ const state = getWidgetInvalidState(widget);
521
+
522
+ return stateResult(widget, "invalid", state !== null && state !== Gtk.AccessibleInvalidState.FALSE);
523
+ }
524
+
525
+ function toBeValid(received: unknown): MatcherResult {
526
+ const widget = asWidget(received, "toBeValid");
527
+ const state = getWidgetInvalidState(widget);
528
+
529
+ return stateResult(widget, "valid", state === null || state === Gtk.AccessibleInvalidState.FALSE);
530
+ }
531
+
532
+ function toHaveFocus(received: unknown): MatcherResult {
533
+ const widget = asWidget(received, "toHaveFocus");
534
+
535
+ return stateResult(widget, "focused", widget.getPlatformState(Gtk.AccessiblePlatformState.FOCUSED));
536
+ }
537
+
538
+ function toHaveRole(received: unknown, expected: Gtk.AccessibleRole): MatcherResult {
539
+ const widget = asWidget(received, "toHaveRole");
540
+ const actual = widget.getAccessibleRole();
541
+ const isPass = actual === expected;
542
+
543
+ return {
544
+ pass: isPass,
545
+ message: () =>
546
+ `expected widget ${negationPrefix(isPass)}to have role '${formatRole(expected)}', ` +
547
+ `but received '${formatRole(actual)}'\n${describeWidget(widget)}`,
548
+ };
549
+ }
550
+
551
+ function toContainElement(received: unknown, descendant: Gtk.Widget | null): MatcherResult {
552
+ const widget = asWidget(received, "toContainElement");
553
+ const isPass = descendant !== null && (descendant === widget || descendant.isAncestor(widget));
554
+
555
+ return {
556
+ pass: isPass,
557
+ message: () =>
558
+ `expected widget ${negationPrefix(isPass)}to contain ` +
559
+ `${descendant === null ? "null" : describeWidget(descendant)}\n${describeWidget(widget)}`,
560
+ };
561
+ }
562
+
563
+ function toHaveClass(received: unknown, ...args: unknown[]): MatcherResult {
564
+ const widget = asWidget(received, "toHaveClass");
565
+ const actual = widget.getCssClasses();
566
+ const { expected, isExact } = parseClassArguments(args);
567
+
568
+ if (isExact) {
569
+ return exactClassResult(widget, actual, expected);
570
+ }
571
+
572
+ if (expected.length === 0) {
573
+ return stateResult(widget, "given any style class", actual.length > 0);
574
+ }
575
+
576
+ const isPass = expected.every((entry) => isClassMatch(actual, entry));
577
+
578
+ return {
579
+ pass: isPass,
580
+ message: () =>
581
+ `expected widget ${negationPrefix(isPass)}to have class ${expected.map(String).join(", ")}, ` +
582
+ `but received ${JSON.stringify(actual)}\n${describeWidget(widget)}`,
583
+ };
584
+ }
585
+
586
+ function toHaveObjectProperty(this: MatcherContext, received: unknown, ...args: unknown[]): MatcherResult {
587
+ const object = asObject(received, "toHaveObjectProperty");
588
+ const [name, expected] = args;
589
+ const actual = readObjectProperty(object, String(name));
590
+
591
+ if (args.length < 2) {
592
+ const isSet = actual !== null && actual !== undefined;
593
+
594
+ return {
595
+ pass: isSet,
596
+ message: () =>
597
+ `expected ${describeObject(object)} ${negationPrefix(isSet)}to have a value for property ` +
598
+ `"${String(name)}", but received ${JSON.stringify(actual)}`,
599
+ };
600
+ }
601
+
602
+ /* eslint-disable-next-line unicorn/no-this-outside-of-class --
603
+ expect.extend invokes matchers with the matcher state as `this` */
604
+ const isPass = isEqualValue(this, actual, expected);
605
+
606
+ return {
607
+ pass: isPass,
608
+ message: () =>
609
+ `expected ${describeObject(object)} ${negationPrefix(isPass)}to have property "${String(name)}" ` +
610
+ `equal to ${JSON.stringify(expected)}, but received ${JSON.stringify(actual)}`,
611
+ };
612
+ }
193
613
 
194
614
  /** Registers the widget matchers on the global `expect`, when one is available. Safe to call more than once. */
195
615
  const registerMatchers = (): void => {
@@ -209,29 +629,37 @@ const registerMatchers = (): void => {
209
629
 
210
630
  declare module "@vitest/expect" {
211
631
  /* eslint-disable @typescript-eslint/consistent-type-definitions -- declaration merging requires interfaces */
212
- interface Assertion {
632
+ interface WidgetMatchers {
213
633
  toHaveDisplayValue(expected?: TextExpectation): void;
214
- toHaveTextContent(expected?: TextExpectation): void;
634
+ toHaveTextContent(expected?: TextExpectation, options?: TextContentOptions): void;
215
635
  toHaveAccessibleName(expected?: TextExpectation): void;
636
+ toHaveAccessibleDescription(expected?: TextExpectation): void;
637
+ toHaveAccessibleErrorMessage(expected?: TextExpectation): void;
216
638
  toHavePlaceholderText(expected?: TextExpectation): void;
639
+ toHaveSelection(expected?: TextExpectation): void;
217
640
  toBeChecked(): void;
641
+ toBePartiallyChecked(): void;
218
642
  toBePressed(): void;
219
643
  toBeExpanded(): void;
220
644
  toBeSelected(): void;
221
- toHaveValue(expected: number): void;
645
+ toBeDisabled(): void;
646
+ toBeEnabled(): void;
647
+ toBeVisible(): void;
648
+ toBeRooted(): void;
649
+ toBeEmpty(): void;
650
+ toBeInvalid(): void;
651
+ toBeValid(): void;
652
+ toBeRequired(): void;
653
+ toHaveFocus(): void;
654
+ toHaveValue(expected?: number | string): void;
655
+ toHaveRole(expected: Gtk.AccessibleRole): void;
656
+ toContainElement(descendant: Gtk.Widget | null): void;
657
+ toHaveClass(...args: (ClassExpectation | { exact: boolean })[]): void;
658
+ toHaveObjectProperty(name: string, expected?: unknown): void;
222
659
  }
223
660
 
224
- interface AsymmetricMatchersContaining {
225
- toHaveDisplayValue(expected?: TextExpectation): void;
226
- toHaveTextContent(expected?: TextExpectation): void;
227
- toHaveAccessibleName(expected?: TextExpectation): void;
228
- toHavePlaceholderText(expected?: TextExpectation): void;
229
- toBeChecked(): void;
230
- toBePressed(): void;
231
- toBeExpanded(): void;
232
- toBeSelected(): void;
233
- toHaveValue(expected: number): void;
234
- }
661
+ interface Assertion extends WidgetMatchers {}
662
+ interface AsymmetricMatchersContaining extends WidgetMatchers {}
235
663
  /* eslint-enable @typescript-eslint/consistent-type-definitions */
236
664
  }
237
665
 
@@ -239,13 +667,32 @@ export {
239
667
  toHaveDisplayValue,
240
668
  toHaveTextContent,
241
669
  toHaveAccessibleName,
670
+ toHaveAccessibleDescription,
671
+ toHaveAccessibleErrorMessage,
242
672
  toHavePlaceholderText,
673
+ toHaveSelection,
243
674
  toBeChecked,
675
+ toBePartiallyChecked,
244
676
  toBePressed,
245
677
  toBeExpanded,
246
678
  toBeSelected,
247
- matchers,
679
+ toBeDisabled,
680
+ toBeEnabled,
681
+ toBeVisible,
682
+ toBeRooted,
683
+ toBeEmpty,
684
+ toBeInvalid,
685
+ toBeValid,
686
+ toBeRequired,
687
+ toHaveFocus,
248
688
  toHaveValue,
689
+ toHaveRole,
690
+ toContainElement,
691
+ toHaveClass,
692
+ toHaveObjectProperty,
693
+ matchers,
249
694
  registerMatchers,
695
+ type ClassExpectation,
696
+ type TextContentOptions,
250
697
  type TextExpectation,
251
698
  };