unittest_php.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2015 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 PHP for unit test blocks.
  22. * @author daarond@gmail.com (Daaron Dwyer)
  23. */
  24. 'use strict';
  25. Blockly.PHP['unittest_main'] = function(block) {
  26. // Container for unit tests.
  27. var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
  28. Blockly.Variables.NAME_TYPE);
  29. var functionName = Blockly.PHP.provideFunction_(
  30. 'unittest_report',
  31. [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '() {',
  32. 'global ' + resultsVar + ';',
  33. ' // Create test report.',
  34. ' $report = array();',
  35. ' $summary = array();',
  36. ' $fails = 0;',
  37. ' for ($x = 0; $x < count(' + resultsVar + '); $x++) {',
  38. ' if (' + resultsVar + '[$x][0]) {',
  39. ' array_push($summary, ".");',
  40. ' } else {',
  41. ' array_push($summary, "F");',
  42. ' $fails++;',
  43. ' array_push($report,"");',
  44. ' array_push($report, "FAIL: " . ' + resultsVar + '[$x][2]);',
  45. ' array_push($report, ' + resultsVar + '[$x][1]);',
  46. ' }',
  47. ' }',
  48. ' array_unshift($report, implode("",$summary));',
  49. ' array_push($report, "");',
  50. ' array_push($report, "Number of tests run: " . count(' + resultsVar + '));',
  51. ' array_push($report, "");',
  52. ' if ($fails) {',
  53. ' array_push($report, "FAILED (failures=" . $fails + ")");',
  54. ' } else {',
  55. ' array_push($report, "OK");',
  56. ' }',
  57. ' return implode("\\n", $report);',
  58. '}']);
  59. // Setup global to hold test results.
  60. var code = resultsVar + ' = array();\n';
  61. // Run tests (unindented).
  62. code += Blockly.PHP.statementToCode(block, 'DO')
  63. .replace(/^ /, '').replace(/\n /g, '\n');
  64. var reportVar = Blockly.PHP.variableDB_.getDistinctName(
  65. 'report', Blockly.Variables.NAME_TYPE);
  66. code += 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 += 'print(' + reportVar + ');\n';
  71. return code;
  72. };
  73. Blockly.PHP['unittest_main'].defineAssert_ = function(block) {
  74. var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
  75. Blockly.Variables.NAME_TYPE);
  76. var functionName = Blockly.PHP.provideFunction_(
  77. 'assertEquals',
  78. [' function equals($a, $b) {',
  79. ' if ($a === $b) {',
  80. ' return true;',
  81. ' } else if ((is_numeric($a)) && (is_numeric($b)) &&',
  82. ' (round($a,15) == round($b,15))) {',
  83. ' return true;',
  84. ' } else if (is_array($a) && is_array($b)) {',
  85. ' if (count($a) != count($b)) {',
  86. ' return false;',
  87. ' }',
  88. ' for ($i = 0; $i < count($a); $i++) {',
  89. ' if (!equals($a[$i], $b[$i])) {',
  90. ' return false;',
  91. ' }',
  92. ' }',
  93. ' return true;',
  94. ' }',
  95. ' return false;',
  96. ' }',
  97. 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
  98. '($actual, $expected, $message) {',
  99. 'global ' + resultsVar + ';',
  100. ' // Asserts that a value equals another value.',
  101. ' if (!is_array(' + resultsVar + ')) {',
  102. ' throw new Exception("Orphaned assert: " . $message);',
  103. ' }',
  104. ' if (equals($actual, $expected)) {',
  105. ' array_push(' + resultsVar + ', [true, "OK", $message]);',
  106. ' } else {',
  107. ' $expected = is_array($expected) ? implode(" ", $expected) : ' +
  108. '$expected;',
  109. ' $actual = is_array($actual) ? implode(" ", $actual) : ' +
  110. '$actual;',
  111. ' array_push(' + resultsVar + ', [false, ' +
  112. '"Expected: " . $expected . "\\nActual: " . $actual, $message]);',
  113. ' }',
  114. '}']);
  115. return functionName;
  116. };
  117. Blockly.PHP['unittest_assertequals'] = function(block) {
  118. // Asserts that a value equals another value.
  119. var message = Blockly.PHP.valueToCode(block, 'MESSAGE',
  120. Blockly.PHP.ORDER_NONE) || '';
  121. var actual = Blockly.PHP.valueToCode(block, 'ACTUAL',
  122. Blockly.PHP.ORDER_COMMA) || 'null';
  123. var expected = Blockly.PHP.valueToCode(block, 'EXPECTED',
  124. Blockly.PHP.ORDER_COMMA) || 'null';
  125. return Blockly.PHP['unittest_main'].defineAssert_() +
  126. '(' + actual + ', ' + expected + ', ' + message + ');\n';
  127. };
  128. Blockly.PHP['unittest_assertvalue'] = function(block) {
  129. // Asserts that a value is true, false, or null.
  130. var message = Blockly.PHP.valueToCode(block, 'MESSAGE',
  131. Blockly.PHP.ORDER_NONE) || '';
  132. var actual = Blockly.PHP.valueToCode(block, 'ACTUAL',
  133. Blockly.PHP.ORDER_COMMA) || 'null';
  134. var expected = block.getFieldValue('EXPECTED');
  135. if (expected == 'TRUE') {
  136. expected = 'true';
  137. } else if (expected == 'FALSE') {
  138. expected = 'false';
  139. } else if (expected == 'NULL') {
  140. expected = 'null';
  141. }
  142. return Blockly.PHP['unittest_main'].defineAssert_() +
  143. '(' + actual + ', ' + expected + ', ' + message + ');\n';
  144. };
  145. Blockly.PHP['unittest_fail'] = function(block) {
  146. // Always assert an error.
  147. var resultsVar = Blockly.PHP.variableDB_.getName('unittestResults',
  148. Blockly.Variables.NAME_TYPE);
  149. var message = Blockly.PHP.valueToCode(block, 'MESSAGE',
  150. Blockly.PHP.ORDER_NONE) || '';
  151. var functionName = Blockly.PHP.provideFunction_(
  152. 'unittest_fail',
  153. [ 'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
  154. '($message) {',
  155. 'global ' + resultsVar + ';',
  156. ' // Always assert an error.',
  157. ' if (!' + resultsVar + ') {',
  158. ' throw new Exception("Orphaned assert fail: " . $message);',
  159. ' }',
  160. ' array_push(' + resultsVar + ', [false, "Fail.", $message]);',
  161. '}']);
  162. return functionName + '(' + message + ');\n';
  163. };
  164. Blockly.PHP['unittest_adjustindex'] = function(block) {
  165. var index = Blockly.PHP.valueToCode(block, 'INDEX',
  166. Blockly.PHP.ORDER_ADDITION) || '0';
  167. // Adjust index if using one-based indexing.
  168. if (block.workspace.options.oneBasedIndex) {
  169. if (Blockly.isNumber(index)) {
  170. // If the index is a naked number, adjust it right now.
  171. return [parseFloat(index) + 1, Blockly.PHP.ORDER_ATOMIC];
  172. } else {
  173. // If the index is dynamic, adjust it in code.
  174. index = index + ' + 1';
  175. }
  176. } else if (Blockly.isNumber(index)) {
  177. return [index, Blockly.PHP.ORDER_ATOMIC];
  178. }
  179. return [index, Blockly.PHP.ORDER_ADDITION];
  180. };