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
46 // For strcasecmp on OS/2
48 #endif // GTEST_OS_WINDOWS_MOBILE
50 #include "src/gtest-internal-inl.h"
56 // Turns the given relative path into an absolute path.
57 FilePath GetAbsolutePathOf(const FilePath& relative_path) {
58 return FilePath::ConcatPaths(FilePath::GetCurrentDir(), relative_path);
61 // Testing UnitTestOptions::GetOutputFormat/GetOutputFile.
63 TEST(XmlOutputTest, GetOutputFormatDefault) {
64 GTEST_FLAG(output) = "";
65 EXPECT_STREQ("", UnitTestOptions::GetOutputFormat().c_str());
68 TEST(XmlOutputTest, GetOutputFormat) {
69 GTEST_FLAG(output) = "xml:filename";
70 EXPECT_STREQ("xml", UnitTestOptions::GetOutputFormat().c_str());
73 TEST(XmlOutputTest, GetOutputFileDefault) {
74 GTEST_FLAG(output) = "";
75 EXPECT_EQ(GetAbsolutePathOf(FilePath("test_detail.xml")).string(),
76 UnitTestOptions::GetAbsolutePathToOutputFile());
79 TEST(XmlOutputTest, GetOutputFileSingleFile) {
80 GTEST_FLAG(output) = "xml:filename.abc";
81 EXPECT_EQ(GetAbsolutePathOf(FilePath("filename.abc")).string(),
82 UnitTestOptions::GetAbsolutePathToOutputFile());
85 TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
86 GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
87 const std::string expected_output_file =
89 FilePath(std::string("path") + GTEST_PATH_SEP_ +
90 GetCurrentExecutableName().string() + ".xml")).string();
91 const std::string& output_file =
92 UnitTestOptions::GetAbsolutePathToOutputFile();
94 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
96 EXPECT_EQ(expected_output_file, output_file.c_str());
100 TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
101 const std::string exe_str = GetCurrentExecutableName().string();
104 _strcmpi("googletest-options-test", exe_str.c_str()) == 0 ||
105 _strcmpi("gtest-options-ex_test", exe_str.c_str()) == 0 ||
106 _strcmpi("gtest_all_test", exe_str.c_str()) == 0 ||
107 _strcmpi("gtest_dll_test", exe_str.c_str()) == 0;
110 strcasecmp("googletest-options-test", exe_str.c_str()) == 0 ||
111 strcasecmp("gtest-options-ex_test", exe_str.c_str()) == 0 ||
112 strcasecmp("gtest_all_test", exe_str.c_str()) == 0 ||
113 strcasecmp("gtest_dll_test", exe_str.c_str()) == 0;
114 #elif GTEST_OS_FUCHSIA
115 const bool success = exe_str == "app";
118 exe_str == "googletest-options-test" ||
119 exe_str == "gtest_all_test" ||
120 exe_str == "lt-gtest_all_test" ||
121 exe_str == "gtest_dll_test";
122 #endif // GTEST_OS_WINDOWS
124 FAIL() << "GetCurrentExecutableName() returns " << exe_str;
127 #if !GTEST_OS_FUCHSIA
129 class XmlOutputChangeDirTest : public Test {
131 void SetUp() override {
132 original_working_dir_ = FilePath::GetCurrentDir();
134 // This will make the test fail if run from the root directory.
135 EXPECT_NE(original_working_dir_.string(),
136 FilePath::GetCurrentDir().string());
139 void TearDown() override {
140 posix::ChDir(original_working_dir_.string().c_str());
143 FilePath original_working_dir_;
146 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
147 GTEST_FLAG(output) = "";
148 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
149 FilePath("test_detail.xml")).string(),
150 UnitTestOptions::GetAbsolutePathToOutputFile());
153 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
154 GTEST_FLAG(output) = "xml";
155 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
156 FilePath("test_detail.xml")).string(),
157 UnitTestOptions::GetAbsolutePathToOutputFile());
160 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
161 GTEST_FLAG(output) = "xml:filename.abc";
162 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
163 FilePath("filename.abc")).string(),
164 UnitTestOptions::GetAbsolutePathToOutputFile());
167 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
168 GTEST_FLAG(output) = "xml:path" GTEST_PATH_SEP_;
169 const std::string expected_output_file =
170 FilePath::ConcatPaths(
171 original_working_dir_,
172 FilePath(std::string("path") + GTEST_PATH_SEP_ +
173 GetCurrentExecutableName().string() + ".xml")).string();
174 const std::string& output_file =
175 UnitTestOptions::GetAbsolutePathToOutputFile();
177 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
179 EXPECT_EQ(expected_output_file, output_file.c_str());
183 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
185 GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc";
186 EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(),
187 UnitTestOptions::GetAbsolutePathToOutputFile());
189 GTEST_FLAG(output) ="xml:/tmp/filename.abc";
190 EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
191 UnitTestOptions::GetAbsolutePathToOutputFile());
195 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
197 const std::string path = "c:\\tmp\\";
199 const std::string path = "/tmp/";
202 GTEST_FLAG(output) = "xml:" + path;
203 const std::string expected_output_file =
204 path + GetCurrentExecutableName().string() + ".xml";
205 const std::string& output_file =
206 UnitTestOptions::GetAbsolutePathToOutputFile();
209 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
211 EXPECT_EQ(expected_output_file, output_file.c_str());
215 #endif // !GTEST_OS_FUCHSIA
218 } // namespace internal
219 } // namespace testing