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