@plugjs/expect5 0.4.4 → 0.4.5

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 (90) hide show
  1. package/dist/cli.mjs +2 -2
  2. package/dist/cli.mjs.map +1 -1
  3. package/dist/execution/executor.cjs.map +1 -1
  4. package/dist/execution/executor.d.ts +1 -1
  5. package/dist/execution/executor.mjs +1 -1
  6. package/dist/execution/executor.mjs.map +1 -1
  7. package/dist/execution/setup.cjs.map +1 -1
  8. package/dist/execution/setup.d.ts +1 -1
  9. package/dist/execution/setup.mjs +1 -1
  10. package/dist/execution/setup.mjs.map +1 -1
  11. package/dist/expectation/async.cjs +58 -45
  12. package/dist/expectation/async.cjs.map +1 -1
  13. package/dist/expectation/async.d.ts +53 -52
  14. package/dist/expectation/async.mjs +59 -42
  15. package/dist/expectation/async.mjs.map +1 -1
  16. package/dist/expectation/diff.cjs.map +1 -1
  17. package/dist/expectation/diff.mjs +6 -1
  18. package/dist/expectation/diff.mjs.map +1 -1
  19. package/dist/expectation/expect.cjs +11 -165
  20. package/dist/expectation/expect.cjs.map +2 -2
  21. package/dist/expectation/expect.d.ts +10 -112
  22. package/dist/expectation/expect.mjs +12 -207
  23. package/dist/expectation/expect.mjs.map +2 -2
  24. package/dist/expectation/expectations.cjs +549 -0
  25. package/dist/expectation/expectations.cjs.map +6 -0
  26. package/dist/expectation/expectations.d.ts +454 -0
  27. package/dist/expectation/expectations.mjs +530 -0
  28. package/dist/expectation/expectations.mjs.map +6 -0
  29. package/dist/expectation/include.cjs +43 -41
  30. package/dist/expectation/include.cjs.map +1 -1
  31. package/dist/expectation/include.d.ts +3 -19
  32. package/dist/expectation/include.mjs +43 -41
  33. package/dist/expectation/include.mjs.map +1 -1
  34. package/dist/expectation/matchers.cjs +350 -0
  35. package/dist/expectation/matchers.cjs.map +6 -0
  36. package/dist/expectation/matchers.d.ts +375 -0
  37. package/dist/expectation/matchers.mjs +328 -0
  38. package/dist/expectation/matchers.mjs.map +6 -0
  39. package/dist/expectation/print.cjs.map +1 -1
  40. package/dist/expectation/print.d.ts +2 -2
  41. package/dist/expectation/print.mjs.map +1 -1
  42. package/dist/expectation/types.cjs +17 -23
  43. package/dist/expectation/types.cjs.map +1 -1
  44. package/dist/expectation/types.d.ts +7 -51
  45. package/dist/expectation/types.mjs +17 -22
  46. package/dist/expectation/types.mjs.map +1 -1
  47. package/dist/globals.d.ts +2 -2
  48. package/dist/index.cjs +5 -5
  49. package/dist/index.cjs.map +1 -1
  50. package/dist/index.d.ts +3 -4
  51. package/dist/index.mjs +11 -12
  52. package/dist/index.mjs.map +1 -1
  53. package/dist/test.cjs +34 -5
  54. package/dist/test.cjs.map +1 -1
  55. package/dist/test.d.ts +2 -2
  56. package/dist/test.mjs +35 -6
  57. package/dist/test.mjs.map +1 -1
  58. package/package.json +2 -2
  59. package/src/cli.mts +1 -1
  60. package/src/execution/executor.ts +1 -3
  61. package/src/execution/setup.ts +1 -3
  62. package/src/expectation/async.ts +116 -145
  63. package/src/expectation/diff.ts +7 -3
  64. package/src/expectation/expect.ts +22 -362
  65. package/src/expectation/expectations.ts +971 -0
  66. package/src/expectation/include.ts +59 -75
  67. package/src/expectation/matchers.ts +698 -0
  68. package/src/expectation/print.ts +8 -4
  69. package/src/expectation/types.ts +22 -94
  70. package/src/globals.ts +2 -2
  71. package/src/index.ts +17 -10
  72. package/src/test.ts +34 -10
  73. package/dist/expectation/basic.cjs +0 -188
  74. package/dist/expectation/basic.cjs.map +0 -6
  75. package/dist/expectation/basic.d.ts +0 -90
  76. package/dist/expectation/basic.mjs +0 -156
  77. package/dist/expectation/basic.mjs.map +0 -6
  78. package/dist/expectation/throwing.cjs +0 -58
  79. package/dist/expectation/throwing.cjs.map +0 -6
  80. package/dist/expectation/throwing.d.ts +0 -36
  81. package/dist/expectation/throwing.mjs +0 -32
  82. package/dist/expectation/throwing.mjs.map +0 -6
  83. package/dist/expectation/trivial.cjs +0 -96
  84. package/dist/expectation/trivial.cjs.map +0 -6
  85. package/dist/expectation/trivial.d.ts +0 -13
  86. package/dist/expectation/trivial.mjs +0 -61
  87. package/dist/expectation/trivial.mjs.map +0 -6
  88. package/src/expectation/basic.ts +0 -413
  89. package/src/expectation/throwing.ts +0 -106
  90. package/src/expectation/trivial.ts +0 -107
@@ -1,103 +1,77 @@
1
- import { diff } from './diff'
1
+ import { diff, type Diff } from './diff'
2
+ import { type Expectations } from './expectations'
2
3
  import {
3
4
  ExpectationError,
4
5
  stringifyObjectType,
5
6
  stringifyValue,
6
7
  } from './types'
7
8
 
8
- import type { Diff } from './diff'
9
- import type { Expectations } from './expect'
10
- import type { ExpectationsContext } from './types'
11
-
12
9
  /* === TO INCLUDE =========================================================== */
13
10
 
14
- /** Expect the value to include _all_ properties from the specified _object_. */
15
- function toInclude<T, P extends Record<string, any>>(this: T, properties: P): T
16
- /** Expect the value to include _all_ mappings from the specified {@link Map}. */
17
- function toInclude<T>(this: T, mappings: Map<any, any>): T
18
- /** Expect the value to include _all_ values from the specified {@link Set}. */
19
- function toInclude<T>(this: T, entries: Set<any>): T
20
- /** Expect the value to include _all_ values in any order from the specified _array_. */
21
- function toInclude<T>(this: T, values: any[]): T
22
-
23
- /* Overloaded function implementation */
24
- function toInclude(
25
- this: ExpectationsContext,
26
- expected:
11
+ export function toInclude(
12
+ expectations: Expectations,
13
+ negative: boolean,
14
+ contents:
27
15
  | Record<string, any>
28
16
  | Map<any, any>
29
17
  | Set<any>
30
18
  | any [],
31
- ): Expectations {
19
+ ): void {
32
20
  // get diff depending on type of "expected"
33
- if (expected instanceof Map) return includesMappings(this, expected)
34
- if (expected instanceof Set) return includesValues(this, expected)
35
- if (Array.isArray(expected)) return includesValues(this, new Set(expected))
36
- if (expected instanceof Object) return includesProps(this, expected)
37
- throw new TypeError(`Invalid type for "toInclude(...)": ${stringifyValue(expected)}`)
21
+ if (contents instanceof Map) return includesMappings(expectations, negative, contents)
22
+ if (contents instanceof Set) return includesValues(expectations, negative, contents)
23
+ if (Array.isArray(contents)) return includesValues(expectations, negative, new Set(contents))
24
+ if (contents instanceof Object) return includesProps(expectations, negative, contents)
25
+ throw new TypeError(`Invalid type for "toInclude(...)": ${stringifyValue(contents)}`)
38
26
  }
39
27
 
40
28
  /* === TO MATCH CONTENTS ==================================================== */
41
29
 
42
- /**
43
- * Expect the value to include _all_ values (and only those values, in any
44
- * order) from the specified _array_.
45
- */
46
- function toMatchContents<T>(this: T, contents: any[]): T
47
- /**
48
- * Expect the value to include _all_ values (and only those values, in any
49
- * order) from the specified {@link Set}.
50
- */
51
- function toMatchContents<T>(this: T, contents: Set<any>): T
52
-
53
- /* Overloaded function implementation */
54
- function toMatchContents(
55
- this: ExpectationsContext,
30
+ export function toMatchContents(
31
+ expectations: Expectations,
56
32
  contents: any[] | Set<any>,
57
- ): Expectations {
33
+ ): void {
58
34
  let actual: Set<any>
59
35
  let expected: Set<any>
60
36
  try {
61
- actual = new Set(this.value as any)
37
+ actual = new Set(expectations.value as any)
62
38
  expected = new Set(contents)
63
39
  } catch (error) {
64
- throw new ExpectationError(this, 'to be an iterable object', false)
40
+ throw new ExpectationError(expectations, 'to be an iterable object')
65
41
  }
66
42
 
67
43
  const result = diff(actual, expected)
68
44
  delete result.error // remove extra error message about size differences...
69
- if (result.diff === this.negative) return this.expects
70
- throw new ExpectationError(this,
71
- `to match contents of ${stringifyObjectType(contents)}`,
72
- { ...result, value: this.value })
73
- }
45
+ if (! result.diff) return
74
46
 
75
- /* === EXPORTS ============================================================== */
76
-
77
- export {
78
- toInclude,
79
- toMatchContents,
47
+ throw new ExpectationError(expectations,
48
+ `to match contents of ${stringifyObjectType(contents)}`,
49
+ { ...result, value: expectations.value })
80
50
  }
81
51
 
82
52
  /* ========================================================================== *
83
53
  * INTERNALS *
84
54
  * ========================================================================== */
85
55
 
86
- function includesProps(context: ExpectationsContext, expected: Record<string, any>): Expectations {
56
+ function includesProps(
57
+ expectations: Expectations,
58
+ negative: boolean,
59
+ expected: Record<string, any>,
60
+ ): void {
87
61
  // simple include for maps with objects...
88
- if (context.value instanceof Map) {
89
- return includesMappings(context, new Map(Object.entries(expected)))
62
+ if (expectations.value instanceof Map) {
63
+ return includesMappings(expectations, negative, new Map(Object.entries(expected)))
90
64
  }
91
65
 
92
66
  // we really need an object as actual
93
- context.expects.toBeInstanceOf(Object)
94
- const actual: Record<string, any> = context.value as any
67
+ expectations.toBeInstanceOf(Object)
68
+ const actual: Record<string, any> = expectations.value as any
95
69
 
96
70
  // get expected key set and process...
97
71
  const keys = new Set(Object.keys(expected))
98
72
  const props: Record<string, Diff> = {}
99
73
 
100
- if (context.negative) {
74
+ if (negative) {
101
75
  // only consider keys... if they exist, fail!
102
76
  for (const key of keys) {
103
77
  if ((actual[key] !== undefined) || (key in actual)) {
@@ -122,27 +96,32 @@ function includesProps(context: ExpectationsContext, expected: Record<string, an
122
96
  }
123
97
 
124
98
  const count = Object.keys(props).length
125
- if (count === 0) return context.expects // no props? no errors!
99
+ if (count === 0) return // no props? no errors!
126
100
 
127
101
  const type = count === 1 ? 'property' : 'properties'
128
- throw new ExpectationError(context, `to include ${count} ${type}`, {
102
+ const not = negative ? 'not ' : ''
103
+ throw new ExpectationError(expectations, `${not}to include ${count} ${type}`, {
129
104
  diff: true,
130
105
  value: actual,
131
106
  props,
132
107
  })
133
108
  }
134
109
 
135
- function includesValues(context: ExpectationsContext, expected: Set<any>): Expectations {
110
+ function includesValues(
111
+ expectations: Expectations,
112
+ negative: boolean,
113
+ expected: Set<any>,
114
+ ): void {
136
115
  // we really need an _iterable_ object as actual
137
- context.expects.toBeInstanceOf(Object)
138
- if (typeof (context.value as any)[Symbol.iterator] !== 'function') {
139
- throw new ExpectationError(context, 'to be an iterable object', false)
116
+ expectations.toBeInstanceOf(Object)
117
+ if (typeof (expectations.value as any)[Symbol.iterator] !== 'function') {
118
+ throw new ExpectationError(expectations, 'to be an iterable object')
140
119
  }
141
- const actual = new Set(context.value as Iterable<any>)
120
+ const actual = new Set(expectations.value as Iterable<any>)
142
121
 
143
122
  // iterate through all the values and see what we can find
144
123
  const values: Diff[] = []
145
- if (context.negative) {
124
+ if (negative) {
146
125
  for (const exp of expected) {
147
126
  for (const act of actual) {
148
127
  const result = diff(act, exp)
@@ -170,25 +149,29 @@ function includesValues(context: ExpectationsContext, expected: Set<any>): Expec
170
149
  }
171
150
 
172
151
  const count = values.length
173
- if (count === 0) return context.expects // no values? no errors!
152
+ if (count === 0) return // no values? no errors!
174
153
 
175
154
  const type = count === 1 ? 'value' : 'values'
176
- throw new ExpectationError(context, `to include ${count} ${type}`, {
155
+ const not = negative ? 'not ' : ''
156
+ throw new ExpectationError(expectations, `${not}to include ${count} ${type}`, {
177
157
  diff: true,
178
- value: context.value,
158
+ value: expectations.value,
179
159
  values,
180
160
  })
181
161
  }
182
162
 
183
- function includesMappings(context: ExpectationsContext, expected: Map<any, any>): Expectations {
184
- context.expects.toBeInstanceOf(Map)
185
- const actual: Map<any, any> = context.value as any
163
+ function includesMappings(
164
+ expectations: Expectations,
165
+ negative: boolean,
166
+ expected: Map<any, any>,
167
+ ): void {
168
+ const actual = expectations.toBeInstanceOf(Map).value
186
169
 
187
170
  // Get expected key set and process...
188
171
  const keys = new Set(expected.keys())
189
172
  const mappings: [ string, Diff ][] = []
190
173
 
191
- if (context.negative) {
174
+ if (negative) {
192
175
  // only consider keys... if they exist, fail!
193
176
  for (const key of keys) {
194
177
  if (actual.has(key)) {
@@ -207,12 +190,13 @@ function includesMappings(context: ExpectationsContext, expected: Map<any, any>)
207
190
  }
208
191
 
209
192
  const count = mappings.length
210
- if (count === 0) return context.expects // no mappings? no errors!
193
+ if (count === 0) return // no mappings? no errors!
211
194
 
212
195
  const type = count === 1 ? 'mapping' : 'mappings'
213
- throw new ExpectationError(context, `to include ${count} ${type}`, {
196
+ const not = negative ? 'not ' : ''
197
+ throw new ExpectationError(expectations, `${not}to include ${count} ${type}`, {
214
198
  diff: true,
215
- value: context.value,
199
+ value: expectations.value,
216
200
  mappings,
217
201
  })
218
202
  }