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