Skip to content

Commit 35a0e32

Browse files
committed
Convert tests to alcotes
1 parent c090038 commit 35a0e32

14 files changed

+384
-317
lines changed

.github/workflows/build.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,16 @@ jobs:
7171
opam-disable-sandboxing: true
7272
dune-cache: false
7373

74-
- run: opam exec -- opam install . --deps-only --update-invariant
74+
- run: opam exec -- opam install . --deps-only --with-test
7575
- run: opam exec -- dune build --verbose
7676
- run: opam exec -- dune exec ODiffBin -- --version
77+
- run: opam exec -- dune runtest
7778

78-
- uses: pnpm/action-setup@v4
79+
- if: failure()
80+
uses: actions/upload-artifact@v4
7981
with:
80-
version: 9
82+
name: test_images
83+
path: _build/default/test/test_images
8184

8285
- name: Set up Node.js
8386
uses: actions/setup-node@v4
@@ -90,5 +93,5 @@ jobs:
9093
run: npm ci
9194

9295
- name: e2e test
93-
run: pnpm test
96+
run: npm test
9497

bin/dune

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
(flags (:standard -w +42))
1212
(ocamlopt_flags (:standard -S)))
1313
(release
14-
(ocamlopt_flags (:standard -O3 -rounds 5 -unbox-closures -inline 200 -inline-max-depth 7 -unbox-closures-factor 50))))
14+
(ocamlopt_flags (:standard -no-g -O3 -rounds 5 -unbox-closures -inline 200 -inline-max-depth 7 -unbox-closures-factor 50))))
1515

dune-project

+8
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@
4646
(dune-configurator (>= 2.8))
4747
)
4848
)
49+
50+
(package
51+
(name odiff-tests)
52+
(depends
53+
(alcotest (= 1.8.0))
54+
odiff-core
55+
)
56+
)

odiff-tests.opam

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This file is generated by dune, edit dune-project instead
2+
opam-version: "2.0"
3+
maintainer: ["https://dmtrkovalenko.dev" "[email protected]"]
4+
authors: ["Dmitriy Kovalenko"]
5+
license: "MIT"
6+
homepage: "https://github.com/dmtrKovalenko/odiff"
7+
bug-reports: "https://github.com/dmtrKovalenko/odiff/issues"
8+
depends: [
9+
"dune" {>= "2.8"}
10+
"alcotest" {= "1.8.0"}
11+
"odiff-core"
12+
"odoc" {with-doc}
13+
]
14+
build: [
15+
["dune" "subst"] {dev}
16+
[
17+
"dune"
18+
"build"
19+
"-p"
20+
name
21+
"-j"
22+
jobs
23+
"@install"
24+
"@runtest" {with-test}
25+
"@doc" {with-doc}
26+
]
27+
]
28+
dev-repo: "git+https://github.com/dmtrKovalenko/odiff.git"

test/RunTests.ml

-1
This file was deleted.

test/TestFramework.ml

-5
This file was deleted.

test/Test_Core.ml

+99-124
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,107 @@
1-
open TestFramework
2-
open ODiffIO
1+
open Alcotest
32
module PNG_Diff = Odiff.Diff.MakeDiff (Png.IO) (Png.IO)
43

5-
let _ =
6-
describe "CORE: Antialiasing" (fun { test; _ } ->
7-
let open Png.IO in
8-
test "does not count anti-aliased pixels as different"
9-
(fun { expect; _ } ->
10-
let img1 = loadImage "test/test-images/aa/antialiasing-on.png" in
11-
let img2 = loadImage "test/test-images/aa/antialiasing-off.png" in
12-
let _, diffPixels, diffPercentage, _ =
13-
PNG_Diff.compare img1 img2 ~outputDiffMask:false ~antialiasing:true
14-
()
15-
in
16-
(expect.int diffPixels).toBe 46;
17-
(expect.float diffPercentage).toBeCloseTo 0.115);
18-
test "tests different sized AA images" (fun { expect; _ } ->
19-
let img1 = loadImage "test/test-images/aa/antialiasing-on.png" in
20-
let img2 =
21-
loadImage "test/test-images/aa/antialiasing-off-small.png"
22-
in
23-
let _, diffPixels, diffPercentage, _ =
24-
PNG_Diff.compare img1 img2 ~outputDiffMask:true ~antialiasing:true
25-
()
26-
in
27-
(expect.int diffPixels).toBe 417;
28-
(expect.float diffPercentage).toBeCloseTo 1.04))
4+
let test_antialiasing () =
5+
Sys.getcwd () |> print_endline;
6+
let img1 = Png.IO.loadImage "test-images/aa/antialiasing-on.png" in
7+
let img2 = Png.IO.loadImage "test-images/aa/antialiasing-off.png" in
8+
let _, diffPixels, diffPercentage, _ =
9+
PNG_Diff.compare img1 img2 ~outputDiffMask:false ~antialiasing:true ()
10+
in
11+
check int "diffPixels" 46 diffPixels;
12+
check (float 0.001) "diffPercentage" 0.115 diffPercentage
2913

30-
let _ =
31-
describe "CORE: Threshold" (fun { test; _ } ->
32-
test "uses provided threshold" (fun { expect; _ } ->
33-
let img1 = Png.IO.loadImage "test/test-images/png/orange.png" in
34-
let img2 =
35-
Png.IO.loadImage "test/test-images/png/orange_changed.png"
36-
in
37-
let _, diffPixels, diffPercentage, _ =
38-
PNG_Diff.compare img1 img2 ~threshold:0.5 ()
39-
in
40-
(expect.int diffPixels).toBe 25;
41-
(expect.float diffPercentage).toBeCloseTo 0.02))
14+
let test_different_sized_aa_images () =
15+
let img1 = Png.IO.loadImage "test-images/aa/antialiasing-on.png" in
16+
let img2 =
17+
Png.IO.loadImage "test-images/aa/antialiasing-off-small.png"
18+
in
19+
let _, diffPixels, diffPercentage, _ =
20+
PNG_Diff.compare img1 img2 ~outputDiffMask:true ~antialiasing:true ()
21+
in
22+
check int "diffPixels" 417 diffPixels;
23+
check (float 0.01) "diffPercentage" 1.04 diffPercentage
4224

43-
let _ =
44-
describe "CORE: Ignore Regions" (fun { test; _ } ->
45-
test "uses provided irgnore regions" (fun { expect; _ } ->
46-
let img1 = Png.IO.loadImage "test/test-images/png/orange.png" in
47-
let img2 =
48-
Png.IO.loadImage "test/test-images/png/orange_changed.png"
49-
in
50-
let _diffOutput, diffPixels, diffPercentage, _ =
51-
PNG_Diff.compare img1 img2
52-
~ignoreRegions:
53-
[ ((150, 30), (310, 105)); ((20, 175), (105, 200)) ]
54-
()
55-
in
56-
(expect.int diffPixels).toBe 0;
57-
(expect.float diffPercentage).toBeCloseTo 0.0))
25+
let test_threshold () =
26+
let img1 = Png.IO.loadImage "test-images/png/orange.png" in
27+
let img2 = Png.IO.loadImage "test-images/png/orange_changed.png" in
28+
let _, diffPixels, diffPercentage, _ =
29+
PNG_Diff.compare img1 img2 ~threshold:0.5 ()
30+
in
31+
check int "diffPixels" 25 diffPixels;
32+
check (float 0.001) "diffPercentage" 0.02 diffPercentage
5833

59-
let _ =
60-
describe "CORE: Diff Color" (fun { test; _ } ->
61-
test "creates diff output image with custom green diff color"
62-
(fun { expect; _ } ->
63-
let img1 = Png.IO.loadImage "test/test-images/png/orange.png" in
64-
let img2 =
65-
Png.IO.loadImage "test/test-images/png/orange_changed.png"
66-
in
67-
let diffOutput, _, _, _ =
68-
PNG_Diff.compare img1 img2
69-
~diffPixel:
70-
(Int32.of_int 4278255360 (*int32 representation of #00ff00*))
71-
()
72-
in
73-
let originalDiff =
74-
Png.IO.loadImage "test/test-images/png/orange_diff_green.png"
75-
in
76-
let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ =
77-
PNG_Diff.compare originalDiff diffOutput ()
78-
in
79-
if diffOfDiffPixels > 0 then (
80-
Png.IO.saveImage diffOutput
81-
"test/test-images/png/diff-output-green.png";
82-
Png.IO.saveImage diffMaskOfDiff
83-
"test/test-images/png/diff-of-diff-green.png");
84-
(expect.int diffOfDiffPixels).toBe 0;
85-
(expect.float diffOfDiffPercentage).toBeCloseTo 0.0))
34+
let test_ignore_regions () =
35+
let img1 = Png.IO.loadImage "test-images/png/orange.png" in
36+
let img2 = Png.IO.loadImage "test-images/png/orange_changed.png" in
37+
let _diffOutput, diffPixels, diffPercentage, _ =
38+
PNG_Diff.compare img1 img2
39+
~ignoreRegions:[ ((150, 30), (310, 105)); ((20, 175), (105, 200)) ]
40+
()
41+
in
42+
check int "diffPixels" 0 diffPixels;
43+
check (float 0.001) "diffPercentage" 0.0 diffPercentage
8644

87-
let _ =
88-
describe "CORE: blendSemiTransparentColor" (fun { test; _ } ->
89-
test "blend 255. alpha" (fun { expect; _ } ->
90-
let r, g, b, a =
91-
Odiff.ColorDelta.blendSemiTransparentColor (0., 128., 255., 255.)
92-
in
93-
(expect.float r).toBeCloseTo 0.;
94-
(expect.float g).toBeCloseTo 128.;
95-
(expect.float b).toBeCloseTo 255.;
96-
(expect.float a).toBeCloseTo 1.);
45+
let test_diff_color () =
46+
let img1 = Png.IO.loadImage "test-images/png/orange.png" in
47+
let img2 = Png.IO.loadImage "test-images/png/orange_changed.png" in
48+
let diffOutput, _, _, _ =
49+
PNG_Diff.compare img1 img2
50+
~diffPixel:(Int32.of_int 4278255360 (*int32 representation of #00ff00*))
51+
()
52+
in
53+
let originalDiff =
54+
Png.IO.loadImage "test-images/png/orange_diff_green.png"
55+
in
56+
let diffMaskOfDiff, diffOfDiffPixels, diffOfDiffPercentage, _ =
57+
PNG_Diff.compare originalDiff diffOutput ()
58+
in
59+
if diffOfDiffPixels > 0 then (
60+
Png.IO.saveImage diffOutput "test-images/png/diff-output-green.png";
61+
Png.IO.saveImage diffMaskOfDiff
62+
"test-images/png/diff-of-diff-green.png");
63+
check int "diffOfDiffPixels" 0 diffOfDiffPixels;
64+
check (float 0.001) "diffOfDiffPercentage" 0.0 diffOfDiffPercentage
9765

98-
test "blend 0. alpha" (fun { expect; _ } ->
99-
let r, g, b, a =
100-
Odiff.ColorDelta.blendSemiTransparentColor (0., 128., 255., 0.)
101-
in
102-
(expect.float r).toBeCloseTo 255.;
103-
(expect.float g).toBeCloseTo 255.;
104-
(expect.float b).toBeCloseTo 255.;
105-
(expect.float a).toBeCloseTo 0.);
66+
let test_blend_semi_transparent_color () =
67+
let test_blend r g b a expected_r expected_g expected_b expected_a =
68+
let r', g', b', a' =
69+
Odiff.ColorDelta.blendSemiTransparentColor (r, g, b, a)
70+
in
71+
check (float 0.01) "r" expected_r r';
72+
check (float 0.01) "g" expected_g g';
73+
check (float 0.01) "b" expected_b b';
74+
check (float 0.01) "a" expected_a a'
75+
in
76+
test_blend 0. 128. 255. 255. 0. 128. 255. 1.;
77+
test_blend 0. 128. 255. 0. 255. 255. 255. 0.;
78+
test_blend 0. 128. 255. 5. 250. 252.51 255. 0.02;
79+
test_blend 0. 128. 255. 51. 204. 229.6 255. 0.2;
80+
test_blend 0. 128. 255. 128. 127. 191.25 255. 0.5
10681

107-
test "blend 5. alpha" (fun { expect; _ } ->
108-
let r, g, b, a =
109-
Odiff.ColorDelta.blendSemiTransparentColor (0., 128., 255., 5.)
110-
in
111-
(expect.float r).toBeCloseTo 250.;
112-
(expect.float g).toBeCloseTo 252.51;
113-
(expect.float b).toBeCloseTo 255.;
114-
(expect.float a).toBeCloseTo 0.02);
115-
116-
test "blend 51. alpha" (fun { expect; _ } ->
117-
let r, g, b, a =
118-
Odiff.ColorDelta.blendSemiTransparentColor (0., 128., 255., 51.)
119-
in
120-
(expect.float r).toBeCloseTo 204.;
121-
(expect.float g).toBeCloseTo 229.6;
122-
(expect.float b).toBeCloseTo 255.;
123-
(expect.float a).toBeCloseTo 0.2);
124-
125-
test "blend 128. alpha" (fun { expect; _ } ->
126-
let r, g, b, a =
127-
Odiff.ColorDelta.blendSemiTransparentColor (0., 128., 255., 128.)
128-
in
129-
(expect.float r).toBeCloseTo 127.;
130-
(expect.float g).toBeCloseTo 191.25;
131-
(expect.float b).toBeCloseTo 255.;
132-
(expect.float a).toBeCloseTo 0.5))
82+
let () =
83+
run "CORE"
84+
[
85+
( "Antialiasing",
86+
[
87+
test_case "does not count anti-aliased pixels as different" `Quick
88+
test_antialiasing;
89+
test_case "tests different sized AA images" `Quick
90+
test_different_sized_aa_images;
91+
] );
92+
( "Threshold",
93+
[ test_case "uses provided threshold" `Quick test_threshold ] );
94+
( "Ignore Regions",
95+
[ test_case "uses provided ignore regions" `Quick test_ignore_regions ]
96+
);
97+
( "Diff Color",
98+
[
99+
test_case "creates diff output image with custom green diff color"
100+
`Quick test_diff_color;
101+
] );
102+
( "blendSemiTransparentColor",
103+
[
104+
test_case "blend semi-transparent colors" `Quick
105+
test_blend_semi_transparent_color;
106+
] );
107+
]

0 commit comments

Comments
 (0)