Ejemplo n.º 1
0
def execute_karel_code(code_file: Path) -> None:
    module_name = code_file.stem
    karel = KarelProgram(module_name)
    student_code = StudentCode(code_file)
    student_code.inject_namespace(karel)
    student_code.mod.main()  # type: ignore
    assert karel.compare_with(
        KarelProgram(f"{module_name}_end"
                     )), "Resulting world did not match expected result."
Ejemplo n.º 2
0
def create_solution_worlds() -> None:
    for problem_name in PROBLEMS:
        karel = KarelProgram(problem_name)
        student_code = StudentCode(Path(f"problems/{problem_name}.py"))
        student_code.inject_namespace(karel)
        student_code.main()
        karel.world.save_to_file(Path(f"worlds/{problem_name}_end.w"))
Ejemplo n.º 3
0
def create_solution_worlds() -> None:
    for problem_name in PROBLEMS:
        karel = KarelProgram(problem_name)
        student_code = StudentCode(Path(problem_name))  # TODO(tyler)
        student_code.inject_namespace(karel)
        student_code.mod.main()  # type: ignore
        karel.world.save_to_file(Path(f"worlds/{problem_name}_end.w"))
Ejemplo n.º 4
0
def execute_karel_code(code_file: Path,
                       world_name: str = "",
                       expected_error: str = "") -> None:
    world_name = world_name or code_file.stem
    karel = KarelProgram(world_name)
    try:
        student_code = StudentCode(code_file)
    except (SyntaxError, RuntimeError) as e:
        assert str(e) == expected_error
        return

    student_code.inject_namespace(karel)
    try:
        student_code.main()
        assert karel.compare_with(
            KarelProgram(f"{world_name}_end"
                         )), "Resulting world did not match expected result."
    except (KarelException, NameError) as e:
        assert str(e) == expected_error
Ejemplo n.º 5
0
    def test_save_to_file(tmp_path: Path) -> None:
        karel = KarelProgram("stone_mason_karel")
        output_file = tmp_path / "test_world.w"
        karel.world.save_to_file(output_file)
        expected = (
            "Dimension: (13, 13)",
            "Wall: (1, 6); south",
            "Wall: (2, 6); west",
            "Wall: (2, 7); south",
            "Wall: (3, 7); west",
            "Wall: (3, 8); south",
            "Wall: (4, 7); south",
            "Wall: (4, 7); west",
            "Wall: (5, 6); south",
            "Wall: (5, 6); west",
            "Wall: (6, 6); west",
            "Wall: (6, 7); south",
            "Wall: (7, 7); west",
            "Wall: (7, 8); south",
            "Wall: (8, 7); south",
            "Wall: (8, 7); west",
            "Wall: (9, 6); south",
            "Wall: (9, 6); west",
            "Wall: (10, 6); west",
            "Wall: (10, 7); south",
            "Wall: (11, 7); west",
            "Wall: (11, 8); south",
            "Wall: (12, 7); south",
            "Wall: (12, 7); west",
            "Wall: (13, 6); south",
            "Wall: (13, 6); west",
            "Beeper: (1, 4); 1",
            "Beeper: (1, 5); 1",
            "Beeper: (5, 1); 1",
            "Beeper: (5, 2); 1",
            "Beeper: (5, 4); 1",
            "Beeper: (9, 3); 1",
            "Beeper: (9, 5); 1",
            "Beeper: (13, 1); 1",
            "Beeper: (13, 3); 1",
            "Beeper: (13, 5); 1",
            "Karel: (1, 1); east",
            "BeeperBag: INFINITY",
        )

        assert output_file.read_text() == "\n".join(expected) + "\n"
Ejemplo n.º 6
0
    def test_karel_ascii() -> None:
        karel = KarelProgram("stone_mason_karel")

        assert str(karel) == "\n".join(STONE_MASON_ASCII_OUTPUT) + "\n"