Skip to content

Commit

Permalink
Added a few snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jwagner committed Aug 22, 2021
1 parent c476a24 commit aa0f966
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 10 deletions.
7 changes: 3 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
module.exports = {
'env': {
'browser': true,
'node': true
'node': true,
'mocha': true,
'es2017': true
},
'extends': 'eslint:recommended',
'globals': {
'Uint8Array': false,
'define': false,
'Float32Array': false
},
'plugins': [
'mocha'
],
'rules': {
'indent': [
'error',
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"eslint": "^7.32.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.24.1",
"eslint-plugin-mocha": "^9.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"mocha": "^9.1.0",
Expand Down
Binary file added snapshots/noise2D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added snapshots/noise3D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added snapshots/noise4D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added snapshots/permutationTable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions test/matches-snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const fs = require('fs');
const PNG = require('pngjs').PNG;
const path = require('path');

const snapshotsPath = 'snapshots';

function assertMatchesImage(actual, imageFilename) {
if(!imageFilename.endsWith('.png')) {
console.log('throwing');
throw new Error('imageFilename must end in .png');
}
let fileBuffer;
try {
fileBuffer = fs.readFileSync(path.join(snapshotsPath, imageFilename));
}
catch (_) {
writeImageSnapshot(actual, imageFilename);
return;
}
const png = PNG.sync.read(fileBuffer);
if (actual.data.length * 4 !== png.data.length) {
throw new Error('Expected actual.length to match png.data.length');
}
const identical = actual.data.every((value, i) => value == png.data[i*4]);
if(!identical) {
console.log(png.data);
writeImageSnapshot(actual, imageFilename.replace('.png', '.error.png'));
throw new Error('expected data to be identitcal');
}

}
exports.assertMatchesImage = assertMatchesImage;

function writeImageSnapshot(actual, imageFilename) {
const png = new PNG({
colorType: 0,
inputColorType: 0,
bitDepth: 16,
width: actual.width,
height: actual.height,
inputHasAlpha: false,
});
if (actual.data.length*4 !== png.data.length) {
console.warn(actual.data.length, png.data.length);
throw new Error('Expected actual.data.length to match png.data.length');
}
png.data.forEach((_,i, a) => a[i] = (i%4==3) ? 255 : actual.data[(i/4)|0]);

fs.writeFileSync(path.join(snapshotsPath, imageFilename), PNG.sync.write(png.pack(), {colorType: 0} ));
}

function sampleFunctionToImageData(f, width, height) {
let imageData = {
width,
height,
data: new Uint8ClampedArray(width*height)
};
for(let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
imageData.data[y*width+x] = f(x, y);
}
}
return imageData;
}
exports.sampleFunctionToImageData = sampleFunctionToImageData;
27 changes: 22 additions & 5 deletions test/simplex-noise-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* global describe, it, beforeEach */

var SimplexNoise = require('../simplex-noise');
var Alea = require('alea');
var assert = require('chai').assert;
const SimplexNoise = require('../simplex-noise');
const Alea = require('alea');
const assert = require('chai').assert;
const {assertMatchesImage, sampleFunctionToImageData} = require('./matches-snapshot.js');

describe('SimplexNoise', function() {
function getRandom() {
Expand All @@ -25,6 +24,12 @@ describe('SimplexNoise', function() {
assert.equal(aTable[i], i);
}
});
it('matches snapshot', function() {
var table = SimplexNoise._buildPermutationTable(getRandom());

const actual = {width: 16, height: 16, data: new Uint8ClampedArray(table)};
assertMatchesImage(actual, 'permutationTable.png');
});
});

describe('constructor', function() {
Expand Down Expand Up @@ -96,6 +101,10 @@ describe('SimplexNoise', function() {
it('should return similar values for similar inputs', function() {
assert(Math.abs(simplex.noise2D(0.1, 0.2) - simplex.noise2D(0.101, 0.202)) < 0.1);
});
it('should match snapshot', function() {
const actual = sampleFunctionToImageData((x,y)=>simplex.noise2D(x/10,y/10)*128+128, 64, 64);
assertMatchesImage(actual, 'noise2D.png');
});
});

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

describe('noise4D', function() {
Expand Down Expand Up @@ -150,6 +163,10 @@ describe('SimplexNoise', function() {
simplex.noise4D(0.101, 0.202, 0.303, 0.404)
) < 0.1);
});
it('should match snapshot', function() {
const actual = sampleFunctionToImageData((x,y)=>simplex.noise4D(x/10,y/10,x/4,y/3)*128+128, 64, 64);
assertMatchesImage(actual, 'noise4D.png');
});
});
});
});

0 comments on commit aa0f966

Please sign in to comment.