Add Buffer unit tests
- test that writing a buffer with no changes results in the identical file being written
This commit is contained in:
parent
63b3848424
commit
10a0b33dbe
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
/.lock-waf*
|
/.lock-waf*
|
||||||
/.waf*
|
/.waf*
|
||||||
/build/
|
/build/
|
||||||
|
/test/tmp/
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "TestSupport.h"
|
#include "TestSupport.h"
|
||||||
#include "File.h"
|
#include "File.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
std::shared_ptr<std::vector<uint8_t>> TestSupport::read_file(const char * filename)
|
std::shared_ptr<std::vector<uint8_t>> TestSupport::read_file(const char * filename)
|
||||||
{
|
{
|
||||||
@ -22,3 +23,14 @@ std::shared_ptr<std::vector<uint8_t>> TestSupport::read_file(const char * filena
|
|||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestSupport::compare_files(const char * f1, const char * f2)
|
||||||
|
{
|
||||||
|
auto fl1 = read_file(f1);
|
||||||
|
auto fl2 = read_file(f2);
|
||||||
|
ASSERT_EQ(fl1->size(), fl2->size());
|
||||||
|
for (size_t i = 0; i < fl1->size(); i++)
|
||||||
|
{
|
||||||
|
ASSERT_EQ((*fl1)[i], (*fl2)[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -9,6 +9,7 @@ class TestSupport
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static std::shared_ptr<std::vector<uint8_t>> read_file(const char * filename);
|
static std::shared_ptr<std::vector<uint8_t>> read_file(const char * filename);
|
||||||
|
static void compare_files(const char * f1, const char * f2);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "System.h"
|
#include "System.h"
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
int main(int argc, char * argv[])
|
int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
System::init();
|
System::init();
|
||||||
|
mkdir("test/tmp", 0777);
|
||||||
return RUN_ALL_TESTS();
|
return RUN_ALL_TESTS();
|
||||||
}
|
}
|
||||||
|
23
test/src/test_Buffer.cc
Normal file
23
test/src/test_Buffer.cc
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "Buffer.h"
|
||||||
|
#include "TestSupport.h"
|
||||||
|
#include "Path.h"
|
||||||
|
|
||||||
|
TEST(BufferTest, writes_an_identical_file_to_what_is_loaded_if_no_changes_were_performed)
|
||||||
|
{
|
||||||
|
const char * files_to_test[] = {
|
||||||
|
"test/files/empty.txt",
|
||||||
|
"test/files/line_endings/cr_format.txt",
|
||||||
|
"test/files/line_endings/crlf_format.txt",
|
||||||
|
"test/files/line_endings/lf_format.txt",
|
||||||
|
"test/files/no_eol_at_eof.txt",
|
||||||
|
};
|
||||||
|
for (auto e : files_to_test)
|
||||||
|
{
|
||||||
|
Buffer b;
|
||||||
|
ASSERT_TRUE(b.load_from_file(e));
|
||||||
|
ASSERT_TRUE(b.write_to_file("test/tmp/f"));
|
||||||
|
ASSERT_TRUE(Path::is_file("test/tmp/f"));
|
||||||
|
TestSupport::compare_files(e, "test/tmp/f");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user