Corrected individual asset path reading

AssetManagerTest class
This commit is contained in:
devZoGok
2023-01-02 14:56:49 +02:00
parent 300d28bab6
commit da7c3a50c1
10 changed files with 110 additions and 20 deletions
+2 -2
View File
@@ -27,7 +27,6 @@ endif()
set(LIB_SRC ${RENDER} ${MATH} ${UTILS} ${GUI} ${ARMATURE} ${ASSET_MANAGER} ${ASSET_READERS} ${ANIM})
include_directories(external/tinydir)
include_directories(external/glm)
include_directories(external/glm/glm)
@@ -88,7 +87,8 @@ if(BUILD_TESTS)
set(RENDER_TEST nodeTest.cpp cameraTest.cpp shaderTest.cpp particleEmitterTest.cpp)
set(ARMATURE_TEST boneTest.cpp ikSolverTest.cpp)
set(ANIMATION_TEST animationChannelTest.cpp)
set(TEST_SRC main.cpp ${LIB_SRC} ${RENDER_TEST} ${ARMATURE_TEST} ${ANIMATION_TEST})
set(ASSET_MANAGER_TEST assetManagerTest.cpp)
set(TEST_SRC main.cpp ${LIB_SRC} ${RENDER_TEST} ${ARMATURE_TEST} ${ANIMATION_TEST} ${ASSET_MANAGER_TEST})
add_executable(vb01Tests ${TEST_SRC})
target_link_libraries(vb01Tests ${LIB_NAME} ${DEPS} cppunit)
+31 -18
View File
@@ -8,6 +8,7 @@
#include <tinydir.h>
#include <algorithm>
#include <iostream>
using namespace std;
@@ -23,46 +24,58 @@ namespace vb01{
void AssetManager::readDir(string path, vector<string> &contents, bool recursive){
tinydir_dir dir;
tinydir_open_sorted(&dir, path.c_str());
if(tinydir_open_sorted(&dir, path.c_str()) == -1){
contents.push_back(path);
return;
}
for(int i = 0; i < dir.n_files; i++){
tinydir_file file;
tinydir_readfile_n(&dir, &file, i);
if(file.name[0] == '.')
continue;
if(file.is_dir && recursive)
readDir(path + file.name, contents, recursive);
readDir(path + file.name + "/", contents, recursive);
else if(!file.is_dir)
contents.push_back(file.name);
contents.push_back(path + file.name);
}
tinydir_close(&dir);
}
void AssetManager::load(string path, bool recursive){
if(getAsset(path))
return;
vector<string> files;
readDir(path, files, recursive);
string format = path.substr(path.find_last_of(".") + 1, string::npos);
AbstractAssetReader *assetReader = nullptr;
vector<string> imageFormats = vector<string>{"png", "jpeg", "jpg"};
for(string file : files){
if(getAsset(file))
continue;
if(find(imageFormats.begin(), imageFormats.end(), format) != imageFormats.end())
assetReader = ImageReader::getSingleton();
string format = file.substr(file.find_last_of(".") + 1, string::npos);
AbstractAssetReader *assetReader = nullptr;
vector<string> imageFormats = vector<string>{"png", "jpeg", "jpg"};
vector<string> fontFormats = vector<string>{"ttf"};
if(find(imageFormats.begin(), imageFormats.end(), format) != imageFormats.end())
assetReader = ImageReader::getSingleton();
if(find(fontFormats.begin(), fontFormats.end(), format) != fontFormats.end())
assetReader = FontReader::getSingleton();
vector<string> fontFormats = vector<string>{"ttf"};
vector<string> modelFormats = vector<string>{"xml"};
if(find(fontFormats.begin(), fontFormats.end(), format) != fontFormats.end())
assetReader = FontReader::getSingleton();
if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end())
assetReader = XmlModelReader::getSingleton();
vector<string> modelFormats = vector<string>{"xml"};
assets.push_back(assetReader->readAsset(path));
if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end())
assetReader = XmlModelReader::getSingleton();
if(assetReader)
assets.push_back(assetReader->readAsset(file));
else
cout << "No asset reader for " << file << endl;
}
}
Asset* AssetManager::getAsset(string path){
+1
View File
@@ -13,6 +13,7 @@ namespace vb01{
static AssetManager* getSingleton();
void load(std::string, bool = false);
Asset* getAsset(std::string);
inline std::vector<Asset*> getAssets(){return assets;}
private:
AssetManager(){}
void readDir(std::string, std::vector<std::string>&, bool);
+48
View File
@@ -0,0 +1,48 @@
#include "assetManagerTest.h"
#include "assetManager.h"
#include <string>
#include <vector>
namespace vb01{
using namespace std;
void AssetManagerTest::setUp(){
assetManager = AssetManager::getSingleton();
string basePath = "../tests/";
recursiveLoadPath = basePath + "recursiveLoad/";
fileLoadPath = basePath + "fileLoad/";
}
void AssetManagerTest::tearDown(){
}
void AssetManagerTest::testLoad(){
vector<string> recursiveLoadFiles = vector<string>{"folderA/a.png", "folderB/b.png"};
assetManager->load(recursiveLoadPath, true);
for(string file : recursiveLoadFiles){
Asset *asset = assetManager->getAsset(recursiveLoadPath + file);
CPPUNIT_ASSERT(asset != nullptr);
}
string file = fileLoadPath + "a.png";
assetManager->load(file);
CPPUNIT_ASSERT(assetManager->getAsset(file) != nullptr);
assetManager->load(file);
vector<Asset*> assets = assetManager->getAssets();
int numSameAssets = 0;
for(int i = 0; i < assets.size(); i++)
if(assets[i]->path == file)
numSameAssets++;
CPPUNIT_ASSERT(numSameAssets == 1);
string nonReadableFile = fileLoadPath + "b.abc";
assetManager->load(nonReadableFile);
CPPUNIT_ASSERT(!assetManager->getAsset(nonReadableFile));
}
}
+26
View File
@@ -0,0 +1,26 @@
#ifndef ASSET_MANAGER_TEST_H
#define ASSET_MANAGER_TEST_H
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
namespace vb01{
class AssetManager;
class AssetManagerTest : public CppUnit::TestFixture{
CPPUNIT_TEST_SUITE(AssetManagerTest);
CPPUNIT_TEST(testLoad);
CPPUNIT_TEST_SUITE_END();
public:
AssetManagerTest(){}
void setUp();
void tearDown();
void testLoad();
private:
AssetManager *assetManager = nullptr;
std::string recursiveLoadPath, fileLoadPath;
};
}
#endif
+2
View File
@@ -10,6 +10,7 @@
#include "shaderTest.h"
#include "particleEmitterTest.h"
#include "ikSolverTest.h"
#include "assetManagerTest.h"
using namespace CppUnit;
using namespace vb01;
@@ -23,6 +24,7 @@ int main(){
runner.addTest(ShaderTest::suite());
runner.addTest(ParticleEmitterTest::suite());
runner.addTest(IkSolverTest::suite());
runner.addTest(AssetManagerTest::suite());
runner.run();
return 0;
}
View File
View File
View File
View File