1 package net.pterodactylus.sone.test
3 import freenet.support.*
4 import net.pterodactylus.sone.freenet.wot.*
5 import net.pterodactylus.sone.utils.*
6 import net.pterodactylus.util.web.*
8 import org.hamcrest.Matchers.*
11 * Returns a [hamcrest matcher][Matcher] constructed from the given predicate.
13 fun <T> matches(description: String? = null, predicate: (T) -> Boolean) = object : TypeSafeDiagnosingMatcher<T>() {
15 override fun matchesSafely(item: T, mismatchDescription: Description) =
16 predicate(item).onFalse {
17 mismatchDescription.appendValue(item).appendText(" did not match predicate")
20 override fun describeTo(description: Description) {
21 description.appendText("matches predicate ").appendValue(predicate)
25 description?.let { describedAs(description, matcher) } ?: matcher
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) }
34 override fun describeTo(description: Description) {
35 description.appendText("name is ").appendValue(name)
36 .appendText(", value is ").appendValue(value)
40 fun <T : Any> compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit) =
41 false.takeUnless { comparison(value) }
42 ?.also { onError(value) }
44 fun <K, V> isEmptyMap() = object : TypeSafeDiagnosingMatcher<Map<K, V>>() {
45 override fun describeTo(description: Description) {
46 description.appendText("empty map")
49 override fun matchesSafely(item: Map<K, V>, mismatchDescription: Description) =
50 item.isEmpty().onFalse {
51 mismatchDescription.appendText("was ").appendValue(item)
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)
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)
68 mismatchDescription.appendText("not trusted")
72 override fun describeTo(description: Description) {
74 .appendText("trusted by ").appendValue(ownIdentity)
75 .appendText(" with ").appendValue(trust)
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)
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)
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)
102 override fun describeTo(description: Description) {
104 .appendText("simple field set with key ").appendValue(name)
105 .appendText(", value ").appendValue(valueMatcher)
110 * [TypeSafeDiagnosingMatcher] implementation that aims to cut down boilerplate on verifying the attributes
111 * of typical container objects.
113 class AttributeMatcher<T>(private val objectName: String) : TypeSafeDiagnosingMatcher<T>() {
115 private data class AttributeToMatch<T, V>(
117 val getter: (T) -> V,
118 val matcher: Matcher<out V>
121 private val attributesToMatch = mutableListOf<AttributeToMatch<T, *>>()
124 * Adds an attribute to check for equality, returning `this`.
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)))
131 * Adds an attribute to check with the given [hamcrest matcher][Matcher].
133 fun <V> addAttribute(name: String, getter: (T) -> V, matcher: Matcher<out V>) = apply {
134 attributesToMatch.add(AttributeToMatch(name, getter, matcher))
137 override fun describeTo(description: Description) {
138 attributesToMatch.forEachIndexed { index, attributeToMatch ->
140 description.appendText("$objectName with ")
142 description.appendText(", ")
144 attributeToMatch.matcher.describeTo(description)
148 override fun matchesSafely(item: T, mismatchDescription: Description): Boolean =
149 attributesToMatch.fold(true) { matches, attributeToMatch ->
153 if (!attributeToMatch.matcher.matches(attributeToMatch.getter(item))) {
154 mismatchDescription.appendText("but ${attributeToMatch.name} ")
155 attributeToMatch.matcher.describeMismatch(attributeToMatch.getter(item), mismatchDescription)