jscanify 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/tests.js CHANGED
@@ -6,71 +6,75 @@ console.log("RUNNING JSCANIFY TESTS");
6
6
  console.log("Warning: This may take a bit");
7
7
 
8
8
  const { loadImage, createCanvas } = require("canvas");
9
- const { mkdirSync, writeFileSync, unlinkSync, existsSync } = require("fs");
9
+ const { mkdirSync, writeFileSync, unlinkSync, existsSync, readdirSync } = require("fs");
10
10
  const assert = require("assert");
11
11
 
12
12
  const jscanify = require("../src/jscanify-node");
13
13
  const path = require("path");
14
14
 
15
- const outputPaths = {
16
- highlight: __dirname + "/output/highlighted.jpg",
17
- extracted: __dirname + "/output/extracted.jpg",
18
- cornerPoints: __dirname + "/output/corner_points.jpg",
19
- };
20
15
 
21
- const baseFolder = __dirname.replaceAll("\\", "/") + "/output/";
16
+ const OUTPUT_FOLDER = __dirname.replaceAll("\\", "/") + "/output/";
22
17
 
23
- const TEST_IMAGE_PATH = path.join(
18
+ const TEST_IMAGE_DIRECTORY = path.join(
24
19
  __dirname,
25
20
  "..",
26
21
  "docs",
27
22
  "images",
28
- "test",
29
- "test.png"
23
+ "test"
30
24
  );
31
25
 
26
+ /**
27
+ * delete previously generated output images
28
+ */
32
29
  function setup() {
33
30
  console.log("=== setting up tests ===");
34
31
  console.log("Deleting previously generated images");
35
- Object.values(outputPaths).forEach((path) => {
36
- if (existsSync(path)) {
37
- unlinkSync(path);
38
- }
39
- });
40
32
 
41
- if (!existsSync(baseFolder)) {
42
- mkdirSync(baseFolder);
33
+ if (!existsSync(OUTPUT_FOLDER)) {
34
+ mkdirSync(OUTPUT_FOLDER);
43
35
  }
36
+
37
+ readdirSync(OUTPUT_FOLDER).forEach((file) => {
38
+ unlinkSync(path.join(OUTPUT_FOLDER, file));
39
+ })
44
40
  }
45
41
 
46
- function test() {
47
- const scanner = new jscanify();
42
+ console.log("=== beginning tests ===");
43
+ console.log("loading OpenCV.js...");
44
+
45
+ const scanner = new jscanify();
46
+ scanner.loadOpenCV(function (cv) {
48
47
 
49
- console.log("=== beginning tests ===");
50
- console.log("loading OpenCV.js...");
51
- scanner.loadOpenCV(function (cv) {
52
- console.log("Finished loading OpenCV.js");
53
- console.log("Writing test images to: " + baseFolder);
54
- describe("feature tests", function () {
48
+ console.log("Finished loading OpenCV.js");
49
+ console.log("Writing test images to: " + OUTPUT_FOLDER);
50
+
51
+ /**
52
+ * tests an individual image
53
+ */
54
+ function test(testImage, imageCount) {
55
+ describe("image #" + imageCount, function () {
55
56
  it("should highlight paper", function (done) {
56
57
  const highlighted = scanner.highlightPaper(testImage);
58
+ const higlightedOutputPath = OUTPUT_FOLDER + "highlighted-" + imageCount + ".jpg";
57
59
  writeFileSync(
58
- outputPaths.highlight,
60
+ higlightedOutputPath,
59
61
  highlighted.toBuffer("image/jpeg")
60
62
  );
61
63
 
62
- assert.ok(existsSync(outputPaths.highlight));
64
+ assert.ok(existsSync(higlightedOutputPath));
63
65
  done();
64
66
  });
65
67
 
66
68
  it("should extract paper", function (done) {
67
69
  const extracted = scanner.extractPaper(testImage, 386, 500);
70
+ const extractedOutputPath = OUTPUT_FOLDER + "extracted-" + imageCount + ".jpg";
71
+
68
72
  writeFileSync(
69
- outputPaths.extracted,
73
+ extractedOutputPath,
70
74
  extracted.toBuffer("image/jpeg")
71
75
  );
72
76
 
73
- assert.ok(existsSync(outputPaths.extracted));
77
+ assert.ok(existsSync(extractedOutputPath));
74
78
  done();
75
79
  });
76
80
 
@@ -103,18 +107,31 @@ function test() {
103
107
  ctx.fill();
104
108
  });
105
109
 
106
- writeFileSync(outputPaths.cornerPoints, canvas.toBuffer("image/jpeg"));
110
+ const cornerPointsOutputPath = OUTPUT_FOLDER + "corner_points-" + imageCount + ".jpg";
111
+ writeFileSync(cornerPointsOutputPath, canvas.toBuffer("image/jpeg"));
107
112
 
108
- assert.ok(existsSync(outputPaths.cornerPoints));
113
+ assert.ok(existsSync(cornerPointsOutputPath));
109
114
  done();
110
115
  });
111
116
  });
112
- });
113
- }
117
+ }
114
118
 
115
- let testImage;
116
- loadImage(TEST_IMAGE_PATH).then(function (image) {
117
- testImage = image;
118
119
  setup();
119
- test();
120
- });
120
+
121
+ let imageCount = 1;
122
+
123
+ /*
124
+ * go through all images in test image directory
125
+ */
126
+ readdirSync(TEST_IMAGE_DIRECTORY).forEach((file) => {
127
+ const TEST_IMAGE_PATH = path.join(TEST_IMAGE_DIRECTORY, file);
128
+
129
+ if(!file.endsWith("-sized.png")){ // these images are for the website, not testing
130
+ let tempCount = imageCount++;
131
+
132
+ loadImage(TEST_IMAGE_PATH).then(function (image) {
133
+ test(image, tempCount);
134
+ });
135
+ }
136
+ })
137
+ });