2019-01-01 22:46:17 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 16:39:50 -05:00
|
|
|
import { test, runIfMain } from "./mod.ts";
|
|
|
|
import {
|
|
|
|
assert,
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals,
|
2019-03-06 16:39:50 -05:00
|
|
|
assertStrictEq,
|
|
|
|
assertThrows,
|
2019-03-08 16:04:43 -05:00
|
|
|
assertThrowsAsync
|
2019-07-28 07:10:29 -04:00
|
|
|
} from "./asserts.ts";
|
2019-01-01 22:46:17 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function testingAssertEqualActualUncoercable(): void {
|
2019-01-01 22:46:17 -05:00
|
|
|
let didThrow = false;
|
|
|
|
const a = Object.create(null);
|
|
|
|
try {
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(a, "bar");
|
2019-01-01 22:46:17 -05:00
|
|
|
} catch (e) {
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(didThrow);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function testingAssertEqualExpectedUncoercable(): void {
|
2019-01-01 22:46:17 -05:00
|
|
|
let didThrow = false;
|
|
|
|
const a = Object.create(null);
|
|
|
|
try {
|
2019-03-06 16:39:50 -05:00
|
|
|
assertStrictEq("bar", a);
|
2019-01-01 22:46:17 -05:00
|
|
|
} catch (e) {
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(didThrow);
|
|
|
|
});
|
2019-01-15 21:57:40 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function testingAssertStrictEqual(): void {
|
2019-01-15 21:57:40 -05:00
|
|
|
const a = {};
|
|
|
|
const b = a;
|
2019-03-06 16:39:50 -05:00
|
|
|
assertStrictEq(a, b);
|
2019-01-15 21:57:40 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function testingAssertNotStrictEqual(): void {
|
2019-01-15 21:57:40 -05:00
|
|
|
let didThrow = false;
|
|
|
|
const a = {};
|
|
|
|
const b = {};
|
|
|
|
try {
|
2019-03-06 16:39:50 -05:00
|
|
|
assertStrictEq(a, b);
|
2019-01-15 21:57:40 -05:00
|
|
|
} catch (e) {
|
|
|
|
assert(e.message === "actual: [object Object] expected: [object Object]");
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(didThrow);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function testingDoesThrow(): void {
|
2019-01-15 21:57:40 -05:00
|
|
|
let count = 0;
|
2019-10-09 17:22:22 -04:00
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
count++;
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
);
|
2019-01-15 21:57:40 -05:00
|
|
|
assert(count === 1);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function testingDoesNotThrow(): void {
|
2019-01-15 21:57:40 -05:00
|
|
|
let count = 0;
|
|
|
|
let didThrow = false;
|
|
|
|
try {
|
2019-10-09 17:22:22 -04:00
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
count++;
|
|
|
|
console.log("Hello world");
|
|
|
|
}
|
|
|
|
);
|
2019-01-15 21:57:40 -05:00
|
|
|
} catch (e) {
|
|
|
|
assert(e.message === "Expected function to throw.");
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(count === 1);
|
|
|
|
assert(didThrow);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function testingThrowsErrorType(): void {
|
2019-01-15 21:57:40 -05:00
|
|
|
let count = 0;
|
2019-04-24 07:41:23 -04:00
|
|
|
assertThrows((): void => {
|
2019-01-15 21:57:40 -05:00
|
|
|
count++;
|
|
|
|
throw new TypeError();
|
|
|
|
}, TypeError);
|
|
|
|
assert(count === 1);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function testingThrowsNotErrorType(): void {
|
2019-01-15 21:57:40 -05:00
|
|
|
let count = 0;
|
|
|
|
let didThrow = false;
|
|
|
|
try {
|
2019-04-24 07:41:23 -04:00
|
|
|
assertThrows((): void => {
|
2019-01-15 21:57:40 -05:00
|
|
|
count++;
|
|
|
|
throw new TypeError();
|
|
|
|
}, RangeError);
|
|
|
|
} catch (e) {
|
|
|
|
assert(e.message === `Expected error to be instance of "RangeError".`);
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(count === 1);
|
|
|
|
assert(didThrow);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function testingThrowsMsgIncludes(): void {
|
2019-01-15 21:57:40 -05:00
|
|
|
let count = 0;
|
2019-03-06 16:39:50 -05:00
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-01-15 21:57:40 -05:00
|
|
|
count++;
|
|
|
|
throw new TypeError("Hello world!");
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"world"
|
|
|
|
);
|
|
|
|
assert(count === 1);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function testingThrowsMsgNotIncludes(): void {
|
2019-01-15 21:57:40 -05:00
|
|
|
let count = 0;
|
|
|
|
let didThrow = false;
|
|
|
|
try {
|
2019-03-06 16:39:50 -05:00
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-01-15 21:57:40 -05:00
|
|
|
count++;
|
|
|
|
throw new TypeError("Hello world!");
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"foobar"
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
assert(
|
|
|
|
e.message ===
|
|
|
|
`Expected error message to include "foobar", but got "Hello world!".`
|
|
|
|
);
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(count === 1);
|
|
|
|
assert(didThrow);
|
|
|
|
});
|
2019-01-22 01:15:25 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function testingDoesThrowAsync(): Promise<void> {
|
2019-01-22 01:15:25 -05:00
|
|
|
let count = 0;
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
count++;
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
);
|
2019-01-22 01:15:25 -05:00
|
|
|
assert(count === 1);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function testingDoesReject(): Promise<void> {
|
2019-01-22 01:15:25 -05:00
|
|
|
let count = 0;
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
(): Promise<never> => {
|
|
|
|
count++;
|
|
|
|
return Promise.reject(new Error());
|
|
|
|
}
|
|
|
|
);
|
2019-01-22 01:15:25 -05:00
|
|
|
assert(count === 1);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function testingDoesNotThrowAsync(): Promise<void> {
|
2019-01-22 01:15:25 -05:00
|
|
|
let count = 0;
|
|
|
|
let didThrow = false;
|
|
|
|
try {
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
count++;
|
|
|
|
console.log("Hello world");
|
|
|
|
}
|
|
|
|
);
|
2019-01-22 01:15:25 -05:00
|
|
|
} catch (e) {
|
|
|
|
assert(e.message === "Expected function to throw.");
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(count === 1);
|
|
|
|
assert(didThrow);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function testingDoesNotRejectAsync(): Promise<void> {
|
2019-01-22 01:15:25 -05:00
|
|
|
let count = 0;
|
|
|
|
let didThrow = false;
|
|
|
|
try {
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
(): Promise<void> => {
|
|
|
|
count++;
|
|
|
|
console.log("Hello world");
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
);
|
2019-01-22 01:15:25 -05:00
|
|
|
} catch (e) {
|
|
|
|
assert(e.message === "Expected function to throw.");
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(count === 1);
|
|
|
|
assert(didThrow);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function testingThrowsAsyncErrorType(): Promise<void> {
|
2019-01-22 01:15:25 -05:00
|
|
|
let count = 0;
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync((): Promise<void> => {
|
2019-01-22 01:15:25 -05:00
|
|
|
count++;
|
|
|
|
throw new TypeError();
|
|
|
|
}, TypeError);
|
|
|
|
assert(count === 1);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function testingThrowsAsyncNotErrorType(): Promise<void> {
|
2019-01-22 01:15:25 -05:00
|
|
|
let count = 0;
|
|
|
|
let didThrow = false;
|
|
|
|
try {
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(async (): Promise<void> => {
|
2019-01-22 01:15:25 -05:00
|
|
|
count++;
|
|
|
|
throw new TypeError();
|
|
|
|
}, RangeError);
|
|
|
|
} catch (e) {
|
|
|
|
assert(e.message === `Expected error to be instance of "RangeError".`);
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(count === 1);
|
|
|
|
assert(didThrow);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function testingThrowsAsyncMsgIncludes(): Promise<void> {
|
2019-01-22 01:15:25 -05:00
|
|
|
let count = 0;
|
2019-03-06 16:39:50 -05:00
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-01-22 01:15:25 -05:00
|
|
|
count++;
|
|
|
|
throw new TypeError("Hello world!");
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"world"
|
|
|
|
);
|
|
|
|
assert(count === 1);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function testingThrowsAsyncMsgNotIncludes(): Promise<void> {
|
2019-01-22 01:15:25 -05:00
|
|
|
let count = 0;
|
|
|
|
let didThrow = false;
|
|
|
|
try {
|
2019-03-06 16:39:50 -05:00
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-01-22 01:15:25 -05:00
|
|
|
count++;
|
|
|
|
throw new TypeError("Hello world!");
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"foobar"
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
assert(
|
|
|
|
e.message ===
|
|
|
|
`Expected error message to include "foobar", but got "Hello world!".`
|
|
|
|
);
|
|
|
|
didThrow = true;
|
|
|
|
}
|
|
|
|
assert(count === 1);
|
|
|
|
assert(didThrow);
|
|
|
|
});
|
2019-03-04 14:19:03 -05:00
|
|
|
|
2019-08-14 16:12:35 -04:00
|
|
|
test("test fn overloading", (): void => {
|
|
|
|
// just verifying that you can use this test definition syntax
|
|
|
|
assert(true);
|
|
|
|
});
|
|
|
|
|
2019-03-11 14:21:13 -04:00
|
|
|
runIfMain(import.meta);
|