syndicate-2017/js/test/test-patch.js

141 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2016-02-03 02:11:50 +00:00
"use strict";
2016-01-31 21:55:24 +00:00
var expect = require('expect.js');
var Immutable = require('immutable');
var Trie = require('../src/trie.js');
2016-01-31 21:55:24 +00:00
var Patch = require('../src/patch.js');
var __ = Trie.__;
var _$ = Trie._$;
2016-01-31 21:55:24 +00:00
function checkPrettyPatch(p, expectedAdded, expectedRemoved) {
expect(p.pretty()).to.equal(
2016-08-08 02:44:56 +00:00
('\n-' + expectedRemoved.join('\n-') +
'\n+' + expectedAdded.join('\n+') + '\n'));
2016-01-31 21:55:24 +00:00
}
describe('basic patch compilation', function () {
it('should print as expected', function () {
checkPrettyPatch(Patch.assert([1, 2]),
[' <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.assert(__),
[' ★ {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub(__),
[' observe<1> ★ {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub([1, 2]),
[' observe<1> <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.pub('x'),
[' advertise<1> "x" {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
});
it('should work at nonzero metalevel', function () {
checkPrettyPatch(Patch.assert([1, 2], 0),
[' <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.assert([1, 2], 1),
[' at-meta<1> <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.assert([1, 2], 2),
[' at-meta<1> at-meta<1> <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub([1, 2], 0),
[' observe<1> <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub([1, 2], 1),
[' at-meta<1> observe<1> <2> 1 2 {true}',
' observe<1> at-meta<1> <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub([1, 2], 2),
[' at-meta<1> at-meta<1> observe<1> <2> 1 2 {true}',
' observe<1> at-meta<1> <2> 1 2 {true}',
' observe<1> at-meta<1> at-meta<1> <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
});
});
describe('patch sequencing', function () {
it('should do the right thing in simple cases', function () {
checkPrettyPatch(Patch.assert(__).andThen(Patch.retract(3)),
[' ★ {true}',
' 3 ::: nothing'],
[' 3 {true}']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.assert(3).andThen(Patch.retract(__)),
[' ::: nothing'],
[' ★ {true}']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.assert(__).andThen(Patch.retract(__)),
[' ::: nothing'],
[' ★ {true}']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.assert(3).andThen(Patch.retract(3)),
[' ::: nothing'],
[' 3 {true}']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub([1, __]).andThen(Patch.unsub([1, 2])),
[' observe<1> <2> 1 ★ {true}',
' 2 ::: nothing'],
[' observe<1> <2> 1 2 {true}']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub([__, 2]).andThen(Patch.unsub([1, 2])),
[' observe<1> <2> ★ 2 {true}',
' 1 ::: nothing'],
[' observe<1> <2> 1 2 {true}']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub([__, __]).andThen(Patch.unsub([1, 2])),
[' observe<1> <2> ★ ★ {true}',
' 1 ★ {true}',
' 2 ::: nothing'],
[' observe<1> <2> 1 2 {true}']);
2016-01-31 21:55:24 +00:00
});
2016-02-02 19:35:08 +00:00
it('works for longer chains of asserts and retracts', function () {
var rawPatch =
Patch.assert(1)
.andThen(Patch.retract(2))
.andThen(Patch.retract(3))
.andThen(Patch.assert(4))
.andThen(Patch.retract(99));
checkPrettyPatch(rawPatch,
[' 1 {true}',
' 4 {true}'],
[' 2 {true}',
' 3 {true}',
' 99 {true}']);
2016-02-02 19:35:08 +00:00
});
2016-01-31 21:55:24 +00:00
});
describe('patch lifting', function () {
it('should basically work', function () {
checkPrettyPatch(Patch.assert([1, 2]).lift(),
[' at-meta<1> <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub([1, 2]).lift(),
[' at-meta<1> observe<1> <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.assert([1, 2]).andThen(Patch.assert(Patch.atMeta([1, 2]))).lift(),
[' at-meta<1> at-meta<1> <2> 1 2 {true}',
' <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
});
});
describe('patch dropping', function () {
it('should basically work', function () {
checkPrettyPatch(Patch.assert([1, 2]).drop(),
[' ::: nothing'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub([1, 2]).drop(),
[' ::: nothing'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.sub([1, 2], 1).drop(),
[' observe<1> <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
checkPrettyPatch(Patch.assert([1, 2]).andThen(Patch.assert(Patch.atMeta([1, 2]))).drop(),
[' <2> 1 2 {true}'],
[' ::: nothing']);
2016-01-31 21:55:24 +00:00
});
});