Create a gist now

Instantly share code, notes, and snippets.

100% code coverage for Object.prototype.define test
<!doctype html>
<html>
<head>
<title>wru</title>
<!-- https://github.com/WebReflection/wru -->
<script src="define.js"></script>
<script>function wru(wru){var assert=wru.assert,async=wru.async;
// enojy your tests!
wru.test([
{
name: "chainability",
test: function () {
var o = {};
assert("the object itself is returned", o.define("property", "value") === o);
}
}
,
{
name: "define is not enumerable",
test: function () {
for (var key in {}) {
assert("define is not exposed", key != "define");
}
assert("define is present tho", "define" in {});
}
}
,
{
name: "property by value",
test: function () {
function method() {}
var o = {};
assert("the object has a property with specified value", {}.define("property", null).property === null);
assert("the object has a property with specified value", {}.define("property", 0).property === 0);
assert("the object has a property with specified value", {}.define("property", false).property === false);
assert("the object has a property with specified value", {}.define("property", "value").property === "value");
assert("the object has a property with specified value", {}.define("property", {value:o}).property === o);
assert("the object has a property with specified value", {}.define("property", method).property === method);
}
}
,
{
name: "by default properties are writable",
test: function () {
var o = {};
o.define("test", "value");
assert(o.test === "value");
o.test = 123;
assert(o.test === 123);
assert(Object.getOwnPropertyDescriptor(o, "test").writable);
}
}
,
{
name: "by default methods are not writable",
test: function () {
function method() {}
var o = {};
o.define("test", method);
assert(o.test === method);
o.test = 123;
assert(o.test === method);
assert(!Object.getOwnPropertyDescriptor(o, "test").writable);
}
}
,
{
name: "by default properties are not configurable",
test: function () {
var o = {};
o.define("test", 123);
assert(Object.getOwnPropertyDescriptor(o, "test").configurable === false);
}
}
,
{
name: "enumerable and not enumerable properties",
test: function () {
var o = {};
o.define(["test", "_test", "method", "_method"], [123, 456, function(){}, function(){}]);
assert(Object.getOwnPropertyDescriptor(o, "test").enumerable);
assert(!Object.getOwnPropertyDescriptor(o, "_test").enumerable);
assert(Object.getOwnPropertyDescriptor(o, "method").enumerable);
assert(!Object.getOwnPropertyDescriptor(o, "_method").enumerable);
}
}
,
{
name: "if descriptor is an object only properties that are not present are set",
test: function () {
var descriptor = Object.getOwnPropertyDescriptor({}.define("a",
// the opposite of what it should be
{value:"a", writable:false, configurable: true, enumerable: false}), "a"
);
assert(!descriptor.writable);
assert(descriptor.configurable);
assert(!descriptor.enumerable);
descriptor = Object.getOwnPropertyDescriptor({}.define("_a",
// the opposite of what it should be
{value:"_a", writable:false, configurable: true, enumerable: true}), "_a"
);
assert(!descriptor.writable);
assert(descriptor.configurable);
assert(descriptor.enumerable);
descriptor = Object.getOwnPropertyDescriptor({}.define("m",
// the opposite of what it should be
{value:function () {}, writable:true, configurable: true, enumerable: false}), "m"
);
assert(descriptor.writable);
assert(descriptor.configurable);
assert(!descriptor.enumerable);
descriptor = Object.getOwnPropertyDescriptor({}.define("_m",
// the opposite of what it should be
{value:function () {}, writable:true, configurable: true, enumerable: true}), "_m"
);
assert(descriptor.writable);
assert(descriptor.configurable);
assert(descriptor.enumerable);
}
}
,
{
name: "if descriptor is not an object defaults are set",
test: function () {
var descriptor = Object.getOwnPropertyDescriptor({}.define("a"), "a");
assert(descriptor.writable);
assert(!descriptor.configurable);
assert(descriptor.enumerable);
descriptor = Object.getOwnPropertyDescriptor({}.define("_b"), "_b");
assert(descriptor.writable);
assert(!descriptor.configurable);
assert(!descriptor.enumerable);
descriptor = Object.getOwnPropertyDescriptor({}.define("m", function(){}), "m");
assert(!descriptor.writable);
assert(!descriptor.configurable);
assert(descriptor.enumerable);
descriptor = Object.getOwnPropertyDescriptor({}.define("_m", function(){}), "_m");
assert(!descriptor.writable);
assert(!descriptor.configurable);
assert(!descriptor.enumerable);
}
}
,
{
name: "multiple properties with same defaults",
test: function () {
var o = {}.define(["a", "b", "_c", "d"], "");
var descriptor = Object.getOwnPropertyDescriptor(o, "a");
assert(descriptor.writable);
assert(!descriptor.configurable);
assert(descriptor.enumerable);
descriptor = Object.getOwnPropertyDescriptor(o, "b");
assert(descriptor.writable);
assert(!descriptor.configurable);
assert(descriptor.enumerable);
descriptor = Object.getOwnPropertyDescriptor(o, "_c");
assert(descriptor.writable);
assert(!descriptor.configurable);
assert(!descriptor.enumerable);
descriptor = Object.getOwnPropertyDescriptor(o, "d");
assert(descriptor.writable);
assert(!descriptor.configurable);
assert(descriptor.enumerable);
}
}
]);
}</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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment