2 * Sone - SoneTest.kt - Copyright © 2020 David ‘Bombe’ Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.data
20 import net.pterodactylus.sone.data.impl.*
21 import net.pterodactylus.sone.test.*
22 import org.hamcrest.MatcherAssert.*
23 import org.hamcrest.Matchers.*
27 * Unit test for functions in Sone.
32 fun `nice name comparator correctly compares Sones by their nice name`() {
33 val sone1 = object : IdOnlySone("1") {
34 override fun getProfile() = Profile(this).apply { firstName = "Left" }
36 val sone2 = object : IdOnlySone("2") {
37 override fun getProfile() = Profile(this).apply { firstName = "Right" }
39 assertThat(niceNameComparator.compare(sone1, sone2), lessThan(0))
43 fun `nice name comparator correctly compares Sones by their ID if nice name is the same`() {
44 val sone1 = object : IdOnlySone("1") {
45 override fun getProfile() = Profile(this).apply { firstName = "Left" }
47 val sone2 = object : IdOnlySone("2") {
48 override fun getProfile() = Profile(this).apply { firstName = "Left" }
50 assertThat(niceNameComparator.compare(sone1, sone2), lessThan(0))
54 fun `nice name comparator treats Sones as equal if nice name and ID are the same`() {
55 val sone1 = object : IdOnlySone("1") {
56 override fun getProfile() = Profile(this).apply { firstName = "Left" }
58 val sone2 = object : IdOnlySone("1") {
59 override fun getProfile() = Profile(this).apply { firstName = "Left" }
61 assertThat(niceNameComparator.compare(sone1, sone2), equalTo(0))
65 fun `last activity comparator correctly compares Sones by last activity`() {
66 val sone1 = object : IdOnlySone("1") {
67 override fun getTime() = 1000L
69 val sone2 = object : IdOnlySone("2") {
70 override fun getTime() = 2000L
72 assertThat(lastActivityComparator.compare(sone1, sone2), greaterThan(0))
76 fun `last activity comparator treats Sones as equal if last activity is the same`() {
77 val sone1 = object : IdOnlySone("1") {
78 override fun getTime() = 1000L
80 val sone2 = object : IdOnlySone("2") {
81 override fun getTime() = 1000L
83 assertThat(lastActivityComparator.compare(sone1, sone2), equalTo(0))
87 fun `post count comparator sorts sones with different number of posts correctly`() {
88 val sone1 = object : IdOnlySone("1") {
89 override fun getPosts() = listOf(createPost(), createPost())
91 val sone2 = object : IdOnlySone("2") {
92 override fun getPosts() = listOf(createPost(), createPost(), createPost())
94 assertThat(postCountComparator.compare(sone1, sone2), greaterThan(0))
98 fun `post count comparator compares replies if posts are not different`() {
99 val sone1 = object : IdOnlySone("1") {
100 override fun getPosts() = listOf(createPost(), createPost())
101 override fun getReplies() = setOf(createPostReply(), createPostReply())
103 val sone2 = object : IdOnlySone("2") {
104 override fun getPosts() = listOf(createPost(), createPost())
105 override fun getReplies() = setOf(createPostReply(), createPostReply(), createPostReply())
107 assertThat(postCountComparator.compare(sone1, sone2), greaterThan(0))
111 fun `post count comparator sorts sone with same amount of posts and replies as equal`() {
112 val sone1 = object : IdOnlySone("1") {
113 override fun getPosts() = listOf(createPost(), createPost())
114 override fun getReplies() = setOf(createPostReply(), createPostReply())
116 val sone2 = object : IdOnlySone("2") {
117 override fun getPosts() = listOf(createPost(), createPost())
118 override fun getReplies() = setOf(createPostReply(), createPostReply())
120 assertThat(postCountComparator.compare(sone1, sone2), equalTo(0))
124 fun `image count comparator sorts Sones correctly if number of images is different`() {
125 val sone1 = object : IdOnlySone("1") {
126 override fun getRootAlbum() = AlbumImpl(this).also { it.addImage(createImage(this)) }
128 val sone2 = object : IdOnlySone("2") {
129 override fun getRootAlbum() = AlbumImpl(this).also { it.addImage(createImage(this)); it.addImage(createImage(this)) }
131 assertThat(imageCountComparator.compare(sone1, sone2), greaterThan(0))
135 fun `image count comparator treats Sones as equal if number of images is the same`() {
136 val sone1 = object : IdOnlySone("1") {
137 override fun getRootAlbum() = AlbumImpl(this).also { it.addImage(createImage(this)) }
139 val sone2 = object : IdOnlySone("2") {
140 override fun getRootAlbum() = AlbumImpl(this).also { it.addImage(createImage(this)) }
142 assertThat(imageCountComparator.compare(sone1, sone2), equalTo(0))
146 fun `allAlbums returns all albums of a Sone but the root album`() {
147 val sone = object : IdOnlySone("1") {
148 private val rootAlbum = AlbumImpl(this)
149 override fun getRootAlbum() = rootAlbum
151 val album1 = AlbumImpl(sone).also(sone.rootAlbum::addAlbum)
152 val album11 = AlbumImpl(sone).also(album1::addAlbum)
153 val album2 = AlbumImpl(sone).also(sone.rootAlbum::addAlbum)
154 assertThat(sone.allAlbums, contains<Album>(album1, album11, album2))
158 fun `allImages returns all images of a Sone`() {
159 val sone = object : IdOnlySone("1") {
160 private val rootAlbum = AlbumImpl(this)
161 override fun getRootAlbum() = rootAlbum
163 val album1 = AlbumImpl(sone).also(sone.rootAlbum::addAlbum)
164 val album11 = AlbumImpl(sone).also(album1::addAlbum)
165 val album2 = AlbumImpl(sone).also(sone.rootAlbum::addAlbum)
166 val image1 = createImage(sone).also(album1::addImage)
167 val image11 = createImage(sone).also(album11::addImage)
168 val image2 = createImage(sone).also(album2::addImage)
169 assertThat(sone.allImages, containsInAnyOrder(image1, image11, image2))