Skip to content

Commit

Permalink
🪲 Numbers are not translated to Arabic when printed (#5567)
Browse files Browse the repository at this point in the history
Fixes #5499
When the language of Hedy is set to Arabic, numbers should be displayed in Arabic numbers regardless of what alphabet was used in the input. This PR fixes the corner case in which when literal numbers are printed, they are not converted to Arabic.

** How to test**
Run Hedy locally, navigate to level 6 and switch the language to Arabic. Execute the following piece of code and ensure that both lines print the result `٢`:
```
print 1 + 1
print 2
```
  • Loading branch information
boryanagoncharenko authored May 28, 2024
1 parent aeab6e3 commit 0d0d159
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 24 deletions.
7 changes: 5 additions & 2 deletions hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2032,12 +2032,15 @@ def process_variable_for_fstring(self, name):
if self.numerals_language == "Latin":
converted = escape_var(name)
else:
converted = f'convert_numerals("{self.numerals_language}",{escape_var(name)})'
converted = f'convert_numerals("{self.numerals_language}", {escape_var(name)})'
return "{" + converted + "}"
else:
# at level 4 backslashes are escaped in preprocessing, so we escape only '
if self.is_quoted(name):
name = name[1:-1]
return name.replace("'", "\\'") # at level 4 backslashes are escaped in preprocessing, so we escape only '
return name.replace("'", "\\'")
name = name.replace("'", "\\'")
return f'{{convert_numerals("{self.numerals_language}", {escape_var(name)})}}'

def var_access(self, meta, args):
name = args[0]
Expand Down
4 changes: 3 additions & 1 deletion tests/test_level/test_level_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ def test_all_colors(self, color):
code=code,
expected=expected,
extra_check_function=self.is_turtle(),
max_level=3
)

def test_color_red_ar(self):
Expand All @@ -504,6 +505,7 @@ def test_color_red_ar(self):
code=code,
expected=expected,
extra_check_function=self.is_turtle(),
max_level=3,
lang='ar'
)

Expand Down Expand Up @@ -533,7 +535,7 @@ def test_color_translated(self):
expected=expected,
extra_check_function=self.is_turtle(),
lang=lang,
max_level=10
max_level=3
)

def test_color_with_number_gives_type_error(self):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_level/test_level_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ def test_color_basic(self):
expected = HedyTester.dedent(HedyTester.turtle_color_command_transpiled("red", 'en'),
HedyTester.forward_transpiled('10', self.level))

self.multi_level_tester(
max_level=11,
self.single_level_tester(
code=code,
translate=False,
expected=expected
Expand Down
54 changes: 54 additions & 0 deletions tests/test_level/test_level_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,3 +1037,57 @@ def test_undefined_list_access(self):
exception=hedy.exceptions.UndefinedVarException,
skip_faulty=False,
max_level=11)

#
# color
#
@parameterized.expand(hedy.english_colors)
def test_all_colors(self, color):
code = f'color {color}'
expected = HedyTester.turtle_color_command_transpiled(f'{{convert_numerals("Latin", {color})}}')

self.multi_level_tester(
code=code,
expected=expected,
extra_check_function=self.is_turtle()
)

def test_color_red(self):
code = "color red"
expected = HedyTester.turtle_color_command_transpiled('{convert_numerals("Latin", red)}')

self.multi_level_tester(
code=code,
expected=expected,
extra_check_function=self.is_turtle(),
max_level=10
)

def test_color_translated(self):
lang = 'nl'
code = "kleur blauw"
expected = HedyTester.turtle_color_command_transpiled('{convert_numerals("Latin", blue)}', lang)

self.multi_level_tester(
code=code,
expected=expected,
extra_check_function=self.is_turtle(),
lang=lang,
max_level=10
)

def test_color_basic(self):
code = textwrap.dedent("""\
color red
forward 10""")

expected = HedyTester.dedent(
HedyTester.turtle_color_command_transpiled('{convert_numerals("Latin", red)}', 'en'),
HedyTester.forward_transpiled('10', self.level))

self.multi_level_tester(
max_level=11,
code=code,
translate=False,
expected=expected
)
13 changes: 8 additions & 5 deletions tests/test_level/test_level_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,13 +943,16 @@ def test_consecutive_if_else_statements(self):
self.multi_level_tester(max_level=7, code=code, expected=expected)

def test_print_single_number(self):
code = textwrap.dedent("""\
print 5""")
code = "print 5"
expected = 'print(f\'{convert_numerals("Latin", 5)}\')'

expected = textwrap.dedent("""\
print(f'5')""")
self.multi_level_tester(max_level=11, code=code, expected=expected)

def test_print_single_number_ar(self):
code = "قول 2"
expected = 'print(f\'{convert_numerals("Arabic", 2)}\')'

self.multi_level_tester(max_level=6, code=code, expected=expected)
self.multi_level_tester(max_level=11, code=code, expected=expected, lang='ar')

def test_negative_variable(self):
code = textwrap.dedent("""\
Expand Down
6 changes: 3 additions & 3 deletions tests/test_level/test_level_09.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ def test_repeat_nested_multi_commands(self):

expected = textwrap.dedent(f"""\
for __i in range({self.int_cast_transpiled(3)}):
print(f'3')
print(f'{{convert_numerals("Latin", 3)}}')
for __i in range({self.int_cast_transpiled(5)}):
print(f'5')
print(f'{{convert_numerals("Latin", 5)}}')
time.sleep(0.1)
print(f'1')
print(f'{{convert_numerals("Latin", 1)}}')
time.sleep(0.1)""")

self.multi_level_tester(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_level/test_level_11.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def test_for_loop_arabic_range_arabic_output(self):
expected = textwrap.dedent("""\
step = 1 if int(1) < int(5) else -1
for دورة in range(int(1), int(5) + step, step):
print(f'{convert_numerals("Arabic",دورة)}')
print(f'{convert_numerals("Arabic", دورة)}')
time.sleep(0.1)""")

output = textwrap.dedent("""\
Expand Down
8 changes: 4 additions & 4 deletions tests/test_level/test_level_12.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_print_float_variable(self):
def test_print_float(self):
code = "print 3.14"

expected = "print(f'''3.14''')"
expected = "print(f'''{convert_numerals(\"Latin\", 3.14)}''')"

self.multi_level_tester(
code=code,
Expand Down Expand Up @@ -1745,11 +1745,11 @@ def test_repeat_nested_multi_commands(self):

expected = textwrap.dedent(f"""\
for __i in range({self.int_cast_transpiled(3)}):
print(f'''3''')
print(f'''{{convert_numerals("Latin", 3)}}''')
for __i in range({self.int_cast_transpiled(5)}):
print(f'''5''')
print(f'''{{convert_numerals("Latin", 5)}}''')
time.sleep(0.1)
print(f'''1''')
print(f'''{{convert_numerals("Latin", 1)}}''')
time.sleep(0.1)""")

self.multi_level_tester(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_level/test_level_17.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,8 @@ def test_not_equal_with_diff_types_gives_error(self, left, right):

def test_if_pressed_with_turtlecolor(self):
code = textwrap.dedent("""\
if x is pressed:
color red""")
if x is pressed:
color red""")

# Need to exgtract this out for now because of an autopep8 bug
# https://github.com/hhatto/autopep8/issues/744
Expand All @@ -548,7 +548,7 @@ def test_if_pressed_with_turtlecolor(self):
if_pressed_mapping = {{"else": "if_pressed_default_else"}}
if_pressed_mapping['x'] = 'if_pressed_x_'
def if_pressed_x_():
__trtl = f'red'
__trtl = f'{{convert_numerals("Latin", red)}}'
color_dict = {color_dict}
if __trtl not in ['black', 'blue', 'brown', 'gray', 'green', 'orange', 'pink', 'purple', 'red', 'white', 'yellow']:
raise Exception(f{self.value_exception_transpiled()})
Expand Down
6 changes: 3 additions & 3 deletions tests/test_level/test_level_18.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ def test_repeat_nested_multi_commands(self):

expected = textwrap.dedent(f"""\
for __i in range({self.int_cast_transpiled(3)}):
print(f'''3''')
print(f'''{{convert_numerals("Latin", 3)}}''')
for __i in range({self.int_cast_transpiled(5)}):
print(f'''5''')
print(f'''{{convert_numerals("Latin", 5)}}''')
time.sleep(0.1)
print(f'''1''')
print(f'''{{convert_numerals("Latin", 1)}}''')
time.sleep(0.1)""")

self.multi_level_tester(
Expand Down

0 comments on commit 0d0d159

Please sign in to comment.