Line data Source code
1 : ///
2 : /// @file ar/internal/utilities/os_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 os module.
6 : ///
7 :
8 : #include <gtest/gtest.h>
9 :
10 : #include "ar/exceptions.h"
11 : #include "ar/internal/utilities/os.h"
12 : #include "ar/test_utilities/tmp_file.h"
13 :
14 : using namespace ar::tests;
15 :
16 : namespace ar {
17 : namespace internal {
18 : namespace tests {
19 :
20 : ///
21 : /// Tests for fileNameFromPath().
22 : ///
23 6 : class FileNameFromPathTests: public testing::Test {};
24 :
25 5 : TEST_F(FileNameFromPathTests,
26 : FileNameFromPathReturnsCorrectNameForPathWithSeparators) {
27 : #ifdef AR_OS_WINDOWS
28 : ASSERT_EQ("file.txt", fileNameFromPath(R"(C:\\path\to\file.txt)"));
29 : ASSERT_EQ("file.txt", fileNameFromPath(R"(C:/path/to/file.txt)"));
30 : #else
31 1 : ASSERT_EQ("file.txt", fileNameFromPath("/path/to/file.txt"));
32 : #endif
33 : }
34 :
35 5 : TEST_F(FileNameFromPathTests,
36 : FileNameFromPathContainingJustFileNameReturnsCorrectName) {
37 1 : ASSERT_EQ("file.txt", fileNameFromPath("file.txt"));
38 : }
39 :
40 5 : TEST_F(FileNameFromPathTests,
41 : FileNameFromPathContainingNoFileNameReturnsEmptyString) {
42 : #ifdef AR_OS_WINDOWS
43 : ASSERT_EQ("", fileNameFromPath(R"(C:\\path\to\dir\)"));
44 : ASSERT_EQ("", fileNameFromPath(R"(C:/path/to/dir/)"));
45 : #else
46 1 : ASSERT_EQ("", fileNameFromPath("/path/to/dir/"));
47 : #endif
48 : }
49 :
50 : ///
51 : /// Tests for joinPaths().
52 : ///
53 10 : class JoinPathsTests: public testing::Test {};
54 :
55 5 : TEST_F(JoinPathsTests,
56 : JoinPathsReturnsSecondPathWhenFirstPathIsEmpty) {
57 1 : ASSERT_EQ("dir", joinPaths("", "dir"));
58 : }
59 :
60 5 : TEST_F(JoinPathsTests,
61 : JoinPathsReturnsFirstPathWhenSecondPathIsEmpty) {
62 1 : ASSERT_EQ("dir", joinPaths("dir", ""));
63 : }
64 :
65 5 : TEST_F(JoinPathsTests,
66 : JoinPathsReturnsSecondPathWhenSecondPathIsAbsolute) {
67 : #ifdef AR_OS_WINDOWS
68 : ASSERT_EQ(R"(C:\\path)", joinPaths("dir", R"(C:\\path)"));
69 : ASSERT_EQ(R"(C:/path)", joinPaths("dir", R"(C:/path)"));
70 : #endif
71 1 : ASSERT_EQ("/path/to", joinPaths("dir", "/path/to"));
72 : }
73 :
74 5 : TEST_F(JoinPathsTests,
75 : TwoPathsAreJoinedCorrectlyWhenFirstPathDoesNotEndWithSlash) {
76 : #ifdef AR_OS_WINDOWS
77 : ASSERT_EQ(R"(C:\\path\to\dir)", joinPaths(R"(C:\\path\to)", "dir"));
78 : ASSERT_EQ(R"(C:/path/to/dir)", joinPaths(R"(C:/path/to)", "dir"));
79 : #endif
80 1 : ASSERT_EQ("/path/to/dir", joinPaths("/path/to", "dir"));
81 : }
82 :
83 5 : TEST_F(JoinPathsTests,
84 : TwoPathsAreJoinedCorrectlyWhenFirstPathEndsWithSlash) {
85 : #ifdef AR_OS_WINDOWS
86 : ASSERT_EQ(R"(C:\\path\to\dir)", joinPaths(R"(C:\\path\to\)", "dir"));
87 : ASSERT_EQ(R"(C:/path/to/dir)", joinPaths(R"(C:/path/to/)", "dir"));
88 : #endif
89 1 : ASSERT_EQ("/path/to/dir", joinPaths("/path/to/", "dir"));
90 : }
91 :
92 : ///
93 : /// Tests for readFile().
94 : ///
95 4 : class ReadFileTests: public testing::Test {};
96 :
97 5 : TEST_F(ReadFileTests,
98 : ReturnsCorrectContentWhenFileExists) {
99 2 : auto tmpFile = TmpFile::createWithContent("content");
100 :
101 1 : ASSERT_EQ("content", readFile(tmpFile->getPath()));
102 : }
103 :
104 5 : TEST_F(ReadFileTests,
105 : ThrowsIOErrorWhenFileDoesNotExist) {
106 1 : ASSERT_THROW(readFile("nonexisting-file"), IOError);
107 : }
108 :
109 : ///
110 : /// Tests for writeFile().
111 : ///
112 4 : class WriteFileTests: public testing::Test {};
113 :
114 5 : TEST_F(WriteFileTests,
115 : WritesCorrectContentToFile) {
116 2 : const std::string Content{"content"};
117 2 : auto tmpFile = TmpFile::createWithContent("");
118 :
119 1 : writeFile(tmpFile->getPath(), Content);
120 :
121 1 : ASSERT_EQ(Content, readFile(tmpFile->getPath()));
122 : }
123 :
124 5 : TEST_F(WriteFileTests,
125 : ThrowsIOErrorWhenFileCannotBeOpenedForWriting) {
126 1 : ASSERT_THROW(writeFile("/", "content"), IOError);
127 : }
128 :
129 : ///
130 : /// Tests for copyFile().
131 : ///
132 4 : class CopyFileTests: public testing::Test {};
133 :
134 5 : TEST_F(CopyFileTests,
135 : WritesCorrectContentToFile) {
136 2 : const std::string Content{"content"};
137 2 : auto tmpInFile = TmpFile::createWithContent(Content);
138 2 : auto tmpOutFile = TmpFile::createWithContent("");
139 :
140 1 : copyFile(tmpInFile->getPath(), tmpOutFile->getPath());
141 :
142 1 : ASSERT_EQ(Content, readFile(tmpOutFile->getPath()));
143 : }
144 :
145 5 : TEST_F(CopyFileTests,
146 : ThrowsIOErrorWhenSourceFileDoesNotExist) {
147 1 : ASSERT_THROW(copyFile("nonexisting-file", "any-file"), IOError);
148 : }
149 :
150 : } // namespace tests
151 : } // namespace internal
152 3 : } // namespace ar
|