예제 #1
0
def test_opening_in_other_mode_fails(unix_newline_filepath):
    for mode in ("r", "w", "w+", "a+", "rb", "r+b", "wb", "w+b", "ab", "a+b"):
        with pytest.raises(
                ValueError,
                match="File used for map creation has to be open in"):
            with open(unix_newline_filepath, mode=mode) as f:
                Map.from_file(f)
예제 #2
0
def test_iterating_over_map(unix_newline_filepath, expected_map):
    with open(unix_newline_filepath, encoding="ascii", mode="r+") as f:
        map = Map.from_file(f)
        # Extend expected map to see that the iteration is finished when elements are exhausted
        expected_map.extend([LAND, LAND, WATER, WATER])
        for expected_field, real_field in zip(expected_map, map):
            assert expected_field == real_field.type
예제 #3
0
def test_neighbours_of_middle(unix_newline_filepath, xy, expected_neighbours):
    x = xy[0]
    y = xy[1]
    with open(unix_newline_filepath, mode="r+") as f:
        map = Map.from_file(f)
        f = map.at(x, y)
        assert expected_neighbours == [n.type for n in map.get_neighbours(f)]
예제 #4
0
def test_opening_with_both_utf_and_ascii_works(unix_newline_filepath,
                                               expected_map):
    for encoding in ("ascii", "utf-8"):
        with open(unix_newline_filepath, encoding=encoding, mode="r+") as f:
            map = Map.from_file(f)
            # Extend expected map to see that the iteration is finished when elements are exhausted
            expected_map.extend([LAND, LAND, WATER, WATER])
            for expected_field, real_field in zip(expected_map, map):
                assert expected_field == real_field.type
예제 #5
0
        default="ascii",
        help="Encoding of the file. Default: ASCII",
    )
    parser.add_argument(
        "--duplicate",
        action="store_true",
        help="should the file be duplicated before working on it. Without using it the file passed to the program will be modified and all the islands will be removed during the execution.",
    )
    # Unkown arguments will be ignored
    args, _ = parser.parse_known_args(args=args)
    return args


def _duplicate_file(path: Path):
    temp_path = str(path) + ".copy"
    return shutil.copy2(path, temp_path)


if __name__ == "__main__":
    parsed_arguments = _parse_arguments()
    input_filepath = parsed_arguments.input
    if parsed_arguments.duplicate:
        input_filepath = _duplicate_file(parsed_arguments.input)

    with open(input_filepath, mode="r+", encoding=parsed_arguments.encoding) as file:
        map = Map.from_file(file)  # type: ignore
        print(count_islands(map))

    if parsed_arguments.duplicate:
        os.unlink(input_filepath)
예제 #6
0
def test_creation_with_closed_file_fails(unix_newline_filepath):
    with pytest.raises(ValueError, match="Creation of Map from closed file."):
        f = open(unix_newline_filepath, mode="r+")
        f.close()
        Map.from_file(f)
예제 #7
0
def test_getting_field_out_of_bounds_throws(unix_newline_filepath):
    with open(unix_newline_filepath, mode="r+") as f:
        map = Map.from_file(f)
        with pytest.raises(IndexError):
            assert map.at(0, 7)
예제 #8
0
def test_getting_one_field_with_windows_newline(windows_newline_filepath):
    with open(windows_newline_filepath, mode="r+") as f:
        map = Map.from_file(f)
        assert MapField(0, 0, WATER) == map.at(0, 0)
        assert MapField(1, 2, LAND) == map.at(1, 2)
        assert MapField(2, 2, WATER) == map.at(2, 2)
예제 #9
0
def test_iterating_over_empty_map(unix_newline_filepath):
    with open(unix_newline_filepath, mode="r+") as f:
        map = Map.from_file(f)
        for field in map:
            raise Exception("Should not be here.")