Skip to content

Instantly share code, notes, and snippets.

@demoth
Last active August 29, 2015 14:19
Show Gist options
  • Save demoth/d6bf7e0878b0a60f342d to your computer and use it in GitHub Desktop.
Save demoth/d6bf7e0878b0a60f342d to your computer and use it in GitHub Desktop.
Logic tests
package org.demoth.aurora.server
import com.jme3.network.*
import org.demoth.aurora.common.*
import org.demoth.aurora.common.messages.TextMessage
import org.demoth.aurora.common.messages.client.*
import org.demoth.aurora.common.messages.server.GameUpdateMessage
import org.demoth.aurora.server.abilities.Ability
import org.demoth.aurora.server.items.*
import org.junit.*
import groovy.util.logging.Log
/**
* Created by demoth on 10.04.15.
*/
@Log
class LogicalStateTest {
static final int SOME_ID = 4
static final String SOME_NAME = 'cadaver'
static final String SOME_TEXT = 'Lol1234'
static final String SOME_PASS = 'asdf_RAZ'
static ConfigurationService cfg
static MapService map
static Server network
static LoginService login
static List<Message> output
static HostedConnection testConnection
static boolean closedConnection
Player testPlayer
LogicalState state
@BeforeClass
static void setupServices() {
cfg = ConfigurationService.instance
cfg.mapName = 'test'
cfg.startLocation = new Point(1, 0)
map = MapServiceImpl.instance
login = [authorized: { String name, String pass -> name == SOME_NAME }] as LoginService
network = [broadcast: { output.add it as Message }] as Server
testConnection = [getId: { SOME_ID }, close: { closedConnection = true }] as HostedConnection
}
@Before
void setup() {
output = []
closedConnection = false
testPlayer = new Player(testConnection)
testPlayer.name = SOME_NAME
testPlayer.speed = 3
testPlayer.energy = 9
state = new LogicalState(map, login, network, cfg)
state.players.put SOME_ID, testPlayer
}
@Test
void sendTextMessage() {
state.messages.add new TextMessage(text: SOME_TEXT, id: SOME_ID)
state.update 0
assert state.messages.isEmpty()
assert output == [new TextMessage(text: SOME_NAME + ': ' + SOME_TEXT)]
}
@Test
void login() {
state.messages.add new LoginMessage(id: SOME_ID, username: SOME_NAME, password: SOME_PASS)
assert state.actors.isEmpty()
state.update 0
assert state.messages.isEmpty()
assert state.actors == [testPlayer]
assert output == [new TextMessage(id: SOME_ID, text: SOME_NAME + ' joined')]
}
@Test
void loginFailAuth() {
state.messages.add new LoginMessage(id: SOME_ID, username: 'FAILED1', password: SOME_PASS)
state.update 0
assert state.messages.isEmpty()
assert state.players.isEmpty()
assert closedConnection
}
@Test
void requestQuit() {
state.messages.add new ActionRequestMessage(id: SOME_ID, type: ActionType.QUIT)
state.actors.add testPlayer
state.update 0
assert state.messages.isEmpty()
assert state.players.isEmpty()
assert state.actors.isEmpty()
assert output == [new GameUpdateMessage(responses:
[new Response(id: SOME_ID, origin: null, target: null, type: ResponseType.quit)])]
}
@Test
void requestWait() {
state.messages.add new ActionRequestMessage(id: SOME_ID, type: ActionType.WAIT)
state.actors.add testPlayer
state.update 0
assert state.messages.isEmpty()
}
@Test
void requestWalkPositive() {
def target = new Point(0, 0)
def oldTarget = testPlayer.pos
state.messages.add new ActionRequestMessage(id: SOME_ID, target: target, type: ActionType.WALK)
state.actors.add testPlayer
state.activeActors.add testPlayer
testPlayer.energy = 9
state.update 0
assert state.messages.isEmpty()
assert testPlayer.pos == target
assert testPlayer.energy == 0
assert output == [new GameUpdateMessage(responses:
[new Response(id: SOME_ID, origin: oldTarget, target: target, type: ResponseType.walk)])]
}
@Test
void requestWalkWrongTarget() {
def target = new Point(77, 0)
def oldTarget = testPlayer.pos
state.messages.add new ActionRequestMessage(id: SOME_ID, target: target, type: ActionType.WALK)
state.actors.add testPlayer
state.activeActors.add testPlayer
testPlayer.energy = 9
state.update 0
assert state.messages.isEmpty()
assert testPlayer.pos == oldTarget
assert testPlayer.energy == 9
assert output.isEmpty()
}
@Test
void requestWalkNonWalkable() {
def target = new Point(1, 1)
def oldTarget = testPlayer.pos
state.messages.add new ActionRequestMessage(id: SOME_ID, target: target, type: ActionType.WALK)
state.actors.add testPlayer
state.activeActors.add testPlayer
testPlayer.energy = 9
state.update 0
assert state.messages.isEmpty()
assert testPlayer.pos == oldTarget
assert testPlayer.energy == 9
assert output.isEmpty()
}
@Test
void testAliveEffect() {
state.actors.add testPlayer
testPlayer.energy = 0
testPlayer.speed = 3
TestEffects.instance.aliveEffect.apply testPlayer
state.update 0
assert testPlayer.energy == 3
}
@Test
void testTimedEffect() {
state.actors.add testPlayer
testPlayer.energy = 0
testPlayer.speed = 3
TestEffects.instance.aliveEffect.apply testPlayer
TestEffects.instance.speedBoost.apply testPlayer
assert testPlayer.speed == 4
state.update 0
assert testPlayer.energy == 4
assert testPlayer.speed == 3
}
@Test
void walkIntoOtherActor() {
Point target = new Point(0, 0)
Point oldPosition = testPlayer.pos
Actor otherActor = new Actor(pos: target)
otherActor.speed = 2
state.actors.add otherActor
state.actors.add testPlayer
testPlayer.energy = 4
testPlayer.defaultAbility = new Ability(cost: 4, effects: [TestEffects.instance.speedBoost])
state.messages.add new ActionRequestMessage(id: SOME_ID, target: target, type: ActionType.WALK)
state.update 0
assert testPlayer.pos == oldPosition
assert otherActor.speed == 3
}
@Test
void pickup() {
state.actors.add testPlayer
Item stuff = new Item(id: SOME_ID + 1, pos: testPlayer.pos, type: ItemType.OTHER, effects: [])
state.items.put SOME_ID + 1, stuff
state.messages.add new ActionRequestMessage(id: SOME_ID, target: testPlayer.pos, targetId: SOME_ID + 1, type: ActionType.PICKUP)
testPlayer.energy = 3
state.update 0
assert testPlayer.inventory == [stuff]
assert state.items.isEmpty()
assert testPlayer.energy == 0
assert state.messages.isEmpty()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment