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 // Authors: keith.ray@gmail.com (Keith Ray)
32 // Google Test UnitTestOptions tests
34 // This file tests classes and functions used internally by
35 // Google Test. They are subject to change without notice.
37 // This file is #included from gtest.cc, to avoid changing build or
38 // make-files on Windows and other platforms. Do not #include this file
41 #include "gtest/gtest.h"
43 #if GTEST_OS_WINDOWS_MOBILE
45 #elif GTEST_OS_WINDOWS
47 #endif // GTEST_OS_WINDOWS_MOBILE
49 // Indicates that this translation unit is part of Google Test's
50 // implementation. It must come before gtest-internal-inl.h is
51 // included, or there will be a compiler error. This trick is to
52 // prevent a user from accidentally including gtest-internal-inl.h in
54 #define GTEST_IMPLEMENTATION_ 1
55 #include "src/gtest-internal-inl.h"
56 #undef GTEST_IMPLEMENTATION_
62 // Turns the given relative path into an absolute path.
63 FilePath
GetAbsolutePathOf(const FilePath
& relative_path
) {
64 return FilePath::ConcatPaths(FilePath::GetCurrentDir(), relative_path
);
67 // Testing UnitTestOptions::GetOutputFormat/GetOutputFile.
69 TEST(XmlOutputTest
, GetOutputFormatDefault
) {
70 GTEST_FLAG(output
) = "";
71 EXPECT_STREQ("", UnitTestOptions::GetOutputFormat().c_str());
74 TEST(XmlOutputTest
, GetOutputFormat
) {
75 GTEST_FLAG(output
) = "xml:filename";
76 EXPECT_STREQ("xml", UnitTestOptions::GetOutputFormat().c_str());
79 TEST(XmlOutputTest
, GetOutputFileDefault
) {
80 GTEST_FLAG(output
) = "";
81 EXPECT_EQ(GetAbsolutePathOf(FilePath("test_detail.xml")).string(),
82 UnitTestOptions::GetAbsolutePathToOutputFile());
85 TEST(XmlOutputTest
, GetOutputFileSingleFile
) {
86 GTEST_FLAG(output
) = "xml:filename.abc";
87 EXPECT_EQ(GetAbsolutePathOf(FilePath("filename.abc")).string(),
88 UnitTestOptions::GetAbsolutePathToOutputFile());
91 TEST(XmlOutputTest
, GetOutputFileFromDirectoryPath
) {
92 GTEST_FLAG(output
) = "xml:path" GTEST_PATH_SEP_
;
93 const std::string expected_output_file
=
95 FilePath(std::string("path") + GTEST_PATH_SEP_
+
96 GetCurrentExecutableName().string() + ".xml")).string();
97 const std::string
& output_file
=
98 UnitTestOptions::GetAbsolutePathToOutputFile();
100 EXPECT_STRCASEEQ(expected_output_file
.c_str(), output_file
.c_str());
102 EXPECT_EQ(expected_output_file
, output_file
.c_str());
106 TEST(OutputFileHelpersTest
, GetCurrentExecutableName
) {
107 const std::string exe_str
= GetCurrentExecutableName().string();
110 _strcmpi("gtest-options_test", exe_str
.c_str()) == 0 ||
111 _strcmpi("gtest-options-ex_test", exe_str
.c_str()) == 0 ||
112 _strcmpi("gtest_all_test", exe_str
.c_str()) == 0 ||
113 _strcmpi("gtest_dll_test", exe_str
.c_str()) == 0;
115 // TODO(wan@google.com): remove the hard-coded "lt-" prefix when
116 // Chandler Carruth's libtool replacement is ready.
118 exe_str
== "gtest-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 class XmlOutputChangeDirTest
: public Test
{
129 virtual void SetUp() {
130 original_working_dir_
= FilePath::GetCurrentDir();
132 // This will make the test fail if run from the root directory.
133 EXPECT_NE(original_working_dir_
.string(),
134 FilePath::GetCurrentDir().string());
137 virtual void TearDown() {
138 posix::ChDir(original_working_dir_
.string().c_str());
141 FilePath original_working_dir_
;
144 TEST_F(XmlOutputChangeDirTest
, PreserveOriginalWorkingDirWithDefault
) {
145 GTEST_FLAG(output
) = "";
146 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_
,
147 FilePath("test_detail.xml")).string(),
148 UnitTestOptions::GetAbsolutePathToOutputFile());
151 TEST_F(XmlOutputChangeDirTest
, PreserveOriginalWorkingDirWithDefaultXML
) {
152 GTEST_FLAG(output
) = "xml";
153 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_
,
154 FilePath("test_detail.xml")).string(),
155 UnitTestOptions::GetAbsolutePathToOutputFile());
158 TEST_F(XmlOutputChangeDirTest
, PreserveOriginalWorkingDirWithRelativeFile
) {
159 GTEST_FLAG(output
) = "xml:filename.abc";
160 EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_
,
161 FilePath("filename.abc")).string(),
162 UnitTestOptions::GetAbsolutePathToOutputFile());
165 TEST_F(XmlOutputChangeDirTest
, PreserveOriginalWorkingDirWithRelativePath
) {
166 GTEST_FLAG(output
) = "xml:path" GTEST_PATH_SEP_
;
167 const std::string expected_output_file
=
168 FilePath::ConcatPaths(
169 original_working_dir_
,
170 FilePath(std::string("path") + GTEST_PATH_SEP_
+
171 GetCurrentExecutableName().string() + ".xml")).string();
172 const std::string
& output_file
=
173 UnitTestOptions::GetAbsolutePathToOutputFile();
175 EXPECT_STRCASEEQ(expected_output_file
.c_str(), output_file
.c_str());
177 EXPECT_EQ(expected_output_file
, output_file
.c_str());
181 TEST_F(XmlOutputChangeDirTest
, PreserveOriginalWorkingDirWithAbsoluteFile
) {
183 GTEST_FLAG(output
) = "xml:c:\\tmp\\filename.abc";
184 EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(),
185 UnitTestOptions::GetAbsolutePathToOutputFile());
187 GTEST_FLAG(output
) ="xml:/tmp/filename.abc";
188 EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
189 UnitTestOptions::GetAbsolutePathToOutputFile());
193 TEST_F(XmlOutputChangeDirTest
, PreserveOriginalWorkingDirWithAbsolutePath
) {
195 const std::string path
= "c:\\tmp\\";
197 const std::string path
= "/tmp/";
200 GTEST_FLAG(output
) = "xml:" + path
;
201 const std::string expected_output_file
=
202 path
+ GetCurrentExecutableName().string() + ".xml";
203 const std::string
& output_file
=
204 UnitTestOptions::GetAbsolutePathToOutputFile();
207 EXPECT_STRCASEEQ(expected_output_file
.c_str(), output_file
.c_str());
209 EXPECT_EQ(expected_output_file
, output_file
.c_str());
214 } // namespace internal
215 } // namespace testing