Skip to content

Commit

Permalink
fix: Allow 'van' only in imports (#1058)
Browse files Browse the repository at this point in the history
* Add test for invalid import

* Put error message in main section of popup

* Uncomment tests
  • Loading branch information
faucomte97 authored Sep 12, 2019
1 parent bf5b69b commit c8320a9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions game/end_to_end_tests/base_game_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ def run_console_attribute_error_test(self, level):
def run_console_print_test(self, level):
return self.go_to_level(level).run_print_program()

def run_invalid_import_test(self, level):
return self.go_to_level(level).run_invalid_import_program()

def running_out_of_instructions_test(self, level, workspace_file):
user_profile = self.login_once()

Expand Down
19 changes: 15 additions & 4 deletions game/end_to_end_tests/game_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,18 @@ def run_undefined_procedure_program(self):
)

def run_parse_error_program(self):
return self._run_python_failing_program("v.", "ParseError: bad input on line")
return self._run_failing_python_program_console("v.", "ParseError: bad input on line")

def run_attribute_error_program(self):
return self._run_python_failing_program(
return self._run_failing_python_program_console(
"v.go()", "AttributeError: 'Van' object has no attribute"
)

def run_print_program(self):
return self._run_python_failing_program('print("hello world")', "hello world")
return self._run_failing_python_program_console('print("hello world")', "hello world")

def run_invalid_import_program(self):
return self._run_failing_python_program_popup("import va", "You're not allowed to import anything other than 'van'.")

def check_python_commands(self):
self.python_commands_button()
Expand Down Expand Up @@ -236,14 +239,22 @@ def _run_failing_program(self, text):
assert_that(error_message, contains_string(text))
return self

def _run_python_failing_program(self, code, console_message):
def _run_failing_python_program_console(self, code, console_message):
self._write_code(code)
self.browser.find_element_by_id("fast_tab").click()
time.sleep(1)
console = self.browser.find_element_by_id("consoleOutput")
assert_that(console.text, contains_string(console_message))
return self

def _run_failing_python_program_popup(self, code, text):
self._write_code(code)
self.browser.find_element_by_id("fast_tab").click()
time.sleep(1)
error_message = self.browser.find_element_by_id("myModal-lead").text
assert_that(error_message, contains_string(text))
return self

def _write_code(self, code):
self.browser.execute_script(
"ocargo.pythonControl.appendCode(arguments[0])", code
Expand Down
3 changes: 3 additions & 0 deletions game/end_to_end_tests/test_python_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ def test_console_attribute_error(self):

def test_console_print(self):
self.run_console_print_test(level=92)

def test_invalid_import(self):
self.run_invalid_import_test(level=109)
10 changes: 9 additions & 1 deletion game/static/game/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,19 @@ ocargo.Game.prototype.reset = function() {
ocargo.Game.prototype.runProgramAndPrepareAnimation = function(blocks) {
this.reset();

let code = ocargo.pythonControl.getCode()

ocargo.event.sendEvent('PlayButtonPressed', { levelName: LEVEL_NAME,
defaultLevel: DEFAULT_LEVEL,
workspace: ocargo.blocklyControl.serialize(),
failures: this.failures,
pythonWorkspace: ocargo.pythonControl.getCode() });
pythonWorkspace: code, });

if (code.match(/import (?!(van))/))
{
ocargo.Drawing.startPopup(gettext('Oh dear!'), "You're not allowed to import anything other than 'van'.", "");
return false;
}

var result = ocargo.controller.prepare(blocks);
if (!result.success) {
Expand Down

0 comments on commit c8320a9

Please sign in to comment.