Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🖊️ Allow if_pressed to work with functions #5933

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 123 additions & 76 deletions hedy.py

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions tests/Tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ def setUpClass(cls):
os.environ["ENABLE_SKIP_FAULTY"] = 'True' # Always test with skipping faulty enabled

def snippet_already_tested_with_current_hedy_version(self, test_hash):
try:
total_hash_incl_the_hedy_language = create_hash(get_hedy_source_hash(), test_hash)
if total_hash_incl_the_hedy_language is None:
return False
filename = get_hash_filename(total_hash_incl_the_hedy_language)
already_successful = os.path.isfile(filename)
return already_successful
except UnicodeEncodeError: # some tests (generated by Hypothesis) can't be hashed
return False
# try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this an unintended change?

# total_hash_incl_the_hedy_language = create_hash(get_hedy_source_hash(), test_hash)
# if total_hash_incl_the_hedy_language is None:
# return False
# filename = get_hash_filename(total_hash_incl_the_hedy_language)
# already_successful = os.path.isfile(filename)
# return already_successful
# except UnicodeEncodeError: # some tests (generated by Hypothesis) can't be hashed
return False

@staticmethod
@contextmanager
Expand Down Expand Up @@ -528,7 +528,7 @@ def int_transpiled(self, value):
def number_transpiled(value):
return f'''number_with_error({value}, {HedyTester.value_exception_transpiled()})'''

def for_loop(self, i, begin, end, num_sys="'Latin'"):
def for_loop(self, i, begin, end, num_sys="'Latin'", scope=''):
def for_loop_arg(arg):
if self.level >= 12:
return arg
Expand Down
58 changes: 34 additions & 24 deletions tests/test_level/test_level_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,7 @@ def test_if_pressed_x_is_letter_key(self):
if x is pressed print 'it is a letter key' else print 'it is another letter key'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1133,18 +1134,17 @@ def test_if_pressed_x_is_var(self):
print x""")

expected = self.dedent("""\
x = 'a'
global_scope_ = dict()
global_scope_["x"] = 'a'
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping[global_scope_.get("x") or x] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
def if_pressed_x_():
global x
print(f'it is a letter key')
def if_pressed_else_():
global x
print(f'it is another letter key')
extensions.if_pressed(if_pressed_mapping)
print(f'{x}')""")
print(f'{global_scope_.get("x") or x}')""")

self.single_level_tester(code=code, expected=expected)

Expand All @@ -1155,18 +1155,17 @@ def test_if_pressed_x_is_var_and_var_reassignment(self):
print x""")

expected = self.dedent("""\
x = 'a'
global_scope_ = dict()
global_scope_["x"] = 'a'
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping[global_scope_.get("x") or x] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
def if_pressed_x_():
global x
x = 'great'
global_scope_["x"] = 'great'
def if_pressed_else_():
global x
x = 'not great'
global_scope_["x"] = 'not great'
extensions.if_pressed(if_pressed_mapping)
print(f'{x}')""")
print(f'{global_scope_.get("x") or x}')""")

self.single_level_tester(code=code, expected=expected)

Expand All @@ -1177,18 +1176,17 @@ def test_if_pressed_x_is_var_and_new_var_assignment(self):
print m""")

expected = self.dedent("""\
x = 'a'
global_scope_ = dict()
global_scope_["x"] = 'a'
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping[global_scope_.get("x") or x] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
def if_pressed_x_():
global m, x
m = 'great'
global_scope_["m"] = 'great'
def if_pressed_else_():
global m, x
m = 'not great'
global_scope_["m"] = 'not great'
extensions.if_pressed(if_pressed_mapping)
print(f'{m}')""")
print(f'{global_scope_.get("m") or m}')""")

self.single_level_tester(code=code, expected=expected)

Expand All @@ -1198,6 +1196,7 @@ def test_double_if_pressed(self):
if y is pressed print 'second key' else print 'something else'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1223,6 +1222,7 @@ def test_if_pressed_has_enter_after_pressed(self):
print 'it is a letter key' else print 'something else'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1239,6 +1239,7 @@ def test_if_pressed_1_is_number_key(self):
if 1 is pressed print 'it is a number key' else print 'something else'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['1'] = 'if_pressed_1_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1255,6 +1256,7 @@ def test_if_pressed_with_trailing_spaces_after_key(self):
if x is pressed print 'trailing spaces!' else print 'something else'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1272,6 +1274,7 @@ def test_if_pressed_has_enter_before_else(self):
else print 'x is not pressed!'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1291,6 +1294,7 @@ def test_if_pressed_has_enter_before_both_prints_and_else(self):
print 'x is not pressed!'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1309,6 +1313,7 @@ def test_if_pressed_has_enter_before_first_print_and_else(self):
else print 'x is not pressed!'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1327,6 +1332,7 @@ def test_if_pressed_has_enter_before_second_print_and_else(self):
print 'x is not pressed!'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1345,6 +1351,7 @@ def test_if_pressed_has_enter_before_both_prints(self):
else print 'x is not pressed!'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1362,6 +1369,7 @@ def test_if_pressed_else_with_turtle(self):

expected = self.dedent(
f"""\
global_scope_ = dict()
if_pressed_mapping = {{"else": "if_pressed_default_else"}}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand All @@ -1385,6 +1393,7 @@ def test_if_pressed_non_latin(self):
if й is pressed print 'russian' else print 'something else'""")

expected = textwrap.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['ض'] = 'if_pressed_ض_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand Down Expand Up @@ -1461,6 +1470,7 @@ def test_source_map(self):
else print 'The prince was eaten by a hippopotamus 😭'""")

expected_code = textwrap.dedent("""\
global_scope_ = dict()
print(f'Do you want a good (g) or bad (b) ending?')
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['g'] = 'if_pressed_g_'
Expand All @@ -1472,11 +1482,11 @@ def if_pressed_else_():
extensions.if_pressed(if_pressed_mapping)""")

expected_source_map = {
'1/1-1/50': '1/1-1/52',
'1/1-3/55': '1/1-9/42',
'2/1-3/54': '2/1-9/42',
'2/17-2/56': '6/3-6/44',
'3/6-3/54': '8/3-8/53'
'1/1-1/50': '2/1-2/52',
'1/1-3/55': '1/1-10/42',
'2/1-3/54': '3/1-10/42',
'2/17-2/56': '7/3-7/44',
'3/6-3/54': '9/3-9/53'
}

self.single_level_tester(code, expected=expected_code)
Expand Down
35 changes: 16 additions & 19 deletions tests/test_level/test_level_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -2673,18 +2673,17 @@ def test_if_pressed_x_is_var(self):
print x""")

expected = self.dedent("""\
x = Value('a')
global_scope_ = dict()
global_scope_["x"] = Value('a')
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping[(global_scope_.get("x") or x).data] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
def if_pressed_x_():
global x
print(f'it is a letter key')
def if_pressed_else_():
global x
print(f'it is another letter key')
extensions.if_pressed(if_pressed_mapping)
print(f'{x}')""")
print(f'{global_scope_.get("x") or x}')""")

self.multi_level_tester(code=code, expected=expected, max_level=7)

Expand All @@ -2695,18 +2694,17 @@ def test_if_pressed_x_is_var_and_var_reassignment(self):
print x""")

expected = self.dedent("""\
x = Value('a')
global_scope_ = dict()
global_scope_["x"] = Value('a')
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping[(global_scope_.get("x") or x).data] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
def if_pressed_x_():
global x
x = Value('great')
global_scope_["x"] = Value('great')
def if_pressed_else_():
global x
x = Value('not great')
global_scope_["x"] = Value('not great')
extensions.if_pressed(if_pressed_mapping)
print(f'{x}')""")
print(f'{global_scope_.get("x") or x}')""")

self.multi_level_tester(code=code, expected=expected, max_level=7)

Expand All @@ -2717,17 +2715,16 @@ def test_if_pressed_x_is_var_and_new_var_assignment(self):
print m""")

expected = self.dedent("""\
x = Value('a')
global_scope_ = dict()
global_scope_["x"] = Value('a')
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping[(global_scope_.get("x") or x).data] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
def if_pressed_x_():
global m, x
m = Value('great')
global_scope_["m"] = Value('great')
def if_pressed_else_():
global m, x
m = Value('not great')
global_scope_["m"] = Value('not great')
extensions.if_pressed(if_pressed_mapping)
print(f'{m}')""")
print(f'{global_scope_.get("m") or m}')""")

self.multi_level_tester(code=code, expected=expected, max_level=7)
23 changes: 13 additions & 10 deletions tests/test_level/test_level_07.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,17 @@ def test_if_pressed_repeat(self):
code = "if x is pressed repeat 5 times print 'doe het 5 keer!' else print 'iets anders'"

expected = self.dedent(f"""\
if_pressed_mapping = {{"else": "if_pressed_default_else"}}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
def if_pressed_x_():
for __i in range(int_with_error('5', {self.value_exception_transpiled()})):
print(f'doe het 5 keer!')
time.sleep(0.1)
def if_pressed_else_():
print(f'iets anders')
extensions.if_pressed(if_pressed_mapping)""")
global_scope_ = dict()
if_pressed_mapping = {{"else": "if_pressed_default_else"}}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
def if_pressed_x_():
for __i in range(int_with_error('5', {self.value_exception_transpiled()})):
print(f'doe het 5 keer!')
time.sleep(0.1)
def if_pressed_else_():
print(f'iets anders')
extensions.if_pressed(if_pressed_mapping)""")

self.single_level_tester(
code=code,
Expand All @@ -375,6 +376,7 @@ def test_if_pressed_multiple(self):
if z is pressed print 'doe het 1 keer!' else print 'iets anders'""")

expected = self.dedent("""\
global_scope_ = dict()
if_pressed_mapping = {"else": "if_pressed_default_else"}
if_pressed_mapping['x'] = 'if_pressed_x_'
if_pressed_mapping['else'] = 'if_pressed_else_'
Expand Down Expand Up @@ -414,6 +416,7 @@ def test_repeat_if_pressed_multiple(self):

expected = self.dedent(
f"""\
global_scope_ = dict()
for __i in range({self.int_transpiled(3)}):
if_pressed_mapping = {{"else": "if_pressed_default_else"}}
if_pressed_mapping['x'] = 'if_pressed_x_'
Expand Down
Loading
Loading