Skip to content

Commit

Permalink
add: tests on environnement and hide print console
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymax25 committed Jan 9, 2022
1 parent 7f5238c commit 36bd30a
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 35 deletions.
1 change: 1 addition & 0 deletions projet/funprog-al/src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
application {
name = "Mower Simulator"
is-env-verbose = true
turn-delay = 500
input-file = "tmp/input.txt"
output-json-file = "tmp/output.json"
Expand Down
23 changes: 17 additions & 6 deletions projet/funprog-al/src/main/scala/progfun/Environnement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Environnement(
val limit_x: Int,
val limit_y: Int,
val mowers: List[Mower],
val isVerbose: Boolean,
val delay: Int
) {

Expand Down Expand Up @@ -70,16 +71,26 @@ object Environnement {
def execute(env: Environnement): Environnement =
env.mowers.filter(m => m.playedActions.length > 0) match {
case first :: rest => {
print("\u001b[2J")
env.display()
Thread.sleep(env.delay.toLong)
if (env.isVerbose) {
print("\u001b[2J")
env.display()
Thread.sleep(env.delay.toLong)
}
execute(
new Environnement(env.limit_x, env.limit_y, env.play, env.delay)
new Environnement(
env.limit_x,
env.limit_y,
env.play,
env.isVerbose,
env.delay
)
)
}
case Nil => {
print("\u001b[2J")
env.display()
if (env.isVerbose) {
print("\u001b[2J")
env.display()
}
env
}
}
Expand Down
1 change: 1 addition & 0 deletions projet/funprog-al/src/main/scala/progfun/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ object Main extends App {
x,
y,
mowers,
conf.getString("application.is-env-verbose").toBoolean,
conf.getString("application.turn-delay").toInt
)

Expand Down
11 changes: 11 additions & 0 deletions projet/funprog-al/src/test/scala/progFun/CSVExporterSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import org.scalatest.funsuite.AnyFunSuite
import fr.esgi.al.funprog._

class CSVExporterSpec extends AnyFunSuite {

test("Should export csv of mower") {
val mower = SpecHelper.getTestMower(0, 1)

assert(CSVExporter.export(mower) == "1,0,1,N,0,1,N,AADGA")
}
}
28 changes: 28 additions & 0 deletions projet/funprog-al/src/test/scala/progFun/EnvironnementSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import org.scalatest.funsuite.AnyFunSuite
import fr.esgi.al.funprog._

class EnvironnementSpec extends AnyFunSuite {

test("Should get env filled with mowers") {
val env = SpecHelper.getTestEnv()

assert(env.mowers.length == 2)
assert(env.limit_x == 5)
assert(env.limit_y == 5)
}

test("Env should be executed and run all the mower actions turns") {
val env = SpecHelper.getTestEnv()

val executed = Environnement.execute(env)

// assert 1nd mower position
assert(executed.mowers(0).point.equals(new Point(1, 3)))
assert(executed.mowers(0).direction.equals(North()))

// assert 2nd mower position
assert(executed.mowers(1).point.equals(new Point(5, 1)))
assert(executed.mowers(1).direction.equals(East()))

}
}
33 changes: 7 additions & 26 deletions projet/funprog-al/src/test/scala/progFun/MowerSpec.scala
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import org.scalatest.funsuite.AnyFunSuite
import fr.esgi.al.funprog._

@SuppressWarnings(Array("org.wartremover.warts.DefaultArguments"))
class MowerSpec extends AnyFunSuite {

def getTestMower(x: Int = 0, y: Int = 0): Mower = {
val id = 1
val point = new Point(x, y)
val direction = new North()
val actions: List[Action] =
"AADGA".split("").toList.map(s => Action(s))

new Mower(
id,
point,
direction,
point,
direction,
actions,
actions
)
}

test("Mower should exist and verify it has an id") {
val mower = getTestMower()
val mower = SpecHelper.getTestMower()

assert(mower.id === 1)
}

test("Mower should advance a mower up once") {
val mower = getTestMower()
val mower = SpecHelper.getTestMower()

val updated = mower.advance(new Point(5, 5), Nil)

Expand All @@ -38,7 +19,7 @@ class MowerSpec extends AnyFunSuite {
}

test("Mower should not advance when advancing out of bounds") {
val mower = getTestMower()
val mower = SpecHelper.getTestMower()

val updated = mower.advance(new Point(0, 0), List())

Expand All @@ -47,22 +28,22 @@ class MowerSpec extends AnyFunSuite {
}

test("Mower should not advance when advancing on another mower") {
val mower_1 = getTestMower()
val mower_2 = getTestMower(0, 1)
val mower_1 = SpecHelper.getTestMower()
val mower_2 = SpecHelper.getTestMower(0, 1)

val updated = mower_1.advance(new Point(2, 2), List(mower_2))

assert(updated.point.equals(mower_1.point))
}

test("Mower direction should go from North to East when turning right") {
val mower = getTestMower()
val mower = SpecHelper.getTestMower()
val updated = mower.turnRight
assert(updated.direction === new East())
}

test("Mower direction should go from North to West when turning left") {
val mower = getTestMower()
val mower = SpecHelper.getTestMower()
val updated = mower.turnLeft
assert(updated.direction === new West())
}
Expand Down
51 changes: 51 additions & 0 deletions projet/funprog-al/src/test/scala/progFun/SpecHelper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package fr.esgi.al.funprog

import fr.esgi.al.funprog._

class SpecHelper {}

@SuppressWarnings(Array("org.wartremover.warts.DefaultArguments"))
object SpecHelper {

def getTestMower(
x: Int = 0,
y: Int = 0,
directionStr: String = "N",
actionsStr: String = "AADGA"
): Mower = {
val id = 1
val point = new Point(x, y)
val direction = Direction(directionStr)
val actions: List[Action] =
actionsStr.split("").toList.map(s => Action(s))

new Mower(
id,
point,
direction,
point,
direction,
actions,
actions
)
}

def getTestEnv(): Environnement = {
val mowers = List(
getTestMower(1, 2, "N", "GAGAGAGAA"),
getTestMower(3, 3, "E", "AADAADADDA")
)

val limit = new Point(5, 5)

new Environnement(
limit.x,
limit.y,
mowers,
false,
0
)

}

}
4 changes: 2 additions & 2 deletions projet/funprog-al/tmp/input.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
5 5
#1 2 N
#GAGAGAGAA
1 2 N
GAGAGAGAA
3 3 E
AADAADADDA
3 changes: 2 additions & 1 deletion projet/funprog-al/tmp/output.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numéro,début_x,début_y,début_direction,fin_x,fin_y,fin_direction,instructions
1,3,3,E,5,1,E,AADAADADDA
1,1,2,N,1,3,N,GAGAGAGAA
2,3,3,E,5,1,E,AADAADADDA
17 changes: 17 additions & 0 deletions projet/funprog-al/tmp/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
},
"tondeuse" : [ {
"id" : 1,
"debut" : {
"point" : {
"x" : 1,
"y" : 2
},
"direction" : "N"
},
"instructions" : [ "G", "A", "G", "A", "G", "A", "G", "A", "A" ],
"fin" : {
"point" : {
"x" : 1,
"y" : 3
},
"direction" : "N"
}
}, {
"id" : 2,
"debut" : {
"point" : {
"x" : 3,
Expand Down

0 comments on commit 36bd30a

Please sign in to comment.