unittest_lua.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2016 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 Lua for unit test blocks.
  22. * @author rodrigoq@google.com (Rodrigo Queiro)
  23. */
  24. 'use strict';
  25. Blockly.Lua['unittest_main'] = function(block) {
  26. // Container for unit tests.
  27. var resultsVar = Blockly.Lua.variableDB_.getName('unittestResults',
  28. Blockly.Variables.NAME_TYPE);
  29. var functionName = Blockly.Lua.provideFunction_(
  30. 'unittest_report',
  31. ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '()',
  32. ' -- Create test report.',
  33. ' local report = {}',
  34. ' local summary = {}',
  35. ' local fails = 0',
  36. ' for _, v in pairs(' + resultsVar + ') do',
  37. ' if v["success"] then',
  38. ' table.insert(summary, ".")',
  39. ' else',
  40. ' table.insert(summary, "F")',
  41. ' fails = fails + 1',
  42. ' table.insert(report, "FAIL: " .. v["title"])',
  43. ' table.insert(report, v["log"])',
  44. ' end',
  45. ' end',
  46. ' table.insert(report, 1, table.concat(summary))',
  47. ' table.insert(report, "")',
  48. ' table.insert(report, "Number of tests run: " .. #' + resultsVar + ')',
  49. ' table.insert(report, "")',
  50. ' if fails > 0 then',
  51. ' table.insert(report, "FAILED (failures=" .. fails .. ")")',
  52. ' else',
  53. ' table.insert(report, "OK")',
  54. ' end',
  55. ' return table.concat(report, "\\n")',
  56. 'end']);
  57. // Setup global to hold test results.
  58. var code = resultsVar + ' = {}\n';
  59. // Run tests (unindented).
  60. code += Blockly.Lua.statementToCode(block, 'DO')
  61. .replace(/^ /, '').replace(/\n /g, '\n');
  62. var reportVar = Blockly.Lua.variableDB_.getDistinctName(
  63. 'report', Blockly.Variables.NAME_TYPE);
  64. code += reportVar + ' = ' + functionName + '()\n';
  65. // Destroy results.
  66. code += resultsVar + ' = nil\n';
  67. // Print the report.
  68. code += 'print(' + reportVar + ')\n';
  69. return code;
  70. };
  71. Blockly.Lua['unittest_main'].defineAssert_ = function(block) {
  72. var resultsVar = Blockly.Lua.variableDB_.getName('unittestResults',
  73. Blockly.Variables.NAME_TYPE);
  74. var functionName = Blockly.Lua.provideFunction_(
  75. 'assertEquals',
  76. ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
  77. '(actual, expected, message)',
  78. ' -- Asserts that a value equals another value.',
  79. ' assert(' + resultsVar + ' ~= nil, ' +
  80. '"Orphaned assert equals: " .. message)',
  81. ' if type(actual) == "table" and type(expected) == "table" then',
  82. ' local lists_match = #actual == #expected',
  83. ' if lists_match then',
  84. ' for i, v1 in ipairs(actual) do',
  85. ' local v2 = expected[i]',
  86. ' if type(v1) == "number" and type(v2) == "number" then',
  87. ' if math.abs(v1 - v2) > 1e-9 then',
  88. ' lists_match = false',
  89. ' end',
  90. ' elseif v1 ~= v2 then',
  91. ' lists_match = false',
  92. ' end',
  93. ' end',
  94. ' end',
  95. ' if lists_match then',
  96. ' table.insert(' + resultsVar +
  97. ', {success=true, log="OK", title=message})',
  98. ' return',
  99. ' else',
  100. ' -- produce the non-matching strings for a human-readable error',
  101. ' expected = "{" .. table.concat(expected, ", ") .. "}"',
  102. ' actual = "{" .. table.concat(actual, ", ") .. "}"',
  103. ' end',
  104. ' end',
  105. ' if actual == expected or (type(actual) == "number" and ' +
  106. 'type(expected) == "number" and math.abs(actual - expected) < ' +
  107. '1e-9) then ',
  108. ' table.insert(' + resultsVar +
  109. ', {success=true, log="OK", title=message})',
  110. ' else',
  111. ' table.insert(' + resultsVar + ', {success=false, ' +
  112. 'log=string.format("Expected: %s\\nActual: %s"' +
  113. ', tostring(expected), tostring(actual)), title=message})',
  114. ' end',
  115. 'end']);
  116. return functionName;
  117. };
  118. Blockly.Lua['unittest_assertequals'] = function(block) {
  119. // Asserts that a value equals another value.
  120. var message = Blockly.Lua.valueToCode(block, 'MESSAGE',
  121. Blockly.Lua.ORDER_NONE) || '';
  122. var actual = Blockly.Lua.valueToCode(block, 'ACTUAL',
  123. Blockly.Lua.ORDER_NONE) || 'nil';
  124. var expected = Blockly.Lua.valueToCode(block, 'EXPECTED',
  125. Blockly.Lua.ORDER_NONE) || 'nil';
  126. return Blockly.Lua['unittest_main'].defineAssert_() +
  127. '(' + actual + ', ' + expected + ', ' + message + ')\n';
  128. };
  129. Blockly.Lua['unittest_assertvalue'] = function(block) {
  130. // Asserts that a value is true, false, or null.
  131. var message = Blockly.Lua.valueToCode(block, 'MESSAGE',
  132. Blockly.Lua.ORDER_NONE) || '';
  133. var actual = Blockly.Lua.valueToCode(block, 'ACTUAL',
  134. Blockly.Lua.ORDER_NONE) || 'nil';
  135. var expected = block.getFieldValue('EXPECTED');
  136. if (expected == 'TRUE') {
  137. expected = 'true';
  138. } else if (expected == 'FALSE') {
  139. expected = 'false';
  140. } else if (expected == 'NULL') {
  141. expected = 'nil';
  142. }
  143. return Blockly.Lua.unittest_main.defineAssert_() +
  144. '(' + actual + ', ' + expected + ', ' + message + ')\n';
  145. };
  146. Blockly.Lua['unittest_fail'] = function(block) {
  147. // Always assert an error.
  148. var resultsVar = Blockly.Lua.variableDB_.getName('unittestResults',
  149. Blockly.Variables.NAME_TYPE);
  150. var message = Blockly.Lua.valueToCode(block, 'MESSAGE',
  151. Blockly.Lua.ORDER_NONE) || '';
  152. var functionName = Blockly.Lua.provideFunction_(
  153. 'unittest_fail',
  154. ['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(message)',
  155. ' -- Always assert an error.',
  156. ' assert(' + resultsVar +
  157. ' ~= nil, "Orphaned assert fail: " .. message)',
  158. ' table.insert(' + resultsVar +
  159. ', {success=false, log="Fail.", title=message})',
  160. 'end']);
  161. return functionName + '(' + message + ')\n';
  162. };
  163. Blockly.Lua['unittest_adjustindex'] = function(block) {
  164. var index = Blockly.Lua.valueToCode(block, 'INDEX',
  165. Blockly.Lua.ORDER_ADDITIVE) || '0';
  166. if (Blockly.isNumber(index)) {
  167. // If the index is a naked number, adjust it right now.
  168. return [parseFloat(index) + 1, Blockly.Lua.ORDER_ATOMIC];
  169. }
  170. // If the index is dynamic, adjust it in code.
  171. return [index + ' + 1', Blockly.Lua.ORDER_ATOMIC];
  172. };