Merge branch 'newtests' into beta

This commit is contained in:
ansuz 2016-04-14 12:33:12 +02:00
commit 3ade46adc7
6 changed files with 128 additions and 8 deletions

View file

@ -4,6 +4,24 @@
<meta content="text/html; charset=utf-8" http-equiv="content-type"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<script data-main="main" src="/bower_components/requirejs/require.js"></script>
<style>
.report {
font-size: 40px;
}
.success {
border: 3px solid green;
}
.failure {
border: 3px solid red;
}
.error {
border: 1px solid red;
}
</style>
</head>
<body>

View file

@ -10,13 +10,30 @@ define([
window.TextPatcher = TextPatcher;
var assertions = 0;
var failed = false;
var failedOn;
var failMessages = [];
var ASSERTS = [];
var runASSERTS = function () {
ASSERTS.forEach(function (f, index) {
f(index);
});
};
var assert = function (test, msg) {
if (test()) {
assertions++;
} else {
throw new Error(msg || '');
}
ASSERTS.push(function (i) {
if (test()) {
assertions++;
} else {
failed = true;
failedOn = assertions;
failMessages.push({
test: i,
message: msg
});
}
});
};
var $body = $('body');
@ -43,8 +60,93 @@ define([
}, "Round trip serialization introduced artifacts.");
};
roundTrip($('#target')[0]);
roundTrip($('#widget')[0]);
[ '#target',
'#widget',
].forEach(function (sel) {
roundTrip($(sel)[0]);
});
var strungJSON = function (orig) {
var result;
assert(function () {
result = JSON.stringify(JSON.parse(orig));
return result === orig;
}, "expected result (" + result + ") to equal original (" + orig + ")");
};
[ '{"border":"1","style":{"width":"500px"}}',
'{"style":{"width":"500px"},"border":"1"}',
].forEach(function (orig) {
strungJSON(orig);
});
//assert(function () { }, "this is expected to fail");
/* TODO Test how browsers handle weird elements
like "_moz-resizing":"true"
and anything else you can think of.
Start with Hyperjson, turn it into a DOM and come back
*/
var swap = function (str, dict) {
return str.replace(/\{\{(.*?)\}\}/g, function (all, key) {
return typeof dict[key] !== 'undefined'? dict[key] : all;
});
};
var multiline = function (f) {
var str;
f.toString().replace(/\/\*([\s\S]*)\*\//g, function (all, out) {
str = out;
});
return str || '';
};
var formatFailures = function () {
var template = multiline(function () { /*
<pre class="error">
Failed on test number {{test}} with error:
> "{{message}}"
</pre>
<br>
*/});
return failMessages.map(function (obj) {
console.log(obj);
return swap(template, obj);
}).join("\n");
};
runASSERTS();
$("body").html(function (i, val) {
var dict = {
previous: val,
totalAssertions: ASSERTS.length,
passedAssertions: assertions,
plural: (assertions === 1? '' : 's'),
failMessages: formatFailures()
};
var SUCCESS = swap(multiline(function(){/*
<div class="report">{{passedAssertions}} / {{totalAssertions}} test{{plural}} passed.
{{failMessages}}
</div>
{{previous}}
*/}), dict);
var report = SUCCESS;
return report;
});
var $report = $('.report');
$report.addClass(failed?'failure':'success');
console.log("%s test%s passed", assertions, assertions === 1? '':'s');
});