/** @file dataTests.cpp * @brief Unit tests for the data layer of a Rally-X game. * * The functionality of each class in the data layer was tested. * * The Config class was tested in terms of its ability to read an existing config file, * as well as its ability to create a new file with default values, or fill missing parameters * with default values. * * The LevelReader class was tested in the normal case, where a correct level file is given, * and in the case where a file that does not exist is given. * * @author Justin Wernick * @author David Schneider */ #include #include using namespace std; #include #include "../source/data/Config.h" #include "../source/data/LevelReader.h" #include "../source/logic/PlayerCar.h" #include "../source/logic/EnemyCar.h" #include "../source/logic/Checkpoint.h" #include "../source/logic/Rock.h" #include "../source/logic/Smokescreen.h" #include "../source/logic/Maze.h" /** * @brief Tests that a normal complete config file can be read. */ TEST(Config, readsSettingsCorrectly) { string testFilepath = "testConfig.txt"; ofstream testFile(testFilepath.c_str()); testFile << "screen_width=123" << endl; testFile << "screen_height=345" << endl; testFile << "fullscreen=true" << endl; testFile.close(); Config testConfig(testFilepath); EXPECT_EQ((unsigned)(123), testConfig.screenWidth()); EXPECT_EQ((unsigned)(345), testConfig.screenHeight()); EXPECT_TRUE(testConfig.fullscreen()); remove(testFilepath.c_str()); } /** * @brief Tests that, if the config file does not exist, it is created with default values. */ TEST(Config, createsFileIfNeeded) { string testFilepath = "testConfig.txt"; Config testConfig(testFilepath); ifstream testFile(testFilepath.c_str()); EXPECT_TRUE(testFile); testFile.close(); //test for default values EXPECT_EQ((unsigned)(800), testConfig.screenWidth()); EXPECT_EQ((unsigned)(600), testConfig.screenHeight()); EXPECT_FALSE(testConfig.fullscreen()); remove(testFilepath.c_str()); } /** * @brief Tests that an incomplete config file is loaded, with defaults for the missing values. */ TEST(Config, incompleteFileFilled) { string testFilepath = "testConfig.txt"; ofstream testFile(testFilepath.c_str()); testFile << "screen_height=345" << endl; testFile.close(); Config testConfig(testFilepath); EXPECT_EQ((unsigned)(800), testConfig.screenWidth()); EXPECT_EQ((unsigned)(345), testConfig.screenHeight()); EXPECT_FALSE(testConfig.fullscreen()); remove(testFilepath.c_str()); } /** * @brief Tests that a level can be loaded correctly from a file. */ TEST(LevelReader, readsFileInfoObjects) { string testFilepath = "testMaze.lvl"; ofstream testFile(testFilepath.c_str()); testFile << " P X " << endl; testFile << " " << endl; testFile << " @ " << endl; testFile << " X" << endl; testFile << "## " << endl; testFile << " P " << endl; testFile << " O " << endl; testFile.close(); LevelReader testReader(testFilepath); Maze maze; list players; list enemies; list checkpoints; list rocks; testReader.readLevel(maze, players, enemies, checkpoints, rocks); list expectedPlayers; list expectedEnemies; list expectedCheckpoints; list expectedRocks; expectedPlayers.push_back(PlayerCar(3,2)); expectedEnemies.push_back(EnemyCar(3,0)); expectedEnemies.push_back(EnemyCar(5,3)); expectedCheckpoints.push_back(Checkpoint(2,0)); expectedCheckpoints.push_back(Checkpoint(3,5)); expectedRocks.push_back(Rock(2,6)); //eqality operator was not implemented for the GameObject class or its subclasses //because it would not be meaningful. Two objects with the same position, type, and facing //are still two different objects. //iterators were used because the list type does not have an 'at' function. list::const_iterator playIter = players.begin(); EXPECT_FLOAT_EQ(playIter->x(), 3); EXPECT_FLOAT_EQ(playIter->y(), 2); ++playIter; EXPECT_EQ(playIter, players.end()); list::const_iterator enemyIter = enemies.begin(); EXPECT_FLOAT_EQ(enemyIter->x(), 3); EXPECT_FLOAT_EQ(enemyIter->y(), 0); ++enemyIter; EXPECT_FLOAT_EQ(enemyIter->x(), 5); EXPECT_FLOAT_EQ(enemyIter->y(), 3); ++enemyIter; EXPECT_EQ(enemyIter, enemies.end()); list::const_iterator checkIter = checkpoints.begin(); EXPECT_FLOAT_EQ(checkIter->x(), 1); EXPECT_FLOAT_EQ(checkIter->y(), 0); ++checkIter; EXPECT_FLOAT_EQ(checkIter->x(), 3); EXPECT_FLOAT_EQ(checkIter->y(), 5); ++checkIter; EXPECT_EQ(checkIter, checkpoints.end()); list::const_iterator rockIter = rocks.begin(); EXPECT_FLOAT_EQ(rockIter->x(), 2); EXPECT_FLOAT_EQ(rockIter->y(), 6); ++rockIter; EXPECT_EQ(rockIter, rocks.end()); remove(testFilepath.c_str()); } /** * @brief Tests that an exception is throws if the selected file does not exist. */ TEST(LevelReader, throwsExceptionOnBadFilename) { string testFilepath = "testLevel.lvl"; LevelReader testReader(testFilepath); Maze maze; list players; list enemies; list checkpoints; list rocks; EXPECT_ANY_THROW(testReader.readLevel(maze, players, enemies, checkpoints, rocks)); }