Skip to content

Commit

Permalink
t.assert asserts properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Feb 25, 2023
1 parent 4b28fa7 commit 69c2a58
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 6 deletions.
7 changes: 7 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"imports": {
"isomorphic.js": "./node_modules/isomorphic.js/node.mjs",
"lib0/crypto": "./crypto.js",
"lib0/webcrypto": "./webcrypto.deno.js"
}
}
122 changes: 122 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions encoding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ const encodingPairs = [
export const testRepeatRandomWrites = tc => {
t.describe(`Writing ${loops} random values`, `defLen=${defLen}`)
const gen = tc.prng
/**
* @type {any}
*/
const ops = []
const encoder = encoding.createEncoder()
for (let i = 0; i < 10000; i++) {
Expand Down
8 changes: 4 additions & 4 deletions list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class QueueItem extends list.ListNode {
}

/**
* @param {t.TestCase} tc
* @param {t.TestCase} _tc
*/
export const testEnqueueDequeue = tc => {
export const testEnqueueDequeue = _tc => {
const N = 30
/**
* @type {list.List<QueueItem>}
Expand Down Expand Up @@ -45,9 +45,9 @@ export const testEnqueueDequeue = tc => {
}

/**
* @param {t.TestCase} tc
* @param {t.TestCase} _tc
*/
export const testSelectivePop = tc => {
export const testSelectivePop = _tc => {
/**
* @type {list.List<QueueItem>}
*/
Expand Down
6 changes: 4 additions & 2 deletions testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,14 @@ const _compare = (a, b, path, message, customCompare) => {
export const compare = (a, b, message = null, customCompare = compareValues) => _compare(a, b, 'obj', message, customCompare)

/**
* @param {boolean} condition
* @template T
* @param {T} property
* @param {string?} [message]
* @return {asserts property is NonNullable<T>}
* @throws {TestError}
*/
/* c8 ignore next */
export const assert = (condition, message = null) => condition || fail(`Assertion failed${message !== null ? `: ${message}` : ''}`)
export const assert = (property, message = null) => { property || fail(`Assertion failed${message !== null ? `: ${message}` : ''}`) }

/**
* @param {function():Promise<any>} f
Expand Down
8 changes: 8 additions & 0 deletions testing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export const nottestingNotTested = () => {
t.assert(false, 'This test should not be executed because the name doesnt start with "test"')
}

export const testAssertTyping = () => {
const q = Math.random()
const x = q === 0.3 ? { a: 4 } : null
// t.assert(x.a === 4) - this will give a type error because the type is uncertain
t.assert(x)
t.assert(x.a === 4) // this works because x is asserted
}

/**
* @param {t.TestCase} _tc
*/
Expand Down
4 changes: 4 additions & 0 deletions webcrypto.deno.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// eslint-disable-next-line
export const subtle = /** @type {any} */ (crypto).subtle
// eslint-disable-next-line
export const getRandomValues = /** @type {any} */ (crypto).getRandomValues.bind(crypto)

0 comments on commit 69c2a58

Please sign in to comment.