/cli/src/test/scala/com/salesforce/op/cli/gen/UserIOTest.scala

https://github.com/salesforce/TransmogrifAI · Scala · 88 lines · 41 code · 13 blank · 34 comment · 3 complexity · 71272f5c4da6e3a9c7b729f75ae63459 MD5 · raw file

  1. /*
  2. * Copyright (c) 2017, Salesforce.com, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * * Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package com.salesforce.op.cli.gen
  31. import com.salesforce.op.test.TestCommon
  32. import org.junit.runner.RunWith
  33. import org.scalatest.junit.JUnitRunner
  34. import org.scalatest.{Assertions, FlatSpec}
  35. /**
  36. * Test for Console methods.
  37. */
  38. @RunWith(classOf[JUnitRunner])
  39. class UserIOTest extends FlatSpec with TestCommon with Assertions {
  40. private case class Oracle(answers: String*) extends UserIO {
  41. private var i = -1
  42. var question = "---"
  43. override def readLine(q: String): Option[String] = {
  44. question = q
  45. i += 1
  46. if (i < answers.length) Some(answers(i)) else throw new IllegalStateException(s"Out of answers, q=$q")
  47. }
  48. }
  49. Spec[UserIO] should "do qna" in {
  50. // @see https://www.urbandictionary.com/define.php?term=aks
  51. def aksme(q: String, answers: String*): Option[String] = {
  52. Oracle(answers: _*).qna(q, _.length == 1, Map("2*3" -> "6", "3+2" -> "5"))
  53. }
  54. aksme("2+2", "11", "22", "?") shouldBe Some("?")
  55. aksme("2+2", "4", "5", "?") shouldBe Some("4")
  56. aksme("2+3", "44", "", "?") shouldBe Some("?")
  57. aksme("2*3", "4", "?") shouldBe Some("6")
  58. aksme("3+2", "4", "?") shouldBe Some("5")
  59. }
  60. it should "ask" in {
  61. // @see https://www.urbandictionary.com/define.php?term=aks
  62. def aksme[Int](q: String, opts: Map[Int, List[String]], answers: String*): (String, Int) = {
  63. val console = Oracle(answers: _*)
  64. val answer = console.ask(q, opts) getOrElse fail(s"A problem answering question $q")
  65. (console.question, answer)
  66. }
  67. an[IllegalStateException] should be thrownBy
  68. aksme("what is your name?", Map(1 -> List("one", "uno")), "11", "1", "?")
  69. aksme("what is your name?",
  70. Map(
  71. 1 -> List("Nessuno", "Nobody"),
  72. 2 -> List("Ishmael", "Gantenbein")),
  73. "5", "1", "?") shouldBe("what is your name? [0] Nessuno [1] Ishmael: ", 2)
  74. }
  75. }