1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
//------------------------------------------------------------------------
//
// Eureka DOOM Editor
//
// Copyright (C) 2021 Ioan Chera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------
#include "Errors.h"
#include "SafeOutFile.h"
#include "testUtils/TempDirContext.hpp"
#include "gtest/gtest.h"
class SafeOutFileTest : public TempDirContext
{
};
static void checkFileContent(const fs::path &path, const char *content)
{
std::ifstream stream(path);
ASSERT_TRUE(stream.is_open());
char msg[128] = {};
ASSERT_TRUE(stream.get(msg, sizeof(msg)));
ASSERT_TRUE(stream.eof());
ASSERT_FALSE(stream.fail());
stream.close();
ASSERT_STREQ(msg, content);
}
TEST_F(SafeOutFileTest, Stuff)
{
fs::path path = getSubPath("somefile.txt");
// Assert no file if merely created (will fail when tearing down)
{
BufferedOutFile sof(path);
}
{
{
BufferedOutFile sof(path);
sof.write("Hello, world!", 13); // and this
sof.write(" more.", 6); // and this
// forget about commiting.
// Tearing down should not be blocked by child folder
}
BufferedOutFile sof(path); // reopen it
sof.write("Hello, world!", 13); // and this
sof.write(" more.", 6); // and this
// Check the destructor too
}
{
// Now it will work
BufferedOutFile sof(path);
sof.write("Hello, world2!", 14); // and this
sof.write(" more.", 6); // and this
sof.commit();
}
mDeleteList.push(path); // the delete list
// Now check it
checkFileContent(path, "Hello, world2! more.");
{
// Check that forgetting to commit won't overwrite the original
BufferedOutFile sof(path);
sof.write("New stuff!", 10);
// no commit
}
checkFileContent(path, "Hello, world2! more.");
{
// Check that we can overwrite an old file
BufferedOutFile sof(path);
sof.write("New stuff!", 10);
sof.commit();
}
checkFileContent(path, "New stuff!");
}
|