Skip to content

Commit

Permalink
🪲 Fix color command error (#5603)
Browse files Browse the repository at this point in the history
Fixes #5597
The color command fails with an unknown variable error because colors are not transpiled to literal colors but to variables.

**How to test**
- In level 4, the following command `color red` should work and make the turtle red. There should be no errors.
  • Loading branch information
boryanagoncharenko authored Jun 7, 2024
1 parent 6e7573d commit 1aeacb0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 3 additions & 1 deletion hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2039,7 +2039,9 @@ def process_variable_for_fstring(self, name):
if self.is_quoted(name):
name = name[1:-1]
return name.replace("'", "\\'")
name = name if self.is_bool(name) else escape_var(name.replace("'", "\\'"))
if not ConvertToPython.is_int(name) and not ConvertToPython.is_float(name):
name = name if self.is_bool(name) else escape_var(name.replace("'", "\\'"))
name = '"' + name + '"'
return f'{{convert_numerals("{self.numerals_language}", {name})}}'

def var_access(self, meta, args):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_level/test_level_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ def test_undefined_list_access(self):
@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})}}')
expected = HedyTester.turtle_color_command_transpiled(f'{{convert_numerals("Latin", "{color}")}}')

self.multi_level_tester(
code=code,
Expand All @@ -1054,7 +1054,7 @@ def test_all_colors(self, color):

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

self.multi_level_tester(
code=code,
Expand All @@ -1066,7 +1066,7 @@ def test_color_red(self):
def test_color_translated(self):
lang = 'nl'
code = "kleur blauw"
expected = HedyTester.turtle_color_command_transpiled('{convert_numerals("Latin", blue)}', lang)
expected = HedyTester.turtle_color_command_transpiled('{convert_numerals("Latin", "blue")}', lang)

self.multi_level_tester(
code=code,
Expand All @@ -1082,7 +1082,7 @@ def test_color_basic(self):
forward 10""")

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

self.multi_level_tester(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_level/test_level_15.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def test_assign_list_var_boolean(self):
)

@parameterized.expand(HedyTester.booleans)
def test_print_boolean(self, value, expected):
def test_print_boolean(self, value, exp):
code = f"print 'variable is ' {value}"
expected = f"print(f'''variable is {{convert_numerals(\"Latin\", {expected})}}''')"
expected = f"print(f'''variable is {{convert_numerals(\"Latin\", \"{exp}\")}}''')"

self.multi_level_tester(
code=code,
Expand All @@ -50,7 +50,7 @@ def test_print_boolean(self, value, expected):
@parameterized.expand([('вярно', True), ('Вярно', True), ('невярно', False), ('Невярно', False)])
def test_print_boolean_bulgarian(self, value, exp):
code = f"принтирай 'Това е ' {value}"
expected = f"print(f'''Това е {{convert_numerals(\"Latin\", {exp})}}''')"
expected = f"print(f'''Това е {{convert_numerals(\"Latin\", \"{exp}\")}}''')"

self.multi_level_tester(
code=code,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_level/test_level_17.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'{{convert_numerals("Latin", 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

0 comments on commit 1aeacb0

Please sign in to comment.