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.*
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) }
16 override fun describeTo(description: Description) {
17 description.appendText("name is ").appendValue(name)
18 .appendText(", value is ").appendValue(value)
22 fun <T : Any> compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit) =
23 false.takeUnless { comparison(value) }
24 ?.also { onError(value) }
26 fun <K, V> isEmptyMap() = object : TypeSafeDiagnosingMatcher<Map<K, V>>() {
27 override fun describeTo(description: Description) {
28 description.appendText("empty map")
31 override fun matchesSafely(item: Map<K, V>, mismatchDescription: Description) =
32 item.isEmpty().onFalse {
33 mismatchDescription.appendText("was ").appendValue(item)
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)
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)
50 mismatchDescription.appendText("not trusted")
54 override fun describeTo(description: Description) {
56 .appendText("trusted by ").appendValue(ownIdentity)
57 .appendText(" with ").appendValue(trust)
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)
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)
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)
84 override fun describeTo(description: Description) {
86 .appendText("simple field set with key ").appendValue(name)
87 .appendText(", value ").appendValue(valueMatcher)
92 * [TypeSafeDiagnosingMatcher] implementation that aims to cut down boilerplate on verifying the attributes
93 * of typical container objects.
95 class AttributeMatcher<T>(private val objectName: String) : TypeSafeDiagnosingMatcher<T>() {
97 private data class AttributeToMatch<T, V>(
100 val matcher: Matcher<out V>
103 private val attributesToMatch = mutableListOf<AttributeToMatch<T, *>>()
106 * Adds an attribute to check for equality, returning `this`.
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)))
113 * Adds an attribute to check with the given [hamcrest matcher][Matcher].
115 fun <V> addAttribute(name: String, getter: (T) -> V, matcher: Matcher<out V>) = apply {
116 attributesToMatch.add(AttributeToMatch(name, getter, matcher))
119 override fun describeTo(description: Description) {
120 attributesToMatch.forEachIndexed { index, attributeToMatch ->
122 description.appendText("$objectName with ")
124 description.appendText(", ")
126 attributeToMatch.matcher.describeTo(description)
130 override fun matchesSafely(item: T, mismatchDescription: Description): Boolean =
131 attributesToMatch.fold(true) { matches, attributeToMatch ->
135 if (!attributeToMatch.matcher.matches(attributeToMatch.getter(item))) {
136 mismatchDescription.appendText("but ${attributeToMatch.name} ")
137 attributeToMatch.matcher.describeMismatch(attributeToMatch.getter(item), mismatchDescription)