6b7e51d5b9dc587c60bf1324c11bdb060bb3f792
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / data / SoneTest.kt
1 /**
2  * Sone - SoneTest.kt - Copyright © 2020 David ‘Bombe’ Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.data
19
20 import net.pterodactylus.sone.data.impl.*
21 import net.pterodactylus.sone.test.*
22 import org.hamcrest.MatcherAssert.*
23 import org.hamcrest.Matchers.*
24 import kotlin.test.*
25
26 /**
27  * Unit test for functions in Sone.
28  */
29 class SoneTest {
30
31         @Test
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" }
35                 }
36                 val sone2 = object : IdOnlySone("2") {
37                         override fun getProfile() = Profile(this).apply { firstName = "Right" }
38                 }
39                 assertThat(niceNameComparator.compare(sone1, sone2), lessThan(0))
40         }
41
42         @Test
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" }
46                 }
47                 val sone2 = object : IdOnlySone("2") {
48                         override fun getProfile() = Profile(this).apply { firstName = "Left" }
49                 }
50                 assertThat(niceNameComparator.compare(sone1, sone2), lessThan(0))
51         }
52
53         @Test
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" }
57                 }
58                 val sone2 = object : IdOnlySone("1") {
59                         override fun getProfile() = Profile(this).apply { firstName = "Left" }
60                 }
61                 assertThat(niceNameComparator.compare(sone1, sone2), equalTo(0))
62         }
63
64         @Test
65         fun `last activity comparator correctly compares Sones by last activity`() {
66                 val sone1 = object : IdOnlySone("1") {
67                         override fun getTime() = 1000L
68                 }
69                 val sone2 = object : IdOnlySone("2") {
70                         override fun getTime() = 2000L
71                 }
72                 assertThat(lastActivityComparator.compare(sone1, sone2), greaterThan(0))
73         }
74
75         @Test
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
79                 }
80                 val sone2 = object : IdOnlySone("2") {
81                         override fun getTime() = 1000L
82                 }
83                 assertThat(lastActivityComparator.compare(sone1, sone2), equalTo(0))
84         }
85
86         @Test
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())
90                 }
91                 val sone2 = object : IdOnlySone("2") {
92                         override fun getPosts() = listOf(createPost(), createPost(), createPost())
93                 }
94                 assertThat(postCountComparator.compare(sone1, sone2), greaterThan(0))
95         }
96
97         @Test
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(emptyPostReply(), emptyPostReply())
102                 }
103                 val sone2 = object : IdOnlySone("2") {
104                         override fun getPosts() = listOf(createPost(), createPost())
105                         override fun getReplies() = setOf(emptyPostReply(), emptyPostReply(), emptyPostReply())
106                 }
107                 assertThat(postCountComparator.compare(sone1, sone2), greaterThan(0))
108         }
109
110         @Test
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(emptyPostReply(), emptyPostReply())
115                 }
116                 val sone2 = object : IdOnlySone("2") {
117                         override fun getPosts() = listOf(createPost(), createPost())
118                         override fun getReplies() = setOf(emptyPostReply(), emptyPostReply())
119                 }
120                 assertThat(postCountComparator.compare(sone1, sone2), equalTo(0))
121         }
122
123 }