|
| 1 | +package com.us.dsb.explore.algs.ttt.manual.ui |
| 2 | + |
| 3 | +import org.scalatest.LoneElement |
| 4 | +import org.scalatest.funspec.AnyFunSpec |
| 5 | +import org.scalatest.matchers.should.Matchers._ |
| 6 | + |
| 7 | +class TextIOTest extends AnyFunSpec { |
| 8 | + |
| 9 | + it/*describe*/("TextIO?:") { |
| 10 | + cancel() |
| 11 | + it("println ...") { |
| 12 | + } |
| 13 | + it("readLine ...") { |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + // Crude, manual stub and spy ConsoleIO. |
| 18 | + class TextConsoleIO(inputLines: String*) extends ConsoleIO { |
| 19 | + private var stringsToRead = inputLines |
| 20 | + private var printedStrings: List[String] = Nil; |
| 21 | + def getPrintedStrings: List[String] = printedStrings |
| 22 | + |
| 23 | + override def println(lineOrLines: String): Unit = { |
| 24 | + printedStrings ::= lineOrLines |
| 25 | + } |
| 26 | + |
| 27 | + override def readLine(prompt: String): String = { |
| 28 | + printedStrings ::= prompt |
| 29 | + |
| 30 | + val lineRead = stringsToRead.head // .get - unsafe - but crude anyway |
| 31 | + stringsToRead = stringsToRead.drop(1) |
| 32 | + lineRead |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + describe("ColoredConsoleTextIO") { |
| 37 | + import org.scalatest.LoneElement._ |
| 38 | + |
| 39 | + def getUUT(consoleIODouble: ConsoleIO): SegregatedTextIO = { |
| 40 | + // Demo: Try injecting "bad" UUT and see how failing conditions show up: |
| 41 | + new ColoredConsoleTextIO(consoleIODouble) |
| 42 | + //new PlainConsoleTextIO(consoleIODouble) |
| 43 | + } |
| 44 | + |
| 45 | + // Demo: Checking subconditions in multiple leaf-level tests: |
| 46 | + describe("printStateText should print given text plainly; output should:") { |
| 47 | + // Note: "lazy" avoids executing during ScalaTest's registration phase. |
| 48 | + lazy val printedStrings = { |
| 49 | + val consoleIODouble = new TextConsoleIO |
| 50 | + val uut = getUUT(consoleIODouble) |
| 51 | + |
| 52 | + uut.printStateText("text") |
| 53 | + |
| 54 | + consoleIODouble.getPrintedStrings |
| 55 | + } |
| 56 | + it("include given text") { |
| 57 | + printedStrings.loneElement should include ("text") |
| 58 | + } |
| 59 | + it("not include color/decoration escape sequences") { |
| 60 | + printedStrings.loneElement should not include (scala.io.AnsiColor.RED.take(1)) |
| 61 | + } |
| 62 | + it("include only given text") { |
| 63 | + printedStrings.loneElement should be ("text") |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + describe("printError should print given text in red; output should:") { |
| 68 | + lazy val printedStrings = { |
| 69 | + val consoleIODouble = new TextConsoleIO |
| 70 | + val uut = getUUT(consoleIODouble) |
| 71 | + |
| 72 | + uut.printError("given text") |
| 73 | + |
| 74 | + consoleIODouble.getPrintedStrings |
| 75 | + } |
| 76 | + it("include given text") { |
| 77 | + printedStrings.loneElement should include ("given text") |
| 78 | + } |
| 79 | + it("include red escape sequence") { |
| 80 | + printedStrings.loneElement should include (scala.io.AnsiColor.RED) // ?? in order too |
| 81 | + } |
| 82 | + it("include reset escape sequence") { |
| 83 | + printedStrings.loneElement should include (scala.io.AnsiColor.RESET) // ?? in order too |
| 84 | + } |
| 85 | + it("not include only given text") { |
| 86 | + printedStrings.loneElement should not be ("given text") |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Demo: checking subconditions with "should ... and ..." (in one test): |
| 91 | + it("printResult should print given text in bold (should ... and ...)") { |
| 92 | + val consoleIODouble = new TextConsoleIO |
| 93 | + val uut = getUUT(consoleIODouble) |
| 94 | + |
| 95 | + uut.printResult("given text") |
| 96 | + val printedStrings = consoleIODouble.getPrintedStrings |
| 97 | + |
| 98 | + // Note: "should ... and ..." reports only _first_condition that failed |
| 99 | + // (though error message does seem to include actual value). |
| 100 | + printedStrings.loneElement should ( |
| 101 | + include ("given text") and |
| 102 | + include (scala.io.AnsiColor.BOLD) and |
| 103 | + include (scala.io.AnsiColor.RESET) and |
| 104 | + not be ("given text")) |
| 105 | + } |
| 106 | + |
| 107 | + describe("readPromptedLine should print given prompt value and get input") { |
| 108 | + lazy val (printedStrings, lineRead) = { |
| 109 | + val consoleIODouble = new TextConsoleIO("given input") |
| 110 | + val uut = getUUT(consoleIODouble) |
| 111 | + |
| 112 | + val lineRead = uut.readPromptedLine("given text") |
| 113 | + |
| 114 | + (consoleIODouble.getPrintedStrings, lineRead) |
| 115 | + } |
| 116 | + describe("should print given prompted text in blue; output should:") { |
| 117 | + it("include given text") { |
| 118 | + printedStrings.loneElement should include("given text") |
| 119 | + } |
| 120 | + it("include blue escape sequence") { |
| 121 | + printedStrings.loneElement should include(scala.io.AnsiColor.BLUE) // ?? in order too |
| 122 | + } |
| 123 | + it("include reset escape sequence") { |
| 124 | + printedStrings.loneElement should include(scala.io.AnsiColor.RESET) // ?? in order too |
| 125 | + } |
| 126 | + it("not include only given text") { |
| 127 | + printedStrings.loneElement should not be ("given text") |
| 128 | + } |
| 129 | + } |
| 130 | + it("should read input line text") { |
| 131 | + lineRead should be ("given input") |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments