... | ... | @@ -198,18 +198,52 @@ fun generateBall(): Ball = Ball.values()[Random.nextInt(Ball.values().size)] |
|
|
#### Testy
|
|
|
|
|
|
```kotlin
|
|
|
///// tests //////
|
|
|
fun main(args: Array<String>) {
|
|
|
|
|
|
///// 1. library //////
|
|
|
|
|
|
val db = Library()
|
|
|
assertEq(0, db.numberOfBorrowedBooks, "Number of borrowed books should be 0")
|
|
|
|
|
|
val users = arrayOf(User("Kamil"), User("Michal"), User("Magda"))
|
|
|
val books = mutableListOf<Book>()
|
|
|
for (i in 1..5) books.add(Book("Book $i"))
|
|
|
for (i in 0 until users.size) db.borrowBook(books[i], users[i])
|
|
|
|
|
|
assertEq(3, db.numberOfBorrowedBooks, "Number of borrowed books should be 3")
|
|
|
assertEq(true, db.isBookBorrowed(books[2]), "Book 3 should be borrowed")
|
|
|
assertEq(false, db.isBookBorrowed(books[4]), "Book 5 should not be borrowed")
|
|
|
assertEq(User("Kamil"), db.bookBorrowedBy(books[0]), "Book should be borrowed by Kamil")
|
|
|
assertEq(User("Michal"), db.bookBorrowedBy(books[1]), "Book should be borrowed by Michal")
|
|
|
assertEq(User("Magda"), db.bookBorrowedBy(books[2]), "Book should be borrowed by Magda")
|
|
|
assertEq(null, db.bookBorrowedBy(books[3]), "Book 4 should not be borrowed")
|
|
|
assertEq(null, db.bookBorrowedBy(books[4]), "Book 5 should not be borrowed")
|
|
|
|
|
|
assertEq(false, db.borrowBook(books[0], users[2]), "Already borrowed book should not be borrowed again")
|
|
|
|
|
|
db.returnBook(books[1])
|
|
|
assertEq(2, db.numberOfBorrowedBooks, "Number of borrowed books should be 2")
|
|
|
assertEq(false, db.isBookBorrowed(books[1]), "Book 2 should not be borrowed")
|
|
|
assertEq(null, db.bookBorrowedBy(books[1]), "Book 2 should not be borrowed")
|
|
|
db.returnBook(books[4])
|
|
|
assertEq(2, db.numberOfBorrowedBooks, "Number of borrowed books should still be 2")
|
|
|
assertEq(true, db.borrowBook(books[1], users[2]), "Returned Book 2 could be borrowed again")
|
|
|
|
|
|
|
|
|
///// 2. game //////
|
|
|
|
|
|
val karel = User("Karel")
|
|
|
|
|
|
assertEq("Karel", karel.toString(), "Karel's name is Karel ")
|
|
|
|
|
|
|
|
|
assertEq(true, karel.addBallIfNotAlready(Ball.MIDDLE), "Karel does not have medium ball yet")
|
|
|
assertEq(true, karel.addBallIfNotAlready(Ball.BIG), "Karel does not have big ball yet")
|
|
|
assertEq(false, karel.addBallIfNotAlready(Ball.BIG), "Karel already has big ball")
|
|
|
|
|
|
val marek = User("Marek")
|
|
|
|
|
|
val marek = User("Marek")
|
|
|
val matej = User("Matej")
|
|
|
|
|
|
|
|
|
val game = BallGame(marek, matej)
|
|
|
game.playTurn(Ball.SMALL)
|
|
|
assertEq(1, marek.balls.size, "Marek should have one small ball")
|
... | ... | @@ -229,7 +263,7 @@ fun main(args: Array<String>) { |
|
|
}
|
|
|
|
|
|
///// custom test method //////
|
|
|
fun assertEq(expectedAny: Any, actualAny: Any, message: String = "") {
|
|
|
fun assertEq(expectedAny: Any?, actualAny: Any?, message: String = "") {
|
|
|
if (expectedAny == actualAny) {
|
|
|
println("===> Test OK $expectedAny == $actualAny")
|
|
|
} else {
|
... | ... | |