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