Guest User

Untitled

a guest
Feb 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /**
  2. * Simple JS test script
  3. * @return
  4. */
  5. function kickTest() {
  6. testLog("This is tester JS");
  7.  
  8. new Test('TEST CASE 1', function(){
  9. testLog("This is TEST CASE1");
  10. }).run();
  11.  
  12. new Test('TEST CASE 2').run();
  13.  
  14. new TestSuite('SUITE for my self')
  15. .add(new Test('TEST CASE 1 OF SUITE', function() {
  16. testLog("Suite1!!");
  17. }))
  18. .add(new Test('TEST CASE 2 OF SUITE', function() {
  19. testLog("Suite2!!");
  20. this.assert(true, true);
  21. }))
  22. .add(new Test('TEST CASE 3 OF SUITE', function() {
  23. testLog("Suite2!!");
  24. this.assert(false, false);
  25. }))
  26. .add(new Test('TEST CASE 4 OF SUITE', function() {
  27. testLog("Suite2!!");
  28. this.assert(true, false);
  29. }))
  30. .run();
  31.  
  32. new TestSuite('SUITE for my self2')
  33. .add(new Test('TEST CASE 1 OF SUITE', function() {
  34. testLog("Suite2-1!!");
  35. }))
  36. .add(new Test('TEST CASE 2 OF SUITE', function() {
  37. testLog("Suite2-2!!");
  38. this.assert(true, true);
  39. }))
  40. .run();
  41. }
  42.  
  43. // Test utility >>>
  44.  
  45. /*
  46. * Show test log to another window
  47. */
  48. (function() {
  49. var _out = null;
  50.  
  51. testLog = function(msg, className) {
  52. className = (className || 'normal');
  53.  
  54. if (!_out) _out = createNewWindow();
  55. try {
  56. _out.write('<span class=' + className + '>' + msg + '</span><br>');
  57. }
  58. catch(e) {
  59. _out = createNewWindow();
  60. _out.write(msg + '<br>');
  61. }
  62. }
  63.  
  64. function createNewWindow() {
  65. var result = window.open().document;
  66. result.write([
  67. '<style type="text/css">' ,
  68. 'span.success { color: green; font-weight: bold; }',
  69. 'span.fail { color: red; font-weight: bold; }',
  70. 'span.normal { color: gray; }',
  71. '</style>'
  72. ].join(' '));
  73. return result;
  74. }
  75. })();
  76.  
  77. /**
  78. * Simple Test case class
  79. * @param name
  80. * @param testImpl
  81. * @return
  82. */
  83. function Test(name, testImpl) {
  84. testLog('create test :' + name);
  85. this.name = name;
  86. this.state = Test.STATE.NOT_RUNNING;
  87. this.impl = (testImpl || function(){this.state = Test.STATE.FAIL});
  88. }
  89.  
  90. Test.STATE = {
  91. NOT_RUNNNING: -1,
  92. SUCCESS: 1,
  93. FAIL: 0
  94. }
  95.  
  96. Test.prototype = {
  97. run : function() {
  98. testLog('run test :' + this.name);
  99.  
  100. this.impl();
  101.  
  102. if(this.state != Test.STATE.FAIL) {
  103. this.state = Test.STATE.SUCCESS;
  104. testLog(this.name + ' : SUCCESS.', 'success');
  105. }
  106. else {
  107. testLog(this.name + ' : FAIL', 'fail');
  108. }
  109. },
  110.  
  111. assert : function(expected, actual) {
  112. if (expected != actual) {
  113. this.state = Test.STATE.FAIL;
  114. }
  115. }
  116. }
  117.  
  118. /**
  119. Simple test suite
  120. */
  121. function TestSuite(name) {
  122. testLog("<hr/>");
  123. testLog("<h1>Create suite [" + name + "].</h1>");
  124.  
  125. this.name = (name || 'no name');
  126. this.tests = [];
  127. }
  128.  
  129. TestSuite.prototype = {
  130. add: function(testObj) {
  131. this.tests.push(testObj);
  132. return this;
  133. },
  134.  
  135. run: function() {
  136. testLog("<h2>Start test suite [" + this.name + "].</h2>");
  137.  
  138. for(var i = 0; i < this.tests.length; i++) {
  139.  
  140. if (this.tests[i]) this.tests[i].run();
  141. }
  142.  
  143. if (this.suiteAllCleared()) {
  144. testLog("<h1>Test suite [" + this.name + "] all SUCCEEDED.</h1>", 'success');
  145. }
  146. else {
  147. testLog("<h1>Test suite [" + this.name + "] some FAILED!!</h1>", 'fail');
  148. }
  149.  
  150. testLog("<hr/>");
  151. },
  152.  
  153. suiteAllCleared: function() {
  154. for (var i = 0; i < this.tests.length; i++) {
  155. if (this.tests[i].state != Test.STATE.SUCCESS) {
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. }
Add Comment
Please, Sign In to add comment