This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add marshal tests, circle ci config, reorganize (#2)
* add marshal tests, reorganize * lint fix, add circle ci
- Loading branch information
1 parent
87e3e10
commit 54a78f0
Showing
6 changed files
with
254 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
version: 2.0 | ||
|
||
aliases: | ||
|
||
install_npm_default: &install_npm | ||
run: | ||
name: Update npm | ||
command: 'sudo npm install -g npm@latest' | ||
restore_cache_default: &restore_cache # special step to restore the dependency cache | ||
restore_cache: | ||
# Read about caching dependencies: https://circleci.com/docs/2.0/caching/ | ||
key: dependency-cache-{{ checksum "package.json" }} | ||
install_npm_packages_default: &install_npm_packages | ||
run: | ||
name: Install npm packages | ||
command: npm install | ||
save_cache_default: &save_cache # special step to save the dependency cache | ||
save_cache: | ||
key: dependency-cache-{{ checksum "package.json" }} | ||
paths: | ||
- ./node_modules | ||
|
||
jobs: | ||
|
||
# Lint and Test | ||
|
||
lint_and_test_node_11: &lint_and_test | ||
docker: | ||
- image: circleci/node:11 | ||
steps: | ||
- checkout | ||
- *install_npm | ||
- *restore_cache | ||
- *install_npm_packages | ||
- *save_cache | ||
# We run the tests first because that builds the kernel as the | ||
# first step | ||
- run: &test | ||
name: Test | ||
command: npm test | ||
- run: &lint | ||
name: Lint | ||
command: npm run lint-check | ||
|
||
|
||
# Automated Npm Audit Fix PR | ||
|
||
npm_audit_node_11: &npm_audit | ||
docker: | ||
- image: circleci/golang:1.12.0-node | ||
steps: | ||
- checkout | ||
- run: | ||
name: Update npm | ||
command: 'sudo npm install -g npm@latest' | ||
- run: | ||
name: install hub | ||
command: | | ||
set -xe | ||
go get -u -d github.com/github/hub | ||
cd /go/src/github.com/github/hub | ||
go install github.com/github/hub | ||
- run: | ||
name: Submit PR if npm audit fix makes changes | ||
command: ./scripts/npm-audit-fix.sh | ||
|
||
|
||
workflows: | ||
version: 2 | ||
test_all: | ||
jobs: | ||
- lint_and_test_node_11 | ||
nightly: | ||
triggers: | ||
- schedule: | ||
cron: "0 0 * * *" | ||
filters: | ||
branches: | ||
only: | ||
- master | ||
jobs: | ||
- npm_audit_node_11 |
Empty file.
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,169 @@ | ||
/* globals BigInt */ | ||
|
||
import { test } from 'tape-promise/tape'; | ||
import harden from '@agoric/harden'; | ||
import { makeMarshal, mustPassByPresence } from '../../util/marshal'; | ||
|
||
// this only includes the tests that do not use liveSlots | ||
|
||
test('serialize static data', t => { | ||
const m = makeMarshal(); | ||
const ser = val => m.serialize(val); | ||
t.throws(() => ser([1, 2]), /cannot pass non-frozen objects like .*/); | ||
t.deepEqual(ser(harden([1, 2])), { argsString: '[1,2]', slots: [] }); | ||
t.deepEqual(ser(harden({ foo: 1 })), { argsString: '{"foo":1}', slots: [] }); | ||
t.deepEqual(ser(true), { argsString: 'true', slots: [] }); | ||
t.deepEqual(ser(1), { argsString: '1', slots: [] }); | ||
t.deepEqual(ser('abc'), { argsString: '"abc"', slots: [] }); | ||
t.deepEqual(ser(undefined), { | ||
argsString: '{"@qclass":"undefined"}', | ||
slots: [], | ||
}); | ||
t.deepEqual(ser(-0), { argsString: '{"@qclass":"-0"}', slots: [] }); | ||
t.deepEqual(ser(NaN), { argsString: '{"@qclass":"NaN"}', slots: [] }); | ||
t.deepEqual(ser(Infinity), { | ||
argsString: '{"@qclass":"Infinity"}', | ||
slots: [], | ||
}); | ||
t.deepEqual(ser(-Infinity), { | ||
argsString: '{"@qclass":"-Infinity"}', | ||
slots: [], | ||
}); | ||
t.deepEqual(ser(Symbol.for('sym1')), { | ||
argsString: '{"@qclass":"symbol","key":"sym1"}', | ||
slots: [], | ||
}); | ||
let bn; | ||
try { | ||
bn = BigInt(4); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
if (bn) { | ||
t.deepEqual(ser(bn), { | ||
argsString: '{"@qclass":"bigint","digits":"4"}', | ||
slots: [], | ||
}); | ||
} | ||
|
||
let em; | ||
try { | ||
throw new ReferenceError('msg'); | ||
} catch (e) { | ||
em = harden(e); | ||
} | ||
t.deepEqual(ser(em), { | ||
argsString: '{"@qclass":"error","name":"ReferenceError","message":"msg"}', | ||
slots: [], | ||
}); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('unserialize static data', t => { | ||
const m = makeMarshal(); | ||
const uns = val => m.unserialize(val, []); | ||
t.equal(uns('1'), 1); | ||
t.equal(uns('"abc"'), 'abc'); | ||
t.equal(uns('false'), false); | ||
|
||
// JS primitives that aren't natively representable by JSON | ||
t.deepEqual(uns('{"@qclass":"undefined"}'), undefined); | ||
t.ok(Object.is(uns('{"@qclass":"-0"}'), -0)); | ||
t.notOk(Object.is(uns('{"@qclass":"-0"}'), 0)); | ||
t.ok(Object.is(uns('{"@qclass":"NaN"}'), NaN)); | ||
t.deepEqual(uns('{"@qclass":"Infinity"}'), Infinity); | ||
t.deepEqual(uns('{"@qclass":"-Infinity"}'), -Infinity); | ||
t.deepEqual(uns('{"@qclass":"symbol", "key":"sym1"}'), Symbol.for('sym1')); | ||
|
||
// Normal json reviver cannot make properties with undefined values | ||
t.deepEqual(uns('[{"@qclass":"undefined"}]'), [undefined]); | ||
t.deepEqual(uns('{"foo": {"@qclass":"undefined"}}'), { foo: undefined }); | ||
let bn; | ||
try { | ||
bn = BigInt(4); | ||
} catch (e) { | ||
if (!(e instanceof ReferenceError)) { | ||
throw e; | ||
} | ||
} | ||
if (bn) { | ||
t.deepEqual(uns('{"@qclass":"bigint","digits":"1234"}'), BigInt(1234)); | ||
} | ||
|
||
const em1 = uns( | ||
'{"@qclass":"error","name":"ReferenceError","message":"msg"}', | ||
); | ||
t.ok(em1 instanceof ReferenceError); | ||
t.equal(em1.message, 'msg'); | ||
t.ok(Object.isFrozen(em1)); | ||
|
||
const em2 = uns('{"@qclass":"error","name":"TypeError","message":"msg2"}'); | ||
t.ok(em2 instanceof TypeError); | ||
t.equal(em2.message, 'msg2'); | ||
|
||
const em3 = uns('{"@qclass":"error","name":"Unknown","message":"msg3"}'); | ||
t.ok(em3 instanceof Error); | ||
t.equal(em3.message, 'msg3'); | ||
|
||
t.deepEqual(uns('[1,2]'), [1, 2]); | ||
t.deepEqual(uns('{"a":1,"b":2}'), { a: 1, b: 2 }); | ||
t.deepEqual(uns('{"a":1,"b":{"c": 3}}'), { a: 1, b: { c: 3 } }); | ||
|
||
// should be frozen | ||
const arr = uns('[1,2]'); | ||
t.ok(Object.isFrozen(arr)); | ||
const a = uns('{"b":{"c":{"d": []}}}'); | ||
t.ok(Object.isFrozen(a)); | ||
t.ok(Object.isFrozen(a.b)); | ||
t.ok(Object.isFrozen(a.b.c)); | ||
t.ok(Object.isFrozen(a.b.c.d)); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('serialize ibid cycle', t => { | ||
const m = makeMarshal(); | ||
const ser = val => m.serialize(val); | ||
const cycle = ['a', 'x', 'c']; | ||
cycle[1] = cycle; | ||
harden(cycle); | ||
|
||
t.deepEqual(ser(cycle), { | ||
argsString: '["a",{"@qclass":"ibid","index":0},"c"]', | ||
slots: [], | ||
}); | ||
t.end(); | ||
}); | ||
|
||
test('forbid ibid cycle', t => { | ||
const m = makeMarshal(); | ||
const uns = val => m.unserialize(val, []); | ||
t.throws( | ||
() => uns('["a",{"@qclass":"ibid","index":0},"c"]'), | ||
/Ibid cycle at 0/, | ||
); | ||
t.end(); | ||
}); | ||
|
||
test('unserialize ibid cycle', t => { | ||
const m = makeMarshal(); | ||
const uns = val => m.unserialize(val, [], 'warnOfCycles'); | ||
const cycle = uns('["a",{"@qclass":"ibid","index":0},"c"]'); | ||
t.ok(Object.is(cycle[1], cycle)); | ||
t.end(); | ||
}); | ||
|
||
test('null cannot be pass-by-presence', t => { | ||
t.throws(() => mustPassByPresence(null), /null cannot be pass-by-presence/); | ||
t.end(); | ||
}); | ||
|
||
test('mal-formed @qclass', t => { | ||
const m = makeMarshal(); | ||
const uns = val => m.unserialize(val, []); | ||
t.throws(() => uns('{"@qclass": 0}'), /invalid qclass/); | ||
t.end(); | ||
}); |