Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
165 lines (148 sloc) 6.43 KB
<!doctype html>
<html>
<head>
<title>wru</title>
<script>function wru(wru){ // enojy your tests!
wru.test([
// how to put a separator before each group of tests
{
name: "<h3 class='group'>tests group name</h3>",
teardown: cleanGroupName
}, {
name: "test 1",
test: wru.assert // function as place holder, will assert the tmp object
}, {
name: "test 2",
test: wru.assert // function as place holder, will assert the tmp object
}, {
name: "<h3 class='group'>assertions tricks</h3>",
teardown: cleanGroupName
}, {
name: "be sure about number of assertions",
setup: function (tmp) {
tmp.assert = wru.assert;
wru.assertions = 0;
wru.assert = function () {
++wru.assertions;
tmp.assert.apply(wru, arguments);
};
},
test: function () {
wru.assert("condition 1");
wru.assert("condition 1");
wru.assert("condition 1");
},
teardown: function (tmp) {
wru.assert = tmp.assert;
wru.assert(
3 === wru.assertions
);
delete wru.assertions;
}
}, {
name: "<h3 class='group'>asynchronous tricks</h3>",
teardown: cleanGroupName
}, {
name: "the double asynchronous possibility",
test: function () {
// lets say it is not about the server
// it's about the image object
var img = new Image;
// how to set two asnchronous calls
// knowing that only one of them will be fired?
var onevent = wru.async("onevent", function (description, assertion) {
wru.assert(description, assertion);
});
// now there is an async call, wru will wait for it
// to be sure we'll trigger it ...
img.onload = function () {
// invoke the asynchronous call
onevent("image loaded", true);
};
img.onerror = function () {
onevent(
"server side problems but image worked",
img instanceof Image
);
};
// now we can set the src
img.src = "invalid.invalid";
}
}
]);
// example: used to clean rows used as tests group name
function cleanGroupName() {
setTimeout(function () {
for (var
h3 = document.getElementsByTagName("h3"),
i = h3.length,
parentNode, current; i--;
) {
// grabe the h3
current = h3[i];
// only those we want to
if (current.className == "group") {
// change only new one
current.className += " ok";
// change some style if necesary
current.style.margin = 0;
current.style.padding = "4px";
current.style.color = "black";
current.style.background = "white";
// grab the parent of the outer span
parentNode = current.parentNode.parentNode;
// clean it up
parentNode.innerHTML = "";
// put current back
parentNode.appendChild(current);
}
}
}, 0);
}
}</script>
<!-- add other scripts here if necessary-->
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
<style type="text/css">
#wru {
font-family: sans-serif;
font-size: 11pt;
border: 1px solid #333;
}
#wru div {
cursor: default;
padding: 0;
color: #000;
}
#wru div span,
#wru div strong {
display: block;
padding: 4px;
margin: 0;
}
#wru div ul {
margin: 0;
padding-bottom: 4px;
}
#wru div.pass {
background: #90EE90;
}
#wru div.fail {
background: #FF6347;
}
#wru div.error {
background: #000;
color: #FFF;
}
</style>
</head>
<body>
<div id="wru"></div>
<script>
/*!
(C) Andrea Giammarchi, @WebReflection - Mit Style License
*/
wru(function(V){function i(){y=I.call(l);if(y){(N=k(k(W.node,"div"),"span"))[C]=((ad(y,P)&&y[P])||(ad(y,e)&&y[e])||O)+h+h;a=[];s=[];Q=[];Y={};b("setup");Q[ae]||b("test");b("teardown");L||p()}else{r()}}function n(ag){try{return M.call(g,ag)}catch(af){return g.createElement(ag)}}function k(af,ag){return af.appendChild(n(ag))}function f(af){N[C]=v.call(N[C],0,-2)+h+af}function r(){var ag=W.node.insertBefore(n("div"),W.node.firstChild),ah,af;if(aa){af="error";ah="There Are Errors: "+aa}else{if(A){af="fail";ah=A+" Tests Failed"}else{af="pass";ah="Passed "+q+" Tests"}}ag[C]="<strong>"+ah+"</strong>";ag.className=af}function E(){var af=this.lastChild.style;af.display=af.display=="none"?"block":"none"}function c(af){N[C]+="<ul>"+B+t.call(af,d+B)+d+"</ul>";(N.onclick=E).call(N)}function p(){q+=a[ae];A+=s[ae];aa+=Q[ae];f("("+t.call([a[ae],K=s[ae],Q[ae]],", ")+")");N=N.parentNode;Q[ae]?c(Q,T="error"):(K?c(s,T="fail"):T="pass");N.className=T;K=0;T=h;i()}function b(af){if(ad(y,af)){try{y[af](Y)}catch(ag){X.call(Q,h+ag)}}}function ad(ag,af){return o.call(ag,af)}function u(){return D()<0.5?-1:1}var W={assert:function R(ag,af){if(arguments[ae]==1){af=ag;ag=O}x=G;X.call(af?a:s,T+ag);return af},async:function S(ag,aj,ah,ai){ai=++L;if(typeof ag=="function"){ah=aj;aj=ag;ag="asynchronous test #"+ai}ah=U(function(){ai=0;X.call(s,ag);--L||p()},J(ah||w)||w);return function af(){if(!ai){return}x=ab;T=ag+": ";try{aj.apply(this,arguments)}catch(ak){x=G;X.call(Q,T+ak)}T=h;if(x){F(ah);--L||p()}}},test:function m(af){l=H.apply(l,[af]);W.random&&ac.call(l,u);L||i()}},G=true,ab=!G,w=100,h=" ",O="unknown",ae="length",P="name",e="description",B="<li>",d="</li>",j="\\|/-",o=W.hasOwnProperty,T=h,Z=T.charAt,v=T.slice,l=[],H=l.concat,t=l.join,X=l.push,I=l.shift,ac=l.sort,L=0,K=0,q=0,A=0,aa=0,C="innerHTML",g=V.document,M=g.createElement,z,J,D,U,F,y,N,a,s,Q,Y,x;z=V.Math;J=z.abs;D=z.random;U=V.setTimeout;F=V.clearTimeout;W.node=(g.getElementById("wru")||g.body||g.documentElement);V.setInterval(function(){L&&f(Z.call(j,K++%4))},w);w*=w;W.random=ab;return W}(this));
</script>
</body>
</html>