Модульные тесты Google с Clion C ++ - PullRequest
0 голосов
/ 06 января 2020

Я не так хорош с c ++ и новичком в модульном тестировании Google, но мне нужно завершить sh этот проект. Это весь мой проект https://github.com/vrige/sfml_1. Мне нужно проверить алгоритм * поиска цели на карте с игроком, который будет двигаться в соответствии с путем, найденным *. Извините за engli sh.

#include "gtest/gtest.h"

#include "Player.h"
#include "TileMap.h"
#include "Player.h"
#include "AStar.h"
using testing::Eq;
using namespace std;

namespace{
    class testMovimentPlayer : public testing::Test{
    public:
        Player* player;
        testMovimentPlayer() {
            std::string tileSetForPlayer = "./Tests/img/game34x34.png";
            sf::Vector2u tileSize(32, 32);
            player = new Player(tileSetForPlayer, tileSize);
        }
    };
    class AStarFindSolutionFixture : public ::testing::Test {
    public:
        AStarFindSolutionFixture(){
            int x = 20;
            int y = 20;
            int tiles = 4;
            int percentageOfZeros = 50;
            float speedView = 30;
            string tileSet ="./Tests/img/tileset.png";
            string tileSetForPlayer = "./Tests/img/game34x34.png";
            sf::Vector2u tileSize(32, 32);
            sf::Vector2i posInitPlayer(10,10);


            int matrice[] = {
                    //0,1,2,3,     4,5,6,7,    8,9,10,11,   12,13,14,15  16,17,18,19
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    1,0,1,1,     1,1,1,1,     1,1,1,1,     1,1,1,1,     1,1,1,1,

                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,

                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,

                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,

                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
                    0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,     0,0,0,0,
            };
            sf::Vector2i goal(10,2);

            auto ptr = unique_ptr<int[]>(matrice);

            Player player(tileSetForPlayer,tileSize);
            TileMap map(x,y,tiles,tileSize,move(ptr),goal);

            if (!map.load(tileSet,posInitPlayer)){
                cout<<"problemi nel rendering della mappa"<<endl;
            }

            posInitPlayer = map.makePlayerStartGreen(posInitPlayer);
            player.setPosInMatrix(posInitPlayer);
            player.setPosInSprite(sf::Vector2f(posInitPlayer.x*tileSize.x,posInitPlayer.y*tileSize.y));


            aStar = new AStar(&map, &player, map.getGoal());

        }
        AStar* aStar;

    };
}
TEST_F(testMovimentPlayer, ExampleIfIsMoving){

    EXPECT_EQ(player->getIsMoved(), false);
}
TEST_F(AStarFindSolutionFixture, findTheGoal){

    EXPECT_EQ(aStar->findPath(),true); 
}

Последний тест не работает, и я знаю, что проблема в том, что я не могу получить доступ к unique_ptr<int[]> matX в Tilemap. Возможно, проблема в том, что int[] matrice будет удалено в конце AStarFindSolutionFixture(), поэтому я попытался переместить int[] matrice за пределы пространства имен, но это не сработало. Обратите внимание, что этот код работает в основном, но не в test.cpp. На самом деле я хотел бы получить решение, подобное этому (это всего лишь идея):

...

namespace{
    class AStarFindSolutionFixture : public ::testing::Test {
    public:
        AStarFindSolutionFixture(unique_std<int[]> matrice){

          //...



          //...

        }
        AStar* aStar;

    };
}

TEST_F(AStarFindSolutionFixture, findTheGoal){
    int[] matrice = {...};  //so i can easly change the matrix
    auto ptr = unique_ptr<int[]>(matrice);

    AStarFindSolutionFixture(std::move(matrice));
    EXPECT_EQ(aStar->findPath(),true); 
}

Так что я хотел бы знать:

  1. Если бы я мог написать некоторый код, подобный этому;
  2. Почему мой тест не работает;

И я хотел бы получить несколько советов (также примеры приветствуются ) о разработке тестов в Clion (я пытался прочитать документацию, но я не помог мне. Поэтому я следую за этим видео: https://www.youtube.com/watch?v=M067vFQG7ZA).

...