extends GdUnitTestSuite ## Characters read from a folder on this computer instead of the server. var _root: String func before_test() -> void: _root = create_temp_dir("folder_characters_%d" % randi()) func _picture(file_name: String, size: Vector2i = Vector2i(8, 4)) -> Image: var image: Image = Image.create(size.x, size.y, false, Image.FORMAT_RGB8) image.fill(Color.FIREBRICK) var bytes: PackedByteArray match file_name.get_extension().to_lower(): "png": bytes = image.save_png_to_buffer() "jpg", "jpeg": bytes = image.save_jpg_to_buffer() "webp": bytes = image.save_webp_to_buffer() _file(file_name, bytes) return image func _file(file_name: String, bytes: PackedByteArray) -> void: FileAccess.open(_root.path_join(file_name), FileAccess.WRITE).store_buffer(bytes) func test_every_picture_is_a_character_in_natural_order() -> void: for file_name: String in ["Santa10.png", "Santa2.jpg", "elf.webp", "Rudolph.JPEG"]: _picture(file_name) assert_array(FolderCharacters.picture_names(_root)).contains_exactly( ["elf.webp", "Rudolph.JPEG", "Santa2.jpg", "Santa10.png"]) func test_other_and_hidden_files_are_left_out() -> void: _picture("Santa.png") _picture(".hidden.png") _file("notes.txt", "hello".to_utf8_buffer()) _file("song.mp3", PackedByteArray([1, 2, 3])) DirAccess.make_dir_absolute(_root.path_join("more.png")) assert_array(FolderCharacters.picture_names(_root)).contains_exactly(["Santa.png"]) func test_missing_folder_has_no_characters() -> void: assert_array(FolderCharacters.picture_names(_root.path_join("gone"))).is_empty() func test_load_list_gives_the_names() -> void: _picture("Santa.png") _picture("Elf.png") var names: Array = [] FolderCharacters.new(_root).load_list(func(list: Array[String]) -> void: names.assign(list)) assert_array(names).contains_exactly(["Elf.png", "Santa.png"]) func test_fetch_image_reads_the_picture() -> void: _picture("Santa.webp", Vector2i(20, 10)) var pictures: Array[Image] = [] FolderCharacters.new(_root).fetch_image("Santa.webp", func(picture: Image) -> void: pictures.append(picture)) assert_int(pictures.size()).is_equal(1) assert_vector(Vector2(pictures[0].get_size())).is_equal(Vector2(20, 10)) func test_fetch_image_of_a_missing_or_broken_picture_gives_nothing() -> void: _file("Broken.png", "not a picture".to_utf8_buffer()) var pictures: Array[Image] = [] var characters: FolderCharacters = FolderCharacters.new(_root) characters.fetch_image("Gone.png", func(picture: Image) -> void: pictures.append(picture)) characters.fetch_image("Broken.png", func(picture: Image) -> void: pictures.append(picture)) assert_array(pictures).is_empty() func test_names_cannot_reach_outside_the_folder() -> void: var outside: String = _root.get_base_dir().path_join("outside_%d.png" % randi()) FileAccess.open(outside, FileAccess.WRITE).store_buffer( Image.create(4, 4, false, Image.FORMAT_RGB8).save_png_to_buffer()) var pictures: Array[Image] = [] FolderCharacters.new(_root).fetch_image("../" + outside.get_file(), func(picture: Image) -> void: pictures.append(picture)) DirAccess.remove_absolute(outside) assert_array(pictures).is_empty()