-
Notifications
You must be signed in to change notification settings - Fork 262
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
9 changed files
with
18,985 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
export const config = {}; | ||
|
||
export function setConfig(options) { | ||
Object.assign(config, options); | ||
} | ||
|
||
function formatMsg(msg) { | ||
return `${msg}${msg ? ': ' : ''}`; | ||
} | ||
|
||
export function assertTruthy(actual, msg = '') { | ||
if (!config.noLint && !actual) { | ||
throw new Error(`${formatMsg(msg)}expected: truthy, actual: ${actual}`); | ||
} | ||
} | ||
|
||
export function assertFalsy(actual, msg = '') { | ||
if (!config.noLint && actual) { | ||
throw new Error(`${formatMsg(msg)}expected: falsy, actual: ${actual}`); | ||
} | ||
} | ||
|
||
export function assertStringMatchesRegEx(actual, regex, msg = '') { | ||
if (!config.noLint && !regex.test(actual)) { | ||
throw new Error(`${formatMsg(msg)}expected: ${regex}, actual: ${actual}`); | ||
} | ||
} | ||
|
||
export function assertEqual(actual, expected, msg = '') { | ||
if (!config.noLint && actual !== expected) { | ||
throw new Error(`${formatMsg(msg)}expected: ${expected} to equal actual: ${actual}`); | ||
} | ||
} | ||
|
||
export function assertNotEqual(actual, expected, msg = '') { | ||
if (!config.noLint && actual === expected) { | ||
throw new Error(`${formatMsg(msg)}expected: ${expected} to not equal actual: ${actual}`); | ||
} | ||
} | ||
|
||
export function assertArrayEqual(actual, expected, msg = '') { | ||
if (actual.length !== expected.length) { | ||
throw new Error(`${formatMsg(msg)}expected: array.length ${expected.length} to equal actual.length: ${actual.length}`); | ||
} | ||
const errors = []; | ||
for (let i = 0; i < actual.length; ++i) { | ||
if (actual[i] !== expected[i]) { | ||
errors.push(`${formatMsg(msg)}expected: expected[${i}] ${expected[i]} to equal actual[${i}]: ${actual[i]}`); | ||
if (errors.length === 10) { | ||
break; | ||
} | ||
} | ||
} | ||
if (errors.length > 0) { | ||
throw new Error(errors.join('\n')); | ||
} | ||
} | ||
|
||
export function assertThrowsWith(func, expectations, msg = '') { | ||
let error = ''; | ||
if (config.throwOnError === false) { | ||
const origFn = console.error; | ||
const errors = []; | ||
console.error = function(...args) { | ||
errors.push(args.join(' ')); | ||
}; | ||
func(); | ||
console.error = origFn; | ||
if (errors.length) { | ||
error = errors.join('\n'); | ||
console.error(error); | ||
} | ||
} else { | ||
try { | ||
func(); | ||
} catch (e) { | ||
console.error(e); // eslint-disable-line | ||
error = e; | ||
} | ||
|
||
} | ||
|
||
if (config.noLint) { | ||
return; | ||
} | ||
|
||
assertStringMatchesREs(error.toString().replace(/\n/g, ' '), expectations, msg); | ||
} | ||
|
||
// check if it throws it throws with x | ||
export function assertIfThrowsItThrowsWith(func, expectations, msg = '') { | ||
let error = ''; | ||
let threw = false; | ||
if (config.throwOnError === false) { | ||
const origFn = console.error; | ||
const errors = []; | ||
console.error = function(...args) { | ||
errors.push(args.join(' ')); | ||
}; | ||
func(); | ||
console.error = origFn; | ||
if (errors.length) { | ||
error = errors.join('\n'); | ||
console.error(error); | ||
} | ||
} else { | ||
try { | ||
func(); | ||
} catch (e) { | ||
console.error(e); // eslint-disable-line | ||
error = e; | ||
threw = true; | ||
} | ||
|
||
} | ||
|
||
if (config.noLint) { | ||
return; | ||
} | ||
|
||
if (!threw) { | ||
return; | ||
} | ||
|
||
assertStringMatchesREs(error.toString().replace(/\n/g, ' '), expectations, msg); | ||
} | ||
|
||
function assertStringMatchesREs(actual, expectations, msg) { | ||
for (const expectation of expectations) { | ||
if (expectation instanceof RegExp) { | ||
if (!expectation.test(actual)) { | ||
throw new Error(`${formatMsg(msg)}expected: ${expectation}, actual: ${actual}`); | ||
} | ||
} | ||
} | ||
|
||
} | ||
export function assertWarnsWith(func, expectations, msg = '') { | ||
const warnings = []; | ||
const origWarnFn = console.warn; | ||
console.warn = function(...args) { | ||
origWarnFn.call(this, ...args); | ||
warnings.push(args.join(' ')); | ||
}; | ||
|
||
let error; | ||
try { | ||
func(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
|
||
console.warn = origWarnFn; | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
if (config.noLint) { | ||
return; | ||
} | ||
|
||
assertStringMatchesREs(warnings.join(' '), expectations, msg); | ||
} | ||
|
||
export default { | ||
false: assertFalsy, | ||
equal: assertEqual, | ||
matchesRegEx: assertStringMatchesRegEx, | ||
notEqual: assertNotEqual, | ||
throwsWith: assertThrowsWith, | ||
true: assertTruthy, | ||
}; |
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,74 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>TWGL Tests</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="stylesheet" href="mocha.css"> | ||
<style> | ||
#mocha #other { | ||
text-decoration: underline; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="mocha"> | ||
</div> | ||
<script> | ||
// this is here for puppeteer. It's resolved in index.js | ||
// so we can await on window.testPromiseInfo | ||
function makePromise() { | ||
const info = {}; | ||
const promise = new Promise((resolve, reject) => { | ||
Object.assign(info, {resolve, reject}); | ||
}); | ||
info.promise = promise; | ||
return info; | ||
} | ||
|
||
window.testsPromiseInfo = makePromise(); | ||
</script> | ||
<script src="mocha.js"></script> | ||
<script type="module"> | ||
/* global document */ | ||
/* global mocha */ | ||
/* global URLSearchParams */ | ||
/* global window */ | ||
import {setConfig} from './assert.js'; | ||
|
||
async function main() { | ||
mocha.setup('bdd'); | ||
mocha.fullTrace(); | ||
mocha.timeout(0); | ||
const query = Object.fromEntries(new URLSearchParams(window.location.search).entries()); | ||
if (query.timeout !== undefined) { | ||
mocha.timeout(query.timeout); | ||
} | ||
const lint = query.lint !== 'false'; | ||
const throwOnError = !query.warn; | ||
|
||
const src = query.src ? '../src/twgl-full.js' : '../dist/4.x/twgl-full.module.js'; | ||
|
||
try { | ||
window.twgl = await import(src); | ||
loadScript('index.js', 'module'); | ||
} catch (e) { | ||
console.err(e); | ||
} | ||
} | ||
|
||
function loadScript(url, type = 'text/javascript') { | ||
return new Promise((resolve, reject) => { | ||
const script = document.createElement('script'); | ||
script.onload = resolve; | ||
script.onerror = reject; | ||
script.type = type; | ||
script.src = url; | ||
document.head.appendChild(script); | ||
}); | ||
} | ||
|
||
main(); | ||
</script> | ||
</body> | ||
</html> |
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,11 @@ | ||
/* global mocha */ | ||
|
||
import './tests/program-tests.js'; | ||
|
||
const settings = Object.fromEntries(new URLSearchParams(window.location.search).entries()); | ||
if (settings.reporter) { | ||
mocha.reporter(settings.reporter); | ||
} | ||
mocha.run((failures) => { | ||
window.testsPromiseInfo.resolve(failures); | ||
}); |
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,7 @@ | ||
export const describe = window.describe; | ||
export const it = window.it; | ||
export const before = window.before; | ||
export const after = window.after; | ||
export const beforeEach = window.beforeEach; | ||
export const afterEach = window.afterEach; | ||
|
Oops, something went wrong.