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) {
const result = new OperatorList();
async function runOperatorListCheck(evaluator, stream, resources) {
const operatorList = new OperatorList();
const task = new WorkerTask("OperatorListCheck");
evaluator
.getOperatorList({
stream,
task,
resources,
operatorList: result,
})
.then(
function () {
callback(result);
},
function (reason) {
callback(reason);
}
);
await evaluator.getOperatorList({
stream,
task,
resources,
operatorList,
});
return operatorList;
}
let partialEvaluator;
@ -73,38 +65,32 @@ describe("evaluator", function () {
});
describe("splitCombinedOperations", function () {
it("should reject unknown operations", function (done) {
it("should reject unknown operations", async function () {
const stream = new StringStream("fTT");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
new ResourcesMock()
);
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");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
new ResourcesMock()
);
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();
imgDict.set("Subtype", Name.get("Image"));
imgDict.set("Width", 1);
@ -120,133 +106,114 @@ describe("evaluator", function () {
resources.XObject = xObject;
const stream = new StringStream("/Res1 DoQ");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
resources
);
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");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
new ResourcesMock()
);
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();
resources.Res1 = {};
const stream = new StringStream("B*Bf*");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
resources
);
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");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
new ResourcesMock()
);
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");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
new ResourcesMock()
);
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 () {
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");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
new ResourcesMock()
);
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");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
new ResourcesMock()
);
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();
gState.set("LW", 2);
gState.set("CA", 0.5);
@ -258,114 +225,111 @@ describe("evaluator", function () {
resources.ExtGState = extGState;
const stream = new StringStream("/F2 /GS2 gs 5.711 Tf");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
resources
);
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");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
new ResourcesMock(),
function (result) {
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
done();
}
new ResourcesMock()
);
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
});
it(
"should error if (many) path operators have too few arguments " +
"(bug 1443140)",
function (done) {
async function () {
const NUM_INVALID_OPS = 25;
const tempArr = new Array(NUM_INVALID_OPS + 1);
// Non-path operators, should be ignored.
const invalidMoveText = tempArr.join("10 Td\n");
const moveTextStream = new StringStream(invalidMoveText);
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
moveTextStream,
new ResourcesMock(),
function (result) {
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
done();
}
new ResourcesMock()
);
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
// Path operators, should throw error.
const invalidLineTo = tempArr.join("20 l\n");
const lineToStream = new StringStream(invalidLineTo);
runOperatorListCheck(
partialEvaluator,
lineToStream,
new ResourcesMock(),
function (error) {
expect(error instanceof FormatError).toEqual(true);
expect(error.message).toEqual(
"Invalid command l: expected 2 args, but received 1 args."
);
done();
}
);
try {
await runOperatorListCheck(
partialEvaluator,
lineToStream,
new ResourcesMock()
);
// Shouldn't get here.
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");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
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();
}
new ResourcesMock()
);
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");
runOperatorListCheck(
partialEvaluator,
stream,
new ResourcesMock(),
function (result) {
expect(result instanceof FormatError).toEqual(true);
expect(result.message).toEqual(
"XObject must be referred to by name."
);
done();
}
);
try {
await runOperatorListCheck(
partialEvaluator,
stream,
new ResourcesMock()
);
// Shouldn't get here.
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();
xobjStreamDict.set("Subtype", Name.get("PS"));
const xobjStream = new Stream([], 0, 0, xobjStreamDict);
@ -377,54 +341,58 @@ describe("evaluator", function () {
resources.set("XObject", xobjs);
const stream = new StringStream("/Res1 Do");
runOperatorListCheck(
const result = await runOperatorListCheck(
partialEvaluator,
stream,
resources,
function (result) {
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
done();
}
resources
);
expect(result.argsArray).toEqual([]);
expect(result.fnArray).toEqual([]);
});
});
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 resources = new ResourcesMock();
const result = new OperatorList();
const task = new WorkerTask("OperatorListAbort");
task.terminate();
partialEvaluator
.getOperatorList({
try {
await partialEvaluator.getOperatorList({
stream,
task,
resources,
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 stream = new StringStream("qqQQ");
const task = new WorkerTask("TextContentAbort");
task.terminate();
partialEvaluator
.getTextContent({
try {
await partialEvaluator.getTextContent({
stream,
task,
resources,
})
.catch(function () {
expect(true).toEqual(true);
done();
});
// Shouldn't get here.
expect(false).toEqual(true);
} catch (_) {
expect(true).toEqual(true);
}
});
});