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