diff --git a/grammars/level12-Additions.lark b/grammars/level12-Additions.lark index a348bd7532f..fa346489852 100644 --- a/grammars/level12-Additions.lark +++ b/grammars/level12-Additions.lark @@ -5,7 +5,7 @@ assign_list: var (_IS| _EQUALS) (text_in_quotes|NUMBER) (_COMMA (text_in_quotes| ?atom: NUMBER | _MINUS NUMBER | var_access | text_in_quotes //unsupported numbers are gone, we can now allow floats with NUMBER ?print_expression: var_access_print | error_unsupported_number | NUMBER -equality_check: (list_access | var_access | text_in_quotes | NUMBER) (_IS| _EQUALS) (list_access | var_access | text_in_quotes | NUMBER) +equality_check: (list_access | var_access | text_in_quotes | NUMBER) (_IS| _EQUALS) (list_access | var_access | text_in_quotes | NUMBER) _SPACE? in_list_check: (list_access | var_access | text_in_quotes | NUMBER) _IN var_access sleep: _SLEEP (NUMBER | list_access | var_access | expression)? diff --git a/grammars/level14-Additions.lark b/grammars/level14-Additions.lark index 4926acc7c5e..c3c2049fbe2 100644 --- a/grammars/level14-Additions.lark +++ b/grammars/level14-Additions.lark @@ -2,12 +2,12 @@ condition:+= equality_check_dequals | smaller | bigger | not_equal | smaller_equal | bigger_equal -equality_check: comparison_arg (_IS | _EQUALS) comparison_arg -equality_check_dequals: comparison_arg _DOUBLE_EQUALS comparison_arg -smaller: comparison_arg _SMALLER comparison_arg -bigger: comparison_arg _LARGER comparison_arg -smaller_equal: comparison_arg _SMALLER_EQUALS comparison_arg -bigger_equal: comparison_arg _LARGER_EQUALS comparison_arg -not_equal: comparison_arg _NOT_EQUALS comparison_arg +equality_check: comparison_arg (_IS | _EQUALS) comparison_arg _SPACE? +equality_check_dequals: comparison_arg _DOUBLE_EQUALS comparison_arg _SPACE? +smaller: comparison_arg _SMALLER comparison_arg _SPACE? +bigger: comparison_arg _LARGER comparison_arg _SPACE? +smaller_equal: comparison_arg _SMALLER_EQUALS comparison_arg _SPACE? +bigger_equal: comparison_arg _LARGER_EQUALS comparison_arg _SPACE? +not_equal: comparison_arg _NOT_EQUALS comparison_arg _SPACE? ?comparison_arg: call | var_access | list_access | text_in_quotes | NUMBER diff --git a/grammars/level16-Additions.lark b/grammars/level16-Additions.lark index 0417c973c38..befa03a0679 100644 --- a/grammars/level16-Additions.lark +++ b/grammars/level16-Additions.lark @@ -10,7 +10,7 @@ error_assign_list_missing_brackets: var (_IS | _EQUALS) (_LEFT_SQUARE_BRACKET)? error_list_access_at -_print_ask_argument: (_SPACE | quoted_text | list_access | error_list_access_at | expression | print_expression)* +_print_ask_argument.1: (_SPACE | quoted_text | list_access | error_list_access_at | expression | print_expression)* assign: var (_IS| _EQUALS) (list_access | error_list_access_at | boolean | atom | expression) error_print_no_quotes: _PRINT (textwithoutspaces | list_access | error_list_access_at | var_access) (_SPACE (textwithoutspaces | list_access | var_access))* -> error_print_nq sleep: _SLEEP (INT | list_access | error_list_access_at | var_access | expression)? diff --git a/grammars/level4-Additions.lark b/grammars/level4-Additions.lark index d616c78b87e..ae01f5fdd77 100644 --- a/grammars/level4-Additions.lark +++ b/grammars/level4-Additions.lark @@ -1,7 +1,7 @@ // redefining it entirely since it has many order-depending rules (e.g ask_no_quotes should be after ask and before assign) command: clear | error_non_decimal | print | ask | play | turtle | assign_list | add | remove | error_add_missing_to | error_remove_missing_from | sleep | error_list_access | error_ask_no_quotes| assign | error_invalid_space | error_print_no_quotes | error_print_one_quote_only | error_text_no_print |error_invalid | empty_line -_print_ask_argument: (_SPACE | list_access | error_list_access | quoted_text | var_access_print)* +_print_ask_argument.1: (_SPACE | list_access | error_list_access | quoted_text | var_access_print)* error_print_no_quotes: _PRINT text -> error_print_nq error_ask_no_quotes: var _IS _ASK text -> error_print_nq diff --git a/grammars/level5-Additions.lark b/grammars/level5-Additions.lark index 98a7e0cbfe1..713532081ed 100644 --- a/grammars/level5-Additions.lark +++ b/grammars/level5-Additions.lark @@ -18,7 +18,7 @@ assign_button: NAME _IS _BUTTON assign_list: var _IS textwithspaces (_COMMA textwithspaces)+ assign: var _IS (list_access | textwithspaces) -error_print_no_quotes: _PRINT (textwithoutspaces | list_access | var_access) (_SPACE (textwithoutspaces | list_access | var_access))* -> error_print_nq +error_print_no_quotes: _PRINT (textwithoutspaces | list_access | var_access) (_SPACE (textwithoutspaces | list_access | var_access))* -> error_print_nq // new commands for level 5 if_pressed_else: _IF (LETTER_OR_NUMERAL | var) _IS _PRESSED _EOL* _if_less_command (_SPACE+ _EOL* | _SPACE* _EOL+) _ELSE (_SPACE+ _EOL* | _SPACE* _EOL+) _if_less_command diff --git a/grammars/level6-Additions.lark b/grammars/level6-Additions.lark index effa407423e..9890d081414 100644 --- a/grammars/level6-Additions.lark +++ b/grammars/level6-Additions.lark @@ -1,4 +1,4 @@ -_print_ask_argument: (_SPACE | quoted_text | list_access | expression | print_expression)* +_print_ask_argument.1: (_SPACE | quoted_text | list_access | expression | print_expression)* ?print_expression: var_access_print | error_unsupported_number | INT // redefining it entirely since it has many order-depending rules diff --git a/grammars/level8-Additions.lark b/grammars/level8-Additions.lark index c5b743bd167..c3135c2fa2f 100644 --- a/grammars/level8-Additions.lark +++ b/grammars/level8-Additions.lark @@ -5,7 +5,7 @@ error_repeat_dep_8 // FH jan 2022 (#1817) we can allow unquoted rhs's again cause there now is a line break after // not sure of we want it, but we could -equality_check: (textwithoutspaces | NUMBER) (_IS | _EQUALS) (textwithoutspaces | NUMBER) (_SPACE | textwithoutspaces | NUMBER)* +equality_check: (textwithoutspaces | NUMBER) (_IS | _EQUALS) (textwithoutspaces | NUMBER | textwithspaces) // from level 8 on if_pressed is implemented slightly differently if_pressed: _IF (LETTER_OR_NUMERAL | NAME) _IS _PRESSED _EOL (_SPACE command) (_EOL _SPACE command)* _EOL? _END_BLOCK diff --git a/grammars/terminals.lark b/grammars/terminals.lark index e5e8db00027..be84af4af90 100644 --- a/grammars/terminals.lark +++ b/grammars/terminals.lark @@ -1,5 +1,8 @@ -// A comment has a negative lookahead to ensure it cannot parse _END_BLOCKS -COMMENT: /#(?!ENDBLOCK)[^\n]*/ +// If the comment takes up the whole line, it should also encompass its leading newline. In this way the whole +// comment line can be skipped for the commands containing it. Comments should escape ENDBLOCKS not only in the start +// but at any point, because if the body of a command (e.g. `if`) ends up with a comment, the ENDBLOCK would appear +// right after it. Therefore, the ENDBLOCK should not be counted as a part of the comment. +COMMENT: /[\n ]*\#(?!ENDBLOCK)(?:[^\n#]|#(?!ENDBLOCK))*/ %ignore COMMENT // Internal symbol added by the preprocess_blocks function to indicate the end of blocks diff --git a/hedy.py b/hedy.py index 4a2eddbfaf8..42228d80c32 100644 --- a/hedy.py +++ b/hedy.py @@ -2112,7 +2112,7 @@ def condition_spaces(self, meta, args): def equality_check(self, meta, args): arg0 = self.process_variable(args[0], meta.line) - arg1 = ' '.join([self.process_variable(a) for a in args[1:]]) + arg1 = self.process_variable(str(args[1]).strip(), meta.line) return f"{arg0} == {arg1}" # TODO, FH 2021: zelfde change moet ik ook nog ff maken voor equal. check in hogere levels @@ -2227,8 +2227,7 @@ def print_ask_args(self, meta, args): def equality_check(self, meta, args): arg0 = self.process_variable(args[0], meta.line) - remaining_text = ' '.join(args[1:]) - arg1 = self.process_variable(remaining_text, meta.line) + arg1 = self.process_variable(str(args[1]).strip(), meta.line) # FH, 2022 this used to be str but convert_numerals in needed to accept non-latin numbers # and works exactly as str for latin numbers (i.e. does nothing on str, makes 3 into '3') @@ -3413,6 +3412,12 @@ def preprocess_blocks(code, level, lang): processed_code.append('') continue + # ignore lines that contain only a comment + comment_reg_ex = r' *\#[^\n]*' + if regex.fullmatch(comment_reg_ex, line): + processed_code.append(line) + continue + # first encounter sets indent size for this program if not indent_size_adapted and leading_spaces > 0: indent_size = leading_spaces diff --git a/tests/test_level/test_level_04.py b/tests/test_level/test_level_04.py index 6acbd9c81e9..71f1ea0a8e3 100644 --- a/tests/test_level/test_level_04.py +++ b/tests/test_level/test_level_04.py @@ -218,6 +218,12 @@ def test_print_asterisk(self): self.multi_level_tester(code=code, expected=expected, max_level=11) + def test_print_hash(self): + code = "print 'comments start with the # sign'" + expected = "print(f'comments start with the # sign')" + + self.multi_level_tester(code=code, expected=expected, max_level=11) + def test_print_without_quotes_gives_error_from_grammar(self): # in some cases, there is no variable confusion since 'hedy 123' can't be a variable # then we can immediately raise the no quoted exception diff --git a/tests/test_level/test_level_05.py b/tests/test_level/test_level_05.py index cf8caa58e60..91d3d92b363 100644 --- a/tests/test_level/test_level_05.py +++ b/tests/test_level/test_level_05.py @@ -40,9 +40,10 @@ def test_if_equality_linebreak_print(self, hedy, python): self.single_level_tester(code=code, expected=expected, unused_allowed=True) def test_if_equality_trailing_space_linebreak_print(self): - code = textwrap.dedent("""\ + value = 'trailing_space ' + code = textwrap.dedent(f"""\ naam is James - if naam is trailing_space + if naam is {value} print 'shaken'""") expected = textwrap.dedent("""\ @@ -66,9 +67,10 @@ def test_if_equality_unquoted_rhs_with_space_linebreak_print(self): self.multi_level_tester(code=code, expected=expected, max_level=7) def test_if_equality_unquoted_rhs_with_space_and_trailing_space_linebreak_print(self): - code = textwrap.dedent("""\ + value = 'trailing space ' + code = textwrap.dedent(f"""\ naam is James - if naam is trailing space + if naam is {value} print 'shaken'""") expected = textwrap.dedent("""\ @@ -416,10 +418,10 @@ def test_if_else_followed_by_print(self): self.single_level_tester(code=code, expected=expected) def test_if_equality_trailing_space_linebreak_print_else(self): - # this code has a space at the end of line 2 - code = textwrap.dedent("""\ + value = 'trailing space ' + code = textwrap.dedent(f"""\ naam is James - if naam is trailing space + if naam is {value} print 'shaken' else print 'biertje!'""") expected = textwrap.dedent("""\ diff --git a/tests/test_level/test_level_06.py b/tests/test_level/test_level_06.py index e247ba60f24..1aa77b1303d 100644 --- a/tests/test_level/test_level_06.py +++ b/tests/test_level/test_level_06.py @@ -176,12 +176,53 @@ def test_if_equality_linebreak_print(self): if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'): print(f'leuk')""") - self.multi_level_tester(max_level=7, code=code, expected=expected) + self.multi_level_tester(max_level=7, code=code, expected=expected, output='leuk') - def test_if_equality_trailing_space_linebreak_print(self): + def test_if_equality_linebreak_comment_print(self): + code = textwrap.dedent("""\ + naam is Hedy + if naam is Hedy + # this linebreak is allowed + print 'leuk'""") + + expected = textwrap.dedent("""\ + naam = 'Hedy' + if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'): + print(f'leuk')""") + + self.multi_level_tester(max_level=7, code=code, expected=expected, output='leuk') + + def test_if_equality_comment_linebreak_print(self): + code = textwrap.dedent("""\ + naam is Hedy + if naam is Hedy # this linebreak is allowed + print 'leuk'""") + + expected = textwrap.dedent("""\ + naam = 'Hedy' + if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'): + print(f'leuk')""") + + self.multi_level_tester(max_level=7, code=code, expected=expected, output='leuk') + + def test_if_equality_linebreak_print_comment(self): code = textwrap.dedent("""\ + naam is Hedy + if naam is Hedy + print 'leuk' # this linebreak is allowed""") + + expected = textwrap.dedent("""\ + naam = 'Hedy' + if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'): + print(f'leuk')""") + + self.multi_level_tester(max_level=7, code=code, expected=expected, output='leuk') + + def test_if_equality_trailing_space_linebreak_print(self): + value = 'trailing_space ' + code = textwrap.dedent(f"""\ naam is James - if naam is trailing_space + if naam is {value} print 'shaken'""") expected = textwrap.dedent("""\ diff --git a/tests/test_level/test_level_08.py b/tests/test_level/test_level_08.py index 08d7918fc61..3cf24cea4c3 100644 --- a/tests/test_level/test_level_08.py +++ b/tests/test_level/test_level_08.py @@ -87,10 +87,51 @@ def test_if_equality_with_equals_sign(self): self.multi_level_tester(code=code, expected=expected, max_level=11) - def test_if_equality_trailing_space_linebreak_print(self): + def test_if_equality_linebreak_comment_print(self): + code = textwrap.dedent("""\ + naam is Hedy + if naam is Hedy + # comment + print 'hedy'""") + + expected = textwrap.dedent("""\ + naam = 'Hedy' + if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'): + print(f'hedy')""") + + self.multi_level_tester(code=code, expected=expected, max_level=11) + + def test_if_equality_comment_linebreak_print(self): code = textwrap.dedent("""\ + naam is Hedy + if naam is Hedy # this linebreak is allowed + print 'leuk'""") + + expected = textwrap.dedent("""\ + naam = 'Hedy' + if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'): + print(f'leuk')""") + + self.multi_level_tester(max_level=11, code=code, expected=expected, output='leuk') + + def test_if_equality_linebreak_print_comment(self): + code = textwrap.dedent("""\ + naam is Hedy + if naam is Hedy + print 'leuk' # this linebreak is allowed""") + + expected = textwrap.dedent("""\ + naam = 'Hedy' + if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'): + print(f'leuk')""") + + self.multi_level_tester(max_level=11, code=code, expected=expected, output='leuk') + + def test_if_equality_trailing_space_linebreak_print(self): + value = 'trailing_space ' + code = textwrap.dedent(f"""\ naam is James - if naam is trailing_space + if naam is {value} print 'shaken'""") expected = textwrap.dedent("""\ @@ -113,10 +154,24 @@ def test_if_equality_unquoted_rhs_with_space(self): self.multi_level_tester(code=code, expected=expected, max_level=11) - def test_if_equality_unquoted_rhs_with_space_and_trailing_space_linebreak_print(self): + def test_if_equality_unquoted_rhs_with_multi_space(self): code = textwrap.dedent("""\ + v is three spaces + if v is three spaces + print '3'""") + + expected = textwrap.dedent("""\ + v = 'three spaces' + if convert_numerals('Latin', v) == convert_numerals('Latin', 'three spaces'): + print(f'3')""") + + self.multi_level_tester(code=code, expected=expected, max_level=11, output='3') + + def test_if_equality_unquoted_rhs_with_space_and_trailing_space_linebreak_print(self): + value = 'trailing space ' + code = textwrap.dedent(f"""\ naam is James - if naam is trailing space + if naam is {value} print 'shaken'""") expected = textwrap.dedent("""\ @@ -572,13 +627,13 @@ def test_repeat_no_indentation(self): def test_repeat_repair_too_few_indents(self): code = textwrap.dedent("""\ repeat 5 times - print('repair') - print('me')""") + print 'repair' + print 'me'""") fixed_code = textwrap.dedent("""\ repeat 5 times - print('repair') - print('me')""") + print 'repair' + print 'me'""") self.single_level_tester( code=code, @@ -589,29 +644,94 @@ def test_repeat_repair_too_few_indents(self): def test_repeat_repair_too_many_indents(self): code = textwrap.dedent("""\ repeat 5 times - print('repair') - print('me')""") + print 'repair' + print 'me'""") fixed_code = textwrap.dedent("""\ repeat 5 times - print('repair') - print('me')""") + print 'repair' + print 'me'""") self.single_level_tester( code=code, exception=hedy.exceptions.TooManyIndentsStartLevelException, - extra_check_function=(lambda x: x.exception.fixed_code == fixed_code) + extra_check_function=(lambda x: x.exception.fixed_code == fixed_code), + skip_faulty=False ) def test_unexpected_indent(self): code = textwrap.dedent("""\ - print('repair') - print('me')""") + print 'repair' + print 'me'""") self.single_level_tester( code=code, exception=hedy.exceptions.TooManyIndentsStartLevelException ) + def test_repeat_empty_lines(self): + code = textwrap.dedent("""\ + repeat 2 times + + + sleep""") + + expected = textwrap.dedent(f"""\ + for __i in range({self.int_cast_transpiled(2)}): + time.sleep(1) + time.sleep(0.1)""") + + self.multi_level_tester(code=code, expected=expected) + + def test_repeat_with_comment(self): + code = textwrap.dedent("""\ + repeat 5 times #This should be ignored + sleep""") + + expected = textwrap.dedent(f"""\ + for __i in range({self.int_cast_transpiled(5)}): + time.sleep(1) + time.sleep(0.1)""") + + self.multi_level_tester(code=code, expected=expected) + + def test_repeat_inner_whole_line_comment(self): + code = textwrap.dedent("""\ + repeat 2 times + # let's sleep! + sleep""") + + expected = textwrap.dedent(f"""\ + for __i in range({self.int_cast_transpiled(2)}): + time.sleep(1) + time.sleep(0.1)""") + + self.multi_level_tester(code=code, expected=expected) + + def test_repeat_end_line_comment(self): + code = textwrap.dedent("""\ + repeat 2 times + sleep # let's print!""") + + expected = textwrap.dedent(f"""\ + for __i in range({self.int_cast_transpiled(2)}): + time.sleep(1) + time.sleep(0.1)""") + + self.multi_level_tester(code=code, expected=expected) + + def test_repeat_whole_line_comment_after(self): + code = textwrap.dedent("""\ + repeat 2 times + sleep + # let's print!""") + + expected = textwrap.dedent(f"""\ + for __i in range({self.int_cast_transpiled(2)}): + time.sleep(1) + time.sleep(0.1)""") + + self.multi_level_tester(code=code, expected=expected) + def test_repeat_turtle(self): code = textwrap.dedent("""\ repeat 3 times @@ -742,20 +862,6 @@ def test_repeat_print_assign_addition(self): self.multi_level_tester(code=code, expected=expected, max_level=11) - def test_repeat_with_comment(self): - code = textwrap.dedent("""\ - repeat 5 times #This should be ignored - print 'koekoek' - print 'koekoek'""") - - expected = textwrap.dedent(f"""\ - for __i in range({self.int_cast_transpiled(5)}): - print(f'koekoek') - print(f'koekoek') - time.sleep(0.1)""") - - self.multi_level_tester(code=code, expected=expected, max_level=11) - def test_repeat_with_string_variable_gives_type_error(self): code = textwrap.dedent("""\ n is 'test' diff --git a/tests/test_level/test_level_09.py b/tests/test_level/test_level_09.py index 858607608b5..64ecca14649 100644 --- a/tests/test_level/test_level_09.py +++ b/tests/test_level/test_level_09.py @@ -165,6 +165,22 @@ def test_repeat_nested_in_repeat(self): self.multi_level_tester(code=code, expected=expected, max_level=11) + def test_repeat_nested_in_repeat_with_comments(self): + code = textwrap.dedent("""\ + repeat 2 times # test + repeat 2 times # test + # test + sleep # test + # test""") + + expected = textwrap.dedent(f"""\ + for __i in range({self.int_cast_transpiled(2)}): + for __i in range({self.int_cast_transpiled(2)}): + time.sleep(1) + time.sleep(0.1)""") + + self.multi_level_tester(code=code, expected=expected) + def test_repeat_nested_multi_commands(self): code = textwrap.dedent("""\ repeat 3 times diff --git a/tests/test_level/test_level_12.py b/tests/test_level/test_level_12.py index a10e0fe6d8b..c8b5d6bae89 100644 --- a/tests/test_level/test_level_12.py +++ b/tests/test_level/test_level_12.py @@ -332,6 +332,12 @@ def test_print_asterisk(self): self.multi_level_tester(code=code, expected=expected, max_level=17) + def test_print_hash(self): + code = "print 'comments start with the # sign'" + expected = "print(f'''comments start with the # sign''')" + + self.multi_level_tester(code=code, expected=expected, max_level=17) + def test_print_single_quoted_text_var(self): code = textwrap.dedent("""\ naam is "'Hedy'" @@ -1339,19 +1345,6 @@ def test_if_2_vars_equality_print(self): self.multi_level_tester(max_level=16, code=code, expected=expected, output='gelijkspel!') - # def test_if_equality_trailing_space_linebreak_print(self): - # code = textwrap.dedent("""\ - # naam is 'James' - # if naam is 'trailing_space' - # print 'shaken'""") - # - # expected = textwrap.dedent("""\ - # naam = 'James' - # if convert_numerals('Latin', naam) == convert_numerals('Latin', 'trailing_space'): - # print(f'''shaken''')""") - # - # self.multi_level_tester(max_level=18, code=code, expected=expected) - # Lists can be compared for equality starting with level 14 def test_if_equality_lists(self): code = textwrap.dedent("""\ @@ -1582,11 +1575,12 @@ def test_if_else_followed_by_print(self): self.multi_level_tester(code=code, expected=expected, max_level=16) def test_if_else_trailing_space_after_else(self): - code = textwrap.dedent("""\ + else_with_space = 'else ' + code = textwrap.dedent(f"""\ a is 1 if a is 1 print a - else + {else_with_space} print 'nee'""") expected = textwrap.dedent("""\ @@ -1688,17 +1682,59 @@ def test_repeat_print_assign_addition(self): self.multi_level_tester(code=code, expected=expected, max_level=17) - def test_repeat_with_comment(self): + def test_if_equality_linebreak_comment_print(self): code = textwrap.dedent("""\ - repeat 5 times #This should be ignored - sleep""") + naam is 'Hedy' + if naam is 'Hedy' + # comment + print 'hedy'""") - expected = textwrap.dedent(f"""\ - for __i in range({self.int_cast_transpiled(5)}): - time.sleep(1) - time.sleep(0.1)""") + expected = textwrap.dedent("""\ + naam = 'Hedy' + if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'): + print(f'''hedy''')""") - self.multi_level_tester(code=code, expected=expected, max_level=17) + self.multi_level_tester(code=code, expected=expected, max_level=16) + + def test_if_equality_comment_linebreak_print(self): + code = textwrap.dedent("""\ + naam is 'Hedy' + if naam is 'Hedy' # this linebreak is allowed + print 'leuk'""") + + expected = textwrap.dedent("""\ + naam = 'Hedy' + if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'): + print(f'''leuk''')""") + + self.multi_level_tester(max_level=16, code=code, expected=expected, output='leuk') + + def test_if_equality_linebreak_print_comment(self): + code = textwrap.dedent("""\ + naam is 'Hedy' + if naam is 'Hedy' + print 'leuk' # this linebreak is allowed""") + + expected = textwrap.dedent("""\ + naam = 'Hedy' + if convert_numerals('Latin', naam) == convert_numerals('Latin', 'Hedy'): + print(f'''leuk''')""") + + self.multi_level_tester(max_level=16, code=code, expected=expected, output='leuk') + + def test_if_equality_trailing_space_linebreak_print(self): + value = "'trailing_space' " + code = textwrap.dedent(f"""\ + naam is 'James' + if naam is {value} + print 'shaken'""") + + expected = textwrap.dedent("""\ + naam = 'James' + if convert_numerals('Latin', naam) == convert_numerals('Latin', 'trailing_space'): + print(f'''shaken''')""") + + self.multi_level_tester(max_level=16, code=code, expected=expected) @parameterized.expand(['5', '𑁫', '५', '૫', '੫', '৫', '೫', '୫', '൫', '௫', '౫', '၅', '༥', '᠕', '៥', '๕', '໕', '꧕', '٥', '۵']) diff --git a/tests/test_level/test_level_14.py b/tests/test_level/test_level_14.py index ddc841be381..ae5e7bc6a8b 100644 --- a/tests/test_level/test_level_14.py +++ b/tests/test_level/test_level_14.py @@ -383,6 +383,35 @@ def test_comparisons_with_boolean(self, comparison): expected=expected, ) + @parameterized.expand(HedyTester.comparison_commands) + def test_comparisons_with_trailing_spaces(self, comparison): + value = '1 ' + code = textwrap.dedent(f"""\ + var is 5 + if var {comparison} {value} + print 'seems correct'""") + + expected = textwrap.dedent(f"""\ + var = 5 + if convert_numerals('Latin', var){comparison}convert_numerals('Latin', 1): + print(f'''seems correct''')""") + + self.multi_level_tester(code=code, expected=expected, max_level=16) + + @parameterized.expand(HedyTester.comparison_commands) + def test_comparisons_with_leading_spaces(self, comparison): + code = textwrap.dedent(f"""\ + var is 5 + if var {comparison} 1 + print 'seems correct'""") + + expected = textwrap.dedent(f"""\ + var = 5 + if convert_numerals('Latin', var){comparison}convert_numerals('Latin', 1): + print(f'''seems correct''')""") + + self.multi_level_tester(code=code, expected=expected, max_level=16) + @parameterized.expand([ ('"text"', '1'), # double-quoted text and number ("'text'", '1'), # single-quoted text and number