Line data Source code
1 : ///
2 : /// @file ar/file_tests.cpp
3 : /// @copyright (c) 2015 by Petr Zemek (s3rvac@gmail.com) and contributors
4 : /// @license MIT, see the @c LICENSE file for more details
5 : /// @brief Tests for the @c file module.
6 : ///
7 :
8 : #include <gtest/gtest.h>
9 :
10 : #include "ar/file.h"
11 : #include "ar/internal/utilities/os.h"
12 :
13 : namespace ar {
14 : namespace tests {
15 :
16 : ///
17 : /// Tests for File.
18 : ///
19 6 : class FileTests: public testing::Test {};
20 :
21 5 : TEST_F(FileTests,
22 : FromContentWithNameReturnsFileWithCorrectContentAndName) {
23 2 : auto file = File::fromContentWithName("content", "file.txt");
24 :
25 1 : ASSERT_EQ("content", file->getContent());
26 1 : ASSERT_EQ("file.txt", file->getName());
27 : }
28 :
29 5 : TEST_F(FileTests,
30 : FromFilesystemReturnsFileWithCorrectName) {
31 : #ifdef AR_OS_WINDOWS
32 : auto file = File::fromFilesystem(R"(C:\\/path/to/file.txt)");
33 : #else
34 2 : auto file = File::fromFilesystem("/path/to/file.txt");
35 : #endif
36 :
37 1 : ASSERT_EQ("file.txt", file->getName());
38 : }
39 :
40 5 : TEST_F(FileTests,
41 : FromFilesystemWithOtherNameReturnsFileWithCorrectName) {
42 : #ifdef AR_OS_WINDOWS
43 : auto file = File::fromFilesystemWithOtherName(
44 : R"(C:\\/path/to/file.txt)", "other.txt");
45 : #else
46 : auto file = File::fromFilesystemWithOtherName(
47 2 : "/path/to/file.txt", "other.txt");
48 : #endif
49 :
50 1 : ASSERT_EQ("other.txt", file->getName());
51 : }
52 :
53 : ///
54 : /// Tests for Files.
55 : ///
56 14 : class FilesTests: public testing::Test {
57 : protected:
58 : std::unique_ptr<File> anyFile();
59 : std::unique_ptr<File> fileNamed(const std::string& name);
60 : };
61 :
62 2 : std::unique_ptr<File> FilesTests::anyFile() {
63 2 : return fileNamed("noname");
64 : }
65 :
66 8 : std::unique_ptr<File> FilesTests::fileNamed(const std::string& name) {
67 8 : return File::fromContentWithName("content", name);
68 : }
69 :
70 5 : TEST_F(FilesTests,
71 : MoveConstructorMoveConstructsFilesFromOtherFiles) {
72 2 : Files files;
73 1 : files.push_back(anyFile());
74 :
75 2 : Files filesMoveCopy(std::move(files));
76 :
77 1 : ASSERT_EQ(1, filesMoveCopy.size());
78 : }
79 :
80 5 : TEST_F(FilesTests,
81 : ContainerIsEmptyAfterCreationByDefault) {
82 2 : Files files;
83 :
84 1 : ASSERT_TRUE(files.empty());
85 : }
86 :
87 5 : TEST_F(FilesTests,
88 : ContainersSizeIsZeroAfterCreationByDefault) {
89 2 : Files files;
90 :
91 1 : ASSERT_EQ(0, files.size());
92 : }
93 :
94 5 : TEST_F(FilesTests,
95 : ContainerIsNoLongerEmptyAfterAppendingFileIntoEmptyContainer) {
96 2 : Files files;
97 :
98 1 : files.push_back(anyFile());
99 :
100 1 : ASSERT_FALSE(files.empty());
101 : }
102 :
103 5 : TEST_F(FilesTests,
104 : FrontReturnsReferenceToFirstFile) {
105 2 : Files files;
106 1 : files.push_back(fileNamed("a"));
107 1 : files.push_back(fileNamed("b"));
108 :
109 1 : ASSERT_EQ(files.front(), files.front());
110 1 : ASSERT_EQ("a", files.front()->getName());
111 : }
112 :
113 5 : TEST_F(FilesTests,
114 : BackReturnsReferenceToLastFile) {
115 2 : Files files;
116 1 : files.push_back(fileNamed("a"));
117 1 : files.push_back(fileNamed("b"));
118 :
119 1 : ASSERT_EQ(files.back(), files.back());
120 1 : ASSERT_EQ("b", files.back()->getName());
121 : }
122 :
123 5 : TEST_F(FilesTests,
124 : IterationOverNonConstContainerThroughBeginEndWorksCorrectly) {
125 2 : Files files;
126 1 : files.push_back(fileNamed("a"));
127 1 : files.push_back(fileNamed("b"));
128 :
129 1 : auto it = files.begin();
130 1 : ASSERT_EQ("a", (*it)->getName());
131 1 : ++it;
132 1 : ASSERT_EQ("b", (*it)->getName());
133 1 : ++it;
134 1 : ASSERT_EQ(it, files.end());
135 : }
136 :
137 : } // namespace tests
138 3 : } // namespace ar
|