unittest_dart.js 6.4 KB

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