Created
January 6, 2012 18:51
-
-
Save WebReflection/1571887 to your computer and use it in GitHub Desktop.
100% Test Coverage for WeakMap.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>WeakMap</title> | |
<!-- https://github.com/WebReflection/wru --> | |
<script src="WeakMap.js">/* | |
https://gist.github.com/1571878 | |
*/</script> | |
<script>function wru(wru){var assert=wru.assert,async=wru.async; | |
// enojy your tests! | |
wru.test([{ | |
name: "existence", | |
test: function () { | |
assert(window.WeakMap); | |
} | |
}, { | |
name: "native constructor behavior", | |
test: function () { | |
assert(new WeakMap instanceof WeakMap); | |
assert(WeakMap() instanceof WeakMap); | |
if ("__proto__" in {}) { | |
assert(WeakMap().__proto__.isPrototypeOf(WeakMap())); | |
assert(WeakMap().__proto__ === WeakMap.prototype); | |
} | |
} | |
},{ | |
name: "WeakMap#has", | |
test: function () { | |
var | |
o = WeakMap(), | |
generic = {}, | |
callback = function () {} | |
; | |
assert("not there yet", false === o.has(callback)); | |
o.set(callback, generic); | |
assert("now it has it", true === o.has(callback)); | |
} | |
},{ | |
name: "WeakMap#get", | |
test: function () { | |
var | |
o = WeakMap(), | |
generic = {}, | |
callback = function () {} | |
; | |
assert("default", o.get(callback, 123) === 123); | |
o.set(callback, generic); | |
assert("generic with default", o.get(callback, 123) === generic); | |
assert("generic without default", o.get(callback) === generic); | |
} | |
},{ | |
name: "WeakMap#set", | |
test: function () { | |
var | |
o = WeakMap(), | |
generic = {}, | |
callback = function () {} | |
; | |
o.set(callback, generic); | |
assert("generic", o.get(callback) === generic); | |
o.set(callback, callback); | |
assert("callback", o.get(callback) === callback); | |
o.set(callback, o); | |
assert("o", o.get(callback) === o); | |
o.set(o, callback); | |
assert("callback", o.get(o) === callback); | |
} | |
},{ | |
name: "WeakMap#['delete']", | |
test: function () { | |
var | |
o = WeakMap(), | |
generic = {}, | |
callback = function () {} | |
; | |
o.set(callback, generic); | |
o.set(generic, callback); | |
o.set(o, callback); | |
assert("has all of them", o.has(callback) && o.has(generic) && o.has(o)); | |
o["delete"](callback); | |
o["delete"](generic); | |
o["delete"](o); | |
assert("has none of them", !o.has(callback) && !o.has(generic) && !o.has(o)); | |
assert("if not found, returns false", o["delete"](o) === false); | |
o.set(o, callback); | |
assert("if found and removed returns true", o["delete"](o)); | |
} | |
},{ | |
name: "non object key throws an error", | |
test: function () { | |
var o = WeakMap(); | |
try { | |
o.set("key", o); | |
assert(false); | |
} catch(ok) { | |
assert(true); | |
} | |
} | |
} | |
]); | |
}</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(X){function j(){z=J.call(m);if(z){(O=l(l(Y.node,"div"),"span"))[D]=((af(z,R)&&z[R])||(af(z,e)&&z[e])||P)+i+i;a=[];t=[];S=[];aa={};b("setup");S[ag]||b("test");M||q()}else{s()}}function o(ai){try{return N.call(h,ai)}catch(ah){return h.createElement(ai)}}function l(ah,ai){return ah.appendChild(o(ai))}function g(ah){O[D]=w.call(O[D],0,-2)+i+ah}function s(){var ai=Y.node.insertBefore(o("div"),Y.node.firstChild),aj,ah;if(ac){ah="error";aj="There Are Errors: "+ac}else{if(B){ah="fail";aj=B+" Tests Failed"}else{ah="pass";aj="Passed "+r+" Tests"}}ai[D]="<strong>"+aj+"</strong>";ai.className=ah}function F(){var ah=this.lastChild.style;ah.display=ah.display=="none"?"block":"none"}function c(ah){O[D]+="<ul>"+C+u.call(ah,d+C)+d+"</ul>";(O.onclick=F).call(O)}function q(){f();r+=a[ag];B+=t[ag];ac+=S[ag];g("("+u.call([a[ag],L=t[ag],S[ag]],", ")+")");O=O.parentNode;S[ag]?c(S,V="error"):(L?c(t,V="fail"):V="pass");O.className=V;L=0;V=i;j()}function b(ah){if(af(z,ah)){try{z[ah](aa)}catch(ai){Z.call(S,i+ai)}}}function af(ai,ah){return p.call(ai,ah)}function v(){return E()<0.5?-1:1}function f(){if(Q){G(Q);Q=0}b("teardown")}var Y={assert:function T(ai,ah){if(arguments[ag]==1){ah=ai;ai=P}y=H;Z.call(ah?a:t,V+ai);return ah},async:function U(ai,al,aj,ak){ak=++M;if(typeof ai=="function"){aj=al;al=ai;ai="asynchronous test #"+ak}aj=W(function(){ak=0;Z.call(t,ai);--M||(Q=W(q,0))},K(aj||x)||x);return function ah(){if(!ak){return}y=ad;V=ai+": ";try{al.apply(this,arguments)}catch(am){y=H;Z.call(S,V+am)}V=i;if(y){G(aj);--M||(Q=W(q,0))}}},test:function n(ah){m=I.apply(m,[ah]);Y.random&&ae.call(m,v);M||j()}},H=true,ad=!H,x=100,i=" ",P="unknown",ag="length",R="name",e="description",C="<li>",d="</li>",k="\\|/-",p=Y.hasOwnProperty,V=i,ab=V.charAt,w=V.slice,m=[],I=m.concat,u=m.join,Z=m.push,J=m.shift,ae=m.sort,M=0,L=0,r=0,B=0,ac=0,Q=0,D="innerHTML",h=X.document,N=h.createElement,A,K,E,W,G,z,O,a,t,S,aa,y;A=X.Math;K=A.abs;E=A.random;W=X.setTimeout;G=X.clearTimeout;Y.node=(h.getElementById("wru")||h.body||h.documentElement);X.setInterval(function(){M&&g(ab.call(k,L++%4))},x);x*=x;Y.random=ad;return Y}(this)); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment