From 3d55b2b10eb9bc0201490184a05fc5ed209521a6 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 19 Apr 2021 13:26:39 +0200 Subject: [PATCH] Replace `done` callbacks in the font-tests with async/await instead --- test/font/font_core_spec.js | 13 ++++----- test/font/font_fpgm_spec.js | 34 ++++++++++------------ test/font/font_os2_spec.js | 41 ++++++++++++-------------- test/font/font_post_spec.js | 58 +++++++++++++++---------------------- test/font/fontutils.js | 32 ++++++++++---------- 5 files changed, 82 insertions(+), 96 deletions(-) diff --git a/test/font/font_core_spec.js b/test/font/font_core_spec.js index 02a43b23f..d9ead4179 100644 --- a/test/font/font_core_spec.js +++ b/test/font/font_core_spec.js @@ -7,13 +7,12 @@ describe("font1", function () { ); describe("test harness testing", function () { - it("returns output", function (done) { - ttx(font1_1, function (output) { - verifyTtxOutput(output); - expect(//.test(output)).toEqual(true); - done(); - }); + it("returns output", async function () { + const output = await ttx(font1_1); + + verifyTtxOutput(output); + expect(//.test(output)).toEqual(true); }); }); }); diff --git a/test/font/font_fpgm_spec.js b/test/font/font_fpgm_spec.js index 0c7cf36c2..cf2698738 100644 --- a/test/font/font_fpgm_spec.js +++ b/test/font/font_fpgm_spec.js @@ -11,26 +11,24 @@ describe("font_fpgm", function () { ); describe("Fixes fpgm table", function () { - it("table was truncated in the middle of functions", function (done) { - CMapFactory.create({ + it("table was truncated in the middle of functions", async function () { + const cMap = await CMapFactory.create({ encoding: Name.get("Identity-H"), - }).then(function (cMap) { - const font = new Font("font", new Stream(font2324), { - loadedName: "font", - type: "CIDFontType2", - differences: [], - defaultEncoding: [], - cMap, - toUnicode: new ToUnicodeMap([]), - }); - ttx(font.data, function (output) { - verifyTtxOutput(output); - expect( - /(ENDF\[ \]|SVTCA\[0\])\s*<\/assembly>\s*<\/fpgm>/.test(output) - ).toEqual(true); - done(); - }); }); + const font = new Font("font", new Stream(font2324), { + loadedName: "font", + type: "CIDFontType2", + differences: [], + defaultEncoding: [], + cMap, + toUnicode: new ToUnicodeMap([]), + }); + const output = await ttx(font.data); + + verifyTtxOutput(output); + expect( + /(ENDF\[ \]|SVTCA\[0\])\s*<\/assembly>\s*<\/fpgm>/.test(output) + ).toEqual(true); }); }); }); diff --git a/test/font/font_os2_spec.js b/test/font/font_os2_spec.js index e8f91c1cb..eef69ed06 100644 --- a/test/font/font_os2_spec.js +++ b/test/font/font_os2_spec.js @@ -15,7 +15,7 @@ describe("font_post", function () { ); describe("OS/2 table removal on bad post table values", function () { - it("has invalid version number", function (done) { + it("has invalid version number", async function () { const font = new Font("font", new Stream(font2154), { loadedName: "font", type: "TrueType", @@ -23,31 +23,28 @@ describe("font_post", function () { defaultEncoding: [], toUnicode: new ToUnicodeMap([]), }); - ttx(font.data, function (output) { - verifyTtxOutput(output); - expect(/\s*/.test(output)).toEqual(true); - done(); - }); + const output = await ttx(font.data); + + verifyTtxOutput(output); + expect(/\s*/.test(output)).toEqual(true); }); - it("has invalid selection attributes presence", function (done) { - CMapFactory.create({ + it("has invalid selection attributes presence", async function () { + const cMap = await CMapFactory.create({ encoding: Name.get("Identity-H"), - }).then(function (cMap) { - const font = new Font("font", new Stream(font1282), { - loadedName: "font", - type: "CIDFontType2", - differences: [], - defaultEncoding: [], - cMap, - toUnicode: new ToUnicodeMap([]), - }); - ttx(font.data, function (output) { - verifyTtxOutput(output); - expect(/\s*/.test(output)).toEqual(true); - done(); - }); }); + const font = new Font("font", new Stream(font1282), { + loadedName: "font", + type: "CIDFontType2", + differences: [], + defaultEncoding: [], + cMap, + toUnicode: new ToUnicodeMap([]), + }); + const output = await ttx(font.data); + + verifyTtxOutput(output); + expect(/\s*/.test(output)).toEqual(true); }); }); }); diff --git a/test/font/font_post_spec.js b/test/font/font_post_spec.js index c130cefa3..5b29e5147 100644 --- a/test/font/font_post_spec.js +++ b/test/font/font_post_spec.js @@ -19,29 +19,25 @@ describe("font_post", function () { ); describe("post table removal on bad post table values", function () { - it("has invalid version number", function (done) { - CMapFactory.create({ + it("has invalid version number", async function () { + const cMap = await CMapFactory.create({ encoding: Name.get("Identity-H"), - }).then(function (cMap) { - const font = new Font("font", new Stream(font2109), { - loadedName: "font", - type: "CIDFontType2", - differences: [], - defaultEncoding: [], - cMap, - toUnicode: new ToUnicodeMap([]), - }); - ttx(font.data, function (output) { - verifyTtxOutput(output); - expect(/\s*/.test(output)).toEqual( - true - ); - done(); - }); }); + const font = new Font("font", new Stream(font2109), { + loadedName: "font", + type: "CIDFontType2", + differences: [], + defaultEncoding: [], + cMap, + toUnicode: new ToUnicodeMap([]), + }); + const output = await ttx(font.data); + + verifyTtxOutput(output); + expect(/\s*/.test(output)).toEqual(true); }); - it("has invalid glyph name indexes", function (done) { + it("has invalid glyph name indexes", async function () { const font = new Font("font", new Stream(font2189), { loadedName: "font", type: "TrueType", @@ -49,16 +45,13 @@ describe("font_post", function () { defaultEncoding: [], toUnicode: new ToUnicodeMap([]), }); - ttx(font.data, function (output) { - verifyTtxOutput(output); - expect(/\s*/.test(output)).toEqual( - true - ); - done(); - }); + const output = await ttx(font.data); + + verifyTtxOutput(output); + expect(/\s*/.test(output)).toEqual(true); }); - it("has right amount of glyphs specified", function (done) { + it("has right amount of glyphs specified", async function () { const font = new Font("font", new Stream(font2374), { loadedName: "font", type: "TrueType", @@ -66,13 +59,10 @@ describe("font_post", function () { defaultEncoding: [], toUnicode: new ToUnicodeMap([]), }); - ttx(font.data, function (output) { - verifyTtxOutput(output); - expect(/\s*/.test(output)).toEqual( - true - ); - done(); - }); + const output = await ttx(font.data); + + verifyTtxOutput(output); + expect(/\s*/.test(output)).toEqual(true); }); }); }); diff --git a/test/font/fontutils.js b/test/font/fontutils.js index 1b4abb8f3..e3af860bf 100644 --- a/test/font/fontutils.js +++ b/test/font/fontutils.js @@ -65,24 +65,26 @@ function encodeFontData(data) { return buffer; } -function ttx(data, callback) { - const xhr = new XMLHttpRequest(); - xhr.open("POST", "/ttx"); +function ttx(data) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.open("POST", "/ttx"); - const encodedData = encodeFontData(data); - xhr.setRequestHeader("Content-type", "text/plain"); - xhr.setRequestHeader("Content-length", encodedData.length); + const encodedData = encodeFontData(data); + xhr.setRequestHeader("Content-type", "text/plain"); + xhr.setRequestHeader("Content-length", encodedData.length); - xhr.onreadystatechange = function getPdfOnreadystatechange(e) { - if (xhr.readyState === 4) { - if (xhr.status === 200) { - callback(xhr.responseText); - } else { - callback("Transport error: " + xhr.statusText + ""); + xhr.onreadystatechange = function getPdfOnreadystatechange(e) { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + resolve(xhr.responseText); + } else { + reject(new Error(xhr.statusText)); + } } - } - }; - xhr.send(encodedData); + }; + xhr.send(encodedData); + }); } function verifyTtxOutput(output) {