Skip to content

Commit

Permalink
Add tests for compat.InputAction
Browse files Browse the repository at this point in the history
  • Loading branch information
NthPortal committed Jan 13, 2017
1 parent a0863e7 commit 58c3db7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.nthportal.shell.async.compat

import java.util
import java.util.Collections
import java.util.concurrent.TimeUnit
import java.util.function.Function

import com.nthportal.shell.SimpleSpec
import com.nthportal.shell.compat.impl.TestCommand
import com.nthportal.shell.compat.{Command, Shell}
import com.nthportal.shell.impl.StatefulOutputProvider
import com.nthportal.shell.parsers.WhitespaceDelineatingParser

import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.util.control.NoStackTrace

class InputActionTest extends SimpleSpec {

behavior of s"${classOf[InputAction[_]].getSimpleName} (Java)"

it should "execute an execution properly" in {
val os = StatefulOutputProvider()
val shell = Shell.create(WhitespaceDelineatingParser, os, Collections.emptyList[Command]())
val iac = inputActionCreator(shell)

val ex = iac.execution("not-a-command")
val f = ex.future
f.isCompleted should be(false)
os.writtenTo should be(false)

ex.doAction(shell)
f.isCompleted should be(true)
os.writtenTo should be(true)
}

it should "execute a tab-completion properly" in {
val t = TestCommand()
val shell = Shell.create(WhitespaceDelineatingParser, StatefulOutputProvider(), util.Arrays.asList[Command](t))
val iac = inputActionCreator(shell)

val tc = iac.tabCompletion(t.name)
val f = tc.future
f.isCompleted should be(false)

tc.doAction(shell)
f.isCompleted should be(true)
tc.completionStage.toCompletableFuture.get(0, TimeUnit.SECONDS) should contain(t.name)
}

it should "return a failed Future if the action throws an exception" in {
val shell = Shell.create(WhitespaceDelineatingParser, StatefulOutputProvider(), Collections.emptyList[Command]())
val iac = inputActionCreator(shell)

val ex = new Exception with NoStackTrace
val action: InputAction[Unit] = iac.inputAction(_ => throw ex)
val f = action.future

action.doAction(shell)
f.isCompleted should be(true)
Await.result(f.failed, Duration.Zero) should be(ex)
}

private def inputActionCreator(shell: Shell): InputActionCreator = new InputActionCreator {
implicit def mapping = Map(shell.underlying -> shell)

override def inputAction[T](action: Function[Shell, T]) = new InputAction[T](action(_))
}
}
2 changes: 1 addition & 1 deletion src/test/scala/com/nthportal/shell/compat/ShellTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ShellTest extends SimpleSpec {
private val commands = util.Arrays.asList(testCommand, WriteCommand)
private val shell = Shell.create(TestParser, outputProvider, commands)

behavior of classOf[Shell].getSimpleName
behavior of s"${classOf[Shell].getSimpleName} (Java)"

it should "produce equivalent shells with both factory methods" in {
val shell2 = Shell.create(asScalaLineParser(TestParser), outputProvider, commands)
Expand Down

0 comments on commit 58c3db7

Please sign in to comment.