UNPKG

wishlist

Version:

Super natural testing framework.

1 lines 18.7 kB
(function(){var wishlist,indexOf=[].indexOf||function(e){for(var t=0,n=this.length;t<n;t++){if(t in this&&this[t]===e)return t}return-1};wishlist={};wishlist.environmentType=typeof exports!=="undefined"&&exports!==null&&(typeof module!=="undefined"&&module!==null?module.exports:void 0)!=null&&(typeof process!=="undefined"&&process!==null?process.execPath:void 0)!=null&&typeof process.execPath==="string"&&process.execPath.search(/node/i)!==-1?"node":typeof window!=="undefined"&&window!==null&&(typeof navigator!=="undefined"&&navigator!==null)&&(typeof HTMLElement!=="undefined"&&HTMLElement!==null)?"browser":void 0;wishlist.moduleSystem=typeof exports!=="undefined"&&exports!==null&&(typeof module!=="undefined"&&module!==null?module.exports:void 0)!=null?"commonjs":null;wishlist.objectIs=function(e,t){if(typeof e==="number"&&typeof t==="number"){if(e===0&&t===0){return 1/e===1/t}else if(isNaN(e)&&isNaN(t)){return true}else{return e===t}}else{return e===t}};wishlist.objectClone=function(e){var t,n,s,r,i;i={};r=Object.keys(e);for(s=0,n=r.length;s<n;s++){t=r[s];i[t]=e[t]}return i};wishlist.hasSameKeys=function(e,t){var n,s;n=Object.keys(e);s=Object.keys(t);if(n.length!==s.length){return false}else{n.sort();s.sort();return n.every(function(e,t){return s[t]===e})}};wishlist.valueToMessage=function(e){var t,n;t=function(e,n){if(e===void 0){return"undefined"}else if(e===null){return"null"}else if(Array.isArray(e)){if(n>0){return"["+e.map(function(e){return t(e,n-1)}).join(",")+"]"}else{return"[Array]"}}else if(typeof e==="function"){return"[Function]"}else if(typeof e==="object"){if(n>0){return"{"+Object.keys(e).map(function(s){return JSON.stringify(s)+":"+t(e[s],n-1)}).join(",")+"}"}else{return"[Object]"}}else if(typeof e==="string"){return JSON.stringify(e.toString())}else if(typeof e==="number"){if(wishlist.objectIs(e,-0)){return"-0"}else{return e.toString()}}else{return e.toString()}};n=t(e,3);if(n.length>1e3){n=t(e,2)}if(n.length>1e3){n=t(e,1)}if(n.length>1e3){n=t(e,0)}return n};wishlist.currentRootTest=null;wishlist.parseExpression=function(e,t){var n,s,r,i,l,u,o,a,f,c,h,p,y,g;e+=" ";if(t.length===0){return[]}h=new RegExp("^("+t.join("|")+")[^a-zA-Z0-9_$]","g");f=[];c=null;y=true;g=false;s=false;i=false;r=0;while(r<e.length-1){n=e[r];o=y;if(c===null){if("a"<=n&&n<="z"||"A"<=n&&n<="Z"||"0"<=n&&n<="9"||n==="_"||n==="$"||n===")"||n==="]"){y=false}else if(n===" "||n===" "||n==="\n"||n==="\r"){}else{y=true}}a=g;if(c===null){if("a"<=n&&n<="z"||"A"<=n&&n<="Z"||"0"<=n&&n<="9"||n==="_"||n==="$"||n==="."){g=true}else{g=false}}l=s;if(c===null){if(n==="."){s=true}else if(n===" "||n===" "||n==="\n"||n==="\r"){}else{s=false}}u=i;if(c===null){if(n==="{"||n===","){i=true}else if(n===" "||n===" "||n==="\n"||n==="\r"){}else{i=false}}if(n==='"'&&c===null){c="double";r++}else if(n==="'"&&c===null){c="single";r++}else if(n==="/"&&c===null&&o){c="slash";r++}else if(n==='"'&&c==="double"||n==="'"&&c==="single"||n==="/"&&c==="slash"){c=null;r++}else if(n==="\\"&&c!==null){r+=2}else if(c===null&&!a&&!l&&("a"<=n&&n<="z"||"A"<=n&&n<="Z")){p=e.substr(r,31);if(!(u&&p.search(/^([a-zA-Z0-9_$])+\s*:/)!==-1)&&p.search(h)!==-1){f.push(r)}r++}else{r++}}return f};wishlist.parseWish=function(e){var t,n;n=null;t=null;[0,1].forEach(function(s){var r,i,l,u,o,a,f,c,h,p,y,g;p=null;h=0;i=0;r=0;g=true;u=false;o=0;while(o<e.length){l=e[o];c=g;if(p===null){if("a"<=l&&l<="z"||"A"<=l&&l<="Z"||"0"<=l&&l<="9"||l==="_"||l==="$"||l===")"||l==="]"){g=false}else if(l===" "||l===" "||l==="\n"||l==="\r"){}else{g=true}}f=u;if(p===null){if(l==="."){u=true}else if(l===" "||l===" "||l==="\n"||l==="\r"){}else{u=false}}if(l==='"'&&p===null){p="double";o++}else if(l==="'"&&p===null){p="single";o++}else if(l==="/"&&p===null&&c){p="slash";o++}else if(l==='"'&&p==="double"||l==="'"&&p==="single"||l==="/"&&p==="slash"){p=null;o++}else if(l==="\\"&&p!==null){o+=2}else if(l==="("){h++;o++}else if(l==="["){i++;o++}else if(l==="{"){r++;o++}else if(l===")"){h--;o++}else if(l==="]"){i--;o++}else if(l==="}"){r--;o++}else if(p===null&&!f&&(h===i&&i===r&&r===0)){if(s===0){if(l===":"){t=e.substr(o+1);e=e.substr(0,o);break}}else if(s===1){y=e.substr(o);if((a=y.match(/^=([^]+)$/))!=null){n={type:"equal",components:[e.substr(0,o),a[1]]};break}else if((a=y.match(/^<>([^]+)$/))!=null){n={type:"notEqual",components:[e.substr(0,o),a[1]]};break}else if((a=y.match(/^\sis\s([^]+)$/))!=null){n={type:"is",components:[e.substr(0,o),a[1]]};break}else if((a=y.match(/^\sisnt\s([^]+)$/))!=null){n={type:"isnt",components:[e.substr(0,o),a[1]]};break}else if((a=y.match(/^\sthrows(?:\s([^]+))?$/))!=null){n={type:"throws",components:[e.substr(0,o),a[1]!=null?a[1]:"undefined"]};break}else if((a=y.match(/^<=([^]+)$/))!=null){n={type:"lessThanOrEqual",components:[e.substr(0,o),a[1]]};break}else if((a=y.match(/^>=([^]+)$/))!=null){n={type:"greaterThanOrEqual",components:[e.substr(0,o),a[1]]};break}else if((a=y.match(/^<([^]+)$/))!=null){n={type:"lessThan",components:[e.substr(0,o),a[1]]};break}else if((a=y.match(/^>([^]+)$/))!=null){n={type:"greaterThan",components:[e.substr(0,o),a[1]]};break}}o++}else{o++}}if(s===1){return n!=null?n:n={type:"doesNotThrow",components:[e]}}});n.components.push(JSON.stringify((t!=null?t:e).trim()));return n};wishlist.parseWishes=function(e){var t,n,s,r,i,l,u,o,a,f,c,h;a=null;u=0;n=0;t=0;h=true;o=[];r=0;while(r<e.length){s=e[r];l=h;if(a===null){if("a"<=s&&s<="z"||"A"<=s&&s<="Z"||"0"<=s&&s<="9"||s==="_"||s==="$"||s===")"||s==="]"){h=false}else if(s===" "||s===" "||s==="\n"||s==="\r"){}else{h=true}}if(s==='"'&&a===null){a="double";r++}else if(s==="'"&&a===null){a="single";r++}else if(s==="/"&&a===null&&l){a="slash";r++}else if(s==='"'&&a==="double"||s==="'"&&a==="single"||s==="/"&&a==="slash"){a=null;r++}else if(s==="\\"&&a!==null){r+=2}else if(s==="("){u++;r++}else if(s==="["){n++;r++}else if(s==="{"){t++;r++}else if(s===")"){u--;r++}else if(s==="]"){n--;r++}else if(s==="}"){t--;r++}else if(a===null&&(u===n&&n===t&&t===0)&&s===";"){o.push(r);r++}else{r++}}f=[];i=-1;o.forEach(function(t){var n;n=e.substring(i+1,t).trim();if(n!==""){f.push(n)}return i=t});c=e.substr(i+1).trim();if(c!==""){f.push(c)}return f};wishlist.sha256=function(e){var t,n,s,r,i,l,u,o,a,f,c,h,p,y,g,m,d,w,v,T,b,x,A,j,k,_,E,M,O,S,C,$,I,W,N,q,R,z,F,Z,K,J;if(e.length>Math.round(Math.pow(2,31)-1)){throw new Error}J=function(e){var t;return function(){var n,s;s=[];for(t=n=7;n>=0;t=--n){s.push(((e>>>t*4)%16).toString(16))}return s}().join("")};g=function(){var e,t,n,s;s=0;for(n=0,t=arguments.length;n<t;n++){e=arguments[n];s=(s+e)%4294967296}return s};u=function(e,t){return e>>>t|e<<32-t};o=function(e,t){return e>>>t};t=function(e,t,n){return e&t^~e&n};i=function(e,t,n){return e&t^e&n^t&n};a=function(e){return u(e,2)^u(e,13)^u(e,22)};f=function(e){return u(e,6)^u(e,11)^u(e,25)};q=function(e){return u(e,7)^u(e,18)^o(e,3)};R=function(e){return u(e,17)^u(e,19)^o(e,10)};s=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298];n=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225];d=e.split("").map(function(e){return e.charCodeAt(0)});E=e.length*8;_=448-E-1;while(_<0){_+=512}C=E+1+_+64;d.push(128);for(j=M=0,I=Math.round((_-7)/8);0<=I?M<I:M>I;j=0<=I?++M:--M){d.push(0)}d.push(0);d.push(0);d.push(0);d.push(0);d.push(E>>>24);d.push((E>>>16)%256);d.push((E>>>8)%256);d.push(E%256);l=Math.round(C/512);r=new Array(l);for(j=S=0,W=l;0<=W?S<W:S>W;j=0<=W?++S:--S){r[j]=new Array(16);for(k=$=0;$<16;k=++$){O=j*64+k*4;r[j][k]=d[O]<<24|d[O+1]<<16|d[O+2]<<8|d[O+3]}}p=new Array(64);for(j=F=0,N=l;0<=N?F<N:F>N;j=0<=N?++F:--F){for(z=Z=0;Z<64;z=++Z){p[z]=z<16?r[j][z]:g(R(p[z-2]),p[z-7],q(p[z-15]),p[z-16])}y=n[0];m=n[1];w=n[2];v=n[3];T=n[4];b=n[5];x=n[6];A=n[7];for(z=K=0;K<64;z=++K){c=g(A,f(T),t(T,b,x),s[z],p[z]);h=g(a(y),i(y,m,w));A=x;x=b;b=T;T=g(v,c);v=w;w=m;m=y;y=g(c,h)}n[0]=g(y,n[0]);n[1]=g(m,n[1]);n[2]=g(w,n[2]);n[3]=g(v,n[3]);n[4]=g(T,n[4]);n[5]=g(b,n[5]);n[6]=g(x,n[6]);n[7]=g(A,n[7])}return n.map(function(e){return J(e)}).join("")};wishlist.Test=function(){function Test(e){this.name=e!=null?e:"";this._children=[];this.fun=function(e){return function(){}}(this);this.afterFun=function(e){return function(){}}(this);this.wishes=[];this.async=false;this.parent=null;this.allCount=1;this._resetContext()}Test.prototype._resetContext=function(){this.env={};this.wishResults=[];this.result=null;return this.endedCount=0};Test.prototype.set=function(){var e,t,n,s,r,i;t=e=i=r=s=void 0;n=function(e){return function(e){var t;t=Array.isArray(e)?e.join(";"):typeof e==="string"?e:"";return wishlist.parseWishes(t)}}(this);if(typeof arguments[0]==="string"){t=arguments[0];e=arguments[1];if(typeof arguments[2]==="object"&&arguments[2]!==null&&!Array.isArray(arguments[2])){s=arguments[2]}else{r=arguments[2];s=arguments[3]}}else{e=arguments[0];if(typeof arguments[1]==="object"&&arguments[1]!==null&&!Array.isArray(arguments[1])){s=arguments[1]}else{r=arguments[1];s=arguments[2]}}i=n(r);if(s==null){s={}}if(t!==void 0){this.name=t}this.fun=e;if(r!==void 0){this.wishes=i}if(s.async!==void 0){this.async=s.async}return this};Test.prototype.setAsync=function(){var e,t,n,s,r;e=[];for(r=0,n=arguments.length;r<n;r++){s=arguments[r];e.push(s)}t=e[e.length-1];if(typeof t==="object"&&t!==null&&!Array.isArray(t)){t.async=true}else{e.push({async:true})}return this.set.apply(this,e)};Test.prototype.after=function(e){this.afterFun=e;return this};Test.prototype.add=function(){var e,t;t=null;e=null;if(arguments[0]instanceof wishlist.Test){t=arguments[0];e=t.getAll().length}else{t=new wishlist.Test;t.set.apply(t,arguments);e=1}t.parent=this;this._children.push(t);this.getAncestorsAndSelf().forEach(function(t){return function(t){return t.allCount+=e}}(this));return this};Test.prototype.addAsync=function(){var e,t,n,s,r;e=[];for(r=0,n=arguments.length;r<n;r++){s=arguments[r];e.push(s)}t=e[e.length-1];if(typeof t==="object"&&t!==null&&!Array.isArray(t)){t.async=true}else{e.push({async:true})}return this.add.apply(this,e)};Test.prototype.getChildren=function(){return this._children.slice(0)};Test.prototype.getAncestors=function(){var e,t;t=this;e=[];while(t.parent!==null){e.push(t.parent);t=t.parent}return e};Test.prototype.getAncestorsAndSelf=function(){return[this].concat(this.getAncestors())};Test.prototype.getAll=function(){var e,t;e=[];e.push(this);t=function(n){return function(n){return n.getChildren().forEach(function(n){e.push(n);return t(n)})}}(this);t(this);return e};Test.prototype.run=function(e){var t,n,s;if(e==null){e=true}if(e){wishlist.currentRootTest=this}this._resetContext();if(this.parent!=null){this.env=wishlist.objectClone(this.parent.env)}setTimeout(function(e){return function(){var t;if(wishlist.environmentType==="node"){t=require("domain").create();t.on("error",function(t){return e.end({type:false,errorMessage:"Error Name: "+t.name+"\nError Message: "+t.message+"\nError Stack: "+t.stack})});return t.run(function(){return process.nextTick(function(){e.fun(e.env,e);if(e.result==null&&!e.async){return e.end({type:true})}})})}else{try{e.fun(e.env,e);if(e.result==null&&!e.async){return e.end({type:true})}}catch(t){return e.end({type:false})}}}}(this),0);if(e){t=this.getAll();console.log();s=function(e){return function(){var e,s,r,i,l,u,o;l=t.filter(function(e){return e.result!=null&&e.result.type===true});e=t.filter(function(e){return e.result!=null&&e.result.type===false});u=t.filter(function(e){return e.result==null});console.log((new Date).toISOString()+" OK: "+l.length+", "+("Exception: "+e.length+", Pending: "+u.length));if(u.length===0){clearInterval(n);e.forEach(function(e){console.log("\n********** Exceptional Test **********");console.log("Test: "+e.name);console.log("Function: "+e.fun.toString());if(e.result.errorMessage!=null){return console.log(e.result.errorMessage)}});s=0;o=0;i="";t.forEach(function(e){return e.wishResults.forEach(function(t){var n,r;i+=" "+t.type.toString();if(t.type===false){s++;n=e.getAncestors();n.reverse();r=n.concat([e]).map(function(e){return e.name}).join(" --> ");console.log("\n********** Broken Wish **********");console.log(" Test: "+r);console.log(" Wish: "+t.name);console.log("Expected: "+t.expected);return console.log(" Actual: "+t.actual)}else{return o++}})});i=i.trim();r=wishlist.sha256(i).substr(0,5);console.log("\n"+((e.length===0?"Tests OK.":e.length+" tests of "+t.length+" exceptional.")+" "+(s===0?"Wishes fulfilled.":s+" wishes of "+(s+o)+" broken.")+" "+("Mark: "+r))+"\n");return wishlist.currentRootTest=null}}}(this);n=setInterval(s,1e3);setTimeout(s,10)}return this};Test.prototype.end=function(e){if(this.result==null){this.result=e!=null?e:{type:true};this.wishes.forEach(function(e){return function(t){return e._checkWish(t)}}(this));this.getAncestorsAndSelf().forEach(function(e){return function(e){e.endedCount++;if(e.endedCount===e.allCount){return e._tryCallFun(function(){return e.afterFun(e.env)})}}}(this));this.getChildren().forEach(function(e){return function(e){return e.run(false)}}(this))}return this};Test.prototype._tryCallFun=function(e){var t;if(wishlist.environmentType==="node"){t=require("domain").create();t.on("error",function(e){return function(e){return console.log(e.stack)}}(this));return t.run(function(t){return function(){return process.nextTick(e)}}(this))}else{try{return e()}catch(e){return console.log("Error!")}}};Test.prototype._checkWish=function(wishStr){var args,interpret,name,parsed,result,that;that=this;interpret=function(e){return function(t){wishlist.parseExpression(t,Object.keys(e.env)).forEach(function(e,n){var s,r;s="that.env.";r=e+s.length*n;return t=t.substr(0,r)+s+t.substr(r)});return t}}(this);parsed=wishlist.parseWish(wishStr);args=parsed.components.map(function(e){return function(e,t){if(t===parsed.components.length-1){return e}else{return interpret(e)}}}(this));name=JSON.parse(args[args.length-1]);result=function(){try{args=args.map(function(_this){return function(m){return eval("(("+m+"))")}}(this));return this["_check_"+parsed.type].apply(this,args)}catch(e){return{type:false,name:name,actual:"unknown",expected:"unknown"}}}.call(this);return this.wishResults.push(result)};Test.prototype.wish=function(e){return wishlist.parseWishes(e).forEach(function(e){return function(t){return e._checkWish(t)}}(this))};return Test}();wishlist.Test.prototype._check_equal=function(e,t,n){var s,r,i;if(n==null){n=""}r=[];s=function(e){return function(e,t){if(Array.isArray(e)&&Array.isArray(t)){if(e.length===t.length&&t.every(function(t,n){if(indexOf.call(r,t)>=0){return wishlist.objectIs(e[n],t)}else{if(typeof t==="object"&&t!==null){r.push(t)}return s(e[n],t)}})){return true}else{return false}}else if(typeof e==="object"&&e!==null&&typeof t==="object"&&t!==null){if(wishlist.hasSameKeys(e,t)&&Object.keys(t).every(function(n){var i;if(i=t[n],indexOf.call(r,i)>=0){return wishlist.objectIs(e[n],t[n])}else{if(typeof t[n]==="object"&&t[n]!==null){r.push(t[n])}return s(e[n],t[n])}})){return true}else{return false}}else{return wishlist.objectIs(e,t)}}}(this);i={type:s(e,t),name:n};if(i.type===false){i.actual=wishlist.valueToMessage(e);i.expected="= "+wishlist.valueToMessage(t)}return i};wishlist.Test.prototype._check_notEqual=function(e,t,n){var s,r,i;if(n==null){n=""}r=[];s=function(e){return function(e,t){if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length||t.some(function(t,n){if(indexOf.call(r,t)>=0){return!wishlist.objectIs(e[n],t)}else{if(typeof t==="object"&&t!==null){r.push(t)}return s(e[n],t)}})){return true}else{return false}}else if(typeof e==="object"&&e!==null&&typeof t==="object"&&t!==null){if(!wishlist.hasSameKeys(e,t)||Object.keys(t).some(function(n){var i;if(i=t[n],indexOf.call(r,i)>=0){return!wishlist.objectIs(e[n],t[n])}else{if(typeof t[n]==="object"&&t[n]!==null){r.push(t[n])}return s(e[n],t[n])}})){return true}else{return false}}else{return!wishlist.objectIs(e,t)}}}(this);i={type:s(e,t),name:n};if(i.type===false){i.actual=wishlist.valueToMessage(e);i.expected="≠ "+wishlist.valueToMessage(t)}return i};wishlist.Test.prototype._check_is=function(e,t,n){var s;if(n==null){n=""}s={type:wishlist.objectIs(e,t),name:n};if(s.type===false){s.actual=wishlist.valueToMessage(e);s.expected="is "+wishlist.valueToMessage(t)}return s};wishlist.Test.prototype._check_isnt=function(e,t,n){var s;if(n==null){n=""}s={type:!wishlist.objectIs(e,t),name:n};if(s.type===false){s.actual=wishlist.valueToMessage(e);s.expected="isn't "+wishlist.valueToMessage(t)}return s};wishlist.Test.prototype._check_throws=function(e,t,n){var s,r,i,l;if(n==null){n=""}r=false;l=function(){try{e();r=true;return false}catch(e){s=e;if(t==null){return true}else if(t instanceof RegExp){if(t.test(s.message)){return true}else{return false}}else{if(s instanceof t){return true}else{return false}}}}();i={type:l,name:n};if(i.type===false){i.actual=r?"no exception":"another exception";i.expected=r?"exception":"an exception"}return i};wishlist.Test.prototype._check_doesNotThrow=function(e,t){var n,s;if(t==null){t=""}s=function(){try{e();return true}catch(e){return false}}();n={type:s,name:t};if(n.type===false){n.actual="exception";n.expected="no exception"}return n};wishlist.Test.prototype._check_lessThan=function(e,t,n){var s;if(n==null){n=""}s={type:e<t,name:n};if(s.type===false){s.actual=wishlist.valueToMessage(e);s.expected="< "+wishlist.valueToMessage(t)}return s};wishlist.Test.prototype._check_lessThanOrEqual=function(e,t,n){var s;if(n==null){n=""}s={type:e<=t,name:n};if(s.type===false){s.actual=wishlist.valueToMessage(e);s.expected="<= "+wishlist.valueToMessage(t)}return s};wishlist.Test.prototype._check_greaterThan=function(e,t,n){var s;if(n==null){n=""}s={type:e>t,name:n};if(s.type===false){s.actual=wishlist.valueToMessage(e);s.expected="> "+wishlist.valueToMessage(t)}return s};wishlist.Test.prototype._check_greaterThanOrEqual=function(e,t,n){var s;if(n==null){n=""}s={type:e>=t,name:n};if(s.type===false){s.actual=wishlist.valueToMessage(e);s.expected=">= "+wishlist.valueToMessage(t)}return s};if(wishlist.environmentType==="browser"){window.npmWishlist=wishlist}if(wishlist.moduleSystem==="commonjs"){module.exports=wishlist}}).call(this);