Add better comments, testing framework, some simple initial tests.

This commit is contained in:
Gareth Latty
2016-06-17 01:58:40 +01:00
parent b12b56a8d8
commit ff441c415f
23 changed files with 787 additions and 30 deletions
@@ -0,0 +1,62 @@
package controllers.massivedecks.lobby
import org.specs2._
import org.specs2.mock.Mockito
import controllers.massivedecks.notifications.Notifiers
import models.massivedecks.Game.DeckInfo
import models.massivedecks.cardcast.CardcastDeck
/**
* Specification for the configuration.
*/
class ConfigSpec extends Specification with Mockito { def is = s2"""
The configuration should
Return added decks $addedDeck
Return added house rules $addedHouseRule
Not return removed house rules $removedHouseRule
Ignore removal of house rule that doesn't exist $nonExistantHouseRule
The model must accurately reflect the configuration $modelReflectsConfiguration
"""
val mockHouseRule = "test"
val mockNotifiers = mock[Notifiers]
val mockDeckInfo = mock[DeckInfo]
val mockDeck = mock[CardcastDeck]
mockDeck.info returns mockDeckInfo
def mockConfig = new Config(mockNotifiers)
def addedDeck = {
val config = mockConfig
config.addDeck(mockDeck)
config.decks must contain(exactly(mockDeck))
}
def addedHouseRule = {
val config = mockConfig
config.addHouseRule(mockHouseRule)
config.houseRules must contain(exactly(mockHouseRule))
}
def removedHouseRule = {
val config = mockConfig
config.addHouseRule(mockHouseRule)
config.removeHouseRule(mockHouseRule)
config.houseRules must beEmpty
}
def nonExistantHouseRule = {
val config = mockConfig
config.removeHouseRule(mockHouseRule)
success
}
def modelReflectsConfiguration = {
val config = mockConfig
config.addHouseRule(mockHouseRule)
config.addDeck(mockDeck)
config.config must_== models.massivedecks.Game.Config(List(mockDeckInfo), Set(mockHouseRule))
}
}
@@ -0,0 +1,47 @@
package controllers.massivedecks.lobby
import controllers.massivedecks.exceptions.BadRequestException
import models.massivedecks.Game.{Call, Response}
import models.massivedecks.cardcast.CardcastDeck
import matchers.Matchers._
import org.specs2._
import org.specs2.mock.Mockito
/**
* Specification for the deck.
*/
class DeckSpec extends Specification with Mockito { def is = s2"""
The deck should
Throw if there are not any responses. $noResponses
Throw if there are not any calls. $noCalls
Reused responses should get unique ids. $uniqueIdsOnReusedResponses
Reused calls should get unique ids. $uniqueIdsOnReusedCalls
"""
def call = Call("call", List("callContent", "callContent"))
def response = Response("response", "responseContent")
def deckNoResponses = CardcastDeck("noResponses", "", List(call), List())
def deckNoCalls = CardcastDeck("noCalls", "", List(), List(response))
def decksOneOfEach = List(deckNoResponses, deckNoCalls)
def noResponses =
new Deck(List(deckNoResponses)) must throwA[BadRequestException]
def noCalls =
new Deck(List(deckNoCalls)) must throwA[BadRequestException]
def uniqueIdsOnReusedResponses = {
val deck = new Deck(decksOneOfEach)
deck.drawResponses(2) must haveNoDuplicateValues
}
def uniqueIdsOnReusedCalls = {
val deck = new Deck(decksOneOfEach)
deck.drawCall() must_!= deck.drawCall()
}
}
@@ -0,0 +1,19 @@
package controllers.massivedecks.lobby
import controllers.massivedecks.exceptions.BadRequestException
import models.massivedecks.Game.{Call, Response}
import models.massivedecks.cardcast.CardcastDeck
import matchers.Matchers._
import org.specs2._
import org.specs2.mock.Mockito
/**
* Specification for the game.
*/
class GameSpec extends Specification with Mockito { def is = s2"""
The game should
"""
}
@@ -0,0 +1,15 @@
package controllers.massivedecks.lobby
import org.specs2._
import org.specs2.mock.Mockito
/**
* Specification for the lobby.
*/
class LobbySpec extends Specification with Mockito { def is = s2"""
The lobby should
"""
}
@@ -0,0 +1,110 @@
package controllers.massivedecks.lobby
import controllers.massivedecks.exceptions.{BadRequestException, ForbiddenException}
import controllers.massivedecks.notifications.Notifiers
import models.massivedecks.Player
import org.specs2._
import org.specs2.mock.Mockito
/**
* Specification for the players.
*/
class PlayersSpec extends Specification with Mockito { def is = s2"""
The players controller should
Not allow new players with the same name as an existing one $notAllowNewPlayerWithSameNameAsExisting
Notify all clients when a new player joins $notifyAllClientsOnNewPlayer
There is no owner until the first player joins $ownerIsNoOneUntilPlayerJoins
Set the owner to the new player if they are first $ownerIsNewPlayerIfFirst
Not set the owner if the new player is not first $ownerIsNotChangedWhenANewPlayerJoinsAfterFirst
Validate a valid secret $validateValidSecret
Not validate a secret for a player that doesn't exist $noValidateSecretForNonExistentPlayer
Not validate a secret where the secret is wrong $noValidateSecretWhereWrong
Adding an AI should never result in a name clash $aiShouldAvoidNameClashes
Adding an AI should notify all clients $aiShouldNotifyAllClients
"""
val playerName = "Player Name"
val playerName2 = "Player 2 Name"
def notAllowNewPlayerWithSameNameAsExisting = {
val mockNotifiers = mock[Notifiers]
val players = new Players(mockNotifiers)
players.addPlayer(playerName)
players.addPlayer(playerName) must throwA[BadRequestException]
}
def notifyAllClientsOnNewPlayer = {
val mockNotifiers = mock[Notifiers]
val players = new Players(mockNotifiers)
val secret = players.addPlayer(playerName)
there was one(mockNotifiers).playerJoin(players.getPlayer(secret.id))
}
def ownerIsNewPlayerIfFirst = {
val mockNotifiers = mock[Notifiers]
val players = new Players(mockNotifiers)
val secret = players.addPlayer(playerName)
players.owner must_== Some(secret.id)
}
/**
* Mostly this exists because we want to change this.
*/
def ownerIsNoOneUntilPlayerJoins = {
val mockNotifiers = mock[Notifiers]
val players = new Players(mockNotifiers)
players.owner must_== None
}
def ownerIsNotChangedWhenANewPlayerJoinsAfterFirst = {
val mockNotifiers = mock[Notifiers]
val players = new Players(mockNotifiers)
val secret = players.addPlayer(playerName)
players.addPlayer(playerName2)
players.owner must_== Some(secret.id)
}
def validateValidSecret = {
val mockNotifiers = mock[Notifiers]
val players = new Players(mockNotifiers)
val secret = players.addPlayer(playerName)
players.validateSecret(secret)
success
}
def noValidateSecretForNonExistentPlayer = {
val mockNotifiers = mock[Notifiers]
val players = new Players(mockNotifiers)
val secret = players.addPlayer(playerName)
players.validateSecret(Player.Secret(Player.Id(-1), secret.secret)) must throwA[ForbiddenException]
}
def noValidateSecretWhereWrong = {
val mockNotifiers = mock[Notifiers]
val players = new Players(mockNotifiers)
val secret = players.addPlayer(playerName)
players.validateSecret(Player.Secret(secret.id, "wrong")) must throwA[ForbiddenException]
}
def aiShouldAvoidNameClashes = {
val mockNotifiers = mock[Notifiers]
val players = new Players(mockNotifiers)
players.addPlayer(Players.aiName)
for (name <- Players.aiNames) {
players.addPlayer(name)
}
players.addAi()
players.addAi()
success
}
def aiShouldNotifyAllClients = {
val mockNotifiers = mock[Notifiers]
val players = new Players(mockNotifiers)
val secret = players.addAi()
there was one(mockNotifiers).playerJoin(Player(Player.Id(0), "Rando Cardrissian", Player.Neutral, 0, disconnected=false, left=false))
there was one(mockNotifiers).playerStatus(Player.Id(0), Player.Ai)
}
}