123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- 'use strict';
- Blockly.Dart['unittest_main'] = function(block) {
-
- var resultsVar = Blockly.Dart.variableDB_.getName('unittestResults',
- Blockly.Variables.NAME_TYPE);
- var functionName = Blockly.Dart.provideFunction_(
- 'unittest_report',
- [ 'String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '() {',
- ' // Create test report.',
- ' List report = [];',
- ' StringBuffer summary = new StringBuffer();',
- ' int fails = 0;',
- ' for (int x = 0; x < ' + resultsVar + '.length; x++) {',
- ' if (' + resultsVar + '[x][0]) {',
- ' summary.write(".");',
- ' } else {',
- ' summary.write("F");',
- ' fails++;',
- ' report.add("");',
- ' report.add("FAIL: ${' + resultsVar + '[x][2]}");',
- ' report.add(' + resultsVar + '[x][1]);',
- ' }',
- ' }',
- ' report.insert(0, summary.toString());',
- ' report.add("");',
- ' report.add("Ran ${' + resultsVar + '.length} tests.");',
- ' report.add("");',
- ' if (fails != 0) {',
- ' report.add("FAILED (failures=$fails)");',
- ' } else {',
- ' report.add("OK");',
- ' }',
- ' return report.join("\\n");',
- '}']);
-
- var code = resultsVar + ' = [];\n';
-
- code += Blockly.Dart.statementToCode(block, 'DO')
- .replace(/^ /, '').replace(/\n /g, '\n');
- var reportVar = Blockly.Dart.variableDB_.getDistinctName(
- 'report', Blockly.Variables.NAME_TYPE);
- code += 'String ' + reportVar + ' = ' + functionName + '();\n';
-
- code += resultsVar + ' = null;\n';
-
- code += 'print(' + reportVar + ');\n';
- return code;
- };
- Blockly.Dart['unittest_main'].defineAssert_ = function() {
- var resultsVar = Blockly.Dart.variableDB_.getName('unittestResults',
- Blockly.Variables.NAME_TYPE);
- var functionName = Blockly.Dart.provideFunction_(
- 'unittest_assertequals',
- [ 'void ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
- '(dynamic actual, dynamic expected, String message) {',
- ' // Asserts that a value equals another value.',
- ' if (' + resultsVar + ' == null) {',
- ' throw "Orphaned assert: ${message}";',
- ' }',
- ' bool equals(a, b) {',
- ' if (a == b) {',
- ' return true;',
- ' } else if (a is List && b is List) {',
- ' if (a.length != b.length) {',
- ' return false;',
- ' }',
- ' for (num i = 0; i < a.length; i++) {',
- ' if (!equals(a[i], b[i])) {',
- ' return false;',
- ' }',
- ' }',
- ' return true;',
- ' }',
- ' return false;',
- ' }',
- ' if (equals(actual, expected)) {',
- ' ' + resultsVar + '.add([true, "OK", message]);',
- ' } else {',
- ' ' + resultsVar + '.add([false, ' +
- '"Expected: $expected\\nActual: $actual", message]);',
- ' }',
- '}']);
- return functionName;
- };
- Blockly.Dart['unittest_assertequals'] = function(block) {
-
- var message = Blockly.Dart.valueToCode(block, 'MESSAGE',
- Blockly.Dart.ORDER_NONE) || '';
- var actual = Blockly.Dart.valueToCode(block, 'ACTUAL',
- Blockly.Dart.ORDER_NONE) || 'null';
- var expected = Blockly.Dart.valueToCode(block, 'EXPECTED',
- Blockly.Dart.ORDER_NONE) || 'null';
- return Blockly.Dart['unittest_main'].defineAssert_() +
- '(' + actual + ', ' + expected + ', ' + message + ');\n';
- };
- Blockly.Dart['unittest_assertvalue'] = function(block) {
-
- var message = Blockly.Dart.valueToCode(block, 'MESSAGE',
- Blockly.Dart.ORDER_NONE) || '';
- var actual = Blockly.Dart.valueToCode(block, 'ACTUAL',
- Blockly.Dart.ORDER_NONE) || 'null';
- var expected = block.getFieldValue('EXPECTED');
- if (expected == 'TRUE') {
- expected = 'true';
- } else if (expected == 'FALSE') {
- expected = 'false';
- } else if (expected == 'NULL') {
- expected = 'null';
- }
- return Blockly.Dart['unittest_main'].defineAssert_() +
- '(' + actual + ', ' + expected + ', ' + message + ');\n';
- };
- Blockly.Dart['unittest_fail'] = function(block) {
-
- var resultsVar = Blockly.Dart.variableDB_.getName('unittestResults',
- Blockly.Variables.NAME_TYPE);
- var message = Blockly.Dart.valueToCode(block, 'MESSAGE',
- Blockly.Dart.ORDER_NONE) || '';
- var functionName = Blockly.Dart.provideFunction_(
- 'unittest_fail',
- [ 'void ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
- '(String message) {',
- ' // Always assert an error.',
- ' if (' + resultsVar + ' == null) {',
- ' throw "Orphaned assert fail: ${message}";',
- ' }',
- ' ' + resultsVar + '.add([false, "Fail.", message]);',
- '}']);
- return functionName + '(' + message + ');\n';
- };
- Blockly.Dart['unittest_adjustindex'] = function(block) {
- var index = Blockly.Dart.valueToCode(block, 'INDEX',
- Blockly.Dart.ORDER_ADDITIVE) || '0';
-
- if (block.workspace.options.oneBasedIndex) {
- if (Blockly.isNumber(index)) {
-
- return [parseFloat(index) + 1, Blockly.Dart.ORDER_ATOMIC];
- } else {
-
- index = index + ' + 1';
- }
- } else if (Blockly.isNumber(index)) {
- return [index, Blockly.Dart.ORDER_ATOMIC];
- }
- return [index, Blockly.Dart.ORDER_ADDITIVE];
- };
|