Skip to content

Commit

Permalink
chore: move createFile to test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Jun 2, 2024
1 parent b87ad96 commit 09077d1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 67 deletions.
8 changes: 7 additions & 1 deletion test/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
#include <filesystem>
#include <fstream>

std::vector<std::byte> readFileToBuffer(const std::string& path) {
std::vector<std::byte> readFile(const std::string& path) {
std::ifstream file(path, std::ios::binary);
file >> std::skipws;
auto size = std::filesystem::file_size(path);
std::vector<std::byte> out(size);
file.read(reinterpret_cast<char*>(out.data()), static_cast<std::streamsize>(size));
return out;
}

void createFile(const std::string& name, const std::vector<std::byte>& data) {
std::ofstream out{name.data(), std::ios::binary | std::ios::trunc};
out.unsetf(std::ios::skipws);
out.write(reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(data.size()));
}
4 changes: 3 additions & 1 deletion test/Helpers.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

#define ASSET_ROOT "${sourcepp_ASSET_ROOT}"

std::vector<std::byte> readFileToBuffer(const std::string& path);
std::vector<std::byte> readFile(const std::string& path);

void createFile(const std::string& name, const std::vector<std::byte>& data);
8 changes: 4 additions & 4 deletions test/dmxpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ using namespace dmxpp;

// v1 and v2 are identical afaik...
TEST(dmxpp, v2_read) {
DMX dmx{::readFileToBuffer(ASSET_ROOT "dmxpp/binary/v2.dmx")};
DMX dmx{::readFile(ASSET_ROOT "dmxpp/binary/v2.dmx")};
ASSERT_TRUE(dmx);
}

TEST(dmxpp, v3_read) {
DMX dmx{::readFileToBuffer(ASSET_ROOT "dmxpp/binary/v3.dmx")};
DMX dmx{::readFile(ASSET_ROOT "dmxpp/binary/v3.dmx")};
ASSERT_TRUE(dmx);
}

TEST(dmxpp, v4_read) {
DMX dmx{::readFileToBuffer(ASSET_ROOT "dmxpp/binary/v4.dmx")};
DMX dmx{::readFile(ASSET_ROOT "dmxpp/binary/v4.dmx")};
ASSERT_TRUE(dmx);
}

TEST(dmxpp, v5_read) {
DMX dmx{::readFileToBuffer(ASSET_ROOT "dmxpp/binary/v5.dmx")};
DMX dmx{::readFile(ASSET_ROOT "dmxpp/binary/v5.dmx")};
ASSERT_TRUE(dmx);
}
18 changes: 9 additions & 9 deletions test/studiomodelpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ using namespace studiomodelpp;

TEST(studiomodelpp, v44_read) {
StudioModel model;
bool opened = model.open(::readFileToBuffer(ASSET_ROOT "studiomodelpp/v44.mdl"),
::readFileToBuffer(ASSET_ROOT "studiomodelpp/v44.vtx"),
::readFileToBuffer(ASSET_ROOT "studiomodelpp/v44.vvd"));
bool opened = model.open(::readFile(ASSET_ROOT "studiomodelpp/v44.mdl"),
::readFile(ASSET_ROOT "studiomodelpp/v44.vtx"),
::readFile(ASSET_ROOT "studiomodelpp/v44.vvd"));
ASSERT_TRUE(opened);
}

TEST(studiomodelpp, v47_read) {
StudioModel model;
bool opened = model.open(::readFileToBuffer(ASSET_ROOT "studiomodelpp/v47.mdl"),
::readFileToBuffer(ASSET_ROOT "studiomodelpp/v47.vtx"),
::readFileToBuffer(ASSET_ROOT "studiomodelpp/v47.vvd"));
bool opened = model.open(::readFile(ASSET_ROOT "studiomodelpp/v47.mdl"),
::readFile(ASSET_ROOT "studiomodelpp/v47.vtx"),
::readFile(ASSET_ROOT "studiomodelpp/v47.vvd"));
ASSERT_TRUE(opened);
}

TEST(studiomodelpp, v49_read) {
StudioModel model;
bool opened = model.open(::readFileToBuffer(ASSET_ROOT "studiomodelpp/v49.mdl"),
::readFileToBuffer(ASSET_ROOT "studiomodelpp/v49.vtx"),
::readFileToBuffer(ASSET_ROOT "studiomodelpp/v49.vvd"));
bool opened = model.open(::readFile(ASSET_ROOT "studiomodelpp/v49.mdl"),
::readFile(ASSET_ROOT "studiomodelpp/v49.vtx"),
::readFile(ASSET_ROOT "studiomodelpp/v49.vvd"));
ASSERT_TRUE(opened);
}
Loading

0 comments on commit 09077d1

Please sign in to comment.