Imported Upstream version 1.12.0
[platform/upstream/gtest.git] / googletest / test / googletest-options-test.cc
1 // Copyright 2008, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29 //
30 // Google Test UnitTestOptions tests
31 //
32 // This file tests classes and functions used internally by
33 // Google Test.  They are subject to change without notice.
34 //
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
37 // anywhere else!
38
39 #include "gtest/gtest.h"
40
41 #if GTEST_OS_WINDOWS_MOBILE
42 #include <windows.h>
43 #elif GTEST_OS_WINDOWS
44 #include <direct.h>
45 #elif GTEST_OS_OS2
46 // For strcasecmp on OS/2
47 #include <strings.h>
48 #endif  // GTEST_OS_WINDOWS_MOBILE
49
50 #include "src/gtest-internal-inl.h"
51
52 namespace testing {
53 namespace internal {
54 namespace {
55
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);
59 }
60
61 // Testing UnitTestOptions::GetOutputFormat/GetOutputFile.
62
63 TEST(XmlOutputTest, GetOutputFormatDefault) {
64   GTEST_FLAG_SET(output, "");
65   EXPECT_STREQ("", UnitTestOptions::GetOutputFormat().c_str());
66 }
67
68 TEST(XmlOutputTest, GetOutputFormat) {
69   GTEST_FLAG_SET(output, "xml:filename");
70   EXPECT_STREQ("xml", UnitTestOptions::GetOutputFormat().c_str());
71 }
72
73 TEST(XmlOutputTest, GetOutputFileDefault) {
74   GTEST_FLAG_SET(output, "");
75   EXPECT_EQ(GetAbsolutePathOf(FilePath("test_detail.xml")).string(),
76             UnitTestOptions::GetAbsolutePathToOutputFile());
77 }
78
79 TEST(XmlOutputTest, GetOutputFileSingleFile) {
80   GTEST_FLAG_SET(output, "xml:filename.abc");
81   EXPECT_EQ(GetAbsolutePathOf(FilePath("filename.abc")).string(),
82             UnitTestOptions::GetAbsolutePathToOutputFile());
83 }
84
85 TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
86   GTEST_FLAG_SET(output, "xml:path" GTEST_PATH_SEP_);
87   const std::string expected_output_file =
88       GetAbsolutePathOf(FilePath(std::string("path") + GTEST_PATH_SEP_ +
89                                  GetCurrentExecutableName().string() + ".xml"))
90           .string();
91   const std::string& output_file =
92       UnitTestOptions::GetAbsolutePathToOutputFile();
93 #if GTEST_OS_WINDOWS
94   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
95 #else
96   EXPECT_EQ(expected_output_file, output_file.c_str());
97 #endif
98 }
99
100 TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
101   const std::string exe_str = GetCurrentExecutableName().string();
102 #if GTEST_OS_WINDOWS
103   const bool success =
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;
108 #elif GTEST_OS_OS2
109   const bool success =
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";
116 #else
117   const bool success =
118       exe_str == "googletest-options-test" || exe_str == "gtest_all_test" ||
119       exe_str == "lt-gtest_all_test" || exe_str == "gtest_dll_test";
120 #endif  // GTEST_OS_WINDOWS
121   if (!success) FAIL() << "GetCurrentExecutableName() returns " << exe_str;
122 }
123
124 #if !GTEST_OS_FUCHSIA
125
126 class XmlOutputChangeDirTest : public Test {
127  protected:
128   void SetUp() override {
129     original_working_dir_ = FilePath::GetCurrentDir();
130     posix::ChDir("..");
131     // This will make the test fail if run from the root directory.
132     EXPECT_NE(original_working_dir_.string(),
133               FilePath::GetCurrentDir().string());
134   }
135
136   void TearDown() override {
137     posix::ChDir(original_working_dir_.string().c_str());
138   }
139
140   FilePath original_working_dir_;
141 };
142
143 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
144   GTEST_FLAG_SET(output, "");
145   EXPECT_EQ(
146       FilePath::ConcatPaths(original_working_dir_, FilePath("test_detail.xml"))
147           .string(),
148       UnitTestOptions::GetAbsolutePathToOutputFile());
149 }
150
151 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
152   GTEST_FLAG_SET(output, "xml");
153   EXPECT_EQ(
154       FilePath::ConcatPaths(original_working_dir_, FilePath("test_detail.xml"))
155           .string(),
156       UnitTestOptions::GetAbsolutePathToOutputFile());
157 }
158
159 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
160   GTEST_FLAG_SET(output, "xml:filename.abc");
161   EXPECT_EQ(
162       FilePath::ConcatPaths(original_working_dir_, FilePath("filename.abc"))
163           .string(),
164       UnitTestOptions::GetAbsolutePathToOutputFile());
165 }
166
167 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
168   GTEST_FLAG_SET(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"))
174           .string();
175   const std::string& output_file =
176       UnitTestOptions::GetAbsolutePathToOutputFile();
177 #if GTEST_OS_WINDOWS
178   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
179 #else
180   EXPECT_EQ(expected_output_file, output_file.c_str());
181 #endif
182 }
183
184 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
185 #if GTEST_OS_WINDOWS
186   GTEST_FLAG_SET(output, "xml:c:\\tmp\\filename.abc");
187   EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(),
188             UnitTestOptions::GetAbsolutePathToOutputFile());
189 #else
190   GTEST_FLAG_SET(output, "xml:/tmp/filename.abc");
191   EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
192             UnitTestOptions::GetAbsolutePathToOutputFile());
193 #endif
194 }
195
196 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
197 #if GTEST_OS_WINDOWS
198   const std::string path = "c:\\tmp\\";
199 #else
200   const std::string path = "/tmp/";
201 #endif
202
203   GTEST_FLAG_SET(output, "xml:" + path);
204   const std::string expected_output_file =
205       path + GetCurrentExecutableName().string() + ".xml";
206   const std::string& output_file =
207       UnitTestOptions::GetAbsolutePathToOutputFile();
208
209 #if GTEST_OS_WINDOWS
210   EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
211 #else
212   EXPECT_EQ(expected_output_file, output_file.c_str());
213 #endif
214 }
215
216 #endif  // !GTEST_OS_FUCHSIA
217
218 }  // namespace
219 }  // namespace internal
220 }  // namespace testing