mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
improve(std/asserts): allow assert functions to specify type parameter (#6413)
This commit is contained in:
parent
cc12e86fe3
commit
538504f57c
5 changed files with 84 additions and 13 deletions
|
@ -246,7 +246,7 @@ Deno.test("callbackify passes arguments to the original", async () => {
|
|||
|
||||
for (const value of values) {
|
||||
// eslint-disable-next-line require-await
|
||||
async function asyncFn<T>(arg: T): Promise<T> {
|
||||
async function asyncFn(arg: typeof value): Promise<typeof value> {
|
||||
assertStrictEquals(arg, value);
|
||||
return arg;
|
||||
}
|
||||
|
@ -263,7 +263,7 @@ Deno.test("callbackify passes arguments to the original", async () => {
|
|||
});
|
||||
});
|
||||
|
||||
function promiseFn<T>(arg: T): Promise<T> {
|
||||
function promiseFn<T>(arg: typeof value): Promise<typeof value> {
|
||||
assertStrictEquals(arg, value);
|
||||
return Promise.resolve(arg);
|
||||
}
|
||||
|
|
|
@ -21,13 +21,30 @@
|
|||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
// Hack: work around the following TypeScript error:
|
||||
// error: TS2345 [ERROR]: Argument of type 'typeof kCustomPromisifiedSymbol'
|
||||
// is not assignable to parameter of type 'typeof kCustomPromisifiedSymbol'.
|
||||
// assertStrictEquals(kCustomPromisifiedSymbol, promisify.custom);
|
||||
// ~~~~~~~~~~~~~~~~
|
||||
declare const _CustomPromisifiedSymbol: unique symbol;
|
||||
declare const _CustomPromisifyArgsSymbol: unique symbol;
|
||||
declare let Symbol: SymbolConstructor;
|
||||
interface SymbolConstructor {
|
||||
for(key: "nodejs.util.promisify.custom"): typeof _CustomPromisifiedSymbol;
|
||||
for(
|
||||
key: "nodejs.util.promisify.customArgs"
|
||||
): typeof _CustomPromisifyArgsSymbol;
|
||||
}
|
||||
// End hack.
|
||||
|
||||
// In addition to being accessible through util.promisify.custom,
|
||||
// this symbol is registered globally and can be accessed in any environment as Symbol.for('nodejs.util.promisify.custom')
|
||||
// this symbol is registered globally and can be accessed in any environment as
|
||||
// Symbol.for('nodejs.util.promisify.custom').
|
||||
const kCustomPromisifiedSymbol = Symbol.for("nodejs.util.promisify.custom");
|
||||
// This is an internal Node symbol used by functions returning multiple arguments
|
||||
// e.g. ['bytesRead', 'buffer'] for fs.read.
|
||||
// This is an internal Node symbol used by functions returning multiple
|
||||
// arguments, e.g. ['bytesRead', 'buffer'] for fs.read().
|
||||
const kCustomPromisifyArgsSymbol = Symbol.for(
|
||||
"deno.nodejs.util.promisify.customArgs"
|
||||
"nodejs.util.promisify.customArgs"
|
||||
);
|
||||
|
||||
class NodeInvalidArgTypeError extends TypeError {
|
||||
|
|
|
@ -30,7 +30,7 @@ import { promisify } from "./_util_promisify.ts";
|
|||
import * as fs from "../fs.ts";
|
||||
|
||||
const readFile = promisify(fs.readFile);
|
||||
const customPromisifyArgs = Symbol.for("deno.nodejs.util.promisify.customArgs");
|
||||
const customPromisifyArgs = Symbol.for("nodejs.util.promisify.customArgs");
|
||||
|
||||
Deno.test(
|
||||
"Errors should reject the promise",
|
||||
|
|
|
@ -133,7 +133,7 @@ export function equal(c: unknown, d: unknown): boolean {
|
|||
})(c, d);
|
||||
}
|
||||
|
||||
/** Make an assertion, if not `true`, then throw. */
|
||||
/** Make an assertion, error will be thrown if `expr` does not have truthy value. */
|
||||
export function assert(expr: unknown, msg = ""): asserts expr {
|
||||
if (!expr) {
|
||||
throw new AssertionError(msg);
|
||||
|
@ -143,7 +143,19 @@ export function assert(expr: unknown, msg = ""): asserts expr {
|
|||
/**
|
||||
* Make an assertion that `actual` and `expected` are equal, deeply. If not
|
||||
* deeply equal, then throw.
|
||||
*
|
||||
* Type parameter can be specified to ensure values under comparison have the same type.
|
||||
* For example:
|
||||
*```ts
|
||||
*assertEquals<number>(1, 2)
|
||||
*```
|
||||
*/
|
||||
export function assertEquals(
|
||||
actual: unknown,
|
||||
expected: unknown,
|
||||
msg?: string
|
||||
): void;
|
||||
export function assertEquals<T>(actual: T, expected: T, msg?: string): void;
|
||||
export function assertEquals(
|
||||
actual: unknown,
|
||||
expected: unknown,
|
||||
|
@ -174,7 +186,19 @@ export function assertEquals(
|
|||
/**
|
||||
* Make an assertion that `actual` and `expected` are not equal, deeply.
|
||||
* If not then throw.
|
||||
*
|
||||
* Type parameter can be specified to ensure values under comparison have the same type.
|
||||
* For example:
|
||||
*```ts
|
||||
*assertNotEquals<number>(1, 2)
|
||||
*```
|
||||
*/
|
||||
export function assertNotEquals(
|
||||
actual: unknown,
|
||||
expected: unknown,
|
||||
msg?: string
|
||||
): void;
|
||||
export function assertNotEquals<T>(actual: T, expected: T, msg?: string): void;
|
||||
export function assertNotEquals(
|
||||
actual: unknown,
|
||||
expected: unknown,
|
||||
|
@ -204,10 +228,13 @@ export function assertNotEquals(
|
|||
/**
|
||||
* Make an assertion that `actual` and `expected` are strictly equal. If
|
||||
* not then throw.
|
||||
* ```ts
|
||||
* assertStrictEquals(1, 2)
|
||||
* ```
|
||||
*/
|
||||
export function assertStrictEquals(
|
||||
actual: unknown,
|
||||
expected: unknown,
|
||||
export function assertStrictEquals<T>(
|
||||
actual: T,
|
||||
expected: T,
|
||||
msg?: string
|
||||
): void {
|
||||
if (actual === expected) {
|
||||
|
@ -265,9 +292,25 @@ export function assertStringContains(
|
|||
}
|
||||
|
||||
/**
|
||||
* Make an assertion that `actual` contains the `expected` values
|
||||
* If not then thrown.
|
||||
* Make an assertion that `actual` contains the `expected` values.
|
||||
* If not then an error will be thrown.
|
||||
*
|
||||
* Type parameter can be specified to ensure values under comparison have the same type.
|
||||
* For example:
|
||||
*```ts
|
||||
*assertArrayContains<number>([1, 2], [2])
|
||||
*```
|
||||
*/
|
||||
export function assertArrayContains(
|
||||
actual: ArrayLike<unknown>,
|
||||
expected: ArrayLike<unknown>,
|
||||
msg?: string
|
||||
): void;
|
||||
export function assertArrayContains<T>(
|
||||
actual: ArrayLike<T>,
|
||||
expected: ArrayLike<T>,
|
||||
msg?: string
|
||||
): void;
|
||||
export function assertArrayContains(
|
||||
actual: ArrayLike<unknown>,
|
||||
expected: ArrayLike<unknown>,
|
||||
|
|
|
@ -412,6 +412,17 @@ Deno.test({
|
|||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "assert* functions with specified type paratemeter",
|
||||
fn(): void {
|
||||
assertEquals<string>("hello", "hello");
|
||||
assertNotEquals<number>(1, 2);
|
||||
assertArrayContains<boolean>([true, false], [true]);
|
||||
const value = { x: 1 };
|
||||
assertStrictEquals<typeof value>(value, value);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test("Assert Throws Non-Error Fail", () => {
|
||||
assertThrows(
|
||||
() => {
|
||||
|
|
Loading…
Add table
Reference in a new issue