Test invalid JSON

Also converted all .cpp and .h line endings to CRLF in this commit.
This commit is contained in:
JosiahWI 2022-10-22 16:36:29 -05:00 committed by Josiah VanderZee
parent a44905b5db
commit 40a1edef7f
5 changed files with 276 additions and 192 deletions

View File

@ -10,7 +10,11 @@
#include "SColor.h"
#include "SMesh.h"
#include <iostream>
#define TINYGLTF_IMPLEMENTATION
#include <tiny_gltf.h>
#include <memory>
#include <string>
namespace irr
{
@ -30,7 +34,7 @@ bool CGLTFMeshFileLoader::isALoadableFileExtension(
IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
{
if (file->getSize() == 0) {
if (file->getSize() == 0 || !tryParseGLTF(file)) {
return nullptr;
}
@ -53,6 +57,19 @@ IAnimatedMesh* CGLTFMeshFileLoader::createMesh(io::IReadFile* file)
return animatedMesh;
}
bool CGLTFMeshFileLoader::tryParseGLTF(io::IReadFile* file) const
{
tinygltf::Model model {};
tinygltf::TinyGLTF loader {};
std::string err {};
std::string warn {};
auto buf = std::make_unique<char[]>(file->getSize());
file->read(buf.get(), file->getSize());
return loader.LoadASCIIFromString(
&model, &err, &warn, buf.get(), file->getSize(), "", 1);
}
} // namespace irr
} // namespace scene

View File

@ -0,0 +1,33 @@
#ifndef __C_GLTF_MESH_FILE_LOADER_INCLUDED__
#define __C_GLTF_MESH_FILE_LOADER_INCLUDED__
#include "IAnimatedMesh.h"
#include "IMeshLoader.h"
#include "IReadFile.h"
#include "path.h"
namespace irr
{
namespace scene
{
class CGLTFMeshFileLoader : public IMeshLoader
{
public:
CGLTFMeshFileLoader();
bool isALoadableFileExtension(const io::path& filename) const override;
IAnimatedMesh* createMesh(io::IReadFile* file) override;
private:
bool tryParseGLTF(io::IReadFile* file) const;
};
} // namespace scene
} // namespace irr
#endif // __C_GLTF_MESH_FILE_LOADER_INCLUDED__

View File

@ -4,44 +4,78 @@
#include <catch.hpp>
#include <irrlicht.h>
class ScopedMesh
{
public:
ScopedMesh(irr::io::IReadFile* file)
: m_device { irr::createDevice(irr::video::EDT_NULL) }
, m_mesh { nullptr }
{
auto* smgr = m_device->getSceneManager();
m_mesh = smgr->getMesh(file);
}
ScopedMesh(const irr::io::path& filepath)
: m_device { irr::createDevice(irr::video::EDT_NULL) }
, m_mesh { nullptr }
{
auto* smgr = m_device->getSceneManager();
m_mesh = smgr->getMesh(filepath, "");
}
~ScopedMesh()
{
m_device->drop();
m_mesh = nullptr;
}
const irr::scene::IAnimatedMesh* getMesh() const
{
return m_mesh;
}
private:
irr::IrrlichtDevice* m_device;
irr::scene::IAnimatedMesh* m_mesh;
};
TEST_CASE("load empty gltf file") {
irr::IrrlichtDevice* device { irr::createDevice(irr::video::EDT_NULL) };
irr::scene::ISceneManager* smgr { device->getSceneManager() };
irr::io::InMemoryFile filebuf {"test.gltf", ""};
auto* mesh { smgr->getMesh(&filebuf) };
CHECK(mesh == nullptr);
device->drop();
ScopedMesh sm { &filebuf };
CHECK(sm.getMesh() == nullptr);
}
TEST_CASE("minimal triangle has correct vertices") {
irr::IrrlichtDevice* device { irr::createDevice(irr::video::EDT_NULL) };
irr::scene::ISceneManager* smgr { device->getSceneManager() };
auto* mesh { smgr->getMesh(
"source/Irrlicht/tests/assets/minimal_triangle.gltf", "") };
ScopedMesh sm { "source/Irrlicht/tests/assets/minimal_triangle.gltf" };
auto* mesh = sm.getMesh();
REQUIRE(mesh != nullptr);
REQUIRE(mesh->getMeshBufferCount() == 1);
auto* meshbuf { mesh->getMeshBuffer(0) };
auto* meshbuf = mesh->getMeshBuffer(0);
REQUIRE(meshbuf->getVertexCount() == 3);
irr::video::S3DVertex* vertices {
reinterpret_cast<irr::video::S3DVertex*>(
meshbuf->getVertices()) };
auto* vertices = reinterpret_cast<irr::video::S3DVertex*>(
meshbuf->getVertices());
CHECK(vertices[0].Pos == irr::core::vector3df {0.0f, 0.0f, 0.0f});
CHECK(vertices[1].Pos == irr::core::vector3df {0.0f, 1.0f, 0.0f});
CHECK(vertices[2].Pos == irr::core::vector3df {-1.0f, 0.0f, 0.0f});
device->drop();
}
TEST_CASE("mesh loader returns nullptr when given null file pointer") {
irr::IrrlichtDevice* device { irr::createDevice(irr::video::EDT_NULL) };
irr::scene::ISceneManager* smgr { device->getSceneManager() };
auto* mesh { smgr->getMesh(nullptr) };
CHECK(mesh == nullptr);
device->drop();
ScopedMesh sm { nullptr };
CHECK(sm.getMesh() == nullptr);
}
TEST_CASE("invalid JSON returns nullptr") {
SECTION("missing closing brace") {
irr::io::InMemoryFile filebuf {"test.gltf", "{"};
ScopedMesh sm { &filebuf };
CHECK(sm.getMesh() == nullptr);
}
SECTION("missing colon") {
irr::io::InMemoryFile filebuf {"test.gltf", "{\"a\" \"b\"}"};
ScopedMesh sm { &filebuf };
CHECK(sm.getMesh() == nullptr);
}
}