🎨 Store shells instead of full posts in in-memory database
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / test / Matchers.kt
1 package net.pterodactylus.sone.test
2
3 import freenet.support.*
4 import net.pterodactylus.sone.data.Post
5 import net.pterodactylus.sone.freenet.wot.*
6 import net.pterodactylus.sone.utils.*
7 import net.pterodactylus.util.web.*
8 import org.hamcrest.*
9 import org.hamcrest.Matchers.*
10
11 /**
12  * Returns a [hamcrest matcher][Matcher] constructed from the given predicate.
13  */
14 fun <T> matches(description: String? = null, predicate: (T) -> Boolean) = object : TypeSafeDiagnosingMatcher<T>() {
15
16         override fun matchesSafely(item: T, mismatchDescription: Description) =
17                         predicate(item).onFalse {
18                                 mismatchDescription.appendValue(item).appendText(" did not match predicate")
19                         }
20
21         override fun describeTo(description: Description) {
22                 description.appendText("matches predicate ").appendValue(predicate)
23         }
24
25 }.let { matcher ->
26         description?.let { describedAs(description, matcher) } ?: matcher
27 }
28
29 fun hasHeader(name: String, value: String) = object : TypeSafeDiagnosingMatcher<Header>() {
30         override fun matchesSafely(item: Header, mismatchDescription: Description) =
31                         compare(item.name, { it.equals(name, ignoreCase = true) }) { mismatchDescription.appendText("name is ").appendValue(it) }
32                                         ?: compare(item.hasValue(value), { it }) { mismatchDescription.appendText("does not have value ").appendValue(value) }
33                                         ?: true
34
35         override fun describeTo(description: Description) {
36                 description.appendText("name is ").appendValue(name)
37                                 .appendText(", value is ").appendValue(value)
38         }
39 }
40
41 fun <T : Any> compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit) =
42                 false.takeUnless { comparison(value) }
43                                 ?.also { onError(value) }
44
45 fun <K, V> isEmptyMap() = object : TypeSafeDiagnosingMatcher<Map<K, V>>() {
46         override fun describeTo(description: Description) {
47                 description.appendText("empty map")
48         }
49
50         override fun matchesSafely(item: Map<K, V>, mismatchDescription: Description) =
51                         item.isEmpty().onFalse {
52                                 mismatchDescription.appendText("was ").appendValue(item)
53                         }
54 }
55
56 fun isTrust(trust: Int?, score: Int?, rank: Int?) =
57                 AttributeMatcher<Trust>("trust")
58                                 .addAttribute("trust", trust, Trust::explicit)
59                                 .addAttribute("score", score, Trust::implicit)
60                                 .addAttribute("rank", rank, Trust::distance)
61
62 fun isTrusted(ownIdentity: OwnIdentity, trust: Matcher<Trust>) = object : TypeSafeDiagnosingMatcher<Identity>() {
63         override fun matchesSafely(item: Identity, mismatchDescription: Description) =
64                         item.getTrust(ownIdentity)?.let { foundTrust ->
65                                 trust.matches(foundTrust).onFalse {
66                                         trust.describeMismatch(foundTrust, mismatchDescription)
67                                 }
68                         } ?: {
69                                 mismatchDescription.appendText("not trusted")
70                                 false
71                         }()
72
73         override fun describeTo(description: Description) {
74                 description
75                                 .appendText("trusted by ").appendValue(ownIdentity)
76                                 .appendText(" with ").appendValue(trust)
77         }
78 }
79
80 fun isIdentity(id: String, nickname: String?, requestUri: String, contexts: Matcher<out Iterable<String>>, properties: Matcher<out Map<out String, String>>) =
81                 AttributeMatcher<Identity>("identity")
82                                 .addAttribute("id", id, Identity::getId)
83                                 .addAttribute("nickname", nickname, Identity::getNickname)
84                                 .addAttribute("requestUri", requestUri, Identity::getRequestUri)
85                                 .addAttribute("contexts", Identity::getContexts, contexts)
86                                 .addAttribute("properties", Identity::getProperties, properties)
87
88 fun isOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: Matcher<Iterable<String>>, properties: Matcher<Map<out String, String>>) =
89                 AttributeMatcher<OwnIdentity>("own identity")
90                                 .addAttribute("id", id, OwnIdentity::getId)
91                                 .addAttribute("nickname", nickname, OwnIdentity::getNickname)
92                                 .addAttribute("request uri", requestUri, OwnIdentity::getRequestUri)
93                                 .addAttribute("insert uri", insertUri, OwnIdentity::getInsertUri)
94                                 .addAttribute("contexts", OwnIdentity::getContexts, contexts)
95                                 .addAttribute("properties", OwnIdentity::getProperties, properties)
96
97 fun hasField(name: String, valueMatcher: Matcher<String>) = object : TypeSafeDiagnosingMatcher<SimpleFieldSet>() {
98         override fun matchesSafely(item: SimpleFieldSet, mismatchDescription: Description) =
99                         valueMatcher.matches(item.get(name)).onFalse {
100                                 valueMatcher.describeMismatch(item, mismatchDescription)
101                         }
102
103         override fun describeTo(description: Description) {
104                 description
105                                 .appendText("simple field set with key ").appendValue(name)
106                                 .appendText(", value ").appendValue(valueMatcher)
107         }
108 }
109
110 fun isPost(isRecipientId: Matcher<String?> = any(String::class.java)) = AttributeMatcher<Post>("post")
111                 .addAttribute("recipient ID", { it.recipientId.orNull() }, isRecipientId)
112
113 /**
114  * [TypeSafeDiagnosingMatcher] implementation that aims to cut down boilerplate on verifying the attributes
115  * of typical container objects.
116  */
117 class AttributeMatcher<T>(private val objectName: String) : TypeSafeDiagnosingMatcher<T>() {
118
119         private data class AttributeToMatch<T, V>(
120                         val name: String,
121                         val getter: (T) -> V,
122                         val matcher: Matcher<out V>
123         )
124
125         private val attributesToMatch = mutableListOf<AttributeToMatch<T, *>>()
126
127         /**
128          * Adds an attribute to check for equality, returning `this`.
129          */
130         fun <V> addAttribute(name: String, expected: V, getter: (T) -> V): AttributeMatcher<T> = apply {
131                 attributesToMatch.add(AttributeToMatch(name, getter, describedAs("$name %0", equalTo(expected), expected)))
132         }
133
134         /**
135          * Adds an attribute to check with the given [hamcrest matcher][Matcher].
136          */
137         fun <V> addAttribute(name: String, getter: (T) -> V, matcher: Matcher<out V>) = apply {
138                 attributesToMatch.add(AttributeToMatch(name, getter, matcher))
139         }
140
141         override fun describeTo(description: Description) {
142                 attributesToMatch.forEachIndexed { index, attributeToMatch ->
143                         if (index == 0) {
144                                 description.appendText("$objectName with ")
145                         } else {
146                                 description.appendText(", ")
147                         }
148                         attributeToMatch.matcher.describeTo(description)
149                 }
150         }
151
152         override fun matchesSafely(item: T, mismatchDescription: Description): Boolean =
153                         attributesToMatch.fold(true) { matches, attributeToMatch ->
154                                 if (!matches) {
155                                         false
156                                 } else {
157                                         if (!attributeToMatch.matcher.matches(attributeToMatch.getter(item))) {
158                                                 mismatchDescription.appendText("but ${attributeToMatch.name} ")
159                                                 attributeToMatch.matcher.describeMismatch(attributeToMatch.getter(item), mismatchDescription)
160                                                 false
161                                         } else {
162                                                 true
163                                         }
164                                 }
165                         }
166
167 }