🔀 Merge “release/v81” into “master”
[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.freenet.wot.*
5 import net.pterodactylus.sone.utils.*
6 import net.pterodactylus.util.web.*
7 import org.hamcrest.*
8 import org.hamcrest.Matchers
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 /**
111  * [TypeSafeDiagnosingMatcher] implementation that aims to cut down boilerplate on verifying the attributes
112  * of typical container objects.
113  */
114 class AttributeMatcher<T>(private val objectName: String) : TypeSafeDiagnosingMatcher<T>() {
115
116         private data class AttributeToMatch<T, V>(
117                         val name: String,
118                         val getter: (T) -> V,
119                         val matcher: Matcher<out V>
120         )
121
122         private val attributesToMatch = mutableListOf<AttributeToMatch<T, *>>()
123
124         /**
125          * Adds an attribute to check for equality, returning `this`.
126          */
127         fun <V> addAttribute(name: String, expected: V, getter: (T) -> V): AttributeMatcher<T> = apply {
128                 attributesToMatch.add(AttributeToMatch(name, getter, describedAs("$name %0", equalTo(expected), expected)))
129         }
130
131         /**
132          * Adds an attribute to check with the given [hamcrest matcher][Matcher].
133          */
134         fun <V> addAttribute(name: String, getter: (T) -> V, matcher: Matcher<out V>) = apply {
135                 attributesToMatch.add(AttributeToMatch(name, getter, matcher))
136         }
137
138         override fun describeTo(description: Description) {
139                 attributesToMatch.forEachIndexed { index, attributeToMatch ->
140                         if (index == 0) {
141                                 description.appendText("$objectName with ")
142                         } else {
143                                 description.appendText(", ")
144                         }
145                         attributeToMatch.matcher.describeTo(description)
146                 }
147         }
148
149         override fun matchesSafely(item: T, mismatchDescription: Description): Boolean =
150                         attributesToMatch.fold(true) { matches, attributeToMatch ->
151                                 if (!matches) {
152                                         false
153                                 } else {
154                                         if (!attributeToMatch.matcher.matches(attributeToMatch.getter(item))) {
155                                                 mismatchDescription.appendText("but ${attributeToMatch.name} ")
156                                                 attributeToMatch.matcher.describeMismatch(attributeToMatch.getter(item), mismatchDescription)
157                                                 false
158                                         } else {
159                                                 true
160                                         }
161                                 }
162                         }
163
164 }