File: unittestutil.cpp

package info (click to toggle)
asc 2.4.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 75,240 kB
  • sloc: cpp: 155,036; sh: 8,829; ansic: 6,890; makefile: 652; perl: 138
file content (115 lines) | stat: -rw-r--r-- 3,406 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include "../actions/moveunitcommand.h"
#include "../actions/attackcommand.h"
#include "../loaders.h"
#include "../viewcalculation.h"
#include "../turncontrol.h"

#include "unittestutil.h"

bool NextTurnStrategy_Abort::continueWhenLastPlayer() const { 
   return false;
};  

bool NextTurnStrategy_Abort::authenticate( GameMap* actmap) const
{
   return true;  
}


void move( Vehicle* veh, const MapCoordinate& dest )
{
   auto_ptr<MoveUnitCommand> muc ( new MoveUnitCommand( veh ));
   muc->setDestination( dest );
   ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
   if ( res.successful() )
      muc.release();
   else
      throw ActionResult(res);
}

void attack( Vehicle* veh, const MapCoordinate& target )
{
   auto_ptr<AttackCommand> muc ( new AttackCommand( veh ));
   muc->setTarget( target );
   ActionResult res = muc->execute( createTestingContext( veh->getMap() ));
   if ( res.successful() )
      muc.release();
   else
      throw ActionResult(res);
}


GameMap* startMap( const ASCString& filename )
{
   GameMap* game = mapLoadingExceptionChecker( filename, MapLoadingFunction( tmaploaders::loadmap  ));
   if ( !game )
      throw TestFailure("could not load map " + filename);;

   game->levelfinished = false;

   if ( game->replayinfo ) {
      delete game->replayinfo;
      game->replayinfo = NULL;
   }
   
   computeview( game );
         
   if ( game && game->actplayer == -1 ) 
      next_turn(game, NextTurnStrategy_Abort(), NULL, -1);
   
   return game;
   
}

Context createTestingContext( GameMap* gamemap )
{
   Context context;
   
   context.gamemap = gamemap;
   context.actingPlayer = &gamemap->getPlayer( gamemap->actplayer );
   context.parentAction = NULL;
   context.display = NULL;
   context.viewingPlayer = gamemap->getPlayerView(); 
   context.actionContainer = &gamemap->actions;
   return context;   
}

void testCargoMovement( Vehicle* veh, int movement )
{
   for ( Vehicle::Cargo::const_iterator i = veh->getCargo().begin(); i != veh->getCargo().end(); ++i )
      if ( *i ) {
         Vehicle* cargo1 = *i;
         assertOrThrow( cargo1->getMovement() == movement );
            
         testCargoMovement( cargo1, movement );
      }
}

void testCargoMovementMax( Vehicle* veh, int movement )
{
   for ( Vehicle::Cargo::const_iterator i = veh->getCargo().begin(); i != veh->getCargo().end(); ++i )
      if ( *i ) {
         Vehicle* cargo1 = *i;
         assertOrThrow( cargo1->getMovement() <= movement );
               
         testCargoMovementMax( cargo1, movement );
      }
}

Vehicle* getFirstCargo( ContainerBase* carrier )
{
   for ( Vehicle::Cargo::const_iterator i = carrier->getCargo().begin(); i != carrier->getCargo().end(); ++i )
      if ( *i ) 
         return *i;
   return NULL;
}