1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-23 15:40:42 +00:00

Merge branch 'fix_tests' into 'master'

Fix  #6771

Closes #6771

See merge request OpenMW/openmw!1925
This commit is contained in:
uramer 2022-05-31 08:54:33 +00:00
commit bca05d018e
15 changed files with 100 additions and 74 deletions

View File

@ -6,6 +6,8 @@ if (GTEST_FOUND AND GMOCK_FOUND)
include_directories(SYSTEM ${GMOCK_INCLUDE_DIRS})
file(GLOB UNITTEST_SRC_FILES
testing_util.hpp
../openmw/mwworld/store.cpp
../openmw/mwworld/esmstore.cpp
mwworld/test_store.cpp

View File

@ -9,9 +9,12 @@
#include <sstream>
#include <string>
#include "../testing_util.hpp"
namespace
{
using namespace testing;
using namespace TestingOpenMW;
using namespace Files;
struct Params
@ -24,7 +27,7 @@ namespace
TEST(FilesGetHash, shouldClearErrors)
{
const std::string fileName = "fileName";
const std::string fileName = temporaryFilePath("fileName");
std::string content;
std::fill_n(std::back_inserter(content), 1, 'a');
std::istringstream stream(content);
@ -34,7 +37,7 @@ namespace
TEST_P(FilesGetHash, shouldReturnHashForStringStream)
{
const std::string fileName = "fileName";
const std::string fileName = temporaryFilePath("fileName");
std::string content;
std::fill_n(std::back_inserter(content), GetParam().mSize, 'a');
std::istringstream stream(content);
@ -47,6 +50,7 @@ namespace
std::replace(fileName.begin(), fileName.end(), '/', '_');
std::string content;
std::fill_n(std::back_inserter(content), GetParam().mSize, 'a');
fileName = outputFilePath(fileName);
std::fstream(fileName, std::ios_base::out | std::ios_base::binary)
.write(content.data(), static_cast<std::streamsize>(content.size()));
const auto stream = Files::openConstrainedFileStream(fileName, 0, content.size());

View File

@ -6,12 +6,12 @@
#include <components/resource/imagemanager.hpp>
#include <components/files/configurationmanager.hpp>
#include "../lua/testing_util.hpp"
#include "../testing_util.hpp"
namespace
{
TestFile technique_properties(R"(
TestingOpenMW::VFSTestFile technique_properties(R"(
fragment main {}
vertex main {}
technique {
@ -27,7 +27,7 @@ TestFile technique_properties(R"(
}
)");
TestFile rendertarget_properties{R"(
TestingOpenMW::VFSTestFile rendertarget_properties{R"(
render_target rendertarget {
width_ratio = 0.5;
height_ratio = 0.5;
@ -54,7 +54,7 @@ TestFile rendertarget_properties{R"(
)"};
TestFile uniform_properties{R"(
TestingOpenMW::VFSTestFile uniform_properties{R"(
uniform_vec4 uVec4 {
default = vec4(0,0,0,0);
min = vec4(0,1,0,0);
@ -69,13 +69,13 @@ TestFile uniform_properties{R"(
)"};
TestFile missing_sampler_source{R"(
TestingOpenMW::VFSTestFile missing_sampler_source{R"(
sampler_1d mysampler1d { }
fragment main { }
technique { passes = main; }
)"};
TestFile repeated_shared_block{R"(
TestingOpenMW::VFSTestFile repeated_shared_block{R"(
shared {
float myfloat = 1.0;
}
@ -95,7 +95,7 @@ TestFile repeated_shared_block{R"(
std::unique_ptr<Technique> mTechnique;
TechniqueTest()
: mVFS(createTestVFS({
: mVFS(TestingOpenMW::createTestVFS({
{"shaders/technique_properties.omwfx", &technique_properties},
{"shaders/rendertarget_properties.omwfx", &rendertarget_properties},
{"shaders/uniform_properties.omwfx", &uniform_properties},

View File

@ -3,7 +3,7 @@
#include <components/lua/configuration.hpp>
#include "testing_util.hpp"
#include "../testing_util.hpp"
namespace
{

View File

@ -6,17 +6,24 @@
#include <components/lua/luastate.hpp>
#include <components/lua/l10n.hpp>
#include "testing_util.hpp"
#include "../testing_util.hpp"
namespace
{
using namespace testing;
using namespace TestingOpenMW;
TestFile invalidScript("not a script");
TestFile incorrectScript("return { incorrectSection = {}, engineHandlers = { incorrectHandler = function() end } }");
TestFile emptyScript("");
template <typename T>
T get(sol::state& lua, const std::string& luaCode)
{
return lua.safe_script("return " + luaCode).get<T>();
}
VFSTestFile invalidScript("not a script");
VFSTestFile incorrectScript("return { incorrectSection = {}, engineHandlers = { incorrectHandler = function() end } }");
VFSTestFile emptyScript("");
TestFile test1En(R"X(
VFSTestFile test1En(R"X(
good_morning: "Good morning."
you_have_arrows: |-
{count, plural,
@ -38,7 +45,7 @@ numbers: "{int} and {double, number, integer} are integers, but {double} is a do
rounding: "{value, number, :: .00}"
)X");
TestFile test1De(R"X(
VFSTestFile test1De(R"X(
good_morning: "Guten Morgen."
you_have_arrows: |-
{count, plural,
@ -48,11 +55,11 @@ you_have_arrows: |-
"Hello {name}!": "Hallo {name}!"
)X");
TestFile test1EnUS(R"X(
VFSTestFile test1EnUS(R"X(
currency: "You have {money, number, currency}"
)X");
TestFile test2En(R"X(
VFSTestFile test2En(R"X(
good_morning: "Morning!"
you_have_arrows: "Arrows count: {count}"
)X");

View File

@ -3,13 +3,13 @@
#include <components/lua/luastate.hpp>
#include "testing_util.hpp"
#include "../testing_util.hpp"
namespace
{
using namespace testing;
TestFile counterFile(R"X(
TestingOpenMW::VFSTestFile counterFile(R"X(
x = 42
return {
get = function() return x end,
@ -17,9 +17,9 @@ return {
}
)X");
TestFile invalidScriptFile("Invalid script");
TestingOpenMW::VFSTestFile invalidScriptFile("Invalid script");
TestFile testsFile(R"X(
TestingOpenMW::VFSTestFile testsFile(R"X(
return {
-- should work
sin = function(x) return math.sin(x) end,
@ -53,7 +53,7 @@ return {
struct LuaStateTest : Test
{
std::unique_ptr<VFS::Manager> mVFS = createTestVFS({
std::unique_ptr<VFS::Manager> mVFS = TestingOpenMW::createTestVFS({
{"aaa/counter.lua", &counterFile},
{"bbb/tests.lua", &testsFile},
{"invalid.lua", &invalidScriptFile}

View File

@ -6,17 +6,18 @@
#include <components/lua/luastate.hpp>
#include <components/lua/scriptscontainer.hpp>
#include "testing_util.hpp"
#include "../testing_util.hpp"
namespace
{
using namespace testing;
using namespace TestingOpenMW;
TestFile invalidScript("not a script");
TestFile incorrectScript("return { incorrectSection = {}, engineHandlers = { incorrectHandler = function() end } }");
TestFile emptyScript("");
VFSTestFile invalidScript("not a script");
VFSTestFile incorrectScript("return { incorrectSection = {}, engineHandlers = { incorrectHandler = function() end } }");
VFSTestFile emptyScript("");
TestFile testScript(R"X(
VFSTestFile testScript(R"X(
return {
engineHandlers = {
onUpdate = function(dt) print(' update ' .. tostring(dt)) end,
@ -30,7 +31,7 @@ return {
}
)X");
TestFile stopEventScript(R"X(
VFSTestFile stopEventScript(R"X(
return {
eventHandlers = {
Event1 = function(eventData)
@ -41,7 +42,7 @@ return {
}
)X");
TestFile loadSaveScript(R"X(
VFSTestFile loadSaveScript(R"X(
x = 0
y = 0
return {
@ -67,7 +68,7 @@ return {
}
)X");
TestFile interfaceScript(R"X(
VFSTestFile interfaceScript(R"X(
return {
interfaceName = "TestInterface",
interface = {
@ -77,7 +78,7 @@ return {
}
)X");
TestFile overrideInterfaceScript(R"X(
VFSTestFile overrideInterfaceScript(R"X(
local old = nil
local interface = {
fn = function(x)
@ -101,7 +102,7 @@ return {
}
)X");
TestFile useInterfaceScript(R"X(
VFSTestFile useInterfaceScript(R"X(
local interfaces = require('openmw.interfaces')
return {
engineHandlers = {

View File

@ -13,7 +13,7 @@
#include <components/misc/endianness.hpp>
#include <components/misc/color.hpp>
#include "testing_util.hpp"
#include "../testing_util.hpp"
namespace
{

View File

@ -4,12 +4,18 @@
#include <components/lua/luastate.hpp>
#include <components/lua/utilpackage.hpp>
#include "testing_util.hpp"
#include "../testing_util.hpp"
namespace
{
using namespace testing;
template <typename T>
T get(sol::state& lua, const std::string& luaCode)
{
return lua.safe_script("return " + luaCode).get<T>();
}
std::string getAsString(sol::state& lua, std::string luaCode)
{
return LuaUtil::toString(lua.safe_script("return " + luaCode));

View File

@ -1,13 +1,13 @@
#include <gtest/gtest.h>
#include "components/misc/resourcehelpers.hpp"
#include "../lua/testing_util.hpp"
#include "../testing_util.hpp"
namespace
{
using namespace Misc::ResourceHelpers;
TEST(CorrectSoundPath, wav_files_not_overridden_with_mp3_in_vfs_are_not_corrected)
{
std::unique_ptr<VFS::Manager> mVFS = createTestVFS({
std::unique_ptr<VFS::Manager> mVFS = TestingOpenMW::createTestVFS({
{"sound/bar.wav", nullptr}
});
EXPECT_EQ(correctSoundPath("sound/bar.wav", mVFS.get()), "sound/bar.wav");
@ -15,7 +15,7 @@ namespace
TEST(CorrectSoundPath, wav_files_overridden_with_mp3_in_vfs_are_corrected)
{
std::unique_ptr<VFS::Manager> mVFS = createTestVFS({
std::unique_ptr<VFS::Manager> mVFS = TestingOpenMW::createTestVFS({
{"sound/foo.mp3", nullptr}
});
EXPECT_EQ(correctSoundPath("sound/foo.wav", mVFS.get()), "sound/foo.mp3");
@ -23,14 +23,14 @@ namespace
TEST(CorrectSoundPath, corrected_path_does_not_check_existence_in_vfs)
{
std::unique_ptr<VFS::Manager> mVFS = createTestVFS({
std::unique_ptr<VFS::Manager> mVFS = TestingOpenMW::createTestVFS({
});
EXPECT_EQ(correctSoundPath("sound/foo.wav", mVFS.get()), "sound/foo.mp3");
}
TEST(CorrectSoundPath, correct_path_normalize_paths)
{
std::unique_ptr<VFS::Manager> mVFS = createTestVFS({
std::unique_ptr<VFS::Manager> mVFS = TestingOpenMW::createTestVFS({
});
EXPECT_EQ(correctSoundPath("sound\\foo.wav", mVFS.get()), "sound/foo.mp3");
EXPECT_EQ(correctSoundPath("SOUND\\foo.WAV", mVFS.get()), "SOUND/foo.mp3");

View File

@ -1,7 +1,6 @@
#include <gtest/gtest.h>
#include <fstream>
#include <filesystem>
#include <components/files/configurationmanager.hpp>
#include <components/esm3/esmreader.hpp>
@ -12,6 +11,8 @@
#include "apps/openmw/mwworld/esmstore.hpp"
#include "apps/openmw/mwmechanics/spelllist.hpp"
#include "../testing_util.hpp"
namespace MWMechanics
{
SpellList::SpellList(const std::string& id, int type) : mId(id), mType(type) {}
@ -62,8 +63,6 @@ struct ContentFileTest : public ::testing::Test
("data-local", boost::program_options::value<Files::MaybeQuotedPathContainer::value_type>()->default_value(Files::MaybeQuotedPathContainer::value_type(), ""));
Files::ConfigurationManager::addCommonOptions(desc);
boost::program_options::notify(variables);
mConfigurationManager.readConfiguration(variables, desc, true);
Files::PathContainer dataDirs, dataLocal;
@ -73,10 +72,7 @@ struct ContentFileTest : public ::testing::Test
Files::PathContainer::value_type local(variables["data-local"].as<Files::MaybeQuotedPathContainer::value_type>());
if (!local.empty())
{
boost::filesystem::create_directories(local);
dataLocal.push_back(local);
}
mConfigurationManager.filterOutNonExistingPaths(dataDirs);
mConfigurationManager.filterOutNonExistingPaths(dataLocal);
@ -109,10 +105,8 @@ TEST_F(ContentFileTest, dialogue_merging_test)
return;
}
const std::string file = "test_dialogue_merging.txt";
std::ofstream stream;
stream.open(file);
const std::string file = TestingOpenMW::outputFilePath("test_dialogue_merging.txt");
std::ofstream stream(file);
const MWWorld::Store<ESM::Dialogue>& dialStore = mEsmStore.get<ESM::Dialogue>();
for (const auto & dial : dialStore)
@ -191,10 +185,8 @@ TEST_F(ContentFileTest, content_diagnostics_test)
return;
}
const std::string file = "test_content_diagnostics.txt";
std::ofstream stream;
stream.open(file);
const std::string file = TestingOpenMW::outputFilePath("test_content_diagnostics.txt");
std::ofstream stream(file);
RUN_TEST_FOR_TYPES(printRecords, mEsmStore, stream);

View File

@ -4,6 +4,8 @@
#include <gtest/gtest.h>
#include "../testing_util.hpp"
namespace
{
using namespace testing;
@ -17,11 +19,11 @@ namespace
template <typename F>
void withSettingsFile( const std::string& content, F&& f)
{
const auto path = std::string(UnitTest::GetInstance()->current_test_info()->name()) + ".cfg";
std::string path = TestingOpenMW::outputFilePath(
std::string(UnitTest::GetInstance()->current_test_info()->name()) + ".cfg");
{
std::ofstream stream;
stream.open(path);
std::ofstream stream(path);
stream << content;
stream.close();
}

View File

@ -1,9 +1,12 @@
#include <components/settings/shadermanager.hpp>
#include <filesystem>
#include <fstream>
#include <gtest/gtest.h>
#include "../testing_util.hpp"
namespace
{
using namespace testing;
@ -14,7 +17,8 @@ namespace
template <typename F>
void withSettingsFile( const std::string& content, F&& f)
{
const auto path = std::string(UnitTest::GetInstance()->current_test_info()->name()) + ".yaml";
std::string path = TestingOpenMW::outputFilePath(
std::string(UnitTest::GetInstance()->current_test_info()->name()) + ".yaml");
{
std::ofstream stream;

View File

@ -4,6 +4,8 @@
#include <gtest/gtest.h>
#include "../testing_util.hpp"
namespace
{
using namespace testing;
@ -28,11 +30,11 @@ namespace
template <class F>
void withShaderFile(const std::string& suffix, const std::string& content, F&& f)
{
const auto path = UnitTest::GetInstance()->current_test_info()->name() + suffix + ".glsl";
std::string path = TestingOpenMW::outputFilePath(
std::string(UnitTest::GetInstance()->current_test_info()->name()) + suffix + ".glsl");
{
std::ofstream stream;
stream.open(path);
std::ofstream stream(path);
stream << content;
stream.close();
}

View File

@ -1,25 +1,31 @@
#ifndef LUA_TESTING_UTIL_H
#define LUA_TESTING_UTIL_H
#ifndef TESTING_UTIL_H
#define TESTING_UTIL_H
#include <filesystem>
#include <sstream>
#include <sol/sol.hpp>
#include <components/vfs/archive.hpp>
#include <components/vfs/manager.hpp>
namespace
namespace TestingOpenMW
{
template <typename T>
T get(sol::state& lua, const std::string& luaCode)
inline std::string outputFilePath(const std::string name)
{
return lua.safe_script("return " + luaCode).get<T>();
std::filesystem::path dir("tests_output");
std::filesystem::create_directory(dir);
return (dir / name).string();
}
class TestFile : public VFS::File
inline std::string temporaryFilePath(const std::string name)
{
return (std::filesystem::temp_directory_path() / name).string();
}
class VFSTestFile : public VFS::File
{
public:
explicit TestFile(std::string content) : mContent(std::move(content)) {}
explicit VFSTestFile(std::string content) : mContent(std::move(content)) {}
Files::IStreamPtr open() override
{
@ -35,11 +41,11 @@ namespace
const std::string mContent;
};
struct TestData : public VFS::Archive
struct VFSTestData : public VFS::Archive
{
std::map<std::string, VFS::File*> mFiles;
TestData(std::map<std::string, VFS::File*> files) : mFiles(std::move(files)) {}
VFSTestData(std::map<std::string, VFS::File*> files) : mFiles(std::move(files)) {}
void listResources(std::map<std::string, VFS::File*>& out, char (*normalize_function) (char)) override
{
@ -58,7 +64,7 @@ namespace
inline std::unique_ptr<VFS::Manager> createTestVFS(std::map<std::string, VFS::File*> files)
{
auto vfs = std::make_unique<VFS::Manager>(true);
vfs->addArchive(new TestData(std::move(files)));
vfs->addArchive(new VFSTestData(std::move(files)));
vfs->buildIndex();
return vfs;
}
@ -68,4 +74,4 @@ namespace
}
#endif // LUA_TESTING_UTIL_H
#endif // TESTING_UTIL_H