fix(generators): Fix an operator precedence issue in the math_number_property generators to remove extra parentheses (#5685)

* Fixed issue where the mathIsPrime function inserted extra parenthesis around certain blocks.
* Added tests to generated JS
* Updated generated code in Lua
* Updated Generated code for tests in Dart
* Updated generated code for tests in PHP
* Updated generated code for tests in Python
* Also changed var to const and let.

Co-authored-by: jeremyjacob123 <43049656+jeremyjacob123@users.noreply.github.com>
Co-authored-by: LouisCatala <86700310+LouisCatala@users.noreply.github.com>
Co-authored-by: jeremyjacob123 <43049656+jeremyjacob123@users.noreply.github.com>
This commit is contained in:
Richard Doherty
2022-01-27 10:54:05 -05:00
committed by GitHub
parent f68043d3eb
commit a31003fab9
11 changed files with 427 additions and 258 deletions

View File

@@ -440,12 +440,16 @@ void test_number_properties() {
unittest_assertequals(42 % 2 == 0, true, 'even');
unittest_assertequals(42.1 % 2 == 1, false, 'odd');
unittest_assertequals(math_isPrime(5), true, 'prime 5');
unittest_assertequals(math_isPrime(5 + 2), true, 'prime 5 + 2 (extra parentheses)');
unittest_assertequals(math_isPrime(25), false, 'prime 25');
unittest_assertequals(math_isPrime(-31.1), false, 'prime negative');
unittest_assertequals(Math.pi % 1 == 0, false, 'whole');
unittest_assertequals(double.infinity > 0, true, 'positive');
unittest_assertequals(5 + 2 > 0, true, '5 + 2 is positive (extra parentheses)');
unittest_assertequals(-42 < 0, true, 'negative');
unittest_assertequals(3 + 2 < 0, false, '3 + 2 is negative (extra parentheses)');
unittest_assertequals(42 % 2 == 0, true, 'divisible');
unittest_assertequals(!false, true, 'divisible by 0');
}
/// Tests the "round" block.

View File

@@ -449,12 +449,16 @@ function test_number_properties() {
assertEquals(42 % 2 === 0, true, 'even');
assertEquals(42.1 % 2 === 1, false, 'odd');
assertEquals(mathIsPrime(5), true, 'prime 5');
assertEquals(mathIsPrime(5 + 2), true, 'prime 5 + 2 (extra parentheses)');
assertEquals(mathIsPrime(25), false, 'prime 25');
assertEquals(mathIsPrime(-31.1), false, 'prime negative');
assertEquals(Math.PI % 1 === 0, false, 'whole');
assertEquals(Infinity > 0, true, 'positive');
assertEquals(5 + 2 > 0, true, '5 + 2 is positive (extra parentheses)');
assertEquals(-42 < 0, true, 'negative');
assertEquals(3 + 2 < 0, false, '3 + 2 is negative (extra parentheses)');
assertEquals(42 % 2 === 0, true, 'divisible');
assertEquals(!(42 % 0 === 0), true, 'divisible by 0');
}
// Tests the "round" block.

View File

@@ -450,12 +450,16 @@ function test_number_properties()
assertEquals(42 % 2 == 0, true, 'even')
assertEquals(42.1 % 2 == 1, false, 'odd')
assertEquals(math_isPrime(5), true, 'prime 5')
assertEquals(math_isPrime(5 + 2), true, 'prime 5 + 2 (extra parentheses)')
assertEquals(math_isPrime(25), false, 'prime 25')
assertEquals(math_isPrime(-31.1), false, 'prime negative')
assertEquals(math.pi % 1 == 0, false, 'whole')
assertEquals(math.huge > 0, true, 'positive')
assertEquals(5 + 2 > 0, true, '5 + 2 is positive (extra parentheses)')
assertEquals(-42 < 0, true, 'negative')
assertEquals(3 + 2 < 0, false, '3 + 2 is negative (extra parentheses)')
assertEquals(42 % 2 == 0, true, 'divisible')
assertEquals(not nil, true, 'divisible by 0')
end

View File

@@ -444,12 +444,16 @@ function test_number_properties() {
assertEquals(42 % 2 == 0, true, 'even');
assertEquals(42.1 % 2 == 1, false, 'odd');
assertEquals(math_isPrime(5), true, 'prime 5');
assertEquals(math_isPrime(5 + 2), true, 'prime 5 + 2 (extra parentheses)');
assertEquals(math_isPrime(25), false, 'prime 25');
assertEquals(math_isPrime(-31.1), false, 'prime negative');
assertEquals(is_int(M_PI), false, 'whole');
assertEquals(INF > 0, true, 'positive');
assertEquals(5 + 2 > 0, true, '5 + 2 is positive (extra parentheses)');
assertEquals(-42 < 0, true, 'negative');
assertEquals(3 + 2 < 0, false, '3 + 2 is negative (extra parentheses)');
assertEquals(42 % 2 == 0, true, 'divisible');
assertEquals(!false, true, 'divisible by 0');
}
// Tests the "round" block.

View File

@@ -398,12 +398,16 @@ def test_number_properties():
assertEquals(42 % 2 == 0, True, 'even')
assertEquals(42.1 % 2 == 1, False, 'odd')
assertEquals(math_isPrime(5), True, 'prime 5')
assertEquals(math_isPrime(5 + 2), True, 'prime 5 + 2 (extra parentheses)')
assertEquals(math_isPrime(25), False, 'prime 25')
assertEquals(math_isPrime(-31.1), False, 'prime negative')
assertEquals(math.pi % 1 == 0, False, 'whole')
assertEquals(float('inf') > 0, True, 'positive')
assertEquals(5 + 2 > 0, True, '5 + 2 is positive (extra parentheses)')
assertEquals(-42 < 0, True, 'negative')
assertEquals(3 + 2 < 0, False, '3 + 2 is negative (extra parentheses)')
assertEquals(42 % 2 == 0, True, 'divisible')
assertEquals(not False, True, 'divisible by 0')
# Tests the "round" block.
def test_round():

View File

@@ -1,4 +1,8 @@
<xml xmlns="https://developers.google.com/blockly/xml">
<variables>
<variable>varToChange</variable>
<variable>rand</variable>
</variables>
<block type="unittest_main" x="13" y="13">
<field name="SUITE_NAME">Math</field>
<statement name="DO">
@@ -992,10 +996,10 @@
</value>
<next>
<block type="unittest_assertvalue" inline="false">
<field name="EXPECTED">FALSE</field>
<field name="EXPECTED">TRUE</field>
<value name="MESSAGE">
<block type="text">
<field name="TEXT">prime 25</field>
<field name="TEXT">prime 5 + 2 (extra parentheses)</field>
</block>
</value>
<value name="ACTUAL">
@@ -1003,8 +1007,18 @@
<mutation divisor_input="false"></mutation>
<field name="PROPERTY">PRIME</field>
<value name="NUMBER_TO_CHECK">
<block type="math_number">
<field name="NUM">25</field>
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
</block>
</value>
</block>
@@ -1014,7 +1028,7 @@
<field name="EXPECTED">FALSE</field>
<value name="MESSAGE">
<block type="text">
<field name="TEXT">prime negative</field>
<field name="TEXT">prime 25</field>
</block>
</value>
<value name="ACTUAL">
@@ -1023,7 +1037,7 @@
<field name="PROPERTY">PRIME</field>
<value name="NUMBER_TO_CHECK">
<block type="math_number">
<field name="NUM">-31.1</field>
<field name="NUM">25</field>
</block>
</value>
</block>
@@ -1033,35 +1047,35 @@
<field name="EXPECTED">FALSE</field>
<value name="MESSAGE">
<block type="text">
<field name="TEXT">whole</field>
<field name="TEXT">prime negative</field>
</block>
</value>
<value name="ACTUAL">
<block type="math_number_property">
<mutation divisor_input="false"></mutation>
<field name="PROPERTY">WHOLE</field>
<field name="PROPERTY">PRIME</field>
<value name="NUMBER_TO_CHECK">
<block type="math_constant">
<field name="CONSTANT">PI</field>
<block type="math_number">
<field name="NUM">-31.1</field>
</block>
</value>
</block>
</value>
<next>
<block type="unittest_assertvalue" inline="false">
<field name="EXPECTED">TRUE</field>
<field name="EXPECTED">FALSE</field>
<value name="MESSAGE">
<block type="text">
<field name="TEXT">positive</field>
<field name="TEXT">whole</field>
</block>
</value>
<value name="ACTUAL">
<block type="math_number_property">
<mutation divisor_input="false"></mutation>
<field name="PROPERTY">POSITIVE</field>
<field name="PROPERTY">WHOLE</field>
<value name="NUMBER_TO_CHECK">
<block type="math_constant">
<field name="CONSTANT">INFINITY</field>
<field name="CONSTANT">PI</field>
</block>
</value>
</block>
@@ -1071,16 +1085,16 @@
<field name="EXPECTED">TRUE</field>
<value name="MESSAGE">
<block type="text">
<field name="TEXT">negative</field>
<field name="TEXT">positive</field>
</block>
</value>
<value name="ACTUAL">
<block type="math_number_property">
<mutation divisor_input="false"></mutation>
<field name="PROPERTY">NEGATIVE</field>
<field name="PROPERTY">POSITIVE</field>
<value name="NUMBER_TO_CHECK">
<block type="math_number">
<field name="NUM">-42</field>
<block type="math_constant">
<field name="CONSTANT">INFINITY</field>
</block>
</value>
</block>
@@ -1090,25 +1104,138 @@
<field name="EXPECTED">TRUE</field>
<value name="MESSAGE">
<block type="text">
<field name="TEXT">divisible</field>
<field name="TEXT">5 + 2 is positive (extra parentheses)</field>
</block>
</value>
<value name="ACTUAL">
<block type="math_number_property">
<mutation divisor_input="true"></mutation>
<field name="PROPERTY">DIVISIBLE_BY</field>
<mutation divisor_input="false"></mutation>
<field name="PROPERTY">POSITIVE</field>
<value name="NUMBER_TO_CHECK">
<block type="math_number">
<field name="NUM">42</field>
</block>
</value>
<value name="DIVISOR">
<block type="math_number">
<field name="NUM">2</field>
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">5</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
</block>
</value>
</block>
</value>
<next>
<block type="unittest_assertvalue" inline="false">
<field name="EXPECTED">TRUE</field>
<value name="MESSAGE">
<block type="text">
<field name="TEXT">negative</field>
</block>
</value>
<value name="ACTUAL">
<block type="math_number_property">
<mutation divisor_input="false"></mutation>
<field name="PROPERTY">NEGATIVE</field>
<value name="NUMBER_TO_CHECK">
<block type="math_number">
<field name="NUM">-42</field>
</block>
</value>
</block>
</value>
<next>
<block type="unittest_assertvalue" inline="false">
<field name="EXPECTED">FALSE</field>
<value name="MESSAGE">
<block type="text">
<field name="TEXT">3 + 2 is negative (extra parentheses)</field>
</block>
</value>
<value name="ACTUAL">
<block type="math_number_property">
<mutation divisor_input="false"></mutation>
<field name="PROPERTY">NEGATIVE</field>
<value name="NUMBER_TO_CHECK">
<block type="math_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<block type="math_number">
<field name="NUM">3</field>
</block>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
</block>
</value>
</block>
</value>
<next>
<block type="unittest_assertvalue" inline="false">
<field name="EXPECTED">TRUE</field>
<value name="MESSAGE">
<block type="text">
<field name="TEXT">divisible</field>
</block>
</value>
<value name="ACTUAL">
<block type="math_number_property">
<mutation divisor_input="true"></mutation>
<field name="PROPERTY">DIVISIBLE_BY</field>
<value name="NUMBER_TO_CHECK">
<block type="math_number">
<field name="NUM">42</field>
</block>
</value>
<value name="DIVISOR">
<block type="math_number">
<field name="NUM">2</field>
</block>
</value>
</block>
</value>
<next>
<block type="unittest_assertvalue">
<field name="EXPECTED">TRUE</field>
<value name="MESSAGE">
<block type="text">
<field name="TEXT">divisible by 0</field>
</block>
</value>
<value name="ACTUAL">
<block type="logic_negate">
<value name="BOOL">
<block type="math_number_property">
<mutation divisor_input="true"></mutation>
<field name="PROPERTY">DIVISIBLE_BY</field>
<value name="NUMBER_TO_CHECK">
<block type="math_number">
<field name="NUM">42</field>
</block>
</value>
<value name="DIVISOR">
<block type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
@@ -1128,7 +1255,7 @@
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="13" y="3738">
<block type="procedures_defnoreturn" x="48" y="3799">
<field name="NAME">test round</field>
<comment pinned="false" h="80" w="160">Tests the "round" block.</comment>
<statement name="STACK">
@@ -1204,7 +1331,7 @@
</block>
</statement>
</block>
<block type="procedures_defnoreturn" x="13" y="4038">
<block type="procedures_defnoreturn" x="46" y="4064">
<field name="NAME">test change</field>
<comment pinned="false" h="80" w="160">Tests the "change" block.</comment>
<statement name="STACK">