From 19cdfd12dfdf279e51fb248e013df2d3f09bef31 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Thu, 23 Jul 2020 15:58:18 +0100 Subject: [PATCH] Unit test for internal failures. --- test/unit/utImporter.cpp | 102 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/test/unit/utImporter.cpp b/test/unit/utImporter.cpp index 0be0037df..33d8cd1e0 100644 --- a/test/unit/utImporter.cpp +++ b/test/unit/utImporter.cpp @@ -279,3 +279,105 @@ TEST_F(ImporterTest, SearchFileHeaderForTokenTest) { //DefaultIOSystem ioSystem; // BaseImporter::SearchFileHeaderForToken( &ioSystem, assetPath, Token, 2 ) } + + +namespace +{ + // Description for an importer which fails in specific ways. + aiImporterDesc s_failingImporterDescription = { + "Failing importer", + "assimp team", + "", + "", + 0, + 1, + 0, + 1, + 0, + "fail" + }; + + // This importer fails in specific ways. + class FailingImporter : public Assimp::BaseImporter { + public: + virtual ~FailingImporter() = default; + virtual bool CanRead( const std::string&, Assimp::IOSystem*, bool ) const override + { + return true; + } + + protected: + virtual const aiImporterDesc* GetInfo() const { return &s_failingImporterDescription; } + + virtual void InternReadFile( const std::string& pFile, aiScene*, Assimp::IOSystem* ) override + { + if (pFile == "deadlyImportError.fail") + { + throw DeadlyImportError("Deadly import error test"); + } + else if (pFile == "stdException.fail") + { + throw std::exception("std::exception test"); + } + else if (pFile == "unexpectedException.fail") + { + throw 5; + } + } + }; +} + +TEST_F(ImporterTest, deadlyImportError) +{ + pImp->RegisterLoader(new FailingImporter); + pImp->SetIOHandler(new TestIOSystem); + const aiScene* scene = pImp->ReadFile("deadlyImportError.fail", 0); + EXPECT_EQ(scene, nullptr); + EXPECT_STREQ(pImp->GetErrorString(), "Deadly import error test"); + EXPECT_EQ(pImp->GetInternalException(), std::exception_ptr()); +} + +TEST_F(ImporterTest, stdException) +{ + pImp->RegisterLoader(new FailingImporter); + pImp->SetIOHandler(new TestIOSystem); + const aiScene* scene = pImp->ReadFile("stdException.fail", 0); + EXPECT_EQ(scene, nullptr); + EXPECT_STREQ(pImp->GetErrorString(), "Internal error"); + EXPECT_NE(pImp->GetInternalException(), std::exception_ptr()); + try + { + std::rethrow_exception(pImp->GetInternalException()); + } + catch(const std::exception& e) + { + EXPECT_STREQ(e.what(), "std::exception test"); + } + catch(...) + { + EXPECT_TRUE(false); + } +} + +TEST_F(ImporterTest, unexpectedException) +{ + pImp->RegisterLoader(new FailingImporter); + pImp->SetIOHandler(new TestIOSystem); + const aiScene* scene = pImp->ReadFile("unexpectedException.fail", 0); + + EXPECT_EQ(scene, nullptr); + EXPECT_STREQ(pImp->GetErrorString(), "Internal error"); + ASSERT_NE(pImp->GetInternalException(), std::exception_ptr()); + try + { + std::rethrow_exception(pImp->GetInternalException()); + } + catch(int x) + { + EXPECT_EQ(x, 5); + } + catch(...) + { + EXPECT_TRUE(false); + } +}