Skip to content
Snippets Groups Projects
Unverified Commit ed69819a authored by khofmans's avatar khofmans
Browse files

testing the frontend like a boss

parent 7ff065e7
Branches
No related tags found
No related merge requests found
package io.tripled.marsrover
import io.tripled.marsrover.commands.CreateGrid
import io.tripled.marsrover.domain.Grid
fun main() {
println("**************************")
println("** Mars Rover **")
println("**************************")
val commandlineIO = CommandlineShizzle()
Frontend(commandlineIO).readInput()
val commandlineIO = CommandlineIO()
Frontend(commandlineIO, RealGrids()).readInput()
}
class CommandlineShizzle : Frontend.IO{
class RealGrids : Grids {
private lateinit var lastSaved: Grid
override fun save(grid: Grid) {
lastSaved = grid
}
}
class CommandlineIO : Frontend.IO{
override fun input(): String = readLine()!!
override fun output(s: String) = println(s)
override fun outputLn(s: String) = println(s)
override fun output(s: String) = print(s)
}
class Frontend(private val ioHandling: IO) {
class Frontend(private val ioHandling: IO, val grids: Grids) {
interface IO {
fun input(): String
fun outputLn(s: String)
fun output(s: String)
}
fun readInput() {
ioHandling.output("> q to quit")
ioHandling.outputLn("> q to quit")
var input: String
var firstTime = true
do {
if (firstTime) {
ioHandling.outputLn("Determine the maxCoordinate of the simulation by setting the maximum coordinate [0-100]")
ioHandling.output("[Enter max coordinate] : ")
input = ioHandling.input()
ioHandling.output("I read :$input")
CreateGrid(grids).create(input, object : CreateGrid.Presenter {
override fun badInput(feedback: String) {
ioHandling.outputLn("[$feedback] is an invalid Simulation maxCoordinate")
}
override fun succes() {
ioHandling.outputLn("Simulation with max coordinate [5] created successfully. Simulation contains [36] coordinates")
}
})
firstTime = false
}
input = ioHandling.input()
ioHandling.outputLn("I read :$input")
if (!isQuit(input)) {
ioHandling.output("[Please enter a command] :")
}
} while (!isQuit(input))
ioHandling.output("*********END*****************")
ioHandling.outputLn("*********END*****************")
}
private fun isQuit(input: String): Boolean {
......
......@@ -6,11 +6,14 @@ import io.tripled.marsrover.domain.Grid
class CreateGrid(private val grids: Grids) {
interface Presenter {
fun badInput(feedback: String)
fun succes()
}
fun create(gridSize: String, presenter: Presenter) {
try {
grids.save(Grid(gridSize.toInt()))
val grid = Grid(gridSize.toInt())
grids.save(grid)
presenter.succes()
} catch (e: Exception){
presenter.badInput(gridSize)
}
......
package io.tripled.marsrover
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
internal class FrontendTests {
val fakeGrids: FakeGrids = FakeGrids()
@Disabled
@Test
fun `check the quit functionality`() {
// given
......@@ -11,28 +15,56 @@ internal class FrontendTests {
val ioHandling = FakeIO(input)
// when
Frontend(ioHandling).readInput()
Frontend(ioHandling, fakeGrids).readInput()
// then
println(ioHandling.output)
assertEquals(ioHandling.output.trimIndent(), """> q to quit
I read :q
*********END*****************""".trimIndent())
}
@Test
fun `allow grid input size`() {
// given
val input = """45
q""".trimMargin()
val ioHandling = FakeIO(input)
// when
Frontend(ioHandling, fakeGrids).readInput()
// then
println(ioHandling.output)
assertEquals(ioHandling.output.trimIndent(), """> q to quit
Determine the maxCoordinate of the simulation by setting the maximum coordinate [0-100]
[Enter max coordinate] : 45
Simulation with max coordinate [5] created successfully. Simulation contains [36] coordinates
q
I read :q
*********END*****************""".trimIndent())
}
}
class FakeIO(var input: String) : Frontend.IO {
class FakeIO(val input: String) : Frontend.IO {
var inputList = input.split("\n").toMutableList()
var output = ""
override fun input(): String {
val linesOfInput = input.split("\n")
input = linesOfInput.subList(1, linesOfInput.size).joinToString { "\n" }
return linesOfInput.first()
val currentInput = inputList.first()
inputList.removeAt(0)
output += currentInput + "\n"
return currentInput
}
override fun output(s: String) {
override fun outputLn(s: String) {
output += s + "\n"
}
override fun output(s: String) {
output += s
}
}
\ No newline at end of file
......@@ -3,33 +3,34 @@ package io.tripled.marsrover
import io.tripled.marsrover.commands.CreateGrid
import io.tripled.marsrover.domain.Grid
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import kotlin.test.assertTrue
internal class GridTests {
val grids = FakeGrids()
@Test
fun `creates a grid`() {
// given
val gridSize = "6"
val grids = FakeGrids()
val presenter = TestPresenter(grids)
// when
CreateGrid(grids).create(gridSize, TestPresenter())
CreateGrid(grids).create(gridSize, presenter)
// then
Assertions.assertEquals(grids.lastSaved.numberOfCoordinates, 36)
assertTrue { presenter.tisGebeurd }
}
@Test
fun `input validation`() {
// given
val requestedGridSize = "bad"
val grids = FakeGrids()
// when
val presenter = TestPresenter(
expectedBadInput = "bad"
expectedBadInput = "bad",
grids = grids
)
CreateGrid(grids).create(requestedGridSize, presenter)
assertTrue { presenter.tisGebeurd }
......@@ -40,13 +41,15 @@ internal class GridTests {
fun `negative grid size is not allowed`() {
// given
val requestedGridSize = "-45"
val grids = FakeGrids()
// when
val presenter = TestPresenter(
expectedBadInput = "-45"
expectedBadInput = "-45",
grids = grids
)
CreateGrid(grids).create(requestedGridSize, presenter)
// then
assertTrue { presenter.tisGebeurd }
}
}
......@@ -59,11 +62,16 @@ class FakeGrids : Grids {
}
}
class TestPresenter(var expectedBadInput: String = "[xxx] is an invalid Simulation maxCoordinate") : CreateGrid.Presenter {
class TestPresenter(val grids: FakeGrids, var expectedBadInput: String = "[xxx] is an invalid Simulation maxCoordinate") : CreateGrid.Presenter {
var tisGebeurd = false
override fun badInput(feedback: String) {
tisGebeurd = true
Assertions.assertEquals(feedback, expectedBadInput)
}
override fun succes() {
tisGebeurd = true
Assertions.assertEquals(grids.lastSaved.numberOfCoordinates, 36)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment