Skip to content

Commit 2b93b68

Browse files
authored
Add more tests for file (#161)
Type traits, member types and input/output operations are now tested
1 parent db6886e commit 2b93b68

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

tests/directory.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ TEST(directory, copy_directory) {
7878
EXPECT_TRUE(fs::is_directory(copy));
7979

8080
std::ifstream stream = std::ifstream(copy / "file");
81-
auto content = std::string(std::istreambuf_iterator<char>(stream), {});
81+
std::string content = std::string(std::istreambuf_iterator(stream), {});
8282
EXPECT_EQ(content, "Hello, world!");
8383
}
8484

@@ -112,8 +112,8 @@ TEST(directory, move_to_self) {
112112
EXPECT_TRUE(fs::exists(path));
113113

114114
{
115-
auto stream = std::ifstream(path / "file");
116-
auto content = std::string(std::istreambuf_iterator<char>(stream), {});
115+
std::ifstream stream = std::ifstream(path / "file");
116+
std::string content = std::string(std::istreambuf_iterator(stream), {});
117117
EXPECT_EQ(content, "Hello, world!");
118118
}
119119

@@ -140,8 +140,8 @@ TEST(directory, move_to_existing_directory) {
140140
EXPECT_FALSE(fs::exists(path));
141141

142142
{
143-
auto stream = std::ifstream(to / "file");
144-
auto content = std::string(std::istreambuf_iterator<char>(stream), {});
143+
std::ifstream stream = std::ifstream(to / "file");
144+
std::string content = std::string(std::istreambuf_iterator(stream), {});
145145
EXPECT_EQ(content, "Hello, world!");
146146
}
147147

tests/file.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ bool is_open(const file& file) {
2626
return file.rdbuf()->is_open();
2727
}
2828

29+
/// Tests file type traits and member types
30+
TEST(file, type_traits) {
31+
using traits = std::char_traits<char>;
32+
33+
static_assert(std::is_base_of_v<std::basic_iostream<char>, file>);
34+
static_assert(std::is_same_v<file::char_type, char>);
35+
static_assert(std::is_same_v<file::traits_type, traits>);
36+
static_assert(std::is_same_v<file::int_type, traits::int_type>);
37+
static_assert(std::is_same_v<file::pos_type, traits::pos_type>);
38+
static_assert(std::is_same_v<file::off_type, traits::off_type>);
39+
}
40+
2941
/// Tests file creation with label
3042
TEST(file, create_with_label) {
3143
file tmpfile = file(LABEL);
@@ -109,8 +121,8 @@ TEST(file, copy_file) {
109121

110122
EXPECT_TRUE(fs::is_regular_file(tmpfile));
111123

112-
auto stream = std::ifstream(copy.path());
113-
auto content = std::string(std::istreambuf_iterator<char>(stream), {});
124+
std::ifstream stream = std::ifstream(copy.path());
125+
std::string content = std::string(std::istreambuf_iterator(stream), {});
114126
EXPECT_EQ(content, "Hello, world!");
115127
}
116128

@@ -136,8 +148,8 @@ TEST(file, move_to_self) {
136148
EXPECT_TRUE(fs::exists(path));
137149

138150
{
139-
auto stream = std::ifstream(path);
140-
auto content = std::string(std::istreambuf_iterator<char>(stream), {});
151+
std::ifstream stream = std::ifstream(path);
152+
std::string content = std::string(std::istreambuf_iterator(stream), {});
141153
EXPECT_EQ(content, "Hello, world!");
142154
}
143155

@@ -165,8 +177,8 @@ TEST(file, move_to_existing_file) {
165177
EXPECT_FALSE(fs::exists(path, ec));
166178

167179
{
168-
auto stream = std::ifstream(to);
169-
auto content = std::string(std::istreambuf_iterator<char>(stream), {});
180+
std::ifstream stream = std::ifstream(to);
181+
std::string content = std::string(std::istreambuf_iterator(stream), {});
170182
EXPECT_EQ(content, "Hello, world!");
171183
}
172184

@@ -207,18 +219,27 @@ TEST(file, destructor) {
207219
/// Tests file move constructor
208220
TEST(file, move_constructor) {
209221
file fst = file();
222+
fst << "Hello!";
223+
210224
file snd = file(std::move(fst));
211225

212226
EXPECT_FALSE(snd.path().empty());
213227
EXPECT_TRUE(fs::exists(snd));
214228
EXPECT_TRUE(is_open(snd));
229+
230+
snd.seekg(0);
231+
std::string content;
232+
snd >> content;
233+
EXPECT_EQ(content, "Hello!");
215234
}
216235

217236
/// Tests file move assignment operator
218237
TEST(file, move_assignment) {
219238
file fst = file();
239+
220240
{
221241
file snd = file();
242+
snd << "Hello!";
222243

223244
fs::path path1 = fst;
224245
fs::path path2 = snd;
@@ -233,6 +254,11 @@ TEST(file, move_assignment) {
233254
}
234255

235256
EXPECT_FALSE(fst.path().empty());
257+
258+
fst.seekg(0);
259+
std::string content;
260+
fst >> content;
261+
EXPECT_EQ(content, "Hello!");
236262
}
237263

238264
/// Tests file swapping

0 commit comments

Comments
 (0)