Skip to content

Commit

Permalink
better test coverage, implement handleEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
mnater committed May 27, 2018
1 parent 2411d77 commit cbc2b25
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 18 deletions.
21 changes: 11 additions & 10 deletions hyphenopoly.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ function loadWasm() {
`${H.c.paths.maindir}hyphenEngine.wasm`,
function cb(err, data) {
if (err) {
console.log(err);
H.events.dispatch("error", {"msg": `${H.c.paths.maindir}hyphenEngine.wasm not found.`});
H.events.dispatch("error", {
"key": "hyphenEngine",
"msg": `${H.c.paths.maindir}hyphenEngine.wasm not found.`
});
} else {
H.binaries.hyphenEngine = new Uint8Array(data).buffer;
H.events.dispatch("engineLoaded");
Expand All @@ -87,7 +89,7 @@ function loadHpb(lang) {
function cb(err, data) {
if (err) {
H.events.dispatch("error", {
"lang": lang,
"key": lang,
"msg": `${H.c.paths.patterndir}${lang}.hpb not found.`
});
} else {
Expand Down Expand Up @@ -556,6 +558,11 @@ H.config = function config(userConfig) {
);
});
H.c = settings;
if (H.c.handleEvent) {
Object.keys(H.c.handleEvent).forEach(function add(name) {
H.events.addListener(name, H.c.handleEvent[name], true);
});
}
loadWasm();
const result = new Map();
H.c.require.forEach(function each(lang) {
Expand All @@ -568,7 +575,7 @@ H.config = function config(userConfig) {
});
H.events.addListener("error", function handler(e) {
e.preventDefault();
if (e.lang === lang) {
if (e.key === lang || e.key === "hyphenEngine") {
reject(e.msg);
}
});
Expand Down Expand Up @@ -670,12 +677,6 @@ H.config = function config(userConfig) {
}
}

if (H.handleEvent) {
Object.keys(H.handleEvent).forEach(function add(name) {
addListener(name, H.handleEvent[name], true);
});
}

H.events = empty();
H.events.dispatch = dispatch;
H.events.define = define;
Expand Down
69 changes: 61 additions & 8 deletions test/hyphenopoly.module_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,22 @@ describe("hyphenopoly.module", function () {
assert.equal(hyphenopoly.get("en-us").toString(), "[object Promise]");
});

it("rejects when a language is not known", function (done) {
it("throws when hyphenEngine.wasm can't be found.", function (done) {
H9Y.config({
"require": ["en-us"],
"paths": {
"maindir": "fail/",
"patterndir": `./patterns/`
}
}).catch(
function (e) {
assert.equal(e.slice(-28), "hyphenEngine.wasm not found.");
done();
}
);
});

it("throws when a language is not known", function (done) {
H9Y.config({"require": ["en"]}).catch(
function (e) {
assert.equal(e.slice(-27), "/patterns/en.hpb not found.");
Expand Down Expand Up @@ -79,7 +94,7 @@ describe("hyphenopoly.module", function () {

it("uses language specific exceptions", function (done) {
H9Y.config({
"exceptions": {"de": "Silben-trennung"},
"exceptions": {"de": "Silben-trennung, Sil-ben-trennung"},
"hyphen": "•",
"require": ["de"]
}).then(
Expand All @@ -99,12 +114,15 @@ describe("hyphenopoly.module", function () {

it("uses language agnostic (global) exceptions", function (done) {
H9Y.config({
"exceptions": {"global": "Silben-trennung"},
"exceptions": {
"global": "Silben-trennung",
"de": "Algo-rithmus"
},
"hyphen": "•",
"require": ["de"]
}).then(
function (hyphenateText) {
if (hyphenateText("Silbentrennung") === "Silben•trennung") {
if (hyphenateText("Silbentrennung Algorithmus") === "Silben•trennung Algo•rithmus") {
done();
} else {
done(new Error(hyphenateText("Silbentrennung")));
Expand Down Expand Up @@ -164,10 +182,10 @@ describe("hyphenopoly.module", function () {
"require": ["de"]
}).then(
function (hyphenateText) {
if (hyphenateText("Silbentrennungs-Algorithmus") === "Silbentrennungs-" + String.fromCharCode(8203) +"Algorithmus") {
if (hyphenateText("Silbentrennungs-Algorithmus Alpha-Version") === "Silbentrennungs-" + String.fromCharCode(8203) +"Algorithmus Alpha-" + String.fromCharCode(8203) + "Version") {
done();
} else {
done(new Error(hyphenateText("Silbentrennungs-Algorithmus")));
done(new Error(hyphenateText("Silbentrennungs-Algorithmus Alpha-Version")));
}
}
).catch(
Expand All @@ -184,10 +202,10 @@ describe("hyphenopoly.module", function () {
"require": ["de"]
}).then(
function (hyphenateText) {
if (hyphenateText("Silbentrennungs-Algorithmus") === "Sil•ben•tren•nungs-Al•go•rith•mus") {
if (hyphenateText("Silbentrennungs-Algorithmus Alpha-Version") === "Sil•ben•tren•nungs-Al•go•rith•mus Alpha-Ver•si•on") {
done();
} else {
done(new Error(hyphenateText("Silbentrennungs-Algorithmus")));
done(new Error(hyphenateText("Silbentrennungs-Algorithmus Alpha-Version")));
}
}
).catch(
Expand Down Expand Up @@ -295,5 +313,40 @@ describe("hyphenopoly.module", function () {
}
);
});

it("handles orphanControl: 3 with hyphen set to *", function (done) {
H9Y.config({
"hyphen": "*",
"orphanControl": 3,
"require": ["de"]
}).then(
function (hyphenateText) {
if (hyphenateText("Die Asse essen lieber gesunde Esswaren") === "Die Asse essen lie*ber ge*sun*de" + String.fromCharCode(160) + "Esswaren") {
done();
} else {
done(new Error(hyphenateText("Die Asse essen lieber gesunde Esswaren")));
}
}
).catch(
function (e) {
done(new Error(e));
}
);
});

it("sets custom listener", function (done) {
H9Y.config({
"require": ["de"],
"handleEvent": {
"engineReady": function (e) {
done();
}
}
}).catch(
function (e) {
done(new Error(e));
}
);
});
});
});

0 comments on commit cbc2b25

Please sign in to comment.