Skip to content

Commit 013f5cf

Browse files
committedMar 15, 2021
Tests: refactor modulate suite, ~20x faster
1 parent d5d008f commit 013f5cf

20 files changed

+108
-85
lines changed
 
-663 KB
Binary file not shown.
Binary file not shown.
-691 KB
Binary file not shown.
-653 KB
Binary file not shown.
-63.8 KB

Error rendering embedded code

Invalid image source.

-63.8 KB

Error rendering embedded code

Invalid image source.

-63.9 KB

Error rendering embedded code

Invalid image source.

-63.7 KB

Error rendering embedded code

Invalid image source.

-64.2 KB

Error rendering embedded code

Invalid image source.

-65.4 KB

Error rendering embedded code

Invalid image source.

-64.8 KB

Error rendering embedded code

Invalid image source.

Error rendering embedded code

Invalid image source.

-64.6 KB

Error rendering embedded code

Invalid image source.

-64.7 KB

Error rendering embedded code

Invalid image source.

-63.9 KB

Error rendering embedded code

Invalid image source.

-63.2 KB

Error rendering embedded code

Invalid image source.

-61.7 KB
Binary file not shown.
Binary file not shown.
-672 KB
Binary file not shown.

‎test/unit/modulate.js

+108-85
Original file line numberDiff line numberDiff line change
@@ -28,115 +28,138 @@ describe('Modulate', function () {
2828
});
2929
});
3030

31-
it('should be able to hue-rotate', function () {
32-
const base = 'modulate-hue-120.jpg';
33-
const actual = fixtures.path('output.' + base);
34-
const expected = fixtures.expected(base);
35-
36-
return sharp(fixtures.inputJpg)
31+
it('should be able to hue-rotate', async () => {
32+
const [r, g, b] = await sharp({
33+
create: {
34+
width: 1,
35+
height: 1,
36+
channels: 3,
37+
background: { r: 153, g: 68, b: 68 }
38+
}
39+
})
3740
.modulate({ hue: 120 })
38-
.toFile(actual)
39-
.then(function () {
40-
fixtures.assertMaxColourDistance(actual, expected, 25);
41-
});
42-
});
41+
.raw()
42+
.toBuffer();
4343

44-
it('should be able to brighten', function () {
45-
const base = 'modulate-brightness-2.jpg';
46-
const actual = fixtures.path('output.' + base);
47-
const expected = fixtures.expected(base);
48-
49-
return sharp(fixtures.inputJpg)
50-
.modulate({ brightness: 2 })
51-
.toFile(actual)
52-
.then(function () {
53-
fixtures.assertMaxColourDistance(actual, expected, 25);
54-
});
44+
assert.deepStrictEqual({ r: 41, g: 107, b: 57 }, { r, g, b });
5545
});
5646

57-
it('should be able to unbrighten', function () {
58-
const base = 'modulate-brightness-0-5.jpg';
59-
const actual = fixtures.path('output.' + base);
60-
const expected = fixtures.expected(base);
47+
it('should be able to brighten', async () => {
48+
const [r, g, b] = await sharp({
49+
create: {
50+
width: 1,
51+
height: 1,
52+
channels: 3,
53+
background: { r: 153, g: 68, b: 68 }
54+
}
55+
})
56+
.modulate({ brightness: 2 })
57+
.raw()
58+
.toBuffer();
6159

62-
return sharp(fixtures.inputJpg)
63-
.modulate({ brightness: 0.5 })
64-
.toFile(actual)
65-
.then(function () {
66-
fixtures.assertMaxColourDistance(actual, expected, 25);
67-
});
60+
assert.deepStrictEqual({ r: 255, g: 173, b: 168 }, { r, g, b });
6861
});
6962

70-
it('should be able to saturate', function () {
71-
const base = 'modulate-saturation-2.jpg';
72-
const actual = fixtures.path('output.' + base);
73-
const expected = fixtures.expected(base);
63+
it('should be able to darken', async () => {
64+
const [r, g, b] = await sharp({
65+
create: {
66+
width: 1,
67+
height: 1,
68+
channels: 3,
69+
background: { r: 153, g: 68, b: 68 }
70+
}
71+
})
72+
.modulate({ brightness: 0.5 })
73+
.raw()
74+
.toBuffer();
7475

75-
return sharp(fixtures.inputJpg)
76-
.modulate({ saturation: 2 })
77-
.toFile(actual)
78-
.then(function () {
79-
fixtures.assertMaxColourDistance(actual, expected, 30);
80-
});
76+
assert.deepStrictEqual({ r: 97, g: 17, b: 25 }, { r, g, b });
8177
});
8278

83-
it('should be able to desaturate', function () {
84-
const base = 'modulate-saturation-0.5.jpg';
85-
const actual = fixtures.path('output.' + base);
86-
const expected = fixtures.expected(base);
79+
it('should be able to saturate', async () => {
80+
const [r, g, b] = await sharp({
81+
create: {
82+
width: 1,
83+
height: 1,
84+
channels: 3,
85+
background: { r: 153, g: 68, b: 68 }
86+
}
87+
})
88+
.modulate({ saturation: 2 })
89+
.raw()
90+
.toBuffer();
8791

88-
return sharp(fixtures.inputJpg)
89-
.modulate({ saturation: 0.5 })
90-
.toFile(actual)
91-
.then(function () {
92-
fixtures.assertMaxColourDistance(actual, expected, 25);
93-
});
92+
assert.deepStrictEqual({ r: 198, g: 0, b: 43 }, { r, g, b });
9493
});
9594

96-
it('should be able to modulate all channels', function () {
97-
const base = 'modulate-all.jpg';
98-
const actual = fixtures.path('output.' + base);
99-
const expected = fixtures.expected(base);
95+
it('should be able to desaturate', async () => {
96+
const [r, g, b] = await sharp({
97+
create: {
98+
width: 1,
99+
height: 1,
100+
channels: 3,
101+
background: { r: 153, g: 68, b: 68 }
102+
}
103+
})
104+
.modulate({ saturation: 0.5 })
105+
.raw()
106+
.toBuffer();
100107

101-
return sharp(fixtures.inputJpg)
102-
.modulate({ brightness: 2, saturation: 0.5, hue: 180 })
103-
.toFile(actual)
104-
.then(function () {
105-
fixtures.assertMaxColourDistance(actual, expected, 25);
106-
});
108+
assert.deepStrictEqual({ r: 127, g: 83, b: 81 }, { r, g, b });
107109
});
108110

109-
describe('hue-rotate', function (done) {
110-
[30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360].forEach(function (angle) {
111-
it('should properly hue rotate by ' + angle + 'deg', function () {
112-
const base = 'modulate-hue-angle-' + angle + '.png';
113-
const actual = fixtures.path('output.' + base);
114-
const expected = fixtures.expected(base);
111+
it('should be able to modulate all channels', async () => {
112+
const [r, g, b] = await sharp({
113+
create: {
114+
width: 1,
115+
height: 1,
116+
channels: 3,
117+
background: { r: 153, g: 68, b: 68 }
118+
}
119+
})
120+
.modulate({ brightness: 2, saturation: 0.5, hue: 180 })
121+
.raw()
122+
.toBuffer();
115123

116-
return sharp(fixtures.testPattern)
117-
.modulate({ hue: angle })
118-
.toFile(actual)
119-
.then(function () {
120-
fixtures.assertMaxColourDistance(actual, expected, 25);
121-
});
122-
});
123-
});
124+
assert.deepStrictEqual({ r: 149, g: 209, b: 214 }, { r, g, b });
124125
});
125126

126-
it('should be able to use linear and modulate together', function () {
127-
const base = 'modulate-linear.jpg';
128-
const actual = fixtures.path('output.' + base);
129-
const expected = fixtures.expected(base);
130-
127+
it('should be able to use linear and modulate together', async () => {
131128
const contrast = 1.5;
132129
const brightness = 0.5;
133130

134-
return sharp(fixtures.testPattern)
131+
const [r, g, b] = await sharp({
132+
create: {
133+
width: 1,
134+
height: 1,
135+
channels: 3,
136+
background: { r: 153, g: 68, b: 68 }
137+
}
138+
})
135139
.linear(contrast, -(128 * contrast) + 128)
136140
.modulate({ brightness })
137-
.toFile(actual)
138-
.then(function () {
139-
fixtures.assertMaxColourDistance(actual, expected);
141+
.raw()
142+
.toBuffer();
143+
144+
assert.deepStrictEqual({ r: 81, g: 0, b: 0 }, { r, g, b });
145+
});
146+
147+
describe('hue-rotate', () => {
148+
[30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360].forEach(angle => {
149+
it(`should hue rotate by ${angle} deg`, async () => {
150+
const base = `modulate-hue-angle-${angle}.png`;
151+
const actual = fixtures.path(`output.${base}`);
152+
const expected = fixtures.expected(base);
153+
154+
await sharp(fixtures.testPattern)
155+
.resize(320)
156+
.modulate({ hue: angle })
157+
.png({ compressionLevel: 0 })
158+
.toFile(actual)
159+
.then(() => {
160+
fixtures.assertMaxColourDistance(actual, expected);
161+
});
140162
});
163+
});
141164
});
142165
});

0 commit comments

Comments
 (0)
Please sign in to comment.