Convert done callbacks to async/await in test/unit/cmap_spec.js

This commit is contained in:
Tim van der Meij 2021-04-14 22:24:28 +02:00
parent 046467ff47
commit 38ed655562
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762

View File

@ -40,305 +40,218 @@ describe("cmap", function () {
fetchBuiltInCMap = null; fetchBuiltInCMap = null;
}); });
it("parses beginbfchar", function (done) { it("parses beginbfchar", async function () {
// prettier-ignore // prettier-ignore
const str = "2 beginbfchar\n" + const str = "2 beginbfchar\n" +
"<03> <00>\n" + "<03> <00>\n" +
"<04> <01>\n" + "<04> <01>\n" +
"endbfchar\n"; "endbfchar\n";
const stream = new StringStream(str); const stream = new StringStream(str);
const cmapPromise = CMapFactory.create({ encoding: stream }); const cmap = await CMapFactory.create({ encoding: stream });
cmapPromise expect(cmap.lookup(0x03)).toEqual(String.fromCharCode(0x00));
.then(function (cmap) { expect(cmap.lookup(0x04)).toEqual(String.fromCharCode(0x01));
expect(cmap.lookup(0x03)).toEqual(String.fromCharCode(0x00)); expect(cmap.lookup(0x05)).toBeUndefined();
expect(cmap.lookup(0x04)).toEqual(String.fromCharCode(0x01));
expect(cmap.lookup(0x05)).toBeUndefined();
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("parses beginbfrange with range", function (done) {
it("parses beginbfrange with range", async function () {
// prettier-ignore // prettier-ignore
const str = "1 beginbfrange\n" + const str = "1 beginbfrange\n" +
"<06> <0B> 0\n" + "<06> <0B> 0\n" +
"endbfrange\n"; "endbfrange\n";
const stream = new StringStream(str); const stream = new StringStream(str);
const cmapPromise = CMapFactory.create({ encoding: stream }); const cmap = await CMapFactory.create({ encoding: stream });
cmapPromise expect(cmap.lookup(0x05)).toBeUndefined();
.then(function (cmap) { expect(cmap.lookup(0x06)).toEqual(String.fromCharCode(0x00));
expect(cmap.lookup(0x05)).toBeUndefined(); expect(cmap.lookup(0x0b)).toEqual(String.fromCharCode(0x05));
expect(cmap.lookup(0x06)).toEqual(String.fromCharCode(0x00)); expect(cmap.lookup(0x0c)).toBeUndefined();
expect(cmap.lookup(0x0b)).toEqual(String.fromCharCode(0x05));
expect(cmap.lookup(0x0c)).toBeUndefined();
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("parses beginbfrange with array", function (done) {
it("parses beginbfrange with array", async function () {
// prettier-ignore // prettier-ignore
const str = "1 beginbfrange\n" + const str = "1 beginbfrange\n" +
"<0D> <12> [ 0 1 2 3 4 5 ]\n" + "<0D> <12> [ 0 1 2 3 4 5 ]\n" +
"endbfrange\n"; "endbfrange\n";
const stream = new StringStream(str); const stream = new StringStream(str);
const cmapPromise = CMapFactory.create({ encoding: stream }); const cmap = await CMapFactory.create({ encoding: stream });
cmapPromise expect(cmap.lookup(0x0c)).toBeUndefined();
.then(function (cmap) { expect(cmap.lookup(0x0d)).toEqual(0x00);
expect(cmap.lookup(0x0c)).toBeUndefined(); expect(cmap.lookup(0x12)).toEqual(0x05);
expect(cmap.lookup(0x0d)).toEqual(0x00); expect(cmap.lookup(0x13)).toBeUndefined();
expect(cmap.lookup(0x12)).toEqual(0x05);
expect(cmap.lookup(0x13)).toBeUndefined();
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("parses begincidchar", function (done) {
it("parses begincidchar", async function () {
// prettier-ignore // prettier-ignore
const str = "1 begincidchar\n" + const str = "1 begincidchar\n" +
"<14> 0\n" + "<14> 0\n" +
"endcidchar\n"; "endcidchar\n";
const stream = new StringStream(str); const stream = new StringStream(str);
const cmapPromise = CMapFactory.create({ encoding: stream }); const cmap = await CMapFactory.create({ encoding: stream });
cmapPromise expect(cmap.lookup(0x14)).toEqual(0x00);
.then(function (cmap) { expect(cmap.lookup(0x15)).toBeUndefined();
expect(cmap.lookup(0x14)).toEqual(0x00);
expect(cmap.lookup(0x15)).toBeUndefined();
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("parses begincidrange", function (done) {
it("parses begincidrange", async function () {
// prettier-ignore // prettier-ignore
const str = "1 begincidrange\n" + const str = "1 begincidrange\n" +
"<0016> <001B> 0\n" + "<0016> <001B> 0\n" +
"endcidrange\n"; "endcidrange\n";
const stream = new StringStream(str); const stream = new StringStream(str);
const cmapPromise = CMapFactory.create({ encoding: stream }); const cmap = await CMapFactory.create({ encoding: stream });
cmapPromise expect(cmap.lookup(0x15)).toBeUndefined();
.then(function (cmap) { expect(cmap.lookup(0x16)).toEqual(0x00);
expect(cmap.lookup(0x15)).toBeUndefined(); expect(cmap.lookup(0x1b)).toEqual(0x05);
expect(cmap.lookup(0x16)).toEqual(0x00); expect(cmap.lookup(0x1c)).toBeUndefined();
expect(cmap.lookup(0x1b)).toEqual(0x05);
expect(cmap.lookup(0x1c)).toBeUndefined();
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("decodes codespace ranges", function (done) {
it("decodes codespace ranges", async function () {
// prettier-ignore // prettier-ignore
const str = "1 begincodespacerange\n" + const str = "1 begincodespacerange\n" +
"<01> <02>\n" + "<01> <02>\n" +
"<00000003> <00000004>\n" + "<00000003> <00000004>\n" +
"endcodespacerange\n"; "endcodespacerange\n";
const stream = new StringStream(str); const stream = new StringStream(str);
const cmapPromise = CMapFactory.create({ encoding: stream }); const cmap = await CMapFactory.create({ encoding: stream });
cmapPromise const c = {};
.then(function (cmap) { cmap.readCharCode(String.fromCharCode(1), 0, c);
const c = {}; expect(c.charcode).toEqual(1);
cmap.readCharCode(String.fromCharCode(1), 0, c); expect(c.length).toEqual(1);
expect(c.charcode).toEqual(1); cmap.readCharCode(String.fromCharCode(0, 0, 0, 3), 0, c);
expect(c.length).toEqual(1); expect(c.charcode).toEqual(3);
cmap.readCharCode(String.fromCharCode(0, 0, 0, 3), 0, c); expect(c.length).toEqual(4);
expect(c.charcode).toEqual(3);
expect(c.length).toEqual(4);
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("decodes 4 byte codespace ranges", function (done) {
it("decodes 4 byte codespace ranges", async function () {
// prettier-ignore // prettier-ignore
const str = "1 begincodespacerange\n" + const str = "1 begincodespacerange\n" +
"<8EA1A1A1> <8EA1FEFE>\n" + "<8EA1A1A1> <8EA1FEFE>\n" +
"endcodespacerange\n"; "endcodespacerange\n";
const stream = new StringStream(str); const stream = new StringStream(str);
const cmapPromise = CMapFactory.create({ encoding: stream }); const cmap = await CMapFactory.create({ encoding: stream });
cmapPromise const c = {};
.then(function (cmap) { cmap.readCharCode(String.fromCharCode(0x8e, 0xa1, 0xa1, 0xa1), 0, c);
const c = {}; expect(c.charcode).toEqual(0x8ea1a1a1);
cmap.readCharCode(String.fromCharCode(0x8e, 0xa1, 0xa1, 0xa1), 0, c); expect(c.length).toEqual(4);
expect(c.charcode).toEqual(0x8ea1a1a1);
expect(c.length).toEqual(4);
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("read usecmap", function (done) {
it("read usecmap", async function () {
const str = "/Adobe-Japan1-1 usecmap\n"; const str = "/Adobe-Japan1-1 usecmap\n";
const stream = new StringStream(str); const stream = new StringStream(str);
const cmapPromise = CMapFactory.create({ const cmap = await CMapFactory.create({
encoding: stream, encoding: stream,
fetchBuiltInCMap, fetchBuiltInCMap,
useCMap: null, useCMap: null,
}); });
cmapPromise expect(cmap instanceof CMap).toEqual(true);
.then(function (cmap) { expect(cmap.useCMap).not.toBeNull();
expect(cmap instanceof CMap).toEqual(true); expect(cmap.builtInCMap).toBeFalsy();
expect(cmap.useCMap).not.toBeNull(); expect(cmap.length).toEqual(0x20a7);
expect(cmap.builtInCMap).toBeFalsy(); expect(cmap.isIdentityCMap).toEqual(false);
expect(cmap.length).toEqual(0x20a7);
expect(cmap.isIdentityCMap).toEqual(false);
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("parses cmapname", function (done) {
it("parses cmapname", async function () {
const str = "/CMapName /Identity-H def\n"; const str = "/CMapName /Identity-H def\n";
const stream = new StringStream(str); const stream = new StringStream(str);
const cmapPromise = CMapFactory.create({ encoding: stream }); const cmap = await CMapFactory.create({ encoding: stream });
cmapPromise expect(cmap.name).toEqual("Identity-H");
.then(function (cmap) {
expect(cmap.name).toEqual("Identity-H");
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("parses wmode", function (done) {
it("parses wmode", async function () {
const str = "/WMode 1 def\n"; const str = "/WMode 1 def\n";
const stream = new StringStream(str); const stream = new StringStream(str);
const cmapPromise = CMapFactory.create({ encoding: stream }); const cmap = await CMapFactory.create({ encoding: stream });
cmapPromise expect(cmap.vertical).toEqual(true);
.then(function (cmap) {
expect(cmap.vertical).toEqual(true);
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("loads built in cmap", function (done) {
const cmapPromise = CMapFactory.create({ it("loads built in cmap", async function () {
const cmap = await CMapFactory.create({
encoding: Name.get("Adobe-Japan1-1"), encoding: Name.get("Adobe-Japan1-1"),
fetchBuiltInCMap, fetchBuiltInCMap,
useCMap: null, useCMap: null,
}); });
cmapPromise expect(cmap instanceof CMap).toEqual(true);
.then(function (cmap) { expect(cmap.useCMap).toBeNull();
expect(cmap instanceof CMap).toEqual(true); expect(cmap.builtInCMap).toBeTruthy();
expect(cmap.useCMap).toBeNull(); expect(cmap.length).toEqual(0x20a7);
expect(cmap.builtInCMap).toBeTruthy(); expect(cmap.isIdentityCMap).toEqual(false);
expect(cmap.length).toEqual(0x20a7);
expect(cmap.isIdentityCMap).toEqual(false);
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("loads built in identity cmap", function (done) {
const cmapPromise = CMapFactory.create({ it("loads built in identity cmap", async function () {
const cmap = await CMapFactory.create({
encoding: Name.get("Identity-H"), encoding: Name.get("Identity-H"),
fetchBuiltInCMap, fetchBuiltInCMap,
useCMap: null, useCMap: null,
}); });
cmapPromise expect(cmap instanceof IdentityCMap).toEqual(true);
.then(function (cmap) { expect(cmap.vertical).toEqual(false);
expect(cmap instanceof IdentityCMap).toEqual(true); expect(cmap.length).toEqual(0x10000);
expect(cmap.vertical).toEqual(false); expect(function () {
expect(cmap.length).toEqual(0x10000); return cmap.isIdentityCMap;
expect(function () { }).toThrow(new Error("should not access .isIdentityCMap"));
return cmap.isIdentityCMap; });
}).toThrow(new Error("should not access .isIdentityCMap"));
done(); it("attempts to load a non-existent built-in CMap", async function () {
}) try {
.catch(function (reason) { await CMapFactory.create({
done.fail(reason); encoding: Name.get("null"),
fetchBuiltInCMap,
useCMap: null,
}); });
// Shouldn't get here.
expect(false).toEqual(true);
} catch (reason) {
expect(reason instanceof Error).toEqual(true);
expect(reason.message).toEqual("Unknown CMap name: null");
}
}); });
it("attempts to load a non-existent built-in CMap", function (done) { it("attempts to load a built-in CMap without the necessary API parameters", async function () {
const cmapPromise = CMapFactory.create({
encoding: Name.get("null"),
fetchBuiltInCMap,
useCMap: null,
});
cmapPromise.then(
function () {
done.fail("No CMap should be loaded");
},
function (reason) {
expect(reason instanceof Error).toEqual(true);
expect(reason.message).toEqual("Unknown CMap name: null");
done();
}
);
});
it("attempts to load a built-in CMap without the necessary API parameters", function (done) {
function tmpFetchBuiltInCMap(name) { function tmpFetchBuiltInCMap(name) {
const CMapReaderFactory = new DefaultCMapReaderFactory({}); const CMapReaderFactory = new DefaultCMapReaderFactory({});
return CMapReaderFactory.fetch({ name });
return CMapReaderFactory.fetch({
name,
});
} }
const cmapPromise = CMapFactory.create({ try {
encoding: Name.get("Adobe-Japan1-1"), await CMapFactory.create({
fetchBuiltInCMap: tmpFetchBuiltInCMap, encoding: Name.get("Adobe-Japan1-1"),
useCMap: null, fetchBuiltInCMap: tmpFetchBuiltInCMap,
}); useCMap: null,
cmapPromise.then( });
function () {
done.fail("No CMap should be loaded"); // Shouldn't get here.
}, expect(false).toEqual(true);
function (reason) { } catch (reason) {
expect(reason instanceof Error).toEqual(true); expect(reason instanceof Error).toEqual(true);
expect(reason.message).toEqual( expect(reason.message).toEqual(
'The CMap "baseUrl" parameter must be specified, ensure that ' + 'The CMap "baseUrl" parameter must be specified, ensure that ' +
'the "cMapUrl" and "cMapPacked" API parameters are provided.' 'the "cMapUrl" and "cMapPacked" API parameters are provided.'
); );
done(); }
}
);
}); });
it("attempts to load a built-in CMap with inconsistent API parameters", function (done) { it("attempts to load a built-in CMap with inconsistent API parameters", async function () {
function tmpFetchBuiltInCMap(name) { function tmpFetchBuiltInCMap(name) {
const CMapReaderFactory = new DefaultCMapReaderFactory({ const CMapReaderFactory = new DefaultCMapReaderFactory({
baseUrl: CMAP_PARAMS.cMapUrl, baseUrl: CMAP_PARAMS.cMapUrl,
isCompressed: false, isCompressed: false,
}); });
return CMapReaderFactory.fetch({ name });
return CMapReaderFactory.fetch({
name,
});
} }
const cmapPromise = CMapFactory.create({ try {
encoding: Name.get("Adobe-Japan1-1"), await CMapFactory.create({
fetchBuiltInCMap: tmpFetchBuiltInCMap, encoding: Name.get("Adobe-Japan1-1"),
useCMap: null, fetchBuiltInCMap: tmpFetchBuiltInCMap,
}); useCMap: null,
cmapPromise.then( });
function () {
done.fail("No CMap should be loaded"); // Shouldn't get here.
}, expect(false).toEqual(true);
function (reason) { } catch (reason) {
expect(reason instanceof Error).toEqual(true); expect(reason instanceof Error).toEqual(true);
const message = reason.message; const message = reason.message;
expect(message.startsWith("Unable to load CMap at: ")).toEqual(true); expect(message.startsWith("Unable to load CMap at: ")).toEqual(true);
expect(message.endsWith("/external/bcmaps/Adobe-Japan1-1")).toEqual( expect(message.endsWith("/external/bcmaps/Adobe-Japan1-1")).toEqual(true);
true }
);
done();
}
);
}); });
}); });