0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-26 13:49:01 -05:00
liabru-matter-js/test/browser/TestDemo.js

192 lines
5.8 KiB
JavaScript
Raw Normal View History

2015-06-22 05:58:09 -04:00
var page = require('webpage').create();
var fs = require('fs');
var Resurrect = require('../lib/resurrect');
2015-08-03 16:00:54 -04:00
var compare = require('fast-json-patch').compare;
var system = require('system');
var demo,
frames = 10,
testUrl = 'http://localhost:8000/demo/dev.html',
2015-08-04 15:25:20 -04:00
refsPath = 'test/browser/refs',
diffsPath = 'test/browser/diffs';
2015-08-03 16:00:54 -04:00
var update = arg('--update'),
updateAll = typeof arg('--updateAll') !== 'undefined',
diff = arg('--diff');
2015-08-12 18:27:39 -04:00
var resurrect = new Resurrect({ cleanup: true, revive: false }),
2015-08-03 16:00:54 -04:00
created = [],
changed = [];
var test = function(status) {
if (status === 'fail') {
console.log('failed to load', testUrl);
console.log('check dev server is running!');
console.log('use `grunt dev`');
phantom.exit(1);
return;
2015-06-22 05:58:09 -04:00
}
var demos = page.evaluate(function() {
2015-08-03 16:00:54 -04:00
var demoSelect = document.getElementById('demo-select'),
options = Array.prototype.slice.call(demoSelect);
return options.map(function(o) { return o.value; });
2015-06-22 05:58:09 -04:00
});
fs.removeTree(diffsPath);
2015-08-03 16:00:54 -04:00
if (diff) {
fs.makeDirectory(diffsPath);
}
2015-06-22 05:58:09 -04:00
for (var i = 0; i < demos.length; i += 1) {
2015-08-03 16:00:54 -04:00
demo = demos[i];
var hasChanged = false,
hasCreated = false,
forceUpdate = update === demo || updateAll,
worldStartPath = refsPath + '/' + demo + '/' + demo + '-0.json',
worldEndPath = refsPath + '/' + demo + '/' + demo + '-' + frames + '.json',
2015-06-22 05:58:09 -04:00
worldStartDiffPath = diffsPath + '/' + demo + '/' + demo + '-0.json',
worldEndDiffPath = diffsPath + '/' + demo + '/' + demo + '-' + frames + '.json';
var worldStart = page.evaluate(function(demo) {
var engine = Matter.Demo._engine;
2015-08-03 19:37:40 -04:00
if (!(demo in Matter.Demo)) {
throw '\'' + demo + '\' is not defined in Matter.Demo';
}
2015-06-22 05:58:09 -04:00
Matter.Demo[demo]();
return engine.world;
}, demo);
var worldEnd = page.evaluate(function(demo, frames) {
2015-08-12 18:27:39 -04:00
var engine = Matter.Demo._engine,
runner = Matter.Runner.create();
2015-08-03 16:00:54 -04:00
2015-06-22 05:58:09 -04:00
for (var j = 0; j <= frames; j += 1) {
2015-08-12 18:27:39 -04:00
Matter.Runner.tick(runner, engine, j * runner.delta);
2015-06-22 05:58:09 -04:00
}
2015-08-03 16:00:54 -04:00
2015-06-22 05:58:09 -04:00
return engine.world;
}, demo, frames);
worldEnd = JSON.parse(resurrect.stringify(worldEnd, precisionLimiter));
worldStart = JSON.parse(resurrect.stringify(worldStart, precisionLimiter));
2015-06-22 05:58:09 -04:00
if (fs.exists(worldStartPath)) {
var worldStartRef = JSON.parse(fs.read(worldStartPath));
2015-08-03 19:37:40 -04:00
var worldStartDiff = compare(worldStartRef, worldStart);
2015-06-22 05:58:09 -04:00
2015-08-03 16:00:54 -04:00
if (worldStartDiff.length !== 0) {
if (diff) {
fs.write(worldStartDiffPath, JSON.stringify(worldStartDiff, precisionLimiter, 2), 'w');
2015-08-03 16:00:54 -04:00
}
if (forceUpdate) {
hasCreated = true;
fs.write(worldStartPath, JSON.stringify(worldStart, precisionLimiter, 2), 'w');
2015-08-03 16:00:54 -04:00
} else {
hasChanged = true;
}
2015-06-22 05:58:09 -04:00
}
} else {
2015-08-03 16:00:54 -04:00
hasCreated = true;
fs.write(worldStartPath, JSON.stringify(worldStart, precisionLimiter, 2), 'w');
2015-06-22 05:58:09 -04:00
}
if (fs.exists(worldEndPath)) {
var worldEndRef = JSON.parse(fs.read(worldEndPath));
2015-08-03 19:37:40 -04:00
var worldEndDiff = compare(worldEndRef, worldEnd);
2015-06-22 05:58:09 -04:00
2015-08-03 16:00:54 -04:00
if (worldEndDiff.length !== 0) {
if (diff) {
fs.write(worldEndDiffPath, JSON.stringify(worldEndDiff, precisionLimiter, 2), 'w');
2015-08-03 16:00:54 -04:00
}
if (forceUpdate) {
hasCreated = true;
fs.write(worldEndPath, JSON.stringify(worldEnd, precisionLimiter, 2), 'w');
2015-08-03 16:00:54 -04:00
} else {
hasChanged = true;
}
2015-06-22 05:58:09 -04:00
}
} else {
2015-08-03 16:00:54 -04:00
hasCreated = true;
fs.write(worldEndPath, JSON.stringify(worldEnd, precisionLimiter, 2), 'w');
2015-06-22 05:58:09 -04:00
}
2015-08-03 16:00:54 -04:00
if (hasChanged) {
changed.push("'" + demo + "'");
system.stdout.write('x');
} else if (hasCreated) {
created.push("'" + demo + "'");
system.stdout.write('+');
} else {
system.stdout.write('.');
}
}
if (created.length > 0) {
console.log('\nupdated', created.join(', '));
}
var isOk = changed.length === 0 ? 1 : 0;
console.log('');
if (isOk) {
console.log('ok');
} else {
2015-08-03 19:37:40 -04:00
console.log('\nchanges detected on:');
2015-08-03 16:00:54 -04:00
console.log(changed.join(', '));
2015-08-03 19:37:40 -04:00
console.log('\nreview, then --update [name] or --updateAll');
2015-08-03 16:00:54 -04:00
console.log('use --diff for diff log');
}
phantom.exit(!isOk);
};
var precisionLimiter = function(key, value) {
if (typeof value === 'number') {
return parseFloat(value.toFixed(5));
}
return value;
};
2015-08-03 16:00:54 -04:00
function arg(name) {
var index = system.args.indexOf(name);
if (index >= 0) {
return system.args[index + 1] || true;
2015-06-22 05:58:09 -04:00
}
2015-08-03 16:00:54 -04:00
return undefined;
}
page.onError = function(msg, trace) {
setTimeout(function() {
var msgStack = ['testing \'' + demo + '\'', msg];
if (trace && trace.length) {
trace.forEach(function(t) {
msgStack.push(' at ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (fn: ' + t.function +')' : ''));
2015-08-03 16:00:54 -04:00
});
}
console.log(msgStack.join('\n'));
phantom.exit(1);
}, 0);
};
page.onResourceReceived = function(res) {
setTimeout(function() {
if (res.stage === 'end'
&& (res.status !== 304 && res.status !== 200 && res.status !== null)) {
console.log('error', res.status, res.url);
phantom.exit(1);
}
}, 0);
};
2015-06-22 05:58:09 -04:00
2015-08-03 16:00:54 -04:00
phantom.onError = page.onError;
2015-06-22 05:58:09 -04:00
2015-08-03 16:00:54 -04:00
page.open(testUrl, test);