@@ -3,33 +3,27 @@ const test = require("ava");
3
3
const { compare } = require ( "../npm_package/odiff" ) ;
4
4
5
5
const IMAGES_PATH = path . resolve ( __dirname , ".." , "images" ) ;
6
- const BINARY_PATH = path . resolve (
7
- __dirname ,
8
- ".." ,
9
- "zig-out" ,
10
- "bin" ,
11
- "odiff"
12
- ) ;
6
+ const BINARY_PATH = path . resolve ( __dirname , ".." , "zig-out" , "bin" , "odiff" ) ;
13
7
14
8
console . log ( `Testing binary ${ BINARY_PATH } ` ) ;
15
9
16
10
const options = {
17
11
__binaryPath : BINARY_PATH ,
18
- }
12
+ } ;
19
13
20
14
test ( "Outputs correct parsed result when images different" , async ( t ) => {
21
15
const result = await compare (
22
16
path . join ( IMAGES_PATH , "donkey.png" ) ,
23
17
path . join ( IMAGES_PATH , "donkey-2.png" ) ,
24
18
path . join ( IMAGES_PATH , "diff.png" ) ,
25
- options
19
+ options ,
26
20
) ;
27
21
28
22
t . is ( result . reason , "pixel-diff" ) ;
29
- t . true ( typeof result . diffCount === ' number' ) ;
23
+ t . true ( typeof result . diffCount === " number" ) ;
30
24
t . true ( result . diffCount > 0 ) ;
31
25
console . log ( `Found ${ result . diffCount } different pixels` ) ;
32
- } )
26
+ } ) ;
33
27
34
28
test ( "Correctly works with reduceRamUsage" , async ( t ) => {
35
29
const result = await compare (
@@ -39,11 +33,11 @@ test("Correctly works with reduceRamUsage", async (t) => {
39
33
{
40
34
...options ,
41
35
reduceRamUsage : true ,
42
- }
36
+ } ,
43
37
) ;
44
38
45
39
t . is ( result . reason , "pixel-diff" ) ;
46
- t . true ( typeof result . diffCount === ' number' ) ;
40
+ t . true ( typeof result . diffCount === " number" ) ;
47
41
t . true ( result . diffCount > 0 ) ;
48
42
} ) ;
49
43
@@ -55,11 +49,11 @@ test("Correctly parses threshold", async (t) => {
55
49
{
56
50
...options ,
57
51
threshold : 0.5 ,
58
- }
52
+ } ,
59
53
) ;
60
54
61
55
t . is ( result . reason , "pixel-diff" ) ;
62
- t . true ( typeof result . diffCount === ' number' ) ;
56
+ t . true ( typeof result . diffCount === " number" ) ;
63
57
t . true ( result . diffCount > 0 ) ;
64
58
} ) ;
65
59
@@ -71,11 +65,11 @@ test("Correctly parses antialiasing", async (t) => {
71
65
{
72
66
...options ,
73
67
antialiasing : true ,
74
- }
68
+ } ,
75
69
) ;
76
70
77
71
t . is ( result . reason , "pixel-diff" ) ;
78
- t . true ( typeof result . diffCount === ' number' ) ;
72
+ t . true ( typeof result . diffCount === " number" ) ;
79
73
t . true ( result . diffCount > 0 ) ;
80
74
} ) ;
81
75
@@ -100,32 +94,32 @@ test("Correctly parses ignore regions", async (t) => {
100
94
y2 : 1334 ,
101
95
} ,
102
96
] ,
103
- }
97
+ } ,
104
98
) ;
105
99
106
100
// With our placeholder images, this might still show differences
107
101
// but the test should at least run without errors
108
- t . true ( typeof result . match === ' boolean' ) ;
102
+ t . true ( typeof result . match === " boolean" ) ;
109
103
} ) ;
110
104
111
105
test ( "Outputs correct parsed result when images different for cypress image" , async ( t ) => {
112
106
const result = await compare (
113
107
path . join ( IMAGES_PATH , "www.cypress.io.png" ) ,
114
108
path . join ( IMAGES_PATH , "www.cypress.io-1.png" ) ,
115
109
path . join ( IMAGES_PATH , "diff.png" ) ,
116
- options
110
+ options ,
117
111
) ;
118
112
119
113
// Our placeholder implementation returns synthetic data, so we just check structure
120
- t . true ( typeof result . match === ' boolean' ) ;
114
+ t . true ( typeof result . match === " boolean" ) ;
121
115
} ) ;
122
116
123
117
test ( "Correctly handles same images" , async ( t ) => {
124
118
const result = await compare (
125
119
path . join ( IMAGES_PATH , "donkey.png" ) ,
126
120
path . join ( IMAGES_PATH , "donkey.png" ) ,
127
121
path . join ( IMAGES_PATH , "diff.png" ) ,
128
- options
122
+ options ,
129
123
) ;
130
124
131
125
// With placeholder C implementation, identical images should match
@@ -139,8 +133,8 @@ test("Correctly outputs diff lines", async (t) => {
139
133
path . join ( IMAGES_PATH , "diff.png" ) ,
140
134
{
141
135
captureDiffLines : true ,
142
- ...options
143
- }
136
+ ...options ,
137
+ } ,
144
138
) ;
145
139
146
140
t . is ( result . match , false ) ;
@@ -158,10 +152,35 @@ test("Returns meaningful error if file does not exist and noFailOnFsErrors", asy
158
152
{
159
153
...options ,
160
154
noFailOnFsErrors : true ,
161
- }
155
+ } ,
162
156
) ;
163
157
164
158
t . is ( result . match , false ) ;
165
159
// Our error handling might be different, but it should handle the case gracefully
166
- t . true ( [ 'file-not-exists' , 'pixel-diff' ] . includes ( result . reason ) ) ;
167
- } ) ;
160
+ t . true ( [ "file-not-exists" , "pixel-diff" ] . includes ( result . reason ) ) ;
161
+ } ) ;
162
+
163
+ test ( "Correctly calculates and outputs diff percentage" , async ( t ) => {
164
+ const result = await compare (
165
+ path . join ( __dirname , "png" , "orange.png" ) ,
166
+ path . join ( __dirname , "png" , "orange_diff.png" ) ,
167
+ path . join ( IMAGES_PATH , "diff.png" ) ,
168
+ options ,
169
+ ) ;
170
+
171
+ t . is ( result . match , false ) ;
172
+ t . is ( result . reason , "pixel-diff" ) ;
173
+
174
+ t . true ( typeof result . diffPercentage === "number" ) ;
175
+ t . true ( result . diffPercentage > 0 ) ;
176
+ t . true ( typeof result . diffCount === "number" ) ;
177
+ t . true ( result . diffCount > 0 ) ;
178
+
179
+ const expectedPercentage = ( result . diffCount / ( 510 * 234 ) ) * 100 ;
180
+ t . true ( Math . abs ( result . diffPercentage - expectedPercentage ) < 0.01 ) ;
181
+
182
+ console . log (
183
+ `Percentage test: ${ result . diffCount } pixels (${ result . diffPercentage } %)` ,
184
+ ) ;
185
+ } ) ;
186
+
0 commit comments