LCOV - code coverage report
Current view: top level - test - gamelevel.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 137 137
Test Date: 2026-04-03 02:26:39 Functions: 100.0 % 2 2

            Line data    Source code
       1              : #include "catch.hpp"
       2              : #include "GameLevel.h"
       3              : #include "Object.h"
       4              : #include "Vector.h"
       5              : 
       6           30 : SCENARIO( "GameLevel", "[GameLevel]" ) {
       7           58 :   GIVEN( "A game level instance" ) {
       8           29 :     GameLevel l(1);
       9              : 
      10           37 :     WHEN( "instantiated" ) {
      11            9 :       THEN( "it is not complete" ) {
      12            1 :         REQUIRE(l.checkComplete() == false);
      13            1 :       }
      14              : 
      15            9 :       THEN( "it has no player" ) {
      16            1 :         REQUIRE(l.getPlayer() == NULL);
      17            1 :       }
      18              : 
      19            9 :       THEN( "it has no camera" ) {
      20            1 :         REQUIRE(l.getCamera() == NULL);
      21            1 :       }
      22              : 
      23            9 :       THEN( "it has no terrain" ) {
      24            1 :         REQUIRE(l.getTerrain() == NULL);
      25            1 :       }
      26              : 
      27            9 :       THEN( "elapsed time is zero" ) {
      28            1 :         REQUIRE(l.getElapsedTime() == Approx(0.0f));
      29            1 :       }
      30              : 
      31            9 :       THEN( "id is zero" ) {
      32            1 :         REQUIRE(l.getId() == 0);
      33            1 :       }
      34              : 
      35            9 :       THEN( "music path is empty" ) {
      36            1 :         REQUIRE(l.getMusicPath() == "");
      37            1 :       }
      38              : 
      39            9 :       THEN( "objects list is accessible" ) {
      40            1 :         REQUIRE(l.getObjects() != NULL);
      41            1 :       }
      42            8 :     }
      43              : 
      44           30 :     WHEN( "loading a level without specifying a file" ) {
      45            1 :       bool result = l.loadLevel("");
      46              : 
      47            2 :       THEN( "it will not load" ) {
      48            1 :         REQUIRE(result == false);
      49            1 :       }
      50            1 :     }
      51              : 
      52           30 :     WHEN( "loading a level without specifying a buffer" ) {
      53            1 :       bool result = l.loadLevelFromBuffer("");
      54              : 
      55            2 :       THEN( "it will not load" ) {
      56            1 :         REQUIRE(result == false);
      57            1 :       }
      58            1 :     }
      59              : 
      60              :     // setCamera registers the camera at idToObjectMap_[0] regardless of whether
      61              :     // a level has been loaded.
      62           31 :     WHEN( "setCamera is called before loading" ) {
      63            2 :       Camera c;
      64            2 :       l.setCamera(&c);
      65              : 
      66            3 :       THEN( "getCamera returns the camera" ) {
      67            1 :         REQUIRE(l.getCamera() == &c);
      68            1 :       }
      69              : 
      70            3 :       THEN( "camera is accessible at object id 0" ) {
      71            1 :         REQUIRE(l.getObjectFromId(0) == (Object*)&c);
      72            1 :       }
      73            2 :     }
      74              : 
      75              :     // setCamera must be called before loadLevelFromBuffer: finishLevelLoad calls
      76              :     // exit(1) if camera_ == NULL (GameLevel.cpp:213).
      77           36 :     WHEN( "loading a level with a valid buffer" ) {
      78            7 :       Camera c;
      79            7 :       l.setCamera(&c);
      80            7 :       bool result = l.loadLevelFromBuffer("test=1");
      81              : 
      82            8 :       THEN( "it will load" ) {
      83            1 :         REQUIRE(result == true);
      84            1 :       }
      85              : 
      86            8 :       THEN( "it has a player" ) {
      87            1 :         REQUIRE(l.getPlayer() != NULL);
      88            1 :       }
      89              : 
      90            8 :       THEN( "it has a camera" ) {
      91            1 :         REQUIRE(l.getCamera() == &c);
      92            1 :       }
      93              : 
      94            8 :       THEN( "it has a terrain" ) {
      95            1 :         REQUIRE(l.getTerrain() != NULL);
      96            1 :       }
      97              : 
      98            8 :       THEN( "camera is registered at object id 0" ) {
      99            1 :         REQUIRE(l.getObjectFromId(0) == (Object*)&c);
     100            1 :       }
     101              : 
     102              :       // finishLevelLoad registers player at id 1 and terrain at id 2 (GameLevel.cpp:197-198).
     103            8 :       THEN( "player is registered at object id 1" ) {
     104            1 :         REQUIRE(l.getObjectFromId(1) == (Object*)l.getPlayer());
     105            1 :       }
     106              : 
     107            8 :       THEN( "terrain is registered at object id 2" ) {
     108            1 :         REQUIRE(l.getObjectFromId(2) == (Object*)l.getTerrain());
     109            1 :       }
     110            7 :     }
     111              : 
     112              :     // prepLevelLoad returns false if luaState_ != NULL, so a second load on
     113              :     // the same instance silently fails and leaves the level state unchanged.
     114           31 :     WHEN( "loadLevelFromBuffer is called a second time on the same instance" ) {
     115            2 :       Camera c;
     116            2 :       l.setCamera(&c);
     117            2 :       l.loadLevelFromBuffer("test=1");
     118            2 :       bool result = l.loadLevelFromBuffer("test=1");
     119              : 
     120            3 :       THEN( "it will not load" ) {
     121            1 :         REQUIRE(result == false);
     122            1 :       }
     123              : 
     124            3 :       THEN( "state from first load is preserved" ) {
     125            1 :         REQUIRE(l.getPlayer() != NULL);
     126            1 :       }
     127            2 :     }
     128              : 
     129           30 :     WHEN( "setComplete is called with true" ) {
     130            1 :       l.setComplete(true);
     131              : 
     132            2 :       THEN( "checkComplete returns true" ) {
     133            1 :         REQUIRE(l.checkComplete() == true);
     134            1 :       }
     135            1 :     }
     136              : 
     137           30 :     WHEN( "setElapsedTime is called" ) {
     138            1 :       l.setElapsedTime(2.25f);
     139              : 
     140            2 :       THEN( "getElapsedTime returns the value" ) {
     141            1 :         REQUIRE(l.getElapsedTime() == Approx(2.25f));
     142            1 :       }
     143            1 :     }
     144              : 
     145           30 :     WHEN( "setId is called" ) {
     146            1 :       l.setId(99);
     147              : 
     148            2 :       THEN( "getId returns the value" ) {
     149            1 :         REQUIRE(l.getId() == 99);
     150            1 :       }
     151            1 :     }
     152              : 
     153           30 :     WHEN( "setLightDirection is called" ) {
     154            1 :       l.setLightDirection(3.0f, 4.0f, 0.0f);
     155            1 :       Vector* dir = l.getLightDirection();
     156              : 
     157            2 :       THEN( "light direction is normalised" ) {
     158            1 :         REQUIRE(dir->x == Approx(0.6f));  // 3 / 5
     159            1 :         REQUIRE(dir->y == Approx(0.8f));  // 4 / 5
     160            1 :         REQUIRE(dir->z == Approx(0.0f));
     161            1 :       }
     162            1 :     }
     163              : 
     164           30 :     WHEN( "setMusicPath is called" ) {
     165            1 :       l.setMusicPath("test/music.ogg");
     166              : 
     167            2 :       THEN( "getMusicPath returns the value" ) {
     168            1 :         REQUIRE(l.getMusicPath() == "test/music.ogg");
     169            1 :       }
     170            1 :     }
     171              : 
     172           30 :     WHEN( "getObjectFromId is called with an unregistered id" ) {
     173            2 :       THEN( "returns NULL" ) {
     174            1 :         REQUIRE(l.getObjectFromId(1) == NULL);
     175            1 :       }
     176            1 :     }
     177              : 
     178              :     // getObjectFromId returns NULL for ids outside [0, MAX_LEVEL_OBJECTS).
     179           31 :     WHEN( "getObjectFromId is called with an out-of-bounds id" ) {
     180            3 :       THEN( "negative id returns NULL" ) {
     181            1 :         REQUIRE(l.getObjectFromId(-1) == NULL);
     182            1 :       }
     183              : 
     184            3 :       THEN( "id >= MAX_LEVEL_OBJECTS returns NULL" ) {
     185            1 :         REQUIRE(l.getObjectFromId(MAX_LEVEL_OBJECTS) == NULL);
     186            1 :       }
     187            2 :     }
     188           29 :   }
     189           29 : }
        

Generated by: LCOV version 2.4-0