Skip to content

Commit

Permalink
Merge pull request #1574 from Agoric/1568-test-with-ava
Browse files Browse the repository at this point in the history
switch from tape/tap to AVA for tests in `assert`, `install-ses`, `import-manager`, and `sparse-ints`
  • Loading branch information
warner authored Aug 21, 2020
2 parents 25c506a + 91887c5 commit 53b3e2e
Show file tree
Hide file tree
Showing 11 changed files with 1,160 additions and 219 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"packages/notifier"
],
"devDependencies": {
"ava": "^3.11.1",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-airbnb-base": "^14.0.0",
Expand Down
15 changes: 8 additions & 7 deletions packages/assert/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"build": "exit 0",
"test": "tape -r esm test/**/test*.js",
"test": "ava",
"lint-fix": "yarn lint --fix",
"lint-check": "yarn lint",
"lint": "yarn lint:types && eslint '**/*.js'",
Expand All @@ -30,19 +30,20 @@
"url": "https://github.com/Agoric/agoric-sdk/issues"
},
"homepage": "https://github.com/Agoric/agoric-sdk#readme",
"dependencies": {
},
"dependencies": {},
"devDependencies": {
"esm": "^3.2.25",
"@agoric/install-ses": "^0.2.0",
"tap-spec": "^5.0.0",
"tape": "^4.11.0",
"tape-promise": "^4.0.0"
"ava": "^3.11.1",
"esm": "^3.2.25"
},
"files": [
"src/",
"NEWS.md"
],
"ava": {
"files": ["test/**/test-*.js"],
"require": ["esm"]
},
"eslintConfig": {
"extends": [
"airbnb-base",
Expand Down
237 changes: 96 additions & 141 deletions packages/assert/test/test-assert.js
Original file line number Diff line number Diff line change
@@ -1,169 +1,124 @@
import '@agoric/install-ses';
import { test } from 'tape';
import test from 'ava';
import { an, assert, details, q } from '../src/assert';
import { throwsAndLogs } from './throwsAndLogs';

test('an', t => {
try {
t.equal(an('object'), 'an object');
t.equal(an('function'), 'a function');
// does not treat an initial 'y' as a vowel
t.equal(an('yaml file'), 'a yaml file');
// recognize upper case vowels
t.equal(an('Object'), 'an Object');
// coerce non-objects to strings.
// non-letters are treated as non-vowels
t.equal(an({}), 'a [object Object]');
} catch (e) {
console.log('unexpected exception', e);
t.assert(false, e);
} finally {
t.end();
}
t.is(an('object'), 'an object');
t.is(an('function'), 'a function');
// does not treat an initial 'y' as a vowel
t.is(an('yaml file'), 'a yaml file');
// recognize upper case vowels
t.is(an('Object'), 'an Object');
// coerce non-objects to strings.
// non-letters are treated as non-vowels
t.is(an({}), 'a [object Object]');
});

// Self-test of the example from the throwsAndLogs comment.
test('throwsAndLogs', t => {
const err = {};
try {
throwsAndLogs(
t,
() => {
console.error('what ', err);
throw new Error('foo');
},
/foo/,
[['error', 'what ', err]],
);
} catch (e) {
console.log('unexpected exception', e);
t.assert(false, e);
} finally {
t.end();
}
throwsAndLogs(
t,
() => {
console.error('what ', err);
throw new Error('foo');
},
/foo/,
[['error', 'what ', err]],
);
});

test('assert', t => {
try {
assert(2 + 3 === 5);
assert(2 + 3 === 5);

throwsAndLogs(t, () => assert(false), /Check failed/, [
['error', 'LOGGED ERROR:', 'Check failed'],
]);
throwsAndLogs(t, () => assert(false, 'foo'), /foo/, [
['error', 'LOGGED ERROR:', 'foo'],
]);
throwsAndLogs(t, () => assert(false), /Check failed/, [
['error', 'LOGGED ERROR:', 'Check failed'],
]);
throwsAndLogs(t, () => assert(false, 'foo'), /foo/, [
['error', 'LOGGED ERROR:', 'foo'],
]);

throwsAndLogs(t, () => assert.fail(), /Assert failed/, [
['error', 'LOGGED ERROR:', 'Assert failed'],
]);
throwsAndLogs(t, () => assert.fail('foo'), /foo/, [
['error', 'LOGGED ERROR:', 'foo'],
]);
} catch (e) {
console.log('unexpected exception', e);
t.assert(false, e);
} finally {
t.end();
}
throwsAndLogs(t, () => assert.fail(), /Assert failed/, [
['error', 'LOGGED ERROR:', 'Assert failed'],
]);
throwsAndLogs(t, () => assert.fail('foo'), /foo/, [
['error', 'LOGGED ERROR:', 'foo'],
]);
});

test('assert equals', t => {
try {
assert.equal(2 + 3, 5);
throwsAndLogs(t, () => assert.equal(5, 6, 'foo'), /foo/, [
['error', 'LOGGED ERROR:', 'foo'],
]);
throwsAndLogs(
t,
() => assert.equal(5, 6, details`${5} !== ${6}`),
/\(a number\) !== \(a number\)/,
[['error', 'LOGGED ERROR:', 5, '!==', 6]],
);
throwsAndLogs(
t,
() => assert.equal(5, 6, details`${5} !== ${q(6)}`),
/\(a number\) !== 6/,
[['error', 'LOGGED ERROR:', 5, '!==', 6]],
);
assert.equal(2 + 3, 5);
throwsAndLogs(t, () => assert.equal(5, 6, 'foo'), /foo/, [
['error', 'LOGGED ERROR:', 'foo'],
]);
throwsAndLogs(
t,
() => assert.equal(5, 6, details`${5} !== ${6}`),
/\(a number\) !== \(a number\)/,
[['error', 'LOGGED ERROR:', 5, '!==', 6]],
);
throwsAndLogs(
t,
() => assert.equal(5, 6, details`${5} !== ${q(6)}`),
/\(a number\) !== 6/,
[['error', 'LOGGED ERROR:', 5, '!==', 6]],
);

assert.equal(NaN, NaN);
throwsAndLogs(
t,
() => assert.equal(-0, 0),
/Expected \(a number\) is same as \(a number\)/,
[['error', 'LOGGED ERROR:', 'Expected', -0, 'is same as', 0]],
);
} catch (e) {
console.log('unexpected exception', e);
t.assert(false, e);
} finally {
t.end();
}
assert.equal(NaN, NaN);
throwsAndLogs(
t,
() => assert.equal(-0, 0),
/Expected \(a number\) is same as \(a number\)/,
[['error', 'LOGGED ERROR:', 'Expected', -0, 'is same as', 0]],
);
});

test('assert typeof', t => {
try {
assert.typeof(2, 'number');
throwsAndLogs(
t,
() => assert.typeof(2, 'string'),
/\(a number\) must be a string/,
[['error', 'LOGGED ERROR:', 2, 'must be a string']],
);
throwsAndLogs(t, () => assert.typeof(2, 'string', 'foo'), /foo/, [
['error', 'LOGGED ERROR:', 'foo'],
]);
} catch (e) {
console.log('unexpected exception', e);
t.assert(false, e);
} finally {
t.end();
}
assert.typeof(2, 'number');
throwsAndLogs(
t,
() => assert.typeof(2, 'string'),
/\(a number\) must be a string/,
[['error', 'LOGGED ERROR:', 2, 'must be a string']],
);
throwsAndLogs(t, () => assert.typeof(2, 'string', 'foo'), /foo/, [
['error', 'LOGGED ERROR:', 'foo'],
]);
});

test('assert q', t => {
try {
throwsAndLogs(
t,
() => assert.fail(details`<${'bar'},${q('baz')}>`),
/<\(a string\),"baz">/,
[['error', 'LOGGED ERROR:', '<', 'bar', ',', 'baz', '>']],
);
throwsAndLogs(
t,
() => assert.fail(details`<${'bar'},${q('baz')}>`),
/<\(a string\),"baz">/,
[['error', 'LOGGED ERROR:', '<', 'bar', ',', 'baz', '>']],
);

const list = ['a', 'b', 'c'];
throwsAndLogs(
t,
() => assert.fail(details`${q(list)}`),
/\["a","b","c"\]/,
[['error', 'LOGGED ERROR:', ['a', 'b', 'c']]],
);
const repeat = { x: list, y: list };
throwsAndLogs(
t,
() => assert.fail(details`${q(repeat)}`),
/{"x":\["a","b","c"\],"y":"<\*\*seen\*\*>"}/,
[['error', 'LOGGED ERROR:', { x: ['a', 'b', 'c'], y: ['a', 'b', 'c'] }]],
);
const list = ['a', 'b', 'c'];
throwsAndLogs(t, () => assert.fail(details`${q(list)}`), /\["a","b","c"\]/, [
['error', 'LOGGED ERROR:', ['a', 'b', 'c']],
]);
const repeat = { x: list, y: list };
throwsAndLogs(
t,
() => assert.fail(details`${q(repeat)}`),
/{"x":\["a","b","c"\],"y":"<\*\*seen\*\*>"}/,
[['error', 'LOGGED ERROR:', { x: ['a', 'b', 'c'], y: ['a', 'b', 'c'] }]],
);

// Make it into a cycle
list[1] = list;
throwsAndLogs(
t,
() => assert.fail(details`${q(list)}`),
/\["a","<\*\*seen\*\*>","c"\]/,
[['error', 'LOGGED ERROR:', ['a', list, 'c']]],
);
throwsAndLogs(
t,
() => assert.fail(details`${q(repeat)}`),
/{"x":\["a","<\*\*seen\*\*>","c"\],"y":"<\*\*seen\*\*>"}/,
[['error', 'LOGGED ERROR:', { x: list, y: ['a', list, 'c'] }]],
);
} catch (e) {
console.log('unexpected exception', e);
t.assert(false, e);
} finally {
t.end();
}
// Make it into a cycle
list[1] = list;
throwsAndLogs(
t,
() => assert.fail(details`${q(list)}`),
/\["a","<\*\*seen\*\*>","c"\]/,
[['error', 'LOGGED ERROR:', ['a', list, 'c']]],
);
throwsAndLogs(
t,
() => assert.fail(details`${q(repeat)}`),
/{"x":\["a","<\*\*seen\*\*>","c"\],"y":"<\*\*seen\*\*>"}/,
[['error', 'LOGGED ERROR:', { x: list, y: ['a', list, 'c'] }]],
);
});
29 changes: 16 additions & 13 deletions packages/assert/test/throwsAndLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,23 @@ const fakeConsole = makeFakeConsole(fakeLog);
// [['error', 'what ', err]]);
// ```
function throwsAndLogs(t, thunk, regexp, goldenLog) {
t.throws(() => {
fakeLog.length = 0;
const originalConsole = console;
// eslint-disable-next-line no-global-assign
console = fakeConsole;
try {
// If thunk() throws, we restore the console but not the fakeLog array.
// An outer catcher could then both takeLog() and check the error.
thunk();
} finally {
t.throws(
() => {
fakeLog.length = 0;
const originalConsole = console;
// eslint-disable-next-line no-global-assign
console = originalConsole;
}
}, regexp);
console = fakeConsole;
try {
// If thunk() throws, we restore the console but not the fakeLog array.
// An outer catcher could then both takeLog() and check the error.
thunk();
} finally {
// eslint-disable-next-line no-global-assign
console = originalConsole;
}
},
{ message: regexp },
);
t.deepEqual(takeLog(), goldenLog);
}
harden(throwsAndLogs);
Expand Down
15 changes: 8 additions & 7 deletions packages/import-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"build": "exit 0",
"test": "tape -r esm 'test/**/test*.js' | tap-spec",
"test": "ava",
"lint-fix": "eslint --fix '**/*.js'",
"lint-check": "eslint '**/*.js'",
"lint-fix-jessie": "eslint -c '.eslintrc-jessie.js' --fix '**/*.js'",
Expand All @@ -27,19 +27,20 @@
"url": "https://github.com/Agoric/agoric-sdk/issues"
},
"homepage": "https://github.com/Agoric/agoric-sdk#readme",
"dependencies": {
},
"dependencies": {},
"devDependencies": {
"@agoric/install-ses": "^0.2.0",
"esm": "^3.2.25",
"tap-spec": "^5.0.0",
"tape": "^4.11.0",
"tape-promise": "^4.0.0"
"ava": "^3.11.1",
"esm": "^3.2.25"
},
"files": [
"src/",
"NEWS.md"
],
"ava": {
"files": ["test/**/test-*.js"],
"require": ["esm"]
},
"eslintConfig": {
"extends": [
"airbnb-base",
Expand Down
Loading

0 comments on commit 53b3e2e

Please sign in to comment.