1 package net.pterodactylus.sone.test
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.*
9 import org.hamcrest.Matchers.*
12 * A kotlin-ified version of Hamcrest’s [anything()][anything]. It matches
13 * everything and has the right type, too!
15 inline fun <reified T> everything(): Matcher<T> = any(T::class.java)
18 * Returns a [hamcrest matcher][Matcher] constructed from the given predicate.
20 fun <T> matches(description: String? = null, predicate: (T) -> Boolean) = object : TypeSafeDiagnosingMatcher<T>() {
22 override fun matchesSafely(item: T, mismatchDescription: Description) =
23 predicate(item).onFalse {
24 mismatchDescription.appendValue(item).appendText(" did not match predicate")
27 override fun describeTo(description: Description) {
28 description.appendText("matches predicate ").appendValue(predicate)
32 description?.let { describedAs(description, matcher) } ?: matcher
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) }
41 override fun describeTo(description: Description) {
42 description.appendText("name is ").appendValue(name)
43 .appendText(", value is ").appendValue(value)
47 fun <T> handleMatcher(matcher: Matcher<T>, item: T, mismatchDescription: Description) =
49 .onFalse { matcher.describeMismatch(item, mismatchDescription) }
51 fun <T : Any> compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit) =
52 false.takeUnless { comparison(value) }
53 ?.also { onError(value) }
55 fun <K, V> isEmptyMap() = object : TypeSafeDiagnosingMatcher<Map<K, V>>() {
56 override fun describeTo(description: Description) {
57 description.appendText("empty map")
60 override fun matchesSafely(item: Map<K, V>, mismatchDescription: Description) =
61 item.isEmpty().onFalse {
62 mismatchDescription.appendText("was ").appendValue(item)
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)
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)
79 mismatchDescription.appendText("not trusted")
83 override fun describeTo(description: Description) {
85 .appendText("trusted by ").appendValue(ownIdentity)
86 .appendText(" with ").appendValue(trust)
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)
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)
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)
111 override fun describeTo(description: Description) {
113 .appendText("simple field set with key ").appendValue(name)
114 .appendText(", value ").appendValue(valueMatcher)
118 fun isPost(isRecipientId: Matcher<String?> = any(String::class.java)) = AttributeMatcher<Post>("post")
119 .addAttribute("recipient ID", { it.recipientId.orNull() }, isRecipientId)
122 * [TypeSafeDiagnosingMatcher] implementation that aims to cut down boilerplate on verifying the attributes
123 * of typical container objects.
125 class AttributeMatcher<T>(private val objectName: String) : TypeSafeDiagnosingMatcher<T>() {
127 private data class AttributeToMatch<T, V>(
129 val getter: (T) -> V,
130 val matcher: Matcher<out V>
133 private val attributesToMatch = mutableListOf<AttributeToMatch<T, *>>()
136 * Adds an attribute to check for equality, returning `this`.
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)))
143 * Adds an attribute to check with the given [hamcrest matcher][Matcher].
145 fun <V> addAttribute(name: String, getter: (T) -> V, matcher: Matcher<out V>) = apply {
146 attributesToMatch.add(AttributeToMatch(name, getter, matcher))
149 override fun describeTo(description: Description) {
150 attributesToMatch.forEachIndexed { index, attributeToMatch ->
152 description.appendText("$objectName with ")
154 description.appendText(", ")
156 attributeToMatch.matcher.describeTo(description)
160 override fun matchesSafely(item: T, mismatchDescription: Description): Boolean =
161 attributesToMatch.fold(true) { matches, attributeToMatch ->
165 if (!attributeToMatch.matcher.matches(attributeToMatch.getter(item))) {
166 mismatchDescription.appendText("but ${attributeToMatch.name} ")
167 attributeToMatch.matcher.describeMismatch(attributeToMatch.getter(item), mismatchDescription)