-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/test/scala/com/nthportal/shell/async/compat/InputActionTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(_)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters