✅ Add test for AlwaysTrigger
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 21 Sep 2020 18:58:32 +0000 (20:58 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 21 Sep 2020 18:58:32 +0000 (20:58 +0200)
src/test/kotlin/net/pterodactylus/rhynodge/Testing.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/rhynodge/triggers/AlwaysTriggerTest.kt [new file with mode: 0644]

diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/Testing.kt b/src/test/kotlin/net/pterodactylus/rhynodge/Testing.kt
new file mode 100644 (file)
index 0000000..87a618a
--- /dev/null
@@ -0,0 +1,15 @@
+package net.pterodactylus.rhynodge
+
+import net.pterodactylus.rhynodge.output.Output
+import net.pterodactylus.rhynodge.states.StateManagerTest.TestState
+
+fun testQuery(state: State = TestState()): Query = TestQuery(state)
+fun testAction(): Action = TestAction()
+
+class TestQuery(private val state: State) : Query {
+       override fun state(): State = state
+}
+
+class TestAction : Action {
+       override fun execute(output: Output?) = Unit
+}
diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/triggers/AlwaysTriggerTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/triggers/AlwaysTriggerTest.kt
new file mode 100644 (file)
index 0000000..0a85bea
--- /dev/null
@@ -0,0 +1,53 @@
+package net.pterodactylus.rhynodge.triggers
+
+import net.pterodactylus.rhynodge.Reaction
+import net.pterodactylus.rhynodge.State
+import net.pterodactylus.rhynodge.states.FailedState
+import net.pterodactylus.rhynodge.states.StateManagerTest.TestState
+import net.pterodactylus.rhynodge.testAction
+import net.pterodactylus.rhynodge.testQuery
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.sameInstance
+import org.junit.Test
+
+class AlwaysTriggerTest {
+
+       @Test
+       fun `merging states returns the current state`() {
+               assertThat(trigger.mergeStates(previousState, successfulState), sameInstance(successfulState))
+       }
+
+       @Test
+       fun `successful state triggers`() {
+               trigger.mergeStates(previousState, successfulState)
+               assertThat(trigger.triggers(), equalTo(true))
+       }
+
+       @Test
+       fun `failed state also triggers`() {
+               trigger.mergeStates(previousState, failedState)
+               assertThat(trigger.triggers(), equalTo(true))
+       }
+
+       @Test
+       @Suppress("NonAsciiCharacters")
+       fun `output returns “true” for plain text`() {
+               trigger.mergeStates(previousState, successfulState)
+               val output = trigger.output(Reaction("Test", testQuery(), trigger, testAction()))
+               assertThat(output.text("text/plain"), equalTo("true"))
+       }
+
+       @Test
+       fun `output returns true in a div for html`() {
+               trigger.mergeStates(previousState, successfulState)
+               val output = trigger.output(Reaction("Test", testQuery(), trigger, testAction()))
+               assertThat(output.text("text/html"), equalTo("<div>true</div>"))
+       }
+
+       private val trigger = AlwaysTrigger()
+       private val previousState = TestState()
+       private val successfulState: State = TestState()
+       private val failedState: State = FailedState()
+
+}