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_STREQ(GetAbsolutePathOf(FilePath("test_detail.xml")).c_str(),
82 UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
85 TEST(XmlOutputTest, GetOutputFileSingleFile) {
86 GTEST_FLAG(output) = "xml:filename.abc";
87 EXPECT_STREQ(GetAbsolutePathOf(FilePath("filename.abc")).c_str(),
88 UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
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().c_str() + ".xml")).c_str();
97 const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
99 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
101 EXPECT_EQ(expected_output_file, output_file.c_str());
105 TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
106 const std::string exe_str = GetCurrentExecutableName().c_str();
109 _strcmpi("gtest-options_test", exe_str.c_str()) == 0 ||
110 _strcmpi("gtest-options-ex_test", exe_str.c_str()) == 0 ||
111 _strcmpi("gtest_all_test", exe_str.c_str()) == 0 ||
112 _strcmpi("gtest_dll_test", exe_str.c_str()) == 0;
114 // TODO(wan@google.com): remove the hard-coded "lt-" prefix when
115 // Chandler Carruth's libtool replacement is ready.
117 exe_str == "gtest-options_test" ||
118 exe_str == "gtest_all_test" ||
119 exe_str == "lt-gtest_all_test" ||
120 exe_str == "gtest_dll_test";
121 #endif // GTEST_OS_WINDOWS
123 FAIL() << "GetCurrentExecutableName() returns " << exe_str;
126 class XmlOutputChangeDirTest : public Test {
128 virtual void SetUp() {
129 original_working_dir_ = FilePath::GetCurrentDir();
131 // This will make the test fail if run from the root directory.
132 EXPECT_STRNE(original_working_dir_.c_str(),
133 FilePath::GetCurrentDir().c_str());
136 virtual void TearDown() {
137 posix::ChDir(original_working_dir_.c_str());
140 FilePath original_working_dir_;
143 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
144 GTEST_FLAG(output) = "";
145 EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
146 FilePath("test_detail.xml")).c_str(),
147 UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
150 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
151 GTEST_FLAG(output) = "xml";
152 EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
153 FilePath("test_detail.xml")).c_str(),
154 UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
157 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
158 GTEST_FLAG(output) = "xml:filename.abc";
159 EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_,
160 FilePath("filename.abc")).c_str(),
161 UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
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().c_str() + ".xml")).c_str();
171 const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
173 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
175 EXPECT_EQ(expected_output_file, output_file.c_str());
179 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
181 GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc";
182 EXPECT_STREQ(FilePath("c:\\tmp\\filename.abc").c_str(),
183 UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
185 GTEST_FLAG(output) ="xml:/tmp/filename.abc";
186 EXPECT_STREQ(FilePath("/tmp/filename.abc").c_str(),
187 UnitTestOptions::GetAbsolutePathToOutputFile().c_str());
191 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
193 const std::string path = "c:\\tmp\\";
195 const std::string path = "/tmp/";
198 GTEST_FLAG(output) = "xml:" + path;
199 const std::string expected_output_file =
200 path + GetCurrentExecutableName().c_str() + ".xml";
201 const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile();
204 EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
206 EXPECT_EQ(expected_output_file, output_file.c_str());
211 } // namespace internal
212 } // namespace testing