unittest_javascript.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Generating JavaScript for unit test blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. Blockly.JavaScript['unittest_main'] = function(block) {
  26. // Container for unit tests.
  27. var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
  28. Blockly.Variables.NAME_TYPE);
  29. var functionName = Blockly.JavaScript.provideFunction_(
  30. 'unittest_report',
  31. [ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '() {',
  32. ' // Create test report.',
  33. ' var report = [];',
  34. ' var summary = [];',
  35. ' var fails = 0;',
  36. ' for (var i = 0; i < ' + resultsVar + '.length; i++) {',
  37. ' if (' + resultsVar + '[i][0]) {',
  38. ' summary.push(".");',
  39. ' } else {',
  40. ' summary.push("F");',
  41. ' fails++;',
  42. ' report.push("");',
  43. ' report.push("FAIL: " + ' + resultsVar + '[i][2]);',
  44. ' report.push(' + resultsVar + '[i][1]);',
  45. ' }',
  46. ' }',
  47. ' report.unshift(summary.join(""));',
  48. ' report.push("");',
  49. ' report.push("Number of tests run: " + ' + resultsVar +
  50. '.length);',
  51. ' report.push("");',
  52. ' if (fails) {',
  53. ' report.push("FAILED (failures=" + fails + ")");',
  54. ' } else {',
  55. ' report.push("OK");',
  56. ' }',
  57. ' return report.join("\\n");',
  58. '}']);
  59. // Setup global to hold test results.
  60. var code = resultsVar + ' = [];\n';
  61. // Run tests (unindented).
  62. code += Blockly.JavaScript.statementToCode(block, 'DO')
  63. .replace(/^ /, '').replace(/\n /g, '\n');
  64. var reportVar = Blockly.JavaScript.variableDB_.getDistinctName(
  65. 'report', Blockly.Variables.NAME_TYPE);
  66. code += 'var ' + reportVar + ' = ' + functionName + '();\n';
  67. // Destroy results.
  68. code += resultsVar + ' = null;\n';
  69. // Send the report to the console (that's where errors will go anyway).
  70. code += 'console.log(' + reportVar + ');\n';
  71. return code;
  72. };
  73. Blockly.JavaScript['unittest_main'].defineAssert_ = function(block) {
  74. var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
  75. Blockly.Variables.NAME_TYPE);
  76. var functionName = Blockly.JavaScript.provideFunction_(
  77. 'assertEquals',
  78. [ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
  79. '(actual, expected, message) {',
  80. ' // Asserts that a value equals another value.',
  81. ' if (!' + resultsVar + ') {',
  82. ' throw "Orphaned assert: " + message;',
  83. ' }',
  84. ' function equals(a, b) {',
  85. ' if (a === b) {',
  86. ' return true;',
  87. ' } else if ((typeof a == "number") && (typeof b == "number") &&',
  88. ' (a.toPrecision(15) == b.toPrecision(15))) {',
  89. ' return true;',
  90. ' } else if (a instanceof Array && b instanceof Array) {',
  91. ' if (a.length != b.length) {',
  92. ' return false;',
  93. ' }',
  94. ' for (var i = 0; i < a.length; i++) {',
  95. ' if (!equals(a[i], b[i])) {',
  96. ' return false;',
  97. ' }',
  98. ' }',
  99. ' return true;',
  100. ' }',
  101. ' return false;',
  102. ' }',
  103. ' if (equals(actual, expected)) {',
  104. ' ' + resultsVar + '.push([true, "OK", message]);',
  105. ' } else {',
  106. ' ' + resultsVar + '.push([false, ' +
  107. '"Expected: " + expected + "\\nActual: " + actual, message]);',
  108. ' }',
  109. '}']);
  110. return functionName;
  111. };
  112. Blockly.JavaScript['unittest_assertequals'] = function(block) {
  113. // Asserts that a value equals another value.
  114. var message = Blockly.JavaScript.valueToCode(block, 'MESSAGE',
  115. Blockly.JavaScript.ORDER_NONE) || '';
  116. var actual = Blockly.JavaScript.valueToCode(block, 'ACTUAL',
  117. Blockly.JavaScript.ORDER_COMMA) || 'null';
  118. var expected = Blockly.JavaScript.valueToCode(block, 'EXPECTED',
  119. Blockly.JavaScript.ORDER_COMMA) || 'null';
  120. return Blockly.JavaScript['unittest_main'].defineAssert_() +
  121. '(' + actual + ', ' + expected + ', ' + message + ');\n';
  122. };
  123. Blockly.JavaScript['unittest_assertvalue'] = function(block) {
  124. // Asserts that a value is true, false, or null.
  125. var message = Blockly.JavaScript.valueToCode(block, 'MESSAGE',
  126. Blockly.JavaScript.ORDER_NONE) || '';
  127. var actual = Blockly.JavaScript.valueToCode(block, 'ACTUAL',
  128. Blockly.JavaScript.ORDER_COMMA) || 'null';
  129. var expected = block.getFieldValue('EXPECTED');
  130. if (expected == 'TRUE') {
  131. expected = 'true';
  132. } else if (expected == 'FALSE') {
  133. expected = 'false';
  134. } else if (expected == 'NULL') {
  135. expected = 'null';
  136. }
  137. return Blockly.JavaScript['unittest_main'].defineAssert_() +
  138. '(' + actual + ', ' + expected + ', ' + message + ');\n';
  139. };
  140. Blockly.JavaScript['unittest_fail'] = function(block) {
  141. // Always assert an error.
  142. var resultsVar = Blockly.JavaScript.variableDB_.getName('unittestResults',
  143. Blockly.Variables.NAME_TYPE);
  144. var message = Blockly.JavaScript.valueToCode(block, 'MESSAGE',
  145. Blockly.JavaScript.ORDER_NONE) || '';
  146. var functionName = Blockly.JavaScript.provideFunction_(
  147. 'unittest_fail',
  148. [ 'function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
  149. '(message) {',
  150. ' // Always assert an error.',
  151. ' if (!' + resultsVar + ') {',
  152. ' throw "Orphaned assert fail: " + message;',
  153. ' }',
  154. ' ' + resultsVar + '.push([false, "Fail.", message]);',
  155. '}']);
  156. return functionName + '(' + message + ');\n';
  157. };
  158. Blockly.JavaScript['unittest_adjustindex'] = function(block) {
  159. var index = Blockly.JavaScript.valueToCode(block, 'INDEX',
  160. Blockly.JavaScript.ORDER_ADDITION) || '0';
  161. // Adjust index if using one-based indexing.
  162. if (block.workspace.options.oneBasedIndex) {
  163. if (Blockly.isNumber(index)) {
  164. // If the index is a naked number, adjust it right now.
  165. return [parseFloat(index) + 1, Blockly.JavaScript.ORDER_ATOMIC];
  166. } else {
  167. // If the index is dynamic, adjust it in code.
  168. index = index + ' + 1';
  169. }
  170. } else if (Blockly.isNumber(index)) {
  171. return [index, Blockly.JavaScript.ORDER_ATOMIC];
  172. }
  173. return [index, Blockly.JavaScript.ORDER_ADDITION];
  174. };