UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_all.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 #include "stdlib.h"
26 #include "stdio.h"
27 #include <gtest/gtest.h>
28 #include <SDL_main.h>
29 
30 #include "test_shared.h"
31 
32 void Sys_Init(void);
33 void Sys_Init (void)
34 {
35 }
36 
37 typedef struct config_s {
38  bool log;
39  bool verbose;
40 } config_t;
41 
42 static const char* resultPrefix = "ufoai";
44 static FILE* logFile;
45 
46 static void Test_Parameters (const int argc, char** argv)
47 {
48  int i;
49 
50  for (i = 1; i < argc; i++) {
51  if (char const* const arg = Q_strstart(argv[i], "-D")) {
52  if (char const* const value = strchr(arg, '=')) {
53  char name[32];
54  size_t const size = value - arg + 1;
55  if (size >= sizeof(name)) {
56  fprintf(stderr, "Error: Argument \"%s\" use a property name too much big.\n", argv[i]);
57  exit(2);
58  }
59  Q_strncpyz(name, arg, size);
60  TEST_RegisterProperty(name, value + 1);
61  } else {
62  fprintf(stderr, "Warning: \"%s\" do not value. Command line argument ignored.\n", argv[i]);
63  }
64  } else if (char const* const prefix = Q_strstart(argv[i], "--output-prefix=")) {
65  resultPrefix = prefix;
66  } else if (Q_streq(argv[i], "--log")) {
67  config.log = true;
68  } else if (Q_streq(argv[i], "-v") || Q_streq(argv[i], "--verbose")) {
69  config.verbose = true;
70  } else if (Q_streq(argv[i], "-h") || Q_streq(argv[i], "--help")) {
71  printf("Usage:\n");
72  printf("-h --help | show this help screen\n");
73  printf("-v --verbose | be verbose\n");
74  printf("-Dprop=value | defines a test property\n");
75  printf(" --log | log ufo output to file (testall.log in working dir)\n");
76  printf(" --output-prefix=PREFIX | set a prefix for the xml result\n");
77  printf(" | default value is \"ufoai\"\n");
78  exit(0);
79  } else {
80  fprintf(stderr, "Error: Param \"%s\" unknown\n", argv[i]);
81  fprintf(stderr, "Use \"%s -h\" to show the help screen\n", argv[0]);
82  exit(2);
83  }
84  }
85 }
86 
87 static void TEST_vPrintfLog (const char* fmt, va_list ap)
88 {
89  if (!logFile)
90  return;
91  static char msg[2048];
92  Q_vsnprintf(msg, sizeof(msg), fmt, ap);
93 
94  fprintf(logFile, "%s", msg);
95  fflush(logFile);
96 }
97 
98 class LocalEnv: public ::testing::Environment {
99 public:
100  virtual ~LocalEnv() {
101  }
102  virtual void SetUp() override {
103  }
104  virtual void TearDown() override {
105  }
106 };
107 
113 extern "C" int main (int argc, char** argv)
114 {
115  Sys_InitSignals();
116 
117  ::testing::AddGlobalTestEnvironment(new LocalEnv);
118  //::testing::GTEST_FLAG(throw_on_failure) = true;
119  ::testing::InitGoogleTest(&argc, argv);
120 
121  Test_Parameters(argc, argv);
122 
123  if (config.log) {
124  const char* path = "testall.log";
125  logFile = Sys_Fopen(path, "wb");
127  } else if (config.verbose){
129  } else {
131  }
132 
133  try {
134  try {
135  return RUN_ALL_TESTS();
136  } catch (const std::exception& e) {
137  std::cerr << e.what() << std::endl;
138  return EXIT_FAILURE;
139  }
140  } catch (comDrop_t const&) {
141  Sys_Error("There was a Com_Error or Com_Drop call during the execution of this test");
142  }
143 
144  if (logFile)
145  fclose(logFile);
146 
147  return EXIT_SUCCESS;
148 }
int Q_vsnprintf(char *str, size_t size, const char *format, va_list ap)
Safe (null terminating) vsnprintf implementation.
Definition: shared.cpp:535
void Sys_Error(const char *error,...)
Definition: g_main.cpp:421
struct config_s config_t
static FILE * logFile
Definition: test_all.cpp:44
#define FILE
Definition: test_webapi.cpp:30
void Sys_InitSignals(void)
bool log
Definition: test_all.cpp:38
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
void TEST_vPrintf(const char *fmt, va_list argptr)
Definition: test_shared.cpp:62
bool verbose
Definition: test_all.cpp:39
static void Test_Parameters(const int argc, char **argv)
Definition: test_all.cpp:46
GLsizei size
Definition: r_gl.h:152
void TEST_RegisterProperty(const char *name, const char *value)
char const * Q_strstart(char const *str, char const *start)
Matches the start of a string.
Definition: shared.cpp:587
static const char * resultPrefix
Definition: test_all.cpp:42
static config_t config
Definition: test_all.cpp:43
static void TEST_vPrintfLog(const char *fmt, va_list ap)
Definition: test_all.cpp:87
virtual void SetUp() override
Definition: test_all.cpp:102
QGL_EXTERN GLint i
Definition: r_gl.h:113
void TEST_vPrintfSilent(const char *fmt, va_list argptr)
Definition: test_shared.cpp:58
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
void Sys_Init(void)
Definition: test_all.cpp:33
void Qcommon_SetPrintFunction(vPrintfPtr_t func)
Definition: common.cpp:1061
virtual void TearDown() override
Definition: test_all.cpp:104
FILE * Sys_Fopen(const char *filename, const char *mode)
Definition: unix_files.cpp:240
#define Q_streq(a, b)
Definition: shared.h:136
virtual ~LocalEnv()
Definition: test_all.cpp:100
int main(int argc, char **argv)
Definition: test_all.cpp:113