From 16f9a6667147ae402155c333fdd6ceb50f7bb7ec Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 29 Nov 2019 11:54:28 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Add=20helper=20methods=20for=20album?= =?utf8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../kotlin/net/pterodactylus/sone/data/Albums.kt | 23 +++++++++++ .../net/pterodactylus/sone/data/AlbumsTest.kt | 48 ++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/main/kotlin/net/pterodactylus/sone/data/Albums.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/data/AlbumsTest.kt 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() + +} -- 2.7.4