vitest-pool-assemblyscript 0.7.0 → 0.8.0
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.
- package/assembly/compare.ts +35 -0
- package/assembly/expect.ts +59 -17
- package/package.json +1 -1
package/assembly/compare.ts
CHANGED
|
@@ -32,6 +32,38 @@ function setEquals<T, U>(actual: T, expected: U): bool {
|
|
|
32
32
|
return false;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
function arrayBufferEquals<T, U>(actual: T, expected: U): bool {
|
|
36
|
+
if (!(actual instanceof ArrayBuffer) || !(expected instanceof ArrayBuffer)) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (actual.byteLength != expected.byteLength) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const actualPtr = changetype<usize>(actual);
|
|
45
|
+
const expectedPtr = changetype<usize>(expected);
|
|
46
|
+
const wordCount: usize = actual.byteLength / 8;
|
|
47
|
+
const remainder: usize = actual.byteLength % 8;
|
|
48
|
+
|
|
49
|
+
// compare 8 bytes at a time (u64 word-sized comparison)
|
|
50
|
+
for (let i: usize = 0; i < wordCount; i++) {
|
|
51
|
+
if (load<u64>(actualPtr + i * 8) != load<u64>(expectedPtr + i * 8)) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// compare remaining 0-7 bytes individually
|
|
57
|
+
const remainderOffset = wordCount * 8;
|
|
58
|
+
for (let i: usize = 0; i < remainder; i++) {
|
|
59
|
+
if (load<u8>(actualPtr + remainderOffset + i) != load<u8>(expectedPtr + remainderOffset + i)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
35
67
|
function mapEquals<T, U>(actual: T, expected: U): bool {
|
|
36
68
|
if (actual instanceof Map && expected instanceof Map) {
|
|
37
69
|
if (actual.size != expected.size) {
|
|
@@ -222,6 +254,9 @@ export function equals<T, U>(actual: T, expected: U): bool {
|
|
|
222
254
|
if (actual instanceof Map && expected instanceof Map) {
|
|
223
255
|
return mapEquals(actual, expected);
|
|
224
256
|
}
|
|
257
|
+
if (actual instanceof ArrayBuffer && expected instanceof ArrayBuffer) {
|
|
258
|
+
return arrayBufferEquals(actual, expected);
|
|
259
|
+
}
|
|
225
260
|
|
|
226
261
|
if (idof<T>() != idof<U>()) {
|
|
227
262
|
throw new Error("Cannot compare equality between " + nameof<T>(actual)
|
package/assembly/expect.ts
CHANGED
|
@@ -96,7 +96,8 @@ abstract class BaseExpectMatcher<T> {
|
|
|
96
96
|
/**
|
|
97
97
|
* Checks that a value is what you expect using identity comparison. Primitives and strings
|
|
98
98
|
* are compared by value, and references are checked for reference equality only (including
|
|
99
|
-
* objects, arrays, etc).
|
|
99
|
+
* objects, arrays, etc). SIMD vectors use WASM's native `==` comparison, which compares at
|
|
100
|
+
* the bit level, ignoring lane type.
|
|
100
101
|
*
|
|
101
102
|
* Cross-type numeric comparisons are allowed where AssemblyScript's own `==` operator
|
|
102
103
|
* permits them (e.g. `f64` vs `i32`). `toBeCloseTo()` is safer for any comparison
|
|
@@ -116,6 +117,9 @@ abstract class BaseExpectMatcher<T> {
|
|
|
116
117
|
* // supported float/integer comparisons (small integer types)
|
|
117
118
|
* expect(f64(42.0)).toBe(i32(42));
|
|
118
119
|
*
|
|
120
|
+
* // SIMD vectors: different lane types with the same underlying bits are identical
|
|
121
|
+
* expect(i64x2(3, 7)).toBe(i32x4(3, 0, 7, 0));
|
|
122
|
+
*
|
|
119
123
|
* // unsupported float/integer comparisons throw an error
|
|
120
124
|
* // expect(f32(42.0)).toBe(i32(42)); // Error: float precision insufficient
|
|
121
125
|
* // expect(f64(42.0)).toBe(i64(42)); // Error: float precision insufficient
|
|
@@ -133,10 +137,15 @@ abstract class BaseExpectMatcher<T> {
|
|
|
133
137
|
*
|
|
134
138
|
* Strings are compared by value equality as with `toBe`. Non-numeric, non-string types return false.
|
|
135
139
|
*
|
|
136
|
-
*
|
|
140
|
+
* SIMD `v128` vectors are not supported, as approximate comparison requires a lane type
|
|
141
|
+
* interpretation to extract numeric values. Extract lane values as needed and compare them individually.
|
|
142
|
+
*
|
|
143
|
+
* @param precision - Specify the number of decimal places that must match for values to be
|
|
137
144
|
* considered close. Defaults to 2 digits, meaning effectively that values must be within 0.005 of
|
|
138
145
|
* each other.
|
|
139
146
|
*
|
|
147
|
+
* @throws When either value is a `v128` vector.
|
|
148
|
+
*
|
|
140
149
|
* @example
|
|
141
150
|
* expect(0.1 + 0.2).toBeCloseTo(0.3);
|
|
142
151
|
* expect(1.005).toBeCloseTo(1.0, 1);
|
|
@@ -153,11 +162,14 @@ abstract class BaseExpectMatcher<T> {
|
|
|
153
162
|
* (more permissive than AS's own `>` operator). Booleans are treated as numeric
|
|
154
163
|
* (true=1, false=0).
|
|
155
164
|
*
|
|
165
|
+
* SIMD `v128` vectors are not supported, as numeric lane-wise comparison requires a
|
|
166
|
+
* specific lane type interpretation. Extract lane values and compare them individually instead.
|
|
167
|
+
*
|
|
156
168
|
* @throws When comparing float/integer types where the float's mantissa cannot losslessly
|
|
157
169
|
* represent the integer type's range (e.g. `f32` vs `i32`, `f64` vs `i64`).
|
|
158
170
|
* @throws When comparing nullable strings where either value is null. Use `toBeNull()`
|
|
159
171
|
* to check for null values.
|
|
160
|
-
* @throws When comparing non-string reference types (objects, arrays, etc).
|
|
172
|
+
* @throws When comparing non-string reference types (objects, arrays, etc) or `v128` vectors.
|
|
161
173
|
*
|
|
162
174
|
* @example
|
|
163
175
|
* expect(10).toBeGreaterThan(5);
|
|
@@ -179,11 +191,14 @@ abstract class BaseExpectMatcher<T> {
|
|
|
179
191
|
* (more permissive than AS's own `>=` operator). Booleans are treated as numeric
|
|
180
192
|
* (true=1, false=0).
|
|
181
193
|
*
|
|
194
|
+
* SIMD `v128` vectors are not supported, as numeric lane-wise comparison requires a
|
|
195
|
+
* specific lane type interpretation. Extract lane values and compare them individually instead.
|
|
196
|
+
*
|
|
182
197
|
* @throws When comparing float/integer types where the float's mantissa cannot losslessly
|
|
183
198
|
* represent the integer type's range (e.g. `f32` vs `i32`, `f64` vs `i64`).
|
|
184
199
|
* @throws When comparing nullable strings where either value is null. Use `toBeNull()`
|
|
185
200
|
* to check for null values.
|
|
186
|
-
* @throws When comparing non-string reference types (objects, arrays, etc).
|
|
201
|
+
* @throws When comparing non-string reference types (objects, arrays, etc) or `v128` vectors.
|
|
187
202
|
*
|
|
188
203
|
* @example
|
|
189
204
|
* expect(10).toBeGreaterThanOrEqual(10);
|
|
@@ -204,11 +219,14 @@ abstract class BaseExpectMatcher<T> {
|
|
|
204
219
|
* (more permissive than AS's own `<` operator). Booleans are treated as numeric
|
|
205
220
|
* (true=1, false=0).
|
|
206
221
|
*
|
|
222
|
+
* SIMD `v128` vectors are not supported, as numeric lane-wise comparison requires a
|
|
223
|
+
* specific lane type interpretation. Extract lane values and compare them individually instead.
|
|
224
|
+
*
|
|
207
225
|
* @throws When comparing float/integer types where the float's mantissa cannot losslessly
|
|
208
226
|
* represent the integer type's range (e.g. `f32` vs `i32`, `f64` vs `i64`).
|
|
209
227
|
* @throws When comparing nullable strings where either value is null. Use `toBeNull()`
|
|
210
228
|
* to check for null values.
|
|
211
|
-
* @throws When comparing non-string reference types (objects, arrays, etc).
|
|
229
|
+
* @throws When comparing non-string reference types (objects, arrays, etc) or `v128` vectors.
|
|
212
230
|
*
|
|
213
231
|
* @example
|
|
214
232
|
* expect(5).toBeLessThan(10);
|
|
@@ -230,11 +248,14 @@ abstract class BaseExpectMatcher<T> {
|
|
|
230
248
|
* (more permissive than AS's own `<=` operator). Booleans are treated as numeric
|
|
231
249
|
* (true=1, false=0).
|
|
232
250
|
*
|
|
251
|
+
* SIMD `v128` vectors are not supported, as numeric lane-wise comparison requires a
|
|
252
|
+
* specific lane type interpretation. Extract lane values and compare them individually instead.
|
|
253
|
+
*
|
|
233
254
|
* @throws When comparing float/integer types where the float's mantissa cannot losslessly
|
|
234
255
|
* represent the integer type's range (e.g. `f32` vs `i32`, `f64` vs `i64`).
|
|
235
256
|
* @throws When comparing nullable strings where either value is null. Use `toBeNull()`
|
|
236
257
|
* to check for null values.
|
|
237
|
-
* @throws When comparing non-string reference types (objects, arrays, etc).
|
|
258
|
+
* @throws When comparing non-string reference types (objects, arrays, etc) or `v128` vectors.
|
|
238
259
|
*
|
|
239
260
|
* @example
|
|
240
261
|
* expect(5).toBeLessThanOrEqual(5);
|
|
@@ -248,17 +269,22 @@ abstract class BaseExpectMatcher<T> {
|
|
|
248
269
|
}
|
|
249
270
|
|
|
250
271
|
/**
|
|
251
|
-
* Checks that two values have the same value (deep equality).
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
* `
|
|
272
|
+
* Checks that two values have the same value (deep equality). Primitives and strings
|
|
273
|
+
* are compared by value. The following reference types are compared with deep equality:
|
|
274
|
+
* - `Array`: element-by-element using `toEqual()` recursively
|
|
275
|
+
* - `ArrayBuffer`: byte-level content comparison
|
|
276
|
+
* - `Set`: membership equality (same elements, order-independent)
|
|
277
|
+
* - `Map`: key-by-key with values compared using `toEqual()`
|
|
278
|
+
*
|
|
279
|
+
* Other object references use `toBe()` rules (reference identity).
|
|
280
|
+
* ⚠️ IMPORTANT: Does not yet support user-defined object deep equality checking.
|
|
256
281
|
*
|
|
257
282
|
* Like `toBe`, cross-type numeric comparisons follow AssemblyScript's own `==` operator
|
|
258
283
|
* restrictions. `toBeCloseTo()` is safer for any comparison involving a float and
|
|
259
284
|
* accurately handles precision-loss edge cases.
|
|
260
|
-
*
|
|
261
|
-
*
|
|
285
|
+
*
|
|
286
|
+
* SIMD vectors use WASM's native `==` comparison, which compares at the bit level,
|
|
287
|
+
* ignoring lane type.
|
|
262
288
|
*
|
|
263
289
|
* @throws When comparing float/integer types where the float's mantissa cannot losslessly
|
|
264
290
|
* represent the integer type's range (e.g. `f32` vs `i32`, `f64` vs `i64`).
|
|
@@ -267,10 +293,20 @@ abstract class BaseExpectMatcher<T> {
|
|
|
267
293
|
* expect([1, 2, 3]).toEqual([1, 2, 3]);
|
|
268
294
|
* expect(["one", "two", "three"]).toEqual(["one", "two", "three"]);
|
|
269
295
|
*
|
|
270
|
-
* //
|
|
271
|
-
* const a
|
|
272
|
-
* const b
|
|
273
|
-
*
|
|
296
|
+
* // ArrayBuffer byte-level comparison
|
|
297
|
+
* const a = new ArrayBuffer(4);
|
|
298
|
+
* const b = new ArrayBuffer(4);
|
|
299
|
+
* store<u8>(changetype<usize>(a), 0x42);
|
|
300
|
+
* store<u8>(changetype<usize>(b), 66); // 66 decimal == 0x42 hex
|
|
301
|
+
* expect(a).toEqual(b);
|
|
302
|
+
*
|
|
303
|
+
* // SIMD vectors: different lane types with the same underlying bits are equal
|
|
304
|
+
* expect(i64x2(3, 7)).toEqual(i32x4(3, 0, 7, 0));
|
|
305
|
+
*
|
|
306
|
+
* // user-defined objects use reference equality (deep equality not yet supported)
|
|
307
|
+
* const x = new MyObject(1);
|
|
308
|
+
* const y = new MyObject(1); // same data, different reference
|
|
309
|
+
* // expect(x).toEqual(y); // throws — not yet supported
|
|
274
310
|
*/
|
|
275
311
|
toEqual<U>(val: U): void {
|
|
276
312
|
this.assertComparison(equals(this.actual, val), this.actual, val, "to deeply equal", true);
|
|
@@ -293,10 +329,13 @@ abstract class BaseExpectMatcher<T> {
|
|
|
293
329
|
* an object reference, not a primitive. An empty string is still an allocated object
|
|
294
330
|
* with a non-zero address, so it evaluates as truthy.
|
|
295
331
|
*
|
|
332
|
+
* A SIMD `v128` vector with at least one non-zero bit is truthy; an all-zero vector is falsy.
|
|
333
|
+
*
|
|
296
334
|
* @example
|
|
297
335
|
* expect(1).toBeTruthy();
|
|
298
336
|
* expect("hello").toBeTruthy();
|
|
299
337
|
* expect("").toBeTruthy(); // truthy in AS (unlike JS)
|
|
338
|
+
* expect(i32x4.splat(1)).toBeTruthy();
|
|
300
339
|
*/
|
|
301
340
|
toBeTruthy(): void {
|
|
302
341
|
this.assertComparison(truthyOrFalsey(this.actual, true), this.actual, true, "to be truthy", false);
|
|
@@ -309,11 +348,14 @@ abstract class BaseExpectMatcher<T> {
|
|
|
309
348
|
* an object reference, not a primitive. An empty string is still an allocated object
|
|
310
349
|
* with a non-zero address, so it evaluates as truthy.
|
|
311
350
|
*
|
|
351
|
+
* An all-zero SIMD `v128` vector is falsy; a vector with at least one non-zero bit is truthy.
|
|
352
|
+
*
|
|
312
353
|
* @example
|
|
313
354
|
* expect(0).toBeFalsy();
|
|
314
355
|
* expect(NaN).toBeFalsy();
|
|
315
356
|
* expect(null).toBeFalsy();
|
|
316
357
|
* expect("").not.toBeFalsy(); // not falsy in AS (unlike JS)
|
|
358
|
+
* expect(i32x4.splat(0)).toBeFalsy();
|
|
317
359
|
*/
|
|
318
360
|
toBeFalsy(): void {
|
|
319
361
|
this.assertComparison(truthyOrFalsey(this.actual, false), this.actual, false, "to be falsey", false);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vitest-pool-assemblyscript",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "AssemblyScript testing with Vitest - Simple, fast, familiar, AS-native, with full coverage output",
|
|
5
5
|
"author": "Matt Ritter <matthew.d.ritter@gmail.com>",
|
|
6
6
|
"license": "MIT",
|