Merge pull request #13256 from timvandermeij/unit-test-async-await-pt4

Convert done callbacks to async/await in the last two unit test files
This commit is contained in:
Tim van der Meij 2021-04-18 14:25:40 +02:00 committed by GitHub
commit fd82adccfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1249 additions and 1586 deletions

File diff suppressed because it is too large Load Diff

View File

@ -37,24 +37,16 @@ describe("evaluator", function () {
}, },
}; };
function runOperatorListCheck(evaluator, stream, resources, callback) { async function runOperatorListCheck(evaluator, stream, resources) {
const result = new OperatorList(); const operatorList = new OperatorList();
const task = new WorkerTask("OperatorListCheck"); const task = new WorkerTask("OperatorListCheck");
evaluator await evaluator.getOperatorList({
.getOperatorList({ stream,
stream, task,
task, resources,
resources, operatorList,
operatorList: result, });
}) return operatorList;
.then(
function () {
callback(result);
},
function (reason) {
callback(reason);
}
);
} }
let partialEvaluator; let partialEvaluator;
@ -73,38 +65,32 @@ describe("evaluator", function () {
}); });
describe("splitCombinedOperations", function () { describe("splitCombinedOperations", function () {
it("should reject unknown operations", function (done) { it("should reject unknown operations", async function () {
const stream = new StringStream("fTT"); const stream = new StringStream("fTT");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
new ResourcesMock(), new ResourcesMock()
function (result) {
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(1);
expect(result.fnArray[0]).toEqual(OPS.fill);
expect(result.argsArray[0]).toEqual(null);
done();
}
); );
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(1);
expect(result.fnArray[0]).toEqual(OPS.fill);
expect(result.argsArray[0]).toEqual(null);
}); });
it("should handle one operation", function (done) { it("should handle one operation", async function () {
const stream = new StringStream("Q"); const stream = new StringStream("Q");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
new ResourcesMock(), new ResourcesMock()
function (result) {
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(1);
expect(result.fnArray[0]).toEqual(OPS.restore);
done();
}
); );
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(1);
expect(result.fnArray[0]).toEqual(OPS.restore);
}); });
it("should handle two glued operations", function (done) { it("should handle two glued operations", async function () {
const imgDict = new Dict(); const imgDict = new Dict();
imgDict.set("Subtype", Name.get("Image")); imgDict.set("Subtype", Name.get("Image"));
imgDict.set("Width", 1); imgDict.set("Width", 1);
@ -120,133 +106,114 @@ describe("evaluator", function () {
resources.XObject = xObject; resources.XObject = xObject;
const stream = new StringStream("/Res1 DoQ"); const stream = new StringStream("/Res1 DoQ");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
resources, resources
function (result) {
expect(result.fnArray.length).toEqual(3);
expect(result.fnArray[0]).toEqual(OPS.dependency);
expect(result.fnArray[1]).toEqual(OPS.paintImageXObject);
expect(result.fnArray[2]).toEqual(OPS.restore);
expect(result.argsArray.length).toEqual(3);
expect(result.argsArray[0]).toEqual(["img_p0_1"]);
expect(result.argsArray[1]).toEqual(["img_p0_1", 1, 1]);
expect(result.argsArray[2]).toEqual(null);
done();
}
); );
expect(result.fnArray.length).toEqual(3);
expect(result.fnArray[0]).toEqual(OPS.dependency);
expect(result.fnArray[1]).toEqual(OPS.paintImageXObject);
expect(result.fnArray[2]).toEqual(OPS.restore);
expect(result.argsArray.length).toEqual(3);
expect(result.argsArray[0]).toEqual(["img_p0_1"]);
expect(result.argsArray[1]).toEqual(["img_p0_1", 1, 1]);
expect(result.argsArray[2]).toEqual(null);
}); });
it("should handle three glued operations", function (done) { it("should handle three glued operations", async function () {
const stream = new StringStream("fff"); const stream = new StringStream("fff");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
new ResourcesMock(), new ResourcesMock()
function (result) {
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(3);
expect(result.fnArray[0]).toEqual(OPS.fill);
expect(result.fnArray[1]).toEqual(OPS.fill);
expect(result.fnArray[2]).toEqual(OPS.fill);
done();
}
); );
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(3);
expect(result.fnArray[0]).toEqual(OPS.fill);
expect(result.fnArray[1]).toEqual(OPS.fill);
expect(result.fnArray[2]).toEqual(OPS.fill);
}); });
it("should handle three glued operations #2", function (done) { it("should handle three glued operations #2", async function () {
const resources = new ResourcesMock(); const resources = new ResourcesMock();
resources.Res1 = {}; resources.Res1 = {};
const stream = new StringStream("B*Bf*"); const stream = new StringStream("B*Bf*");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
resources, resources
function (result) {
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(3);
expect(result.fnArray[0]).toEqual(OPS.eoFillStroke);
expect(result.fnArray[1]).toEqual(OPS.fillStroke);
expect(result.fnArray[2]).toEqual(OPS.eoFill);
done();
}
); );
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(3);
expect(result.fnArray[0]).toEqual(OPS.eoFillStroke);
expect(result.fnArray[1]).toEqual(OPS.fillStroke);
expect(result.fnArray[2]).toEqual(OPS.eoFill);
}); });
it("should handle glued operations and operands", function (done) { it("should handle glued operations and operands", async function () {
const stream = new StringStream("f5 Ts"); const stream = new StringStream("f5 Ts");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
new ResourcesMock(), new ResourcesMock()
function (result) {
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(2);
expect(result.fnArray[0]).toEqual(OPS.fill);
expect(result.fnArray[1]).toEqual(OPS.setTextRise);
expect(result.argsArray.length).toEqual(2);
expect(result.argsArray[1].length).toEqual(1);
expect(result.argsArray[1][0]).toEqual(5);
done();
}
); );
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(2);
expect(result.fnArray[0]).toEqual(OPS.fill);
expect(result.fnArray[1]).toEqual(OPS.setTextRise);
expect(result.argsArray.length).toEqual(2);
expect(result.argsArray[1].length).toEqual(1);
expect(result.argsArray[1][0]).toEqual(5);
}); });
it("should handle glued operations and literals", function (done) { it("should handle glued operations and literals", async function () {
const stream = new StringStream("trueifalserinulln"); const stream = new StringStream("trueifalserinulln");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
new ResourcesMock(), new ResourcesMock()
function (result) {
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(3);
expect(result.fnArray[0]).toEqual(OPS.setFlatness);
expect(result.fnArray[1]).toEqual(OPS.setRenderingIntent);
expect(result.fnArray[2]).toEqual(OPS.endPath);
expect(result.argsArray.length).toEqual(3);
expect(result.argsArray[0].length).toEqual(1);
expect(result.argsArray[0][0]).toEqual(true);
expect(result.argsArray[1].length).toEqual(1);
expect(result.argsArray[1][0]).toEqual(false);
expect(result.argsArray[2]).toEqual(null);
done();
}
); );
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(3);
expect(result.fnArray[0]).toEqual(OPS.setFlatness);
expect(result.fnArray[1]).toEqual(OPS.setRenderingIntent);
expect(result.fnArray[2]).toEqual(OPS.endPath);
expect(result.argsArray.length).toEqual(3);
expect(result.argsArray[0].length).toEqual(1);
expect(result.argsArray[0][0]).toEqual(true);
expect(result.argsArray[1].length).toEqual(1);
expect(result.argsArray[1][0]).toEqual(false);
expect(result.argsArray[2]).toEqual(null);
}); });
}); });
describe("validateNumberOfArgs", function () { describe("validateNumberOfArgs", function () {
it("should execute if correct number of arguments", function (done) { it("should execute if correct number of arguments", async function () {
const stream = new StringStream("5 1 d0"); const stream = new StringStream("5 1 d0");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
new ResourcesMock(), new ResourcesMock()
function (result) {
expect(result.argsArray[0][0]).toEqual(5);
expect(result.argsArray[0][1]).toEqual(1);
expect(result.fnArray[0]).toEqual(OPS.setCharWidth);
done();
}
); );
expect(result.argsArray[0][0]).toEqual(5);
expect(result.argsArray[0][1]).toEqual(1);
expect(result.fnArray[0]).toEqual(OPS.setCharWidth);
}); });
it("should execute if too many arguments", function (done) {
it("should execute if too many arguments", async function () {
const stream = new StringStream("5 1 4 d0"); const stream = new StringStream("5 1 4 d0");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
new ResourcesMock(), new ResourcesMock()
function (result) {
expect(result.argsArray[0][0]).toEqual(1);
expect(result.argsArray[0][1]).toEqual(4);
expect(result.fnArray[0]).toEqual(OPS.setCharWidth);
done();
}
); );
expect(result.argsArray[0][0]).toEqual(1);
expect(result.argsArray[0][1]).toEqual(4);
expect(result.fnArray[0]).toEqual(OPS.setCharWidth);
}); });
it("should execute if nested commands", function (done) {
it("should execute if nested commands", async function () {
const gState = new Dict(); const gState = new Dict();
gState.set("LW", 2); gState.set("LW", 2);
gState.set("CA", 0.5); gState.set("CA", 0.5);
@ -258,114 +225,111 @@ describe("evaluator", function () {
resources.ExtGState = extGState; resources.ExtGState = extGState;
const stream = new StringStream("/F2 /GS2 gs 5.711 Tf"); const stream = new StringStream("/F2 /GS2 gs 5.711 Tf");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
resources, resources
function (result) {
expect(result.fnArray.length).toEqual(3);
expect(result.fnArray[0]).toEqual(OPS.setGState);
expect(result.fnArray[1]).toEqual(OPS.dependency);
expect(result.fnArray[2]).toEqual(OPS.setFont);
expect(result.argsArray.length).toEqual(3);
expect(result.argsArray[0]).toEqual([
[
["LW", 2],
["CA", 0.5],
],
]);
expect(result.argsArray[1]).toEqual(["g_font_error"]);
expect(result.argsArray[2]).toEqual(["g_font_error", 5.711]);
done();
}
); );
expect(result.fnArray.length).toEqual(3);
expect(result.fnArray[0]).toEqual(OPS.setGState);
expect(result.fnArray[1]).toEqual(OPS.dependency);
expect(result.fnArray[2]).toEqual(OPS.setFont);
expect(result.argsArray.length).toEqual(3);
expect(result.argsArray[0]).toEqual([
[
["LW", 2],
["CA", 0.5],
],
]);
expect(result.argsArray[1]).toEqual(["g_font_error"]);
expect(result.argsArray[2]).toEqual(["g_font_error", 5.711]);
}); });
it("should skip if too few arguments", function (done) {
it("should skip if too few arguments", async function () {
const stream = new StringStream("5 d0"); const stream = new StringStream("5 d0");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
new ResourcesMock(), new ResourcesMock()
function (result) {
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
done();
}
); );
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
}); });
it( it(
"should error if (many) path operators have too few arguments " + "should error if (many) path operators have too few arguments " +
"(bug 1443140)", "(bug 1443140)",
function (done) { async function () {
const NUM_INVALID_OPS = 25; const NUM_INVALID_OPS = 25;
const tempArr = new Array(NUM_INVALID_OPS + 1); const tempArr = new Array(NUM_INVALID_OPS + 1);
// Non-path operators, should be ignored. // Non-path operators, should be ignored.
const invalidMoveText = tempArr.join("10 Td\n"); const invalidMoveText = tempArr.join("10 Td\n");
const moveTextStream = new StringStream(invalidMoveText); const moveTextStream = new StringStream(invalidMoveText);
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
moveTextStream, moveTextStream,
new ResourcesMock(), new ResourcesMock()
function (result) {
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
done();
}
); );
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
// Path operators, should throw error. // Path operators, should throw error.
const invalidLineTo = tempArr.join("20 l\n"); const invalidLineTo = tempArr.join("20 l\n");
const lineToStream = new StringStream(invalidLineTo); const lineToStream = new StringStream(invalidLineTo);
runOperatorListCheck(
partialEvaluator, try {
lineToStream, await runOperatorListCheck(
new ResourcesMock(), partialEvaluator,
function (error) { lineToStream,
expect(error instanceof FormatError).toEqual(true); new ResourcesMock()
expect(error.message).toEqual( );
"Invalid command l: expected 2 args, but received 1 args."
); // Shouldn't get here.
done(); expect(false).toEqual(true);
} } catch (reason) {
); expect(reason instanceof FormatError).toEqual(true);
expect(reason.message).toEqual(
"Invalid command l: expected 2 args, but received 1 args."
);
}
} }
); );
it("should close opened saves", function (done) { it("should close opened saves", async function () {
const stream = new StringStream("qq"); const stream = new StringStream("qq");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
new ResourcesMock(), new ResourcesMock()
function (result) {
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(4);
expect(result.fnArray[0]).toEqual(OPS.save);
expect(result.fnArray[1]).toEqual(OPS.save);
expect(result.fnArray[2]).toEqual(OPS.restore);
expect(result.fnArray[3]).toEqual(OPS.restore);
done();
}
); );
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(4);
expect(result.fnArray[0]).toEqual(OPS.save);
expect(result.fnArray[1]).toEqual(OPS.save);
expect(result.fnArray[2]).toEqual(OPS.restore);
expect(result.fnArray[3]).toEqual(OPS.restore);
}); });
it("should error on paintXObject if name is missing", function (done) {
it("should error on paintXObject if name is missing", async function () {
const stream = new StringStream("/ Do"); const stream = new StringStream("/ Do");
runOperatorListCheck(
partialEvaluator, try {
stream, await runOperatorListCheck(
new ResourcesMock(), partialEvaluator,
function (result) { stream,
expect(result instanceof FormatError).toEqual(true); new ResourcesMock()
expect(result.message).toEqual( );
"XObject must be referred to by name."
); // Shouldn't get here.
done(); expect(false).toEqual(true);
} } catch (reason) {
); expect(reason instanceof FormatError).toEqual(true);
expect(reason.message).toEqual("XObject must be referred to by name.");
}
}); });
it("should skip paintXObject if subtype is PS", function (done) {
it("should skip paintXObject if subtype is PS", async function () {
const xobjStreamDict = new Dict(); const xobjStreamDict = new Dict();
xobjStreamDict.set("Subtype", Name.get("PS")); xobjStreamDict.set("Subtype", Name.get("PS"));
const xobjStream = new Stream([], 0, 0, xobjStreamDict); const xobjStream = new Stream([], 0, 0, xobjStreamDict);
@ -377,54 +341,58 @@ describe("evaluator", function () {
resources.set("XObject", xobjs); resources.set("XObject", xobjs);
const stream = new StringStream("/Res1 Do"); const stream = new StringStream("/Res1 Do");
runOperatorListCheck( const result = await runOperatorListCheck(
partialEvaluator, partialEvaluator,
stream, stream,
resources, resources
function (result) {
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
done();
}
); );
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
}); });
}); });
describe("thread control", function () { describe("thread control", function () {
it("should abort operator list parsing", function (done) { it("should abort operator list parsing", async function () {
const stream = new StringStream("qqQQ"); const stream = new StringStream("qqQQ");
const resources = new ResourcesMock(); const resources = new ResourcesMock();
const result = new OperatorList(); const result = new OperatorList();
const task = new WorkerTask("OperatorListAbort"); const task = new WorkerTask("OperatorListAbort");
task.terminate(); task.terminate();
partialEvaluator
.getOperatorList({ try {
await partialEvaluator.getOperatorList({
stream, stream,
task, task,
resources, resources,
operatorList: result, operatorList: result,
})
.catch(function () {
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(0);
done();
}); });
// Shouldn't get here.
expect(false).toEqual(true);
} catch (_) {
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
expect(result.fnArray.length).toEqual(0);
}
}); });
it("should abort text parsing parsing", function (done) {
it("should abort text content parsing", async function () {
const resources = new ResourcesMock(); const resources = new ResourcesMock();
const stream = new StringStream("qqQQ"); const stream = new StringStream("qqQQ");
const task = new WorkerTask("TextContentAbort"); const task = new WorkerTask("TextContentAbort");
task.terminate(); task.terminate();
partialEvaluator
.getTextContent({ try {
await partialEvaluator.getTextContent({
stream, stream,
task, task,
resources, resources,
})
.catch(function () {
expect(true).toEqual(true);
done();
}); });
// Shouldn't get here.
expect(false).toEqual(true);
} catch (_) {
expect(true).toEqual(true);
}
}); });
}); });