relu-core
Version:
290 lines (285 loc) • 6.82 kB
JavaScript
var should = require("should");
var ActionsList = require("../lib/ActionsList");
describe("ActionsList", function() {
it("should update a value", function() {
var al = new ActionsList();
al.update([], 5);
al.x.should.be.eql({
t: 2,
v: 5
});
al.update([], {test: "test"});
al.x.should.be.eql({
t: 2,
v: {test: "test"}
});
});
it("should add and remove values", function() {
var al = new ActionsList();
al.add([], 2, "two");
al.x.should.be.eql([
{ t: 1, i: 2, v: "two" }
]);
al.add([], 1, {test: "test"});
al.x.should.be.eql([
{ t: 1, i: 1, v: {test: "test"} },
{ t: 1, i: 3, v: "two" }
]);
al.remove([], 5);
al.remove([], 0);
al.remove([], 2);
al.x.should.be.eql([
{ t: -1, i: 3 },
{ t: -1, i: 0 },
{ t: 1, i: 0, v: {test: "test"} }
]);
});
it("should access properties", function() {
var al = new ActionsList();
al.update(["updated"], { a: 1 });
al.update(["updated", "b"], 2);
al.add(["array"], 2, 2);
al.add(["array"], 2, 1);
al.update(["updated", "a"], "a");
al.x.should.be.eql({
t: 3,
p: {
updated: {
t: 2,
v: { a: "a", b: 2 }
},
array: [
{ t: 1, i: 2, v: 1 },
{ t: 1, i: 3, v: 2 }
]
}
});
});
it("should access array items", function() {
var al = new ActionsList();
al.update([0, 1], "two");
al.update([0, 0], "one");
al.update([0, 2], "???");
al.update([0, 3], "three");
al.add([0], 0, "zero");
al.remove([0], 3);
al.update([1], [1,2,3]);
al.add([1], 0, 0);
al.add([], 2, [1,2,3]);
al.add([2], 0, 0);
al.x.should.be.eql([
{ t: 0, i: 0, a: [
{ t: -1, i: 2 },
{ t: 0, i: 0, a: { t: 2, v: "one" } },
{ t: 0, i: 1, a: { t: 2, v: "two" } },
{ t: 0, i: 2, a: { t: 2, v: "three" } },
{ t: 1, i: 0, v: "zero" },
]},
{ t: 0, i: 1, a: {t: 2, v: [0,1,2,3]} },
{ t: 1, i: 2, v: [0,1,2,3] }
]);
});
it("should do some fancy splicing", function() {
var al = new ActionsList();
al.splice([], Infinity, "test");
al.splice([], Infinity, "test2");
al.splice([], 2, 1, "test3");
al.splice([], Infinity);
al.update([0], [1,2,3,4]);
al.splice([0], Infinity, "test");
al.splice([0], Infinity, "test2");
al.splice([0], 2, 1, "test3");
al.splice([0], Infinity);
});
it("should access mixed", function() {
var al = new ActionsList();
});
it("should order actions correctly", function() {
var al = new ActionsList();
function add(idx, item) {
al.add([], idx, item);
}
function remove(idx) {
al.remove([], idx);
}
add(10, "A");
al.x.should.be.eql([
{t: 1, i: 10, v: "A"}
]);
add(20, "B");
al.x.should.be.eql([
{t: 1, i: 10, v: "A"},
{t: 1, i: 20, v: "B"}
]);
add(15, "C");
al.x.should.be.eql([
{t: 1, i: 10, v: "A"},
{t: 1, i: 15, v: "C"},
{t: 1, i: 21, v: "B"}
]);
remove(20, "D");
al.x.should.be.eql([
{t: -1, i: 18},
{t: 1, i: 10, v: "A"},
{t: 1, i: 15, v: "C"},
{t: 1, i: 20, v: "B"}
]);
remove(15, "C");
al.x.should.be.eql([
{t: -1, i: 18},
{t: 1, i: 10, v: "A"},
{t: 1, i: 19, v: "B"}
]);
remove(15, "E");
al.x.should.be.eql([
{t: -1, i: 18},
{t: -1, i: 14},
{t: 1, i: 10, v: "A"},
{t: 1, i: 18, v: "B"}
]);
remove(30, "F");
al.x.should.be.eql([
{t: -1, i: 30},
{t: -1, i: 18},
{t: -1, i: 14},
{t: 1, i: 10, v: "A"},
{t: 1, i: 18, v: "B"}
]);
});
it("should add up modifications", function() {
var al = new ActionsList();
var fn = function() {};
var fn2 = function() {};
al.modify([], fn);
al.modify([], fn2);
al.x.should.be.eql({
t: 4,
m: [fn, fn2]
});
al.update([], 4);
al.x.should.be.eql({
t: 2,
v: 4
});
al.modify([], function(x) {return x+1});
al.x.should.be.eql({
t: 2,
v: 5
});
});
it("should refer to old array indices (simple remove)", function() {
var al = new ActionsList();
al.removeBefore([], 2);
al.removeBefore([], 1);
al.removeBefore([], 3);
al.x.should.be.eql([
{t: -1, i: 3},
{t: -1, i: 2},
{t: -1, i: 1},
]);
al.removeBefore([], 0);
al.removeBefore([], 4);
al.x.should.be.eql([
{t: -1, i: 4},
{t: -1, i: 3},
{t: -1, i: 2},
{t: -1, i: 1},
{t: -1, i: 0},
]);
});
it("should refer to old array indices (remove)", function() {
var al = new ActionsList();
al.update([10], 11);
al.add([], 10, 10);
al.add([], 20, 20);
al.update([21], 21);
al.x.should.be.eql([
{t: 0, i: 10, a: {t: 2, v: 11}},
{t: 0, i: 19, a: {t: 2, v: 21}},
{t: 1, i: 10, v: 10},
{t: 1, i: 20, v: 20},
]);
al.removeBefore([], 15);
al.x.should.be.eql([
{t: -1, i: 15},
{t: 0, i: 10, a: {t: 2, v: 11}},
{t: 0, i: 18, a: {t: 2, v: 21}},
{t: 1, i: 10, v: 10},
{t: 1, i: 19, v: 20},
]);
});
it("should refer to old array indices (add)", function() {
var al = new ActionsList();
al.update([10], 11);
al.add([], 10, 10);
al.add([], 20, 20);
al.update([21], 21);
al.x.should.be.eql([
{t: 0, i: 10, a: {t: 2, v: 11}},
{t: 0, i: 19, a: {t: 2, v: 21}},
{t: 1, i: 10, v: 10},
{t: 1, i: 20, v: 20},
]);
al.spliceBefore([], 15, 1, 1, 2);
al.x.should.be.eql([
{t: -1, i: 15},
{t: 0, i: 10, a: {t: 2, v: 11}},
{t: 0, i: 18, a: {t: 2, v: 21}},
{t: 1, i: 10, v: 10},
{t: 1, i: 16, v: 1},
{t: 1, i: 17, v: 2},
{t: 1, i: 21, v: 20},
]);
});
it("should refer to old array indices (add complex)", function() {
var al = new ActionsList();
al.add([], 10, 10);
al.add([], 20, 20);
al.remove([], 5);
al.remove([], 15);
al.x.should.be.eql([
{t: -1, i: 15},
{t: -1, i: 5},
{t: 1, i: 9, v: 10},
{t: 1, i: 18, v: 20},
]);
al.spliceBefore([], 15, 1, 1, 2);
al.x.should.be.eql([
{t: -1, i: 15},
{t: -1, i: 5},
{t: 1, i: 9, v: 10},
{t: 1, i: 15, v: 1},
{t: 1, i: 16, v: 2},
{t: 1, i: 20, v: 20},
]);
});
it("issue 1", function() {
var al = new ActionsList();
al.update([0], 0);
al.update([1], 1);
al.update([5], 5);
al.removeBefore([], 2);
al.removeBefore([], 3);
al.removeBefore([], 4);
al.x.should.be.eql([
{t: -1, i: 4},
{t: -1, i: 3},
{t: -1, i: 2},
{t: 0, i: 0, a: { t: 2, v: 0}},
{t: 0, i: 1, a: { t: 2, v: 1}},
{t: 0, i: 2, a: { t: 2, v: 5}},
]);
});
it("remove already removed", function() {
var al = new ActionsList();
al.update([0], 0);
al.update([5], 5);
al.removeBefore([], 2);
al.removeBefore([], 2);
al.x.should.be.eql([
{t: -1, i: 2},
{t: 0, i: 0, a: { t: 2, v: 0}},
{t: 0, i: 4, a: { t: 2, v: 5}},
]);
});
});