Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
googletest-options-test.cc
Go to the documentation of this file.
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 <string>
40 
41 #include "gtest/gtest.h"
42 
43 #ifdef GTEST_OS_WINDOWS_MOBILE
44 #include <windows.h>
45 #elif defined(GTEST_OS_WINDOWS)
46 #include <direct.h>
47 #elif defined(GTEST_OS_OS2)
48 // For strcasecmp on OS/2
49 #include <strings.h>
50 #endif // GTEST_OS_WINDOWS_MOBILE
51 
52 #include "src/gtest-internal-inl.h"
53 
54 namespace testing {
55 namespace internal {
56 namespace {
57 
58 // Turns the given relative path into an absolute path.
59 FilePath GetAbsolutePathOf(const FilePath& relative_path) {
60  return FilePath::ConcatPaths(FilePath::GetCurrentDir(), relative_path);
61 }
62 
63 // Testing UnitTestOptions::GetOutputFormat/GetOutputFile.
64 
65 TEST(XmlOutputTest, GetOutputFormatDefault) {
66  GTEST_FLAG_SET(output, "");
68 }
69 
70 TEST(XmlOutputTest, GetOutputFormat) {
71  GTEST_FLAG_SET(output, "xml:filename");
73 }
74 
75 TEST(XmlOutputTest, GetOutputFileDefault) {
76  GTEST_FLAG_SET(output, "");
77  EXPECT_EQ(GetAbsolutePathOf(FilePath("test_detail.xml")).string(),
79 }
80 
81 TEST(XmlOutputTest, GetOutputFileSingleFile) {
82  GTEST_FLAG_SET(output, "xml:filename.abc");
83  EXPECT_EQ(GetAbsolutePathOf(FilePath("filename.abc")).string(),
85 }
86 
87 TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
88  GTEST_FLAG_SET(output, "xml:path" GTEST_PATH_SEP_);
89  const std::string expected_output_file =
90  GetAbsolutePathOf(FilePath(std::string("path") + GTEST_PATH_SEP_ +
91  GetCurrentExecutableName().string() + ".xml"))
92  .string();
93  const std::string& output_file =
95 #ifdef GTEST_OS_WINDOWS
96  EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
97 #else
98  EXPECT_EQ(expected_output_file, output_file.c_str());
99 #endif
100 }
101 
102 TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
103  const std::string exe_str = GetCurrentExecutableName().string();
104 #ifdef GTEST_OS_WINDOWS
105  const bool success =
106  _strcmpi("googletest-options-test", exe_str.c_str()) == 0 ||
107  _strcmpi("gtest-options-ex_test", exe_str.c_str()) == 0 ||
108  _strcmpi("gtest_all_test", exe_str.c_str()) == 0 ||
109  _strcmpi("gtest_dll_test", exe_str.c_str()) == 0;
110 #elif defined(GTEST_OS_OS2)
111  const bool success =
112  strcasecmp("googletest-options-test", exe_str.c_str()) == 0 ||
113  strcasecmp("gtest-options-ex_test", exe_str.c_str()) == 0 ||
114  strcasecmp("gtest_all_test", exe_str.c_str()) == 0 ||
115  strcasecmp("gtest_dll_test", exe_str.c_str()) == 0;
116 #elif defined(GTEST_OS_FUCHSIA)
117  const bool success = exe_str == "app";
118 #elif defined(__EMSCRIPTEN__)
119  const bool success = exe_str == "patched_googletest-options-test.js";
120 #else
121  const bool success =
122  exe_str == "googletest-options-test" || exe_str == "gtest_all_test" ||
123  exe_str == "lt-gtest_all_test" || exe_str == "gtest_dll_test";
124 #endif // platform ifdefs
125 
126  if (!success) FAIL() << "GetCurrentExecutableName() returns " << exe_str;
127 }
128 
129 #ifndef GTEST_OS_FUCHSIA
130 
131 class XmlOutputChangeDirTest : public Test {
132  protected:
133  void SetUp() override {
134  original_working_dir_ = FilePath::GetCurrentDir();
135  posix::ChDir("..");
136  // This will make the test fail if run from the root directory.
138  FilePath::GetCurrentDir().string());
139  }
140 
141  void TearDown() override {
142  posix::ChDir(original_working_dir_.string().c_str());
143  }
144 
146 };
147 
148 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
149  GTEST_FLAG_SET(output, "");
150  EXPECT_EQ(
151  FilePath::ConcatPaths(original_working_dir_, FilePath("test_detail.xml"))
152  .string(),
154 }
155 
156 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
157  GTEST_FLAG_SET(output, "xml");
158  EXPECT_EQ(
159  FilePath::ConcatPaths(original_working_dir_, FilePath("test_detail.xml"))
160  .string(),
162 }
163 
164 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
165  GTEST_FLAG_SET(output, "xml:filename.abc");
166  EXPECT_EQ(
167  FilePath::ConcatPaths(original_working_dir_, FilePath("filename.abc"))
168  .string(),
170 }
171 
172 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
173  GTEST_FLAG_SET(output, "xml:path" GTEST_PATH_SEP_);
174  const std::string expected_output_file =
175  FilePath::ConcatPaths(
177  FilePath(std::string("path") + GTEST_PATH_SEP_ +
178  GetCurrentExecutableName().string() + ".xml"))
179  .string();
180  const std::string& output_file =
182 #ifdef GTEST_OS_WINDOWS
183  EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
184 #else
185  EXPECT_EQ(expected_output_file, output_file.c_str());
186 #endif
187 }
188 
189 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
190 #ifdef GTEST_OS_WINDOWS
191  GTEST_FLAG_SET(output, "xml:c:\\tmp\\filename.abc");
192  EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(),
194 #else
195  GTEST_FLAG_SET(output, "xml:/tmp/filename.abc");
196  EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
198 #endif
199 }
200 
201 TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
202 #ifdef GTEST_OS_WINDOWS
203  const std::string path = "c:\\tmp\\";
204 #else
205  const std::string path = "/tmp/";
206 #endif
207 
208  GTEST_FLAG_SET(output, "xml:" + path);
209  const std::string expected_output_file =
210  path + GetCurrentExecutableName().string() + ".xml";
211  const std::string& output_file =
213 
214 #ifdef GTEST_OS_WINDOWS
215  EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
216 #else
217  EXPECT_EQ(expected_output_file, output_file.c_str());
218 #endif
219 }
220 
221 #endif // !GTEST_OS_FUCHSIA
222 
223 } // namespace
224 } // namespace internal
225 } // namespace testing
#define GTEST_PATH_SEP_
Definition: gtest-port.h:1958
#define EXPECT_STRCASEEQ(s1, s2)
Definition: gtest.h:1957
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1886
int ChDir(const char *dir)
Definition: gtest-port.h:2135
static std::string GetAbsolutePathToOutputFile()
TEST_F(ListenerTest, DoesFoo)
static std::string GetOutputFormat()
Definition: gtest.cc:680
#define GTEST_FLAG_SET(name, value)
Definition: gtest-port.h:2343
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1953
FilePath original_working_dir_
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1884
#define FAIL()
Definition: gtest.h:1769
TEST(IsXDigitTest, WorksForNarrowAscii)