unittest_python.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Python for unit test blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. Blockly.Python['unittest_main'] = function(block) {
  26. // Container for unit tests.
  27. var resultsVar = Blockly.Python.variableDB_.getName('unittestResults',
  28. Blockly.Variables.NAME_TYPE);
  29. var functionName = Blockly.Python.provideFunction_(
  30. 'unittest_report',
  31. ['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '():',
  32. ' # Create test report.',
  33. ' report = []',
  34. ' summary = []',
  35. ' fails = 0',
  36. ' for (success, log, message) in ' + resultsVar + ':',
  37. ' if success:',
  38. ' summary.append(".")',
  39. ' else:',
  40. ' summary.append("F")',
  41. ' fails += 1',
  42. ' report.append("")',
  43. ' report.append("FAIL: " + message)',
  44. ' report.append(log)',
  45. ' report.insert(0, "".join(summary))',
  46. ' report.append("")',
  47. ' report.append("Number of tests run: %d" % len(' + resultsVar + '))',
  48. ' report.append("")',
  49. ' if fails:',
  50. ' report.append("FAILED (failures=%d)" % fails)',
  51. ' else:',
  52. ' report.append("OK")',
  53. ' return "\\n".join(report)']);
  54. // Setup global to hold test results.
  55. var code = resultsVar + ' = []\n';
  56. // Run tests (unindented).
  57. code += Blockly.Python.statementToCode(block, 'DO')
  58. .replace(/^ /, '').replace(/\n /g, '\n');
  59. var reportVar = Blockly.Python.variableDB_.getDistinctName(
  60. 'report', Blockly.Variables.NAME_TYPE);
  61. code += reportVar + ' = ' + functionName + '()\n';
  62. // Destroy results.
  63. code += resultsVar + ' = None\n';
  64. // Print the report.
  65. code += 'print(' + reportVar + ')\n';
  66. return code;
  67. };
  68. Blockly.Python['unittest_main'].defineAssert_ = function() {
  69. var resultsVar = Blockly.Python.variableDB_.getName('unittestResults',
  70. Blockly.Variables.NAME_TYPE);
  71. var functionName = Blockly.Python.provideFunction_(
  72. 'assertEquals',
  73. ['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
  74. '(actual, expected, message):',
  75. ' # Asserts that a value equals another value.',
  76. ' if ' + resultsVar + ' == None:',
  77. ' raise Exception("Orphaned assert equals: " + message)',
  78. ' if actual == expected:',
  79. ' ' + resultsVar + '.append((True, "OK", message))',
  80. ' else:',
  81. ' ' + resultsVar + '.append((False, ' +
  82. '"Expected: %s\\nActual: %s" % (expected, actual), message))']);
  83. return functionName;
  84. };
  85. Blockly.Python['unittest_assertequals'] = function(block) {
  86. // Asserts that a value equals another value.
  87. var message = Blockly.Python.valueToCode(block, 'MESSAGE',
  88. Blockly.Python.ORDER_NONE) || '';
  89. var actual = Blockly.Python.valueToCode(block, 'ACTUAL',
  90. Blockly.Python.ORDER_NONE) || 'None';
  91. var expected = Blockly.Python.valueToCode(block, 'EXPECTED',
  92. Blockly.Python.ORDER_NONE) || 'None';
  93. return Blockly.Python['unittest_main'].defineAssert_() +
  94. '(' + actual + ', ' + expected + ', ' + message + ')\n';
  95. };
  96. Blockly.Python['unittest_assertvalue'] = function(block) {
  97. // Asserts that a value is true, false, or null.
  98. var message = Blockly.Python.valueToCode(block, 'MESSAGE',
  99. Blockly.Python.ORDER_NONE) || '';
  100. var actual = Blockly.Python.valueToCode(block, 'ACTUAL',
  101. Blockly.Python.ORDER_NONE) || 'None';
  102. var expected = block.getFieldValue('EXPECTED');
  103. if (expected == 'TRUE') {
  104. expected = 'True';
  105. } else if (expected == 'FALSE') {
  106. expected = 'False';
  107. } else if (expected == 'NULL') {
  108. expected = 'None';
  109. }
  110. return Blockly.Python['unittest_main'].defineAssert_() +
  111. '(' + actual + ', ' + expected + ', ' + message + ')\n';
  112. };
  113. Blockly.Python['unittest_fail'] = function(block) {
  114. // Always assert an error.
  115. var resultsVar = Blockly.Python.variableDB_.getName('unittestResults',
  116. Blockly.Variables.NAME_TYPE);
  117. var message = Blockly.Python.valueToCode(block, 'MESSAGE',
  118. Blockly.Python.ORDER_NONE) || '';
  119. var functionName = Blockly.Python.provideFunction_(
  120. 'fail',
  121. ['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(message):',
  122. ' # Always assert an error.',
  123. ' if ' + resultsVar + ' == None:',
  124. ' raise Exception("Orphaned assert equals: " + message)',
  125. ' ' + resultsVar + '.append((False, "Fail.", message))']);
  126. return functionName + '(' + message + ')\n';
  127. };
  128. Blockly.Python['unittest_adjustindex'] = function(block) {
  129. var index = Blockly.Python.valueToCode(block, 'INDEX',
  130. Blockly.Python.ORDER_ADDITIVE) || '0';
  131. // Adjust index if using one-based indexing.
  132. if (block.workspace.options.oneBasedIndex) {
  133. if (Blockly.isNumber(index)) {
  134. // If the index is a naked number, adjust it right now.
  135. return [parseFloat(index) + 1, Blockly.Python.ORDER_ATOMIC];
  136. } else {
  137. // If the index is dynamic, adjust it in code.
  138. index = index + ' + 1';
  139. }
  140. } else if (Blockly.isNumber(index)) {
  141. return [index, Blockly.Python.ORDER_ATOMIC];
  142. }
  143. return [index, Blockly.Python.ORDER_ADDITIVE];
  144. };