package net.pterodactylus.sone.test
+import freenet.support.*
+import net.pterodactylus.sone.data.Post
import net.pterodactylus.sone.freenet.wot.*
import net.pterodactylus.sone.utils.*
import net.pterodactylus.util.web.*
import org.hamcrest.*
import org.hamcrest.Matchers.*
+/**
+ * A kotlin-ified version of Hamcrest’s [anything()][anything]. It matches
+ * everything and has the right type, too!
+ */
+inline fun <reified T> everything(): Matcher<T> = any(T::class.java)
+
+/**
+ * Returns a [hamcrest matcher][Matcher] constructed from the given predicate.
+ */
+fun <T> matches(description: String? = null, predicate: (T) -> Boolean) = object : TypeSafeDiagnosingMatcher<T>() {
+
+ override fun matchesSafely(item: T, mismatchDescription: Description) =
+ predicate(item).onFalse {
+ mismatchDescription.appendValue(item).appendText(" did not match predicate")
+ }
+
+ override fun describeTo(description: Description) {
+ description.appendText("matches predicate ").appendValue(predicate)
+ }
+
+}.let { matcher ->
+ description?.let { describedAs(description, matcher) } ?: matcher
+}
+
fun hasHeader(name: String, value: String) = object : TypeSafeDiagnosingMatcher<Header>() {
override fun matchesSafely(item: Header, mismatchDescription: Description) =
compare(item.name, { it.equals(name, ignoreCase = true) }) { mismatchDescription.appendText("name is ").appendValue(it) }
}
}
+fun <T> handleMatcher(matcher: Matcher<T>, item: T, mismatchDescription: Description) =
+ matcher.matches(item)
+ .onFalse { matcher.describeMismatch(item, mismatchDescription) }
+
fun <T : Any> compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit) =
false.takeUnless { comparison(value) }
?.also { onError(value) }
}
}
-fun isIdentity(id: String, nickname: String, requestUri: String, contexts: Matcher<out Iterable<String>>, properties: Matcher<out Map<out String, String>>) =
+fun isIdentity(id: String, nickname: String?, requestUri: String, contexts: Matcher<out Iterable<String>>, properties: Matcher<out Map<out String, String>>) =
AttributeMatcher<Identity>("identity")
.addAttribute("id", id, Identity::getId)
.addAttribute("nickname", nickname, Identity::getNickname)
.addAttribute("contexts", OwnIdentity::getContexts, contexts)
.addAttribute("properties", OwnIdentity::getProperties, properties)
+fun hasField(name: String, valueMatcher: Matcher<String>) = object : TypeSafeDiagnosingMatcher<SimpleFieldSet>() {
+ override fun matchesSafely(item: SimpleFieldSet, mismatchDescription: Description) =
+ handleMatcher(valueMatcher, item.get(name), mismatchDescription)
+
+ override fun describeTo(description: Description) {
+ description
+ .appendText("simple field set with key ").appendValue(name)
+ .appendText(", value ").appendValue(valueMatcher)
+ }
+}
+
+fun isPost(isRecipientId: Matcher<String?> = any(String::class.java)) = AttributeMatcher<Post>("post")
+ .addAttribute("recipient ID", { it.recipientId.orNull() }, isRecipientId)
+
/**
* [TypeSafeDiagnosingMatcher] implementation that aims to cut down boilerplate on verifying the attributes
* of typical container objects.