Skip to content

Commit aa0f966

Browse files
committed
Added a few snapshot tests
1 parent c476a24 commit aa0f966

File tree

8 files changed

+90
-10
lines changed

8 files changed

+90
-10
lines changed

.eslintrc.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
module.exports = {
22
'env': {
33
'browser': true,
4-
'node': true
4+
'node': true,
5+
'mocha': true,
6+
'es2017': true
57
},
68
'extends': 'eslint:recommended',
79
'globals': {
810
'Uint8Array': false,
911
'define': false,
1012
'Float32Array': false
1113
},
12-
'plugins': [
13-
'mocha'
14-
],
1514
'rules': {
1615
'indent': [
1716
'error',

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"eslint": "^7.32.0",
1717
"eslint-config-standard": "^16.0.3",
1818
"eslint-plugin-import": "^2.24.1",
19-
"eslint-plugin-mocha": "^9.0.0",
2019
"eslint-plugin-node": "^11.1.0",
2120
"eslint-plugin-promise": "^5.1.0",
2221
"mocha": "^9.1.0",

snapshots/noise2D.png

3.17 KB
Loading

snapshots/noise3D.png

4.13 KB
Loading

snapshots/noise4D.png

3.72 KB
Loading

snapshots/permutationTable.png

340 Bytes
Loading

test/matches-snapshot.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const fs = require('fs');
2+
const PNG = require('pngjs').PNG;
3+
const path = require('path');
4+
5+
const snapshotsPath = 'snapshots';
6+
7+
function assertMatchesImage(actual, imageFilename) {
8+
if(!imageFilename.endsWith('.png')) {
9+
console.log('throwing');
10+
throw new Error('imageFilename must end in .png');
11+
}
12+
let fileBuffer;
13+
try {
14+
fileBuffer = fs.readFileSync(path.join(snapshotsPath, imageFilename));
15+
}
16+
catch (_) {
17+
writeImageSnapshot(actual, imageFilename);
18+
return;
19+
}
20+
const png = PNG.sync.read(fileBuffer);
21+
if (actual.data.length * 4 !== png.data.length) {
22+
throw new Error('Expected actual.length to match png.data.length');
23+
}
24+
const identical = actual.data.every((value, i) => value == png.data[i*4]);
25+
if(!identical) {
26+
console.log(png.data);
27+
writeImageSnapshot(actual, imageFilename.replace('.png', '.error.png'));
28+
throw new Error('expected data to be identitcal');
29+
}
30+
31+
}
32+
exports.assertMatchesImage = assertMatchesImage;
33+
34+
function writeImageSnapshot(actual, imageFilename) {
35+
const png = new PNG({
36+
colorType: 0,
37+
inputColorType: 0,
38+
bitDepth: 16,
39+
width: actual.width,
40+
height: actual.height,
41+
inputHasAlpha: false,
42+
});
43+
if (actual.data.length*4 !== png.data.length) {
44+
console.warn(actual.data.length, png.data.length);
45+
throw new Error('Expected actual.data.length to match png.data.length');
46+
}
47+
png.data.forEach((_,i, a) => a[i] = (i%4==3) ? 255 : actual.data[(i/4)|0]);
48+
49+
fs.writeFileSync(path.join(snapshotsPath, imageFilename), PNG.sync.write(png.pack(), {colorType: 0} ));
50+
}
51+
52+
function sampleFunctionToImageData(f, width, height) {
53+
let imageData = {
54+
width,
55+
height,
56+
data: new Uint8ClampedArray(width*height)
57+
};
58+
for(let y = 0; y < height; y++) {
59+
for (let x = 0; x < width; x++) {
60+
imageData.data[y*width+x] = f(x, y);
61+
}
62+
}
63+
return imageData;
64+
}
65+
exports.sampleFunctionToImageData = sampleFunctionToImageData;

test/simplex-noise-test.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/* global describe, it, beforeEach */
2-
3-
var SimplexNoise = require('../simplex-noise');
4-
var Alea = require('alea');
5-
var assert = require('chai').assert;
1+
const SimplexNoise = require('../simplex-noise');
2+
const Alea = require('alea');
3+
const assert = require('chai').assert;
4+
const {assertMatchesImage, sampleFunctionToImageData} = require('./matches-snapshot.js');
65

76
describe('SimplexNoise', function() {
87
function getRandom() {
@@ -25,6 +24,12 @@ describe('SimplexNoise', function() {
2524
assert.equal(aTable[i], i);
2625
}
2726
});
27+
it('matches snapshot', function() {
28+
var table = SimplexNoise._buildPermutationTable(getRandom());
29+
30+
const actual = {width: 16, height: 16, data: new Uint8ClampedArray(table)};
31+
assertMatchesImage(actual, 'permutationTable.png');
32+
});
2833
});
2934

3035
describe('constructor', function() {
@@ -96,6 +101,10 @@ describe('SimplexNoise', function() {
96101
it('should return similar values for similar inputs', function() {
97102
assert(Math.abs(simplex.noise2D(0.1, 0.2) - simplex.noise2D(0.101, 0.202)) < 0.1);
98103
});
104+
it('should match snapshot', function() {
105+
const actual = sampleFunctionToImageData((x,y)=>simplex.noise2D(x/10,y/10)*128+128, 64, 64);
106+
assertMatchesImage(actual, 'noise2D.png');
107+
});
99108
});
100109

101110
describe('noise3D', function() {
@@ -121,6 +130,10 @@ describe('SimplexNoise', function() {
121130
it('should return similar values for similar inputs', function() {
122131
assert(Math.abs(simplex.noise3D(0.1, 0.2, 0.3) - simplex.noise3D(0.101, 0.202, 0.303)) < 0.1);
123132
});
133+
it('should match snapshot', function() {
134+
const actual = sampleFunctionToImageData((x,y)=>simplex.noise3D(x/10,y/10,(x+y)/2)*128+128, 64, 64);
135+
assertMatchesImage(actual, 'noise3D.png');
136+
});
124137
});
125138

126139
describe('noise4D', function() {
@@ -150,6 +163,10 @@ describe('SimplexNoise', function() {
150163
simplex.noise4D(0.101, 0.202, 0.303, 0.404)
151164
) < 0.1);
152165
});
166+
it('should match snapshot', function() {
167+
const actual = sampleFunctionToImageData((x,y)=>simplex.noise4D(x/10,y/10,x/4,y/3)*128+128, 64, 64);
168+
assertMatchesImage(actual, 'noise4D.png');
169+
});
153170
});
154171
});
155172
});

0 commit comments

Comments
 (0)