1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Google Test UnitTestOptions tests
32 // This file tests classes and functions used internally by
33 // Google Test. They are subject to change without notice.
35 // This file is #included from gtest.cc, to avoid changing build or
36 // make-files on Windows and other platforms. Do not #include this file
39 #include "gtest/gtest.h"
41 #if GTEST_OS_WINDOWS_MOBILE
43 #elif GTEST_OS_WINDOWS
45 #endif // GTEST_OS_WINDOWS_MOBILE
47 #include "src/gtest-internal-inl.h"
53 // Turns the given relative path into an absolute path.
54 FilePath GetAbsolutePathOf(const FilePath& relative_path) {
55 return FilePath::ConcatPaths(FilePath::GetCurrentDir(), relative_path);
58 // Testing UnitTestOptions::GetOutputFormat/GetOutputFile.
60 TEST(XmlOutputTest, GetOutputFormatDefault) {
61 GTEST_FLAG(output) = "";
62 EXPECT_STREQ("", UnitTestOptions::GetOutputFormat().c_str());
65 TEST(XmlOutputTest, GetOutputFormat) {
66 GTEST_FLAG(output) = "xml:filename";
67 EXPECT_STREQ("xml", UnitTestOptions::GetOutputFormat().c_str());
70 TEST(XmlOutputTest, GetOutputFileDefault) {
71 GTEST_FLAG(output) = "";
72 EXPECT_EQ(GetAbsolutePathOf(FilePath("test_detail.xml")).string(),
73 UnitTestOptions::GetAbsolutePathToOutputFile());
76 TEST(XmlOutputTest, GetOutputFileSingleFile) {
77 GTEST_FLAG(output) = "xml:filename.abc";
78 EXPECT_EQ(GetAbsolutePathOf(FilePath("filename.abc")).string(),
79 UnitTestOptions::GetAbsolutePathToOutputFile());
82 TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
83 GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
84 const std::string expected_output_file =
86 FilePath(std::string("path") + GTEST_PATH_SEP_ +
87 GetCurrentExecutableName().string() + ".xml")).string();
88 const std::string& output_file =
89 UnitTestOptions::GetAbsolutePathToOutputFile();
91 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
93 EXPECT_EQ(expected_output_file, output_file.c_str());
97 TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
98 const std::string exe_str = GetCurrentExecutableName().string();
101 _strcmpi("googletest-options-test", exe_str.c_str()) == 0 ||
102 _strcmpi("gtest-options-ex_test", exe_str.c_str()) == 0 ||
103 _strcmpi("gtest_all_test", exe_str.c_str()) == 0 ||
104 _strcmpi("gtest_dll_test", exe_str.c_str()) == 0;
107 strcasecmp("googletest-options-test", exe_str.c_str()) == 0 ||
108 strcasecmp("gtest-options-ex_test", exe_str.c_str()) == 0 ||
109 strcasecmp("gtest_all_test", exe_str.c_str()) == 0 ||
110 strcasecmp("gtest_dll_test", exe_str.c_str()) == 0;
111 #elif GTEST_OS_FUCHSIA
112 const bool success = exe_str == "app";
115 exe_str == "googletest-options-test" ||
116 exe_str == "gtest_all_test" ||
117 exe_str == "lt-gtest_all_test" ||
118 exe_str == "gtest_dll_test";
119 #endif // GTEST_OS_WINDOWS
121 FAIL() << "GetCurrentExecutableName() returns " << exe_str;
124 #if !GTEST_OS_FUCHSIA
126 class XmlOutputChangeDirTest : public Test {
128 void SetUp() override {
129 original_working_dir_ = FilePath::GetCurrentDir();
131 // This will make the test fail if run from the root directory.
132 EXPECT_NE(original_working_dir_.string(),
133 FilePath::GetCurrentDir().string());
136 void TearDown() override {
137 posix::ChDir(original_working_dir_.string().c_str());
140 FilePath original_working_dir_;
143 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
144 GTEST_FLAG(output) = "";
145 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
146 FilePath("test_detail.xml")).string(),
147 UnitTestOptions::GetAbsolutePathToOutputFile());
150 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
151 GTEST_FLAG(output) = "xml";
152 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
153 FilePath("test_detail.xml")).string(),
154 UnitTestOptions::GetAbsolutePathToOutputFile());
157 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
158 GTEST_FLAG(output) = "xml:filename.abc";
159 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
160 FilePath("filename.abc")).string(),
161 UnitTestOptions::GetAbsolutePathToOutputFile());
164 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
165 GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
166 const std::string expected_output_file =
167 FilePath::ConcatPaths(
168 original_working_dir_,
169 FilePath(std::string("path") + GTEST_PATH_SEP_ +
170 GetCurrentExecutableName().string() + ".xml")).string();
171 const std::string& output_file =
172 UnitTestOptions::GetAbsolutePathToOutputFile();
174 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
176 EXPECT_EQ(expected_output_file, output_file.c_str());
180 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
182 GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc";
183 EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(),
184 UnitTestOptions::GetAbsolutePathToOutputFile());
186 GTEST_FLAG(output) ="xml:/tmp/filename.abc";
187 EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
188 UnitTestOptions::GetAbsolutePathToOutputFile());
192 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
194 const std::string path = "c:\\tmp\\";
196 const std::string path = "/tmp/";
199 GTEST_FLAG(output) = "xml:" + path;
200 const std::string expected_output_file =
201 path + GetCurrentExecutableName().string() + ".xml";
202 const std::string& output_file =
203 UnitTestOptions::GetAbsolutePathToOutputFile();
206 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
208 EXPECT_EQ(expected_output_file, output_file.c_str());
212 #endif // !GTEST_OS_FUCHSIA
215 } // namespace internal
216 } // namespace testing