From: David ‘Bombe’ Roden Date: Fri, 29 Nov 2019 10:54:28 +0000 (+0100) Subject: ✨ Add helper methods for albums X-Git-Tag: v81^2~15^2~11 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=16f9a6667147ae402155c333fdd6ceb50f7bb7ec;p=Sone.git ✨ Add helper methods for albums --- diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt b/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt new file mode 100644 index 0000000..f1c7d3b --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/data/Albums.kt @@ -0,0 +1,23 @@ +/** + * Sone - Albums.kt - Copyright © 2019 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.data + +/** Returns all images contained in this album and all its albums. */ +val Album.allImages: Collection + get() = + images + albums.flatMap { it.allImages } diff --git a/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt new file mode 100644 index 0000000..9c06a5e --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt @@ -0,0 +1,48 @@ +/** + * Sone - AlbumsTest.kt - Copyright © 2019 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.data + +import net.pterodactylus.sone.data.impl.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import kotlin.test.* + +/** + * Unit test for various helper method in `Albums.kt`. + */ +class AlbumsTest { + + @Test + fun `recursive list of all images for album is returned correctly`() { + val sone = IdOnlySone("sone") + val album = AlbumImpl(sone) + val firstNestedAlbum = AlbumImpl(sone) + val secondNestedAlbum = AlbumImpl(sone) + firstNestedAlbum.addImage(createImage(sone, "image-1")) + firstNestedAlbum.addImage(createImage(sone, "image-2")) + secondNestedAlbum.addImage(createImage(sone, "image-3")) + album.addImage(createImage(sone, "image-4")) + album.addAlbum(firstNestedAlbum) + album.addAlbum(secondNestedAlbum) + val images = album.allImages + assertThat(images.map(Image::id), containsInAnyOrder("image-1", "image-2", "image-3", "image-4")) + } + + private fun createImage(sone: IdOnlySone, id: String) = ImageImpl(id).modify().setSone(sone).update() + +}