UNPKG

hopjs

Version:

A RESTful declarative API framework, with stub generators for Shell, and Android

152 lines (110 loc) 5.89 kB
<html> <head> <title>Foo</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script src="/_hopjs/apitest.js"></script> </head> <script> $(document).ready(function(){ console = new Object(); console.log = function(log) { $("#log").append(log+"<br>"); } console.debug = console.log; console.info = console.log; console.warn = console.log; console.error = console.log; if(typeof Hop!="undefined"){ Hop.TestCase.callbackId=0; Hop.TestCase.callbacks={}; //Implement Hop.TestCase.prototype.do to call the native calls Hop.TestCase.prototype.do=function(method,inputValue,onComplete){ inputValue=inputValue||{}; onComplete=onComplete||{}; Hop.TestCase.callbacks[Hop.TestCase.callbackId]=onComplete; var what = { input: inputValue, method: method, onComplete:"(Hop.TestCase.callbacks["+Hop.TestCase.callbackId+"])(#{ERROR},#{OUTPUT})" } var iframe = document.createElement("IFRAME"); iframe.setAttribute("src", "api:" + JSON.stringify(what)); document.documentElement.appendChild(iframe); iframe.parentNode.removeChild(iframe); iframe = null; } //Implement Hop.TestCase.prototype.testResult to capture the results of the test Hop.TestCase.prototype.testResult=function(taskIndex,method,inputValue,err,outputValue,testType,result){ if(Hop.TestCase.lastTaskIndex!=taskIndex){ Hop.TestCase.resultIndex++; $(Hop.TestCase.resultSelector).append("<p><div>#"+(taskIndex+1)+" "+method+"(<a href='#foo' onclick='$(\"#testResult_"+Hop.TestCase.resultIndex+"\").slideToggle();return false;'>Details</a>)</div><p>"); Hop.TestCase.lastTaskIndex=taskIndex $(Hop.TestCase.resultSelector).append("<div class='testDetails' id='testResult_"+Hop.TestCase.resultIndex+"' style='display:none'><h4>Input</h4><div class='testInput'>"+JSON.stringify(inputValue)+"</div><h4>Error</h4><div class='testResultError'>"+err+"</div><h4>Result</h4><div class='testResult'>"+JSON.stringify(outputValue)+"</div><br></div>"); } var what = Hop.Tests[testType]; if(what){ if(result==false) $(Hop.TestCase.resultSelector).append("<div style='color:red'>"+what.fail+"</div>"); else $(Hop.TestCase.resultSelector).append("<div style='color:green'>"+what.pass+"</div>"); } else { if(result==false) $(Hop.TestCase.resultSelector).append("<div style='color:red'>"+testType+"</div>"); else $(Hop.TestCase.resultSelector).append("<div style='color:green'>"+testType+"</div>"); } if(result==true){ Hop.TestCase.results.pass++ } else { Hop.TestCase.results.fail++; } } Hop.TestCase.resultSelector=null; Hop.TestCase.runInDialog=function(what,selector){ Hop.TestCase.resultSelector=selector; Hop.TestCase.resultIndex=0; Hop.TestCase.lastTaskIndex=-1; Hop.TestCase.results={ pass:0, fail:0 }; $(selector).empty(); if(Hop.TestCases[what]){ Hop.TestCase.run(what); $(Hop.TestCase.resultSelector).append("<p>Passed: "+Hop.TestCase.results.pass+" Failed: "+Hop.TestCase.results.fail); } else { if(typeof what=="string" && what.length>0){ var toRun=[]; for(var i in Hop.TestCases){ if(i.indexOf(what)==0){ toRun.push(i); } } } else { var toRun=Object.keys(Hop.TestCases); } var run=function(){ if (toRun.length>0){ var testCase = toRun.shift(); $(Hop.TestCase.resultSelector).append("<h3>Running :"+testCase+"</h3>"); Hop.TestCase.run(testCase,function(){ console.log("Done running test cases"); Hop.TestCase.lastTaskIndex=-1; run(); }); } else { $(Hop.TestCase.resultSelector).append("<p><span id='allTestResults'>Passed: "+Hop.TestCase.results.pass+" Failed: "+Hop.TestCase.results.fail+" "+((Hop.TestCase.results.pass/(Hop.TestCase.results.pass+Hop.TestCase.results.fail))*100.0).toFixed(1)+"%</span>"); } } run(); } } Hop.TestCase.runInDialog(null,"#results"); } }); </script> <body> <div id="results"> </div> <h3>Log</h3> <div id="log"> </div> </body> </html>