Skip to content

Commit

Permalink
got code coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Prest committed Jul 1, 2019
1 parent 492be3a commit b878440
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 11 deletions.
16 changes: 8 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ export module Serial {
return result;
} else {
var oresult: object = Object.create(Object.getPrototypeOf(o));
if (o.constructor) oresult.constructor();
oresult.constructor();
for (var prop in o) {
if (typeof o[prop] !== "function" || !oresult[prop])
oresult[prop] = copy(o[prop]);
oresult[prop] = copy(o[prop]);
}
return oresult;
}
Expand All @@ -84,11 +83,10 @@ export module Serial {
[ 4,-1,-1,-1,10,-1,-1,-1,-1,-1,-1,-1], // 7 = center front & x & y
];

function reorderLabelsIn(labels, align, def: string | null = null) {
function reorderLabelsIn(labels, align) {
var ret: Array<any> = [];
for (var i = 0; i < labels.length; ++i) {
if (labels[i] && labels[i] !== def)
ret[labelMap[align][i]] = labels[i];
if (labels[i]) ret[labelMap[align][i]] = labels[i];
}
return ret;
}
Expand Down Expand Up @@ -166,8 +164,8 @@ export module Serial {
if (item.c) current.color = item.c;
if (item.t) {
var split = item.t.split("\n");
current.default.textColor = split[0];
current.textColor = reorderLabelsIn(split, align, current.default.textColor);
if (split[0] != "") current.default.textColor = split[0];
current.textColor = reorderLabelsIn(split, align);
}
if (item.x) current.x += item.x;
if (item.y) current.y += item.y;
Expand Down Expand Up @@ -200,6 +198,8 @@ export module Serial {
for (let prop in kbd.meta) {
if (rows[r][prop]) kbd.meta[prop] = rows[r][prop];
}
} else {
deserializeError("unexpected", rows[r]);
}
}
return kbd;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"prepublish": "npm run build",
"build": "tsc",
"test": "mocha --reporter spec",
"cover": "node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec"
"cover": "node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- -R spec"
},
"repository": {
"type": "git",
Expand Down
107 changes: 105 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ describe("deserialization", function() {
expect(result).to.throw();
});

it("should fail on non array/object data", function() {
var result = () => kbd.Serial.deserialize(["test"]);
expect(result).to.throw();
});

it("should return empty keyboard on empty array", function() {
var result = kbd.Serial.deserialize([]);
expect(result).to.be.an.instanceOf(kbd.Keyboard);
Expand Down Expand Up @@ -80,7 +85,9 @@ describe("deserialization", function() {
expect(result.keys[0].x2).to.equal(0);
expect(result.keys[0].y2).to.equal(0);

var result = kbd.Serial.deserialize([[{ x: 1, y: 1, x2: 2, y2: 2 }, "1"]]);
var result = kbd.Serial.deserialize([
[{ x: 1, y: 1, x2: 2, y2: 2 }, "1"]
]);
expect(result).to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys).to.have.length(1);
expect(result.keys[0].x).to.not.equal(0);
Expand Down Expand Up @@ -132,6 +139,49 @@ describe("deserialization", function() {
expect(result.keys[1].nub).to.be.false;
expect(result.keys[1].decal).to.be.false;
});

it("should propagate the ghost flag", function() {
var result = kbd.Serial.deserialize([["0", { g: true }, "1", "2"]]);
expect(result).to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys).to.have.length(3);
expect(result.keys[0].ghost).to.be.false;
expect(result.keys[1].ghost).to.be.true;
expect(result.keys[2].ghost).to.be.true;
});

it("should propagate the profile flag", function() {
var result = kbd.Serial.deserialize([["0", { p: "DSA" }, "1", "2"]]);
expect(result).to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys).to.have.length(3);
expect(result.keys[0].profile).to.be.empty;
expect(result.keys[1].profile).to.equal("DSA");
expect(result.keys[2].profile).to.equal("DSA");
});

it("should propagate switch properties", function() {
var result = kbd.Serial.deserialize([["1", { sm: "cherry" }, "2", "3"]]);
expect(result, "sm").to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys, "sm").to.have.length(3);
expect(result.keys[0].sm, "sm_0").to.equal("");
expect(result.keys[1].sm, "sm_1").to.equal("cherry");
expect(result.keys[2].sm, "sm_2").to.equal("cherry");

var result = kbd.Serial.deserialize([["1", { sb: "cherry" }, "2", "3"]]);
expect(result, "sb").to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys, "sb").to.have.length(3);
expect(result.keys[0].sb, "sb_0").to.equal("");
expect(result.keys[1].sb, "sb_1").to.equal("cherry");
expect(result.keys[2].sb, "sb_2").to.equal("cherry");

var result = kbd.Serial.deserialize([
["1", { st: "MX1A-11Nx" }, "2", "3"]
]);
expect(result, "st").to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys, "st").to.have.length(3);
expect(result.keys[0].st, "st_0").to.equal("");
expect(result.keys[1].st, "st_1").to.equal("MX1A-11Nx");
expect(result.keys[2].st, "st_2").to.equal("MX1A-11Nx");
});
});

describe("of text color", function() {
Expand All @@ -154,7 +204,9 @@ describe("deserialization", function() {
expect(result).to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys).to.have.length(1);
expect(result.keys[0].default.textColor).to.equal("#444444");
expect(result.keys[0].textColor).to.have.length(0);
for (var i = 0; i < 12; ++i) {
expect(result.keys[0].textColor[i], `[${i}]`).to.be.undefined;
}
});

it("should handle generic case", function() {
Expand Down Expand Up @@ -196,6 +248,35 @@ describe("deserialization", function() {
else expect(color, `i=${i}`).to.equal(result.keys[0].labels[i]);
}
});

it("should not reset default color if blank", function() {
var result = kbd.Serial.deserialize([
[{ t: "#ff0000" }, "1", { t: "\n#00ff00" }, "2"]
]);
expect(result).to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys).to.have.length(2);
expect(result.keys[0].default.textColor, "[0]").to.equal("#ff0000");
expect(result.keys[1].default.textColor, "[1]").to.equal("#ff0000");
});

it("should delete values equal to the default", function() {
var result = kbd.Serial.deserialize([
[
{ t: "#ff0000" },
"1",
{ t: "\n#ff0000" },
"\n2",
{ t: "\n#00ff00" },
"\n3"
]
]);
expect(result).to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys).to.have.length(3);
expect(result.keys[1].labels[6]).to.equal("2");
expect(result.keys[1].textColor[6]).to.be.undefined;
expect(result.keys[2].labels[6]).to.equal("3");
expect(result.keys[2].textColor[6]).to.equal("#00ff00");
});
});

describe("of rotation", function() {
Expand Down Expand Up @@ -345,6 +426,28 @@ describe("deserialization", function() {
}
}
});

it("should not reset default size if blank", function() {
var result = kbd.Serial.deserialize([
[{ f: 1 }, "1", { fa: [, 2] }, "2"]
]);
expect(result).to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys).to.have.length(2);
expect(result.keys[0].default.textSize, "[0]").to.equal(1);
expect(result.keys[1].default.textSize, "[1]").to.equal(1);
});

it("should delete values equal to the default", function() {
var result = kbd.Serial.deserialize([
[{ f: 1 }, "1", { fa: "\n1" }, "\n2", { fa: "\n2" }, "\n3"]
]);
expect(result).to.be.an.instanceOf(kbd.Keyboard);
expect(result.keys).to.have.length(3);
expect(result.keys[1].labels[6]).to.equal("2");
expect(result.keys[1].textSize[6]).to.be.undefined;
expect(result.keys[2].labels[6]).to.equal("3");
expect(result.keys[2].textSize[6]).to.equal("2");
});
});

describe("of strings", function() {
Expand Down

0 comments on commit b878440

Please sign in to comment.