@@ -2,43 +2,19 @@ import * as path from "path";
22import { Image } from "../../image.class" ;
33import { MatchRequest } from "../../match-request.class" ;
44import { Region } from "../../region.class" ;
5+ import { ImageReader } from "./image-reader.class" ;
56import { TemplateMatchingFinder } from "./template-matching-finder.class" ;
67
78describe ( "Template-matching finder" , ( ) => {
8- it ( "loadImage should resolve to a non-empty Mat on successful load" , async ( ) => {
9- // GIVEN
10- const SUT = new TemplateMatchingFinder ( ) ;
11- const imagePath = path . resolve ( __dirname , "./__mocks__/mouse.png" ) ;
12-
13- // WHEN
14- const result = await SUT . loadImage ( imagePath ) ;
15-
16- // THEN
17- expect ( result . rows ) . toBeGreaterThan ( 0 ) ;
18- expect ( result . cols ) . toBeGreaterThan ( 0 ) ;
19- expect ( result . empty ) . toBeFalsy ( ) ;
20- } ) ;
21-
22- it ( "loadImage should reject on unsuccessful load" , async ( ) => {
23- // GIVEN
24- const SUT = new TemplateMatchingFinder ( ) ;
25- const imagePath = "./__mocks__/foo.png" ;
26-
27- // WHEN
28- const call = SUT . loadImage ;
29-
30- // THEN
31- await expect ( call ( imagePath ) ) . rejects . toEqual ( "empty Mat" ) ;
32- } ) ;
33-
349 it ( "findMatch should return a match when present in image" , async ( ) => {
3510 // GIVEN
11+ const imageLoader = new ImageReader ( ) ;
3612 const SUT = new TemplateMatchingFinder ( ) ;
3713 const imagePath = path . resolve ( __dirname , "./__mocks__/mouse.png" ) ;
38- const needle = await SUT . loadImage ( imagePath ) ;
14+ const needle = await imageLoader . load ( imagePath ) ;
3915 const minConfidence = 0.99 ;
40- const searchRegion = new Region ( 0 , 0 , needle . cols , needle . rows ) ;
41- const haystack = new Image ( needle . cols , needle . rows , needle . getData ( ) , 3 ) ;
16+ const searchRegion = new Region ( 0 , 0 , needle . width , needle . height ) ;
17+ const haystack = new Image ( needle . width , needle . height , needle . data , 3 ) ;
4218 const matchRequest = new MatchRequest ( haystack , imagePath , searchRegion , minConfidence ) ;
4319
4420 // WHEN
@@ -51,12 +27,13 @@ describe("Template-matching finder", () => {
5127
5228 it ( "findMatch should return a match within a search region when present in image" , async ( ) => {
5329 // GIVEN
30+ const imageLoader = new ImageReader ( ) ;
5431 const SUT = new TemplateMatchingFinder ( ) ;
5532 const imagePath = path . resolve ( __dirname , "./__mocks__/mouse.png" ) ;
56- const needle = await SUT . loadImage ( imagePath ) ;
33+ const needle = await imageLoader . load ( imagePath ) ;
5734 const minConfidence = 0.99 ;
5835 const searchRegion = new Region ( 10 , 20 , 100 , 100 ) ;
59- const haystack = new Image ( needle . cols , needle . rows , needle . getData ( ) , 3 ) ;
36+ const haystack = new Image ( needle . width , needle . height , needle . data , 3 ) ;
6037 const matchRequest = new MatchRequest ( haystack , imagePath , searchRegion , minConfidence ) ;
6138
6239 // WHEN
@@ -69,13 +46,20 @@ describe("Template-matching finder", () => {
6946
7047 it ( "findMatch should throw on invalid image paths" , async ( ) => {
7148 // GIVEN
49+ const imageLoader = new ImageReader ( ) ;
7250 const SUT = new TemplateMatchingFinder ( ) ;
73- const imagePath = path . resolve ( __dirname , "./__mocks__/foo.png" ) ;
51+ const pathToNeedle = path . resolve ( __dirname , "./__mocks__/mouse.png" ) ;
52+ const pathToHaystack = "./__mocks__/foo.png" ;
53+ const needle = await imageLoader . load ( pathToNeedle ) ;
54+ const minConfidence = 0.99 ;
55+ const searchRegion = new Region ( 0 , 0 , 100 , 100 ) ;
56+ const haystack = new Image ( needle . width , needle . height , needle . data , 3 ) ;
57+ const matchRequest = new MatchRequest ( haystack , pathToHaystack , searchRegion , minConfidence ) ;
7458
7559 // WHEN
76- const call = await SUT . loadImage ;
60+ const result = SUT . findMatch ( matchRequest ) ;
7761
7862 // THEN
79- await expect ( call ( imagePath ) ) . rejects . toEqual ( "empty Mat" ) ;
63+ expect ( result ) . rejects . toEqual ( `Failed to load image from ' ${ pathToHaystack } '` ) ;
8064 } ) ;
8165} ) ;
0 commit comments