mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-10 14:58:10 -05:00
chore: add abstraction for opening output files to be able to mock it
This commit is contained in:
49
test/ObjCommonTestUtils/SearchPath/MockOutputPath.cpp
Normal file
49
test/ObjCommonTestUtils/SearchPath/MockOutputPath.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
#include "MockOutputPath.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace
|
||||
{
|
||||
class MockFileWrapper final : public std::ostringstream
|
||||
{
|
||||
public:
|
||||
MockFileWrapper(std::string name, std::vector<MockOutputFile>& files)
|
||||
: m_name(std::move(name)),
|
||||
m_files(files)
|
||||
{
|
||||
}
|
||||
|
||||
~MockFileWrapper() override
|
||||
{
|
||||
m_files.emplace_back(std::move(m_name), str());
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
std::vector<MockOutputFile>& m_files;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
MockOutputFile::MockOutputFile() = default;
|
||||
|
||||
MockOutputFile::MockOutputFile(std::string name, std::string data)
|
||||
: m_name(std::move(name)),
|
||||
m_data(std::move(data))
|
||||
{
|
||||
}
|
||||
|
||||
std::unique_ptr<std::ostream> MockOutputPath::Open(const std::string& fileName)
|
||||
{
|
||||
return std::make_unique<MockFileWrapper>(fileName, m_files);
|
||||
}
|
||||
|
||||
const MockOutputFile* MockOutputPath::GetMockedFile(const std::string& name) const
|
||||
{
|
||||
for (const auto& file : m_files)
|
||||
{
|
||||
if (file.m_name == name)
|
||||
return &file;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
28
test/ObjCommonTestUtils/SearchPath/MockOutputPath.h
Normal file
28
test/ObjCommonTestUtils/SearchPath/MockOutputPath.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "SearchPath/IOutputPath.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class MockOutputFile
|
||||
{
|
||||
public:
|
||||
std::string m_name;
|
||||
std::string m_data;
|
||||
|
||||
MockOutputFile();
|
||||
MockOutputFile(std::string name, std::string data);
|
||||
};
|
||||
|
||||
class MockOutputPath final : public IOutputPath
|
||||
{
|
||||
public:
|
||||
std::unique_ptr<std::ostream> Open(const std::string& fileName) override;
|
||||
|
||||
[[nodiscard]] const MockOutputFile* GetMockedFile(const std::string& name) const;
|
||||
|
||||
private:
|
||||
std::vector<MockOutputFile> m_files;
|
||||
};
|
Reference in New Issue
Block a user