Skip to content

Commit

Permalink
fix: replace openDetail with quoting q
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed May 24, 2020
1 parent 81eb6ba commit ffd671f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 39 deletions.
25 changes: 13 additions & 12 deletions packages/assert/src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ harden(an);
const declassifiers = new WeakSet();

/**
* To "declassify" a substitution value used in a details`...` template literal,
* enclose that substitution expression in a call to openDetail. This states
* that the argument should appear, stringified, in the error message of the
* thrown error.
* To "declassify" and quote a substitution value used in a
* details`...` template literal, enclose that substitution expression
* in a call to q. This states that the argument should appear quoted (with
* JSON.stringify), in the error message of the thrown error. The payload
* itself is still passed unquoted, to the console as it would be without q.
*
* Starting from the example in the `details` comment, say instead that the
* color the sky is supposed to be is also computed. Say that we still don't
Expand All @@ -41,7 +42,7 @@ const declassifiers = new WeakSet();
* assert.equal(
* sky.color,
* color,
* details`${sky.color} should be ${openDetail(color)}`,
* details`${sky.color} should be ${q(color)}`,
* );
* ```
*
Expand All @@ -52,24 +53,24 @@ const declassifiers = new WeakSet();
* @param {*} payload What to declassify
* @returns {StringablePayload} The declassified payload
*/
function openDetail(payload) {
function q(payload) {
const result = harden({
payload,
toString() {
return payload.toString();
return JSON.stringify(payload);
},
});
declassifiers.add(result);
return result;
}
harden(openDetail);
harden(q);

/**
* Use the `details` function as a template literal tag to create
* informative error messages. The assertion functions take such messages
* as optional arguments:
* ```js
* assert(sky.isBlue(), details`${sky.color} should be blue`);
* assert(sky.isBlue(), details`${sky.color} should be "blue"`);
* ```
* The details template tag returns an object that can print itself with the
* formatted message in two ways. It will report the real details to the
Expand Down Expand Up @@ -158,7 +159,7 @@ harden(details);
*/
function fail(optDetails = details`Assert failed`) {
if (typeof optDetails === 'string') {
optDetails = details`${openDetail(optDetails)}`;
optDetails = details`${q(optDetails)}`;
}
throw optDetails.complain();
}
Expand Down Expand Up @@ -215,7 +216,7 @@ function equal(
function assertTypeof(
specimen,
typename,
optDetails = details`${specimen} must be ${openDetail(an(typename))}`,
optDetails = details`${specimen} must be ${q(an(typename))}`,
) {
assert(typeof typename === 'string', details`${typename} must be a string`);
equal(typeof specimen, typename, optDetails);
Expand All @@ -226,4 +227,4 @@ assert.fail = fail;
assert.typeof = assertTypeof;
harden(assert);

export { assert, details, openDetail, an };
export { assert, details, q, an };
4 changes: 2 additions & 2 deletions packages/assert/test/test-assert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from 'tape';
import { an, assert, details, openDetail } from '../src/assert';
import { an, assert, details, q } from '../src/assert';
import { throwsAndLogs } from './throwsAndLogs';

test('an', t => {
Expand Down Expand Up @@ -66,7 +66,7 @@ test('assert', t => {
);
throwsAndLogs(
t,
() => assert.equal(5, 6, details`${5} !== ${openDetail(6)}`),
() => assert.equal(5, 6, details`${5} !== ${q(6)}`),
/\(a number\) !== 6/,
[['error', 'LOGGED ERROR:', 5, '!==', 6]],
);
Expand Down
4 changes: 2 additions & 2 deletions packages/same-structure/src/sameStructure.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import harden from '@agoric/harden';
import { sameValueZero, passStyleOf } from '@agoric/marshal';
import { assert, details, openDetail } from '@agoric/assert';
import { assert, details, q } from '@agoric/assert';

// Shim of Object.fromEntries from
// https://github.com/tc39/proposal-object-from-entries/blob/master/polyfill.js
Expand Down Expand Up @@ -166,7 +166,7 @@ function pathStr(path) {
function mustBeSameStructureInternal(left, right, message, path) {
function complain(problem) {
assert.fail(
details`${openDetail(message)}: ${openDetail(problem)} at ${openDetail(
details`${q(message)}: ${q(problem)} at ${q(
pathStr(path),
)}: (${left}) vs (${right})`,
);
Expand Down
9 changes: 3 additions & 6 deletions packages/store/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @ts-check

import rawHarden from '@agoric/harden';
import { assert, details, openDetail } from '@agoric/assert';
import { assert, details, q } from '@agoric/assert';

const harden = /** @type {<T>(x: T) => T} */ (rawHarden);

Expand Down Expand Up @@ -32,12 +32,9 @@ const harden = /** @type {<T>(x: T) => T} */ (rawHarden);
function makeStore(keyName = 'key') {
const store = new Map();
const assertKeyDoesNotExist = key =>
assert(
!store.has(key),
details`${openDetail(keyName)} already registered: ${key}`,
);
assert(!store.has(key), details`${q(keyName)} already registered: ${key}`);
const assertKeyExists = key =>
assert(store.has(key), details`${openDetail(keyName)} not found: ${key}`);
assert(store.has(key), details`${q(keyName)} not found: ${key}`);
return harden({
has: key => store.has(key),
init: (key, value) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-frontend/src/utils/fetch-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function doFetch(req) {
}

const socket = websocket;

let resolve;
const p = new Promise(res => {
resolve = res;
Expand Down
9 changes: 3 additions & 6 deletions packages/weak-store/src/weakStore.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) 2019 Agoric, under Apache license 2.0

import harden from '@agoric/harden';
import { assert, details, openDetail } from '@agoric/assert';
import { assert, details, q } from '@agoric/assert';
/**
* Distinguishes between adding a new key (init) and updating or
* referencing a key (get, set, delete).
Expand All @@ -13,12 +13,9 @@ import { assert, details, openDetail } from '@agoric/assert';
function makeStore(keyName = 'key') {
const wm = new WeakMap();
const assertKeyDoesNotExist = key =>
assert(
!wm.has(key),
details`${openDetail(keyName)} already registered: ${key}`,
);
assert(!wm.has(key), details`${q(keyName)} already registered: ${key}`);
const assertKeyExists = key =>
assert(wm.has(key), details`${openDetail(keyName)} not found: ${key}`);
assert(wm.has(key), details`${q(keyName)} not found: ${key}`);
return harden({
has: key => wm.has(key),
init: (key, value) => {
Expand Down
16 changes: 8 additions & 8 deletions packages/zoe/src/objArrayConversion.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, details, openDetail } from '@agoric/assert';
import { assert, details, q } from '@agoric/assert';

export const arrayToObj = (array, keywords) => {
assert(
Expand All @@ -14,7 +14,7 @@ export const objToArray = (obj, keywords) => {
const keys = Object.getOwnPropertyNames(obj);
assert(
keys.length === keywords.length,
details`object keys (${openDetail(keys)}) and keywords (${openDetail(
details`object keys (${q(keys)}) and keywords (${q(
keywords,
)}) must be of equal length`,
);
Expand All @@ -26,9 +26,9 @@ export const assertSubset = (whole, part) => {
assert.typeof(key, 'string');
assert(
whole.includes(key),
details`key ${openDetail(
key,
)} was not one of the expected keys (${openDetail(whole.join(', '))})`,
details`key ${q(key)} was not one of the expected keys (${q(
whole.join(', '),
)})`,
);
});
};
Expand All @@ -38,7 +38,7 @@ export const objToArrayAssertFilled = (obj, keywords) => {
const keys = Object.getOwnPropertyNames(obj);
assert(
keys.length === keywords.length,
details`object keys (${openDetail(keys)}) and keywords (${openDetail(
details`object keys (${q(keys)}) and keywords (${q(
keywords,
)}) must be of equal length`,
);
Expand All @@ -47,7 +47,7 @@ export const objToArrayAssertFilled = (obj, keywords) => {
return keywords.map(keyword => {
assert(
obj[keyword] !== undefined,
details`obj[keyword] must be defined for keyword ${openDetail(keyword)}`,
details`obj[keyword] must be defined for keyword ${q(keyword)}`,
);
return obj[keyword];
});
Expand All @@ -63,7 +63,7 @@ export const filterObj = /** @type {function<T>(T, string[]): T} */ (
subsetKeywords.forEach(keyword => {
assert(
obj[keyword] !== undefined,
details`obj[keyword] must be defined for keyword ${openDetail(keyword)}`,
details`obj[keyword] must be defined for keyword ${q(keyword)}`,
);
newObj[keyword] = obj[keyword];
});
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import harden from '@agoric/harden';
import { E, HandledPromise } from '@agoric/eventual-send';
import makeStore from '@agoric/weak-store';
import produceIssuer from '@agoric/ertp';
import { assert, details, openDetail } from '@agoric/assert';
import { assert, details, q } from '@agoric/assert';
import { produceNotifier } from '@agoric/notifier';
import { producePromise } from '@agoric/produce-promise';

Expand Down Expand Up @@ -871,7 +871,7 @@ const makeZoe = (additionalEndowments = {}) => {
} else {
assert(
exitKind === 'waived',
details`exit kind was not recognized: ${openDetail(exitKind)}`,
details`exit kind was not recognized: ${q(exitKind)}`,
);
}

Expand Down

0 comments on commit ffd671f

Please sign in to comment.