Skip to content

Commit 76e9efd

Browse files
committed
(#371) Added early returns to image processor + test case
1 parent fa7e472 commit 76e9efd

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Image } from "../../image.class";
2+
import { Point } from "../../point.class";
3+
import JimpImageProcessor from "./jimp-image-processor.class";
4+
5+
const imageWidth = 10;
6+
const imageHeight = 20;
7+
8+
describe("JimpImageProcessor", () => {
9+
it.each([[new Point(-1, 5)], [new Point(imageWidth + 5, 5)]])(
10+
`should reject on out of bounds x coordinates`,
11+
async (outOfBoundsPoint: Point) => {
12+
// GIVEN
13+
const inputImage = new Image(
14+
imageWidth,
15+
imageHeight,
16+
Buffer.from([0, 0, 0]),
17+
3,
18+
"input_image",
19+
4,
20+
4
21+
);
22+
const SUT = new JimpImageProcessor();
23+
24+
// WHEN
25+
const result = SUT.colorAt(inputImage, outOfBoundsPoint);
26+
27+
// THEN
28+
await expect(result).rejects.toBe(
29+
`Query location out of bounds. Should be in range 0 <= x < image.width, is ${outOfBoundsPoint.x}`
30+
);
31+
}
32+
);
33+
34+
it.each([[new Point(5, -1)], [new Point(5, imageHeight + 10)]])(
35+
`should reject on out of bounds y coordinates`,
36+
async (outOfBoundsPoint: Point) => {
37+
// GIVEN
38+
const imageWidth = 10;
39+
const imageHeight = 20;
40+
const inputImage = new Image(
41+
imageWidth,
42+
imageHeight,
43+
Buffer.from([0, 0, 0]),
44+
3,
45+
"input_image",
46+
4,
47+
4
48+
);
49+
const SUT = new JimpImageProcessor();
50+
51+
// WHEN
52+
const result = SUT.colorAt(inputImage, outOfBoundsPoint);
53+
54+
// THEN
55+
await expect(result).rejects.toBe(
56+
`Query location out of bounds. Should be in range 0 <= y < image.height, is ${outOfBoundsPoint.y}`
57+
);
58+
}
59+
);
60+
});

Diff for: lib/provider/image/jimp-image-processor.class.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ export default class implements ImageProcessor {
1717
reject(
1818
`Query location out of bounds. Should be in range 0 <= x < image.width, is ${location.x}`
1919
);
20+
return;
2021
}
2122
if (location.y < 0 || location.y >= img.height) {
2223
reject(
2324
`Query location out of bounds. Should be in range 0 <= y < image.height, is ${location.y}`
2425
);
26+
return;
2527
}
2628
const jimpImage = imageToJimp(img);
2729
const rgba = Jimp.intToRGBA(

0 commit comments

Comments
 (0)