def validate(program, bounds): parse_tree = xdrone_parser.parse(program) validator = Validate() validator.visit(parse_tree) if validator.max_z > bounds["height"]: return {"success": False, "message": "The drone flies too high"} if (abs(validator.max_x) > bounds["width"] / 2 or abs(validator.min_x) > bounds["width"] / 2 or abs(validator.max_y) > bounds["depth"] / 2 or abs(validator.min_y) > bounds["depth"] / 2): return {"success": False, "message": "The drone flies out of bounds"} return {"success": True}
def test_missing_brackets(): sample_program = "fly() TAKEOFF() LAND() }" with pytest.raises(Exception) as excinfo: parse_tree = xdrone_parser.parse(sample_program)
def test_blank(): sample_program = "fly() { }" with pytest.raises(Exception) as excinfo: parse_tree = xdrone_parser.parse(sample_program)
def test_land_before_takeoff(): sample_program = "fly() { LAND() TAKEOFF() }" with pytest.raises(Exception) as excinfo: parse_tree = xdrone_parser.parse(sample_program)
def test_missing_takeoff(): sample_program = "fly() { LAND() }" with pytest.raises(Exception) as excinfo: parse_tree = xdrone_parser.parse(sample_program)
def fly(program, rs, addr="d0:3a:86:9d:e6:5a"): parse_tree = xdrone_parser.parse(program) requirements = generate_requirements(rs) Fly(addr, requirements).visit(parse_tree) return all(r.is_completed() for r in requirements)
def gen_simulate_commands(program): parse_tree = xdrone_parser.parse(program) return Simulate().transform(parse_tree)