diff --git a/tests/generators/golden/generated.dart b/tests/generators/golden/generated.dart index 8977cbf2c..2c45db3b4 100644 --- a/tests/generators/golden/generated.dart +++ b/tests/generators/golden/generated.dart @@ -937,6 +937,20 @@ void test_replace() { unittest_assertequals(''.replaceAll('a', 'chicken'), '', 'empty source'); } +/// Tests the "multiline" block. +void test_multiline() { + unittest_assertequals('', '', 'no text'); + unittest_assertequals('Google', 'Google', 'simple'); + unittest_assertequals('paragraph' + '\n' + + 'with newlines' + '\n' + + 'yup', 'paragraph' + '\n' + + 'with newlines' + '\n' + + 'yup', 'no compile error with newlines'); + unittest_assertequals(text_count('bark bark' + '\n' + + 'bark bark bark' + '\n' + + 'bark bark bark bark', 'bark'), 9, 'count with newlines'); +} + /// Checks that the number of calls is one in order /// to confirm that a function was only called once. void check_number_of_calls2(test_name) { @@ -1624,6 +1638,7 @@ main() { test_count_text(); test_text_reverse(); test_replace(); + test_multiline(); print(unittest_report()); unittestResults = null; diff --git a/tests/generators/golden/generated.js b/tests/generators/golden/generated.js index 7c6ee3502..d6361ae1a 100644 --- a/tests/generators/golden/generated.js +++ b/tests/generators/golden/generated.js @@ -916,6 +916,20 @@ function test_replace() { assertEquals(textReplace('', 'a', 'chicken'), '', 'empty source'); } +// Tests the "multiline" block. +function test_multiline() { + assertEquals('', '', 'no text'); + assertEquals('Google', 'Google', 'simple'); + assertEquals('paragraph' + '\n' + + 'with newlines' + '\n' + + 'yup', 'paragraph' + '\n' + + 'with newlines' + '\n' + + 'yup', 'no compile error with newlines'); + assertEquals(textCount('bark bark' + '\n' + + 'bark bark bark' + '\n' + + 'bark bark bark bark', 'bark'), 9, 'count with newlines'); +} + // Checks that the number of calls is one in order // to confirm that a function was only called once. function check_number_of_calls2(test_name) { @@ -1558,6 +1572,7 @@ test_trim(); test_count_text(); test_text_reverse(); test_replace(); +test_multiline(); console.log(unittest_report()); unittestResults = null; diff --git a/tests/generators/golden/generated.lua b/tests/generators/golden/generated.lua index d96ad64a9..2724c1d3b 100644 --- a/tests/generators/golden/generated.lua +++ b/tests/generators/golden/generated.lua @@ -1012,6 +1012,21 @@ function test_replace() end +-- Tests the "multiline" block. +function test_multiline() + assertEquals('', '', 'no text') + assertEquals('Google', 'Google', 'simple') + assertEquals('paragraph' .. '\n' .. + 'with newlines' .. '\n' .. + 'yup', 'paragraph' .. '\n' .. + 'with newlines' .. '\n' .. + 'yup', 'no compile error with newlines') + assertEquals(text_count('bark bark' .. '\n' .. + 'bark bark bark' .. '\n' .. + 'bark bark bark bark', 'bark'), 9, 'count with newlines') +end + + -- Checks that the number of calls is one in order -- to confirm that a function was only called once. function check_number_of_calls2(test_name) @@ -1828,6 +1843,7 @@ test_trim() test_count_text() test_text_reverse() test_replace() +test_multiline() print(unittest_report()) unittestResults = nil diff --git a/tests/generators/golden/generated.php b/tests/generators/golden/generated.php index 39cb098ac..8066edb69 100644 --- a/tests/generators/golden/generated.php +++ b/tests/generators/golden/generated.php @@ -925,6 +925,23 @@ function test_replace() { assertEquals(str_replace('a', 'chicken', ''), '', 'empty source'); } +// Tests the "multiline" block. +function test_multiline() { + global $test_name, $naked, $proc_x, $proc_y, $func_x, $func_y, $func_a, $n, $ok, $log, $count, $varToChange, $rand, $item, $text, $number_of_calls, $list2, $proc_z, $func_z, $x, $proc_w, $func_c, $if2, $i, $loglist, $changing_list, $list_copy, $unittestResults; + assertEquals('', '', 'no text'); + assertEquals('Google', 'Google', 'simple'); + assertEquals('paragraph' . "\n" . + 'with newlines' . "\n" . + 'yup', 'paragraph' . "\n" . + 'with newlines' . "\n" . + 'yup', 'no compile error with newlines'); + assertEquals(strlen('bark') === 0 ? strlen('bark bark' . "\n" . + 'bark bark bark' . "\n" . + 'bark bark bark bark') + 1 : substr_count('bark bark' . "\n" . + 'bark bark bark' . "\n" . + 'bark bark bark bark', 'bark'), 9, 'count with newlines'); +} + // Checks that the number of calls is one in order // to confirm that a function was only called once. function check_number_of_calls2($test_name) { @@ -1652,6 +1669,7 @@ test_trim(); test_count_text(); test_text_reverse(); test_replace(); +test_multiline(); print(unittest_report()); $unittestResults = null; diff --git a/tests/generators/golden/generated.py b/tests/generators/golden/generated.py index 04baa0ec3..42764d368 100644 --- a/tests/generators/golden/generated.py +++ b/tests/generators/golden/generated.py @@ -782,6 +782,20 @@ def test_replace(): assertEquals('aaaaa'.replace('a', ''), '', 'empty replacement 3') assertEquals(''.replace('a', 'chicken'), '', 'empty source') +# Tests the "multiline" block. +def test_multiline(): + global test_name, naked, proc_x, proc_y, func_x, func_y, func_a, n, ok, log, count, varToChange, rand, item, text, number_of_calls, list2, proc_z, func_z, x, proc_w, func_c, if2, i, loglist, changing_list, list_copy, unittestResults + assertEquals('', '', 'no text') + assertEquals('Google', 'Google', 'simple') + assertEquals('paragraph' + '\n' + + 'with newlines' + '\n' + + 'yup', 'paragraph' + '\n' + + 'with newlines' + '\n' + + 'yup', 'no compile error with newlines') + assertEquals(('bark bark' + '\n' + + 'bark bark bark' + '\n' + + 'bark bark bark bark').count('bark'), 9, 'count with newlines') + # Checks that the number of calls is one in order # to confirm that a function was only called once. def check_number_of_calls2(test_name): @@ -1396,6 +1410,7 @@ test_trim() test_count_text() test_text_reverse() test_replace() +test_multiline() print(unittest_report()) unittestResults = None diff --git a/tests/generators/index.html b/tests/generators/index.html index a7082e286..7ce86c724 100644 --- a/tests/generators/index.html +++ b/tests/generators/index.html @@ -362,6 +362,7 @@ h1 { + diff --git a/tests/generators/text.xml b/tests/generators/text.xml index 9f96175c6..d10593735 100644 --- a/tests/generators/text.xml +++ b/tests/generators/text.xml @@ -1,5 +1,5 @@ - + Text @@ -46,6 +46,11 @@ + + + + + @@ -77,7 +82,7 @@ - + @@ -113,7 +118,7 @@ - + test create text Tests the "create text with" block with varying number of inputs. @@ -322,7 +327,7 @@ - + get empty Creates an empty string for use with the empty test. @@ -332,7 +337,7 @@ - + test empty text Tests the "is empty" block". @@ -424,7 +429,7 @@ - + test text length Tests the "length" block. @@ -509,7 +514,7 @@ - + test append Tests the "append text" block with different types of parameters. @@ -642,7 +647,7 @@ - + test find text simple Tests the "find" block with a variable. @@ -755,7 +760,7 @@ - + get fruit Creates a string for use with the find test. @@ -774,7 +779,7 @@ - + test find text complex Tests the "find" block with a function call. @@ -1144,7 +1149,7 @@ - + test get text simple Tests the "get letter" block with a variable. @@ -1412,7 +1417,7 @@ - + get Blockly Creates a string for use with the get test. @@ -1431,7 +1436,7 @@ - + test get text complex Tests the "get letter" block with a function call. @@ -2074,7 +2079,7 @@ - + get numbers Creates a string for use with the substring test. @@ -2093,7 +2098,7 @@ - + test substring simple Tests the "get substring" block with a variable. @@ -2735,7 +2740,7 @@ - + test substring complex Tests the "get substring" block with a function call. @@ -3699,7 +3704,7 @@ - + test case Tests the "change casing" block. @@ -3913,7 +3918,7 @@ - + test trim Tests the "trim" block. @@ -4107,7 +4112,7 @@ - + test count text Tests the "trim" block. @@ -4317,7 +4322,7 @@ - + test text reverse Tests the "trim" block. @@ -4413,7 +4418,7 @@ - + test replace Tests the "trim" block. @@ -4648,4 +4653,93 @@ - + + test multiline + Tests the "multiline" block. + + + + + no text + + + + + + + + + + + + + + + + + simple + + + + + Google + + + + + Google + + + + + + + no compile error with newlines + + + + + paragraph with newlines yup + + + + + paragraph with newlines yup + + + + + + + count with newlines + + + + + + + bark + + + + + bark bark bark bark bark bark bark bark bark + + + + + + + 9 + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/mocha/field_multilineinput_test.js b/tests/mocha/field_multilineinput_test.js new file mode 100644 index 000000000..8e932c367 --- /dev/null +++ b/tests/mocha/field_multilineinput_test.js @@ -0,0 +1,150 @@ +/** + * @license + * Copyright 2020 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +suite('Multiline Input Fields', function() { + /** + * Configuration for field tests with invalid values. + * @type {!Array} + */ + var invalidValueTestCases = [ + {title: 'Undefined', value: undefined}, + {title: 'Null', value: null}, + ]; + /** + * Configuration for field tests with valid values. + * @type {!Array} + */ + var validValueTestCases = [ + {title: 'Empty string', value: '', expectedValue: ''}, + {title: 'String no newline', value: 'value', expectedValue: 'value'}, + {title: 'String with newline', value: 'bark bark\n bark bark bark\n bark bar bark bark\n', expectedValue: 'bark bark\n bark bark bark\n bark bar bark bark\n'}, + {title: 'Boolean true', value: true, expectedValue: 'true'}, + {title: 'Boolean false', value: false, expectedValue: 'false'}, + {title: 'Number (Truthy)', value: 1, expectedValue: '1'}, + {title: 'Number (Falsy)', value: 0, expectedValue: '0'}, + {title: 'NaN', value: NaN, expectedValue: 'NaN'}, + ]; + var addArgsAndJson = function(testCase) { + testCase.args = [testCase.value]; + testCase.json = {'text': testCase.value}; + }; + invalidValueTestCases.forEach(addArgsAndJson); + validValueTestCases.forEach(addArgsAndJson); + + /** + * The expected default value for the field being tested. + * @type {*} + */ + var defaultFieldValue = ''; + /** + * Asserts that the field property values are set to default. + * @param {!Blockly.FieldNumber} field The field to check. + */ + var assertFieldDefault = function(field) { + testHelpers.assertFieldValue(field, defaultFieldValue); + }; + /** + * Asserts that the field properties are correct based on the test case. + * @param {!Blockly.FieldNumber} field The field to check. + * @param {!FieldValueTestCase} testCase The test case. + */ + var validTestCaseAssertField = function(field, testCase) { + testHelpers.assertFieldValue(field, testCase.expectedValue); + }; + + testHelpers.runConstructorSuiteTests( + Blockly.FieldMultilineInput, validValueTestCases, invalidValueTestCases, + validTestCaseAssertField, assertFieldDefault); + + testHelpers.runFromJsonSuiteTests( + Blockly.FieldMultilineInput, validValueTestCases,invalidValueTestCases, + validTestCaseAssertField, assertFieldDefault); + + suite('setValue', function() { + suite('Empty -> New Value', function() { + setup(function() { + this.field = new Blockly.FieldMultilineInput(); + }); + testHelpers.runSetValueTests( + validValueTestCases, invalidValueTestCases, defaultFieldValue); + test('With source block', function() { + this.field.setSourceBlock(createTestBlock()); + this.field.setValue('value'); + testHelpers.assertFieldValue(this.field, 'value'); + }); + }); + suite('Value -> New Value', function() { + var initialValue = 'oldValue'; + setup(function() { + this.field = new Blockly.FieldMultilineInput(initialValue); + }); + testHelpers.runSetValueTests( + validValueTestCases, invalidValueTestCases, initialValue); + test('With source block', function() { + this.field.setSourceBlock(createTestBlock()); + this.field.setValue('value'); + testHelpers.assertFieldValue(this.field, 'value'); + }); + }); + }); + + suite('blockToCode', function() { + setup(function() { + this.workspace = new Blockly.Workspace(); + }); + const createBlockFn = (value) => { + return (workspace) => { + var block = workspace.newBlock('text_multiline'); + var textField = block.getField('TEXT'); + textField.setValue(value); + return block; + }; + }; + + /** + * Test suites for code generation tests.s + * @type {Array} + */ + const testSuites = [ + {title: 'Dart', generator: Blockly.Dart, + testCases: [ + {title: 'Empty string', expectedCode: '\'\'', + createBlock: createBlockFn('')}, + {title: 'String with newline', expectedCode: '\'bark bark\' + \'\\n\' + \n\' bark bark bark\' + \'\\n\' + \n\' bark bar bark bark\' + \'\\n\' + \n\'\'', + createBlock: createBlockFn('bark bark\n bark bark bark\n bark bar bark bark\n')}, + ]}, + {title: 'JavaScript', generator: Blockly.JavaScript, + testCases: [ + {title: 'Empty string', expectedCode: '\'\'', + createBlock: createBlockFn('')}, + {title: 'String with newline', expectedCode: '\'bark bark\' + \'\\n\' +\n\' bark bark bark\' + \'\\n\' +\n\' bark bar bark bark\' + \'\\n\' +\n\'\'', + createBlock: createBlockFn('bark bark\n bark bark bark\n bark bar bark bark\n')}, + ]}, + {title: 'Lua', generator: Blockly.Lua, + testCases: [ + {title: 'Empty string', expectedCode: '\'\'', + createBlock: createBlockFn('')}, + {title: 'String with newline', expectedCode: '\'bark bark\' .. \'\\n\' ..\n\' bark bark bark\' .. \'\\n\' ..\n\' bark bar bark bark\' .. \'\\n\' ..\n\'\'', + createBlock: createBlockFn('bark bark\n bark bark bark\n bark bar bark bark\n')}, + ]}, + {title: 'PHP', generator: Blockly.PHP, + testCases: [ + {title: 'Empty string', expectedCode: '\'\'', + createBlock: createBlockFn('')}, + {title: 'String with newline', expectedCode: '\'bark bark\' . "\\n" .\n\' bark bark bark\' . "\\n" .\n\' bark bar bark bark\' . "\\n" .\n\'\'', + createBlock: createBlockFn('bark bark\n bark bark bark\n bark bar bark bark\n')}, + ]}, + {title: 'Python', generator: Blockly.Python, + testCases: [ + {title: 'Empty string', expectedCode: '\'\'', + createBlock: createBlockFn('')}, + {title: 'String with newline', expectedCode: '\'bark bark\' + \'\\n\' + \n\' bark bark bark\' + \'\\n\' + \n\' bark bar bark bark\' + \'\\n\' + \n\'\'', + createBlock: createBlockFn('bark bark\n bark bark bark\n bark bar bark bark\n')}, + ]}, + ]; + testHelpers.runCodeGenerationTestSuites(testSuites); + }); +}); diff --git a/tests/mocha/index.html b/tests/mocha/index.html index d144f4865..9266a4179 100644 --- a/tests/mocha/index.html +++ b/tests/mocha/index.html @@ -11,6 +11,13 @@ + + + + + + + @@ -63,6 +70,7 @@ +