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 * Returns a [hamcrest matcher][Matcher] constructed from the given predicate.
14 fun <T> matches(description: String? = null, predicate: (T) -> Boolean) = object : TypeSafeDiagnosingMatcher<T>() {
16 override fun matchesSafely(item: T, mismatchDescription: Description) =
17 predicate(item).onFalse {
18 mismatchDescription.appendValue(item).appendText(" did not match predicate")
21 override fun describeTo(description: Description) {
22 description.appendText("matches predicate ").appendValue(predicate)
26 description?.let { describedAs(description, matcher) } ?: matcher
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) }
35 override fun describeTo(description: Description) {
36 description.appendText("name is ").appendValue(name)
37 .appendText(", value is ").appendValue(value)
41 fun <T : Any> compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit) =
42 false.takeUnless { comparison(value) }
43 ?.also { onError(value) }
45 fun <K, V> isEmptyMap() = object : TypeSafeDiagnosingMatcher<Map<K, V>>() {
46 override fun describeTo(description: Description) {
47 description.appendText("empty map")
50 override fun matchesSafely(item: Map<K, V>, mismatchDescription: Description) =
51 item.isEmpty().onFalse {
52 mismatchDescription.appendText("was ").appendValue(item)
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)
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)
69 mismatchDescription.appendText("not trusted")
73 override fun describeTo(description: Description) {
75 .appendText("trusted by ").appendValue(ownIdentity)
76 .appendText(" with ").appendValue(trust)
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)
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)
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)
103 override fun describeTo(description: Description) {
105 .appendText("simple field set with key ").appendValue(name)
106 .appendText(", value ").appendValue(valueMatcher)
110 fun isPost(isRecipientId: Matcher<String?> = any(String::class.java)) = AttributeMatcher<Post>("post")
111 .addAttribute("recipient ID", { it.recipientId.orNull() }, isRecipientId)
114 * [TypeSafeDiagnosingMatcher] implementation that aims to cut down boilerplate on verifying the attributes
115 * of typical container objects.
117 class AttributeMatcher<T>(private val objectName: String) : TypeSafeDiagnosingMatcher<T>() {
119 private data class AttributeToMatch<T, V>(
121 val getter: (T) -> V,
122 val matcher: Matcher<out V>
125 private val attributesToMatch = mutableListOf<AttributeToMatch<T, *>>()
128 * Adds an attribute to check for equality, returning `this`.
130 fun <V> addAttribute(name: String, expected: V, getter: (T) -> V): AttributeMatcher<T> = apply {
131 attributesToMatch.add(AttributeToMatch(name, getter, describedAs("$name %0", equalTo(expected), expected)))
135 * Adds an attribute to check with the given [hamcrest matcher][Matcher].
137 fun <V> addAttribute(name: String, getter: (T) -> V, matcher: Matcher<out V>) = apply {
138 attributesToMatch.add(AttributeToMatch(name, getter, matcher))
141 override fun describeTo(description: Description) {
142 attributesToMatch.forEachIndexed { index, attributeToMatch ->
144 description.appendText("$objectName with ")
146 description.appendText(", ")
148 attributeToMatch.matcher.describeTo(description)
152 override fun matchesSafely(item: T, mismatchDescription: Description): Boolean =
153 attributesToMatch.fold(true) { matches, attributeToMatch ->
157 if (!attributeToMatch.matcher.matches(attributeToMatch.getter(item))) {
158 mismatchDescription.appendText("but ${attributeToMatch.name} ")
159 attributeToMatch.matcher.describeMismatch(attributeToMatch.getter(item), mismatchDescription)