-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
391 changed files
with
3,327 additions
and
3,020 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
export const CAN_NOT_DISPLAY = "[Cannot display]"; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { AssertionError } from "./assertion_error.ts"; | ||
|
||
/** 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { AssertionError } from "./assertion_error.ts"; | ||
|
||
/** | ||
* Make an assertion that `actual` and `expected` are almost equal numbers through | ||
* a given tolerance. It can be used to take into account IEEE-754 double-precision | ||
* floating-point representation limitations. | ||
* If the values are not almost equal then throw. | ||
* | ||
* @example | ||
* ```ts | ||
* import { assertAlmostEquals, assertThrows } from "https://deno.land/std@$STD_VERSION/assert/mod.ts"; | ||
* | ||
* assertAlmostEquals(0.1, 0.2); | ||
* | ||
* // Using a custom tolerance value | ||
* assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); | ||
* assertThrows(() => assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17)); | ||
* ``` | ||
*/ | ||
export function assertAlmostEquals( | ||
actual: number, | ||
expected: number, | ||
tolerance = 1e-7, | ||
msg?: string, | ||
) { | ||
if (Object.is(actual, expected)) { | ||
return; | ||
} | ||
const delta = Math.abs(expected - actual); | ||
if (delta <= tolerance) { | ||
return; | ||
} | ||
|
||
const msgSuffix = msg ? `: ${msg}` : "."; | ||
const f = (n: number) => Number.isInteger(n) ? n : n.toExponential(); | ||
throw new AssertionError( | ||
`Expected actual: "${f(actual)}" to be close to "${f(expected)}": \ | ||
delta "${f(delta)}" is greater than "${f(tolerance)}"${msgSuffix}`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { assertAlmostEquals, AssertionError, assertThrows } from "./mod.ts"; | ||
|
||
Deno.test("assert almost equals number", () => { | ||
//Default precision | ||
assertAlmostEquals(-0, +0); | ||
assertAlmostEquals(Math.PI, Math.PI); | ||
assertAlmostEquals(0.1 + 0.2, 0.3); | ||
assertAlmostEquals(NaN, NaN); | ||
assertAlmostEquals(Number.NaN, Number.NaN); | ||
assertThrows(() => assertAlmostEquals(1, 2)); | ||
assertThrows(() => assertAlmostEquals(1, 1.1)); | ||
|
||
//Higher precision | ||
assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); | ||
assertThrows( | ||
() => assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17), | ||
AssertionError, | ||
`Expected actual: "${ | ||
( | ||
0.1 + 0.2 | ||
).toExponential() | ||
}" to be close to "${(0.3).toExponential()}"`, | ||
); | ||
|
||
//Special cases | ||
assertAlmostEquals(Infinity, Infinity); | ||
assertThrows( | ||
() => assertAlmostEquals(0, Infinity), | ||
AssertionError, | ||
'Expected actual: "0" to be close to "Infinity"', | ||
); | ||
assertThrows( | ||
() => assertAlmostEquals(-Infinity, +Infinity), | ||
AssertionError, | ||
'Expected actual: "-Infinity" to be close to "Infinity"', | ||
); | ||
assertThrows( | ||
() => assertAlmostEquals(Infinity, NaN), | ||
AssertionError, | ||
'Expected actual: "Infinity" to be close to "NaN"', | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { equal } from "./equal.ts"; | ||
import { format } from "./_format.ts"; | ||
import { AssertionError } from "./assertion_error.ts"; | ||
|
||
/** | ||
* Make an assertion that `actual` includes 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. | ||
* | ||
* @example | ||
* ```ts | ||
* import { assertArrayIncludes } from "https://deno.land/std@$STD_VERSION/assert/assert_array_includes.ts"; | ||
* | ||
* assertArrayIncludes<number>([1, 2], [2]) | ||
* ``` | ||
*/ | ||
export function assertArrayIncludes<T>( | ||
actual: ArrayLike<T>, | ||
expected: ArrayLike<T>, | ||
msg?: string, | ||
) { | ||
const missing: unknown[] = []; | ||
for (let i = 0; i < expected.length; i++) { | ||
let found = false; | ||
for (let j = 0; j < actual.length; j++) { | ||
if (equal(expected[i], actual[j])) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
if (!found) { | ||
missing.push(expected[i]); | ||
} | ||
} | ||
if (missing.length === 0) { | ||
return; | ||
} | ||
|
||
const msgSuffix = msg ? `: ${msg}` : "."; | ||
msg = `Expected actual: "${format(actual)}" to include: "${ | ||
format(expected) | ||
}"${msgSuffix}\nmissing: ${format(missing)}`; | ||
throw new AssertionError(msg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { assertArrayIncludes, AssertionError, assertThrows } from "./mod.ts"; | ||
|
||
Deno.test("ArrayContains", function () { | ||
const fixture = ["deno", "iz", "luv"]; | ||
const fixtureObject = [{ deno: "luv" }, { deno: "Js" }]; | ||
assertArrayIncludes(fixture, ["deno"]); | ||
assertArrayIncludes(fixtureObject, [{ deno: "luv" }]); | ||
assertArrayIncludes( | ||
Uint8Array.from([1, 2, 3, 4]), | ||
Uint8Array.from([1, 2, 3]), | ||
); | ||
assertThrows( | ||
() => assertArrayIncludes(fixtureObject, [{ deno: "node" }]), | ||
AssertionError, | ||
`Expected actual: "[ | ||
{ | ||
deno: "luv", | ||
}, | ||
{ | ||
deno: "Js", | ||
}, | ||
]" to include: "[ | ||
{ | ||
deno: "node", | ||
}, | ||
]". | ||
missing: [ | ||
{ | ||
deno: "node", | ||
}, | ||
]`, | ||
); | ||
}); |
Oops, something went wrong.