fix skipped tests with before()

This commit is contained in:
ColonelParrot 2025-02-18 11:36:12 -05:00
parent b4e68b12c9
commit 58b26c34c7

View File

@ -16,122 +16,125 @@ const path = require("path");
const OUTPUT_FOLDER = __dirname.replaceAll("\\", "/") + "/output/"; const OUTPUT_FOLDER = __dirname.replaceAll("\\", "/") + "/output/";
const TEST_IMAGE_DIRECTORY = path.join( const TEST_IMAGE_DIRECTORY = path.join(
__dirname, __dirname,
"..", "..",
"docs", "docs",
"images", "images",
"test" "test"
); );
/** /**
* delete previously generated output images * delete previously generated output images
*/ */
function setup() { function setup() {
console.log("=== setting up tests ==="); console.log("=== setting up tests ===");
console.log("Deleting previously generated images"); console.log("Deleting previously generated images");
if (!existsSync(OUTPUT_FOLDER)) { if (!existsSync(OUTPUT_FOLDER)) {
mkdirSync(OUTPUT_FOLDER); mkdirSync(OUTPUT_FOLDER);
} }
readdirSync(OUTPUT_FOLDER).forEach((file) => { readdirSync(OUTPUT_FOLDER).forEach((file) => {
unlinkSync(path.join(OUTPUT_FOLDER, file)); unlinkSync(path.join(OUTPUT_FOLDER, file));
}) })
} }
console.log("=== beginning tests ==="); let scanner;
console.log("loading OpenCV.js..."); let cv;
const scanner = new jscanify(); before(function (done) {
scanner.loadOpenCV(function (cv) { console.log("=== beginning tests ===");
console.log("loading OpenCV.js...");
console.log("Finished loading OpenCV.js"); scanner = new jscanify();
console.log("Writing test images to: " + OUTPUT_FOLDER); scanner.loadOpenCV(function (loadedCv) {
cv = loadedCv;
/** console.log("Finished loading OpenCV.js");
* tests an individual image console.log("Writing test images to: " + OUTPUT_FOLDER);
*/ setup()
function test(testImage, imageCount) { done();
});
});
/**
* tests an individual image
*/
function test(testImage, imageCount) {
describe("image #" + imageCount, function () { describe("image #" + imageCount, function () {
it("should highlight paper", function (done) { it("should highlight paper", function () {
const highlighted = scanner.highlightPaper(testImage); const highlighted = scanner.highlightPaper(testImage);
const higlightedOutputPath = OUTPUT_FOLDER + "highlighted-" + imageCount + ".jpg"; const higlightedOutputPath = OUTPUT_FOLDER + "highlighted-" + imageCount + ".jpg";
writeFileSync( writeFileSync(
higlightedOutputPath, higlightedOutputPath,
highlighted.toBuffer("image/jpeg") highlighted.toBuffer("image/jpeg")
); );
assert.ok(existsSync(higlightedOutputPath)); assert.ok(existsSync(higlightedOutputPath));
done();
});
it("should extract paper", function (done) {
const extracted = scanner.extractPaper(testImage, 386, 500);
const extractedOutputPath = OUTPUT_FOLDER + "extracted-" + imageCount + ".jpg";
writeFileSync(
extractedOutputPath,
extracted.toBuffer("image/jpeg")
);
assert.ok(existsSync(extractedOutputPath));
done();
});
it("should label corner points", function (done) {
const parsedImage = cv.imread(testImage);
const paperContour = scanner.findPaperContour(parsedImage);
const {
topLeftCorner,
topRightCorner,
bottomLeftCorner,
bottomRightCorner,
} = scanner.getCornerPoints(paperContour, testImage);
const canvas = createCanvas();
cv.imshow(canvas, parsedImage);
const ctx = canvas.getContext("2d");
const points = [
{ p: topLeftCorner, text: "top left corner" },
{ p: topRightCorner, text: "top right corner" },
{ p: bottomLeftCorner, text: "bottom left corner" },
{ p: bottomRightCorner, text: "bottom right corner" },
];
ctx.fillStyle = "cyan";
ctx.font = "25px serif";
points.forEach(({ p: point, text }) => {
ctx.beginPath();
ctx.arc(point.x, point.y, 15, 0, 2 * Math.PI, false);
ctx.fillText(text, point.x + 30, point.y)
ctx.fill();
}); });
const cornerPointsOutputPath = OUTPUT_FOLDER + "corner_points-" + imageCount + ".jpg"; it("should extract paper", function () {
writeFileSync(cornerPointsOutputPath, canvas.toBuffer("image/jpeg")); const extracted = scanner.extractPaper(testImage, 386, 500);
const extractedOutputPath = OUTPUT_FOLDER + "extracted-" + imageCount + ".jpg";
assert.ok(existsSync(cornerPointsOutputPath)); writeFileSync(
done(); extractedOutputPath,
}); extracted.toBuffer("image/jpeg")
);
assert.ok(existsSync(extractedOutputPath));
});
it("should label corner points", function () {
const parsedImage = cv.imread(testImage);
const paperContour = scanner.findPaperContour(parsedImage);
const {
topLeftCorner,
topRightCorner,
bottomLeftCorner,
bottomRightCorner,
} = scanner.getCornerPoints(paperContour, testImage);
const canvas = createCanvas();
cv.imshow(canvas, parsedImage);
const ctx = canvas.getContext("2d");
const points = [
{ p: topLeftCorner, text: "top left corner" },
{ p: topRightCorner, text: "top right corner" },
{ p: bottomLeftCorner, text: "bottom left corner" },
{ p: bottomRightCorner, text: "bottom right corner" },
];
ctx.fillStyle = "cyan";
ctx.font = "25px serif";
points.forEach(({ p: point, text }) => {
ctx.beginPath();
ctx.arc(point.x, point.y, 15, 0, 2 * Math.PI, false);
ctx.fillText(text, point.x + 30, point.y)
ctx.fill();
});
const cornerPointsOutputPath = OUTPUT_FOLDER + "corner_points-" + imageCount + ".jpg";
writeFileSync(cornerPointsOutputPath, canvas.toBuffer("image/jpeg"));
assert.ok(existsSync(cornerPointsOutputPath));
});
}); });
} }
setup(); let imageCount = 1;
let imageCount = 1; /*
* go through all images in test image directory
/* */
* go through all images in test image directory readdirSync(TEST_IMAGE_DIRECTORY).forEach((file) => {
*/
readdirSync(TEST_IMAGE_DIRECTORY).forEach((file) => {
const TEST_IMAGE_PATH = path.join(TEST_IMAGE_DIRECTORY, file); const TEST_IMAGE_PATH = path.join(TEST_IMAGE_DIRECTORY, file);
if(!file.endsWith("-sized.png")){ // these images are for the website, not testing if (!file.endsWith("-sized.png")) { // these images are for the website, not testing
let tempCount = imageCount++; let tempCount = imageCount++;
loadImage(TEST_IMAGE_PATH).then(function (image) { loadImage(TEST_IMAGE_PATH).then(function (image) {
test(image, tempCount); test(image, tempCount);
}); });
} }
}) })
});