Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
199 lines (163 sloc) 5.65 KB
<!DOCTYPE html>
<html>
<head>
<title>expression tester</title>
<script>
window.gaffa = {
model : {
array : [1, 2, 3],
prop : 15,
prop2 : 10,
empty: {},
get: function (path) {
// slice of []
path = path.slice(1,-1);
var index = path.indexOf("model/");
if (index >= 0) {
path = path.substring(index + 6);
}
return this[path];
}
}
};
</script>
<script src="gel.js" type="text/javascript"></script>
<script src="gelFunctions.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
String.prototype.format = String.prototype.f = function () {
var s = this,
i = arguments.length;
while (i--) {
var v;
try {
v = arguments[i].toString();
}
catch (ex) {
v = arguments[i];
}
s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), v);
}
return s;
};
function arrayEqual(arrayX, arrayY) {
if (!arrayX || !arrayY) {
return false;
}
if (arrayX.length != arrayY.length) return false;
for (var i = 0; i < arrayX.length; i++) {
if (arrayX[i] != arrayY[i]) {
return false;
}
}
return true;
}
var context = {
contextProp:10,
customFunc: function(item){
return item < 10;
},
console:{
log: function(){console.log(arguments)}
},
anArray: ["a","b","c"],
anOtherArray: [1,2,3]
}
$(document).ready(function () {
$.getJSON("expressionTests.json", function(tests) {
//console.log(tests);
var numPassed = 0,
numFailed = 0;
for (var index = 0; index < tests.length; index++) {
var test = tests[index][0],
expected = tests[index][1],
a,
e;
var actual = "[[ERROR]]";
var exceptionMsg = "";
try {
actual = gel.parse(test, context);
}
catch (ex) {
exceptionMsg = ex;
console.warn("Scan function failed!" + ex , ex);
}
finally { }
// special case for printed functions
if (typeof actual === "function"){
a = actual.toString().replace(/\s+/g, '');
e = expected.replace(/\s+/g, '');
passFail = a === e;
} else if(typeof actual === "object"){
actual = a = JSON.stringify(actual);
expected = e = JSON.stringify(expected);
passFail = a === e;
} else {
passFail = actual === expected;
}
passFail ? numPassed++ : numFailed++;
var trString =
'<tr class="{0}"><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>'
.format((passFail ? "pass" : "fail"), index, test, actual, expected, exceptionMsg);
$("#initialtests tbody").append(trString);
}
if(numFailed){
$("#initialtests").before('<h1 class="fail">FAIL</h1>');
}else{
$("#initialtests").before('<h1 class="pass">SUCCESS</h1>');
}
$("#initialtests").before('<h2>' + numPassed + ' Passed, ' + numFailed + ' Failed</h2>');
}).error(function() {console.log(arguments)});
});
$(document).on('click', 'button.run',function(){
var expression = $('.expressions').val(),
resultBox = $('.results').removeClass('fail').text(''),
result;
try{
result = gel.parse(expression, context);
}catch(error){
resultBox.text(error).addClass('fail');
}
if(typeof result === 'function'){
resultBox.text(result.toString());
}else{
resultBox.text(JSON.stringify(result));
}
});
</script>
<style type="text/css">
textarea {
width: 90%;
height:250px;
}
td, th {
padding: 3px;
}
.pass {
background-color:#b6ff00;
}
.fail {
background-color: #ff6b6b;
}
.results{
font-size:2em;
}
</style>
</head>
<body>
<h1>Expression tester</h1>
<textarea class="expressions"></textarea>
<br />
<button class="run">Run</button> <br />
<pre class="results"></pre>
<table id="initialtests">
<thead>
<tr>
<th>Id</th><th>Input</th><th>Actual</th><th>Expected</th><th>Exception</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>