1
1
// This file contains implementations that extend the functionality of the
2
2
// google test framework
3
3
// Version: 0.1.1-beta
4
-
4
+
5
5
#include < gtest/gtest.h>
6
6
#include < random>
7
7
#include < string>
10
10
#include < chrono>
11
11
#include < map>
12
12
#include " termcolor/termcolor.hpp"
13
-
13
+
14
14
class SkipListener : public ::testing::EmptyTestEventListener
15
15
{
16
16
private:
@@ -30,7 +30,7 @@ class SkipListener : public ::testing::EmptyTestEventListener
30
30
}
31
31
}
32
32
};
33
-
33
+
34
34
// Run and retrieves the output of an executable program from
35
35
// the command line.
36
36
//
@@ -49,7 +49,7 @@ std::string exec_program(std::string prog_name, std::string input)
49
49
pclose (fp);
50
50
return output;
51
51
}
52
-
52
+
53
53
// Converts a double value to a formatted string
54
54
//
55
55
// @param val value to be formatted
@@ -62,7 +62,7 @@ std::string to_string_double(double val, const int prec = 2)
62
62
out << std::fixed << std::setprecision (prec) << val;
63
63
return out.str ();
64
64
}
65
-
65
+
66
66
// Generate a string with random values from an alphanumeric character set
67
67
//
68
68
// @param max_length length of string to generate
@@ -80,7 +80,7 @@ std::string generate_string(int max_length){
80
80
}
81
81
return ret;
82
82
}
83
-
83
+
84
84
std::string expose_special_characters (const std::string &source) {
85
85
if (source.length () == 0 ) {
86
86
return " <empty>" ;
@@ -99,7 +99,7 @@ std::string expose_special_characters(const std::string &source) {
99
99
}
100
100
return exposed_string.str ();
101
101
}
102
-
102
+
103
103
// This macro is used to simulate the standard input (cin) for a code block
104
104
//
105
105
// @param input simulated input
@@ -113,7 +113,7 @@ std::string expose_special_characters(const std::string &source) {
113
113
std::cin.rdbuf (old_input_buf); \
114
114
std::cout.rdbuf (old_output_buf); \
115
115
}
116
-
116
+
117
117
::testing::AssertionResult AssertExecStdOut (const char * prog_name_expr,
118
118
const char * prog_input_expr,
119
119
const char * prog_output_expr,
@@ -156,36 +156,36 @@ ::testing::AssertionResult AssertExecStdOut(const char* prog_name_expr,
156
156
}
157
157
exec_diff = expose_special_characters (exec_diff);
158
158
prog_diff = expose_special_characters (prog_diff);
159
-
159
+
160
160
std::ostringstream error_str_stream;
161
161
std::ostringstream exec_str_stream;
162
162
exec_str_stream << termcolor::colorize << termcolor::green
163
163
<< exec_output.substr (0 , pos)
164
164
<< termcolor::red << exec_output.substr (pos)
165
165
<< termcolor::reset;
166
-
166
+
167
167
std::ostringstream prog_str_stream;
168
168
prog_str_stream << termcolor::colorize << termcolor::green
169
169
<< prog_output.substr (0 , pos)
170
170
<< termcolor::red << prog_output.substr (pos)
171
171
<< termcolor::reset;
172
-
172
+
173
173
error_str_stream << " Your program's output did not match the expected "
174
174
<< " output starting on line " << line_pos + 1
175
175
<< " character " << char_pos
176
176
<< " .\n Expected " << prog_diff
177
177
<< " instead of " << exec_diff
178
178
// << "\n\nExpected output: \n" << expose_special_characters(prog_str_stream.str())
179
179
<< " \n\n Expected output: \n " << prog_str_stream.str ()
180
- << " \n\n Your program's output: \n "
180
+ << " \n\n Your program's output: \n "
181
181
// << expose_special_characters(exec_str_stream.str()) << "\n\nTest Input: \n"
182
- << exec_str_stream.str () << " \n\n Test Input : \n "
182
+ << exec_str_stream.str () << " \n\n Input : \n "
183
183
<< prog_input ;
184
-
184
+
185
185
return ::testing::AssertionFailure () << error_str_stream.str ();
186
186
}
187
187
}
188
-
188
+
189
189
// This macro checks if the output of an executable program matches an expected
190
190
// output.
191
191
//
@@ -194,7 +194,7 @@ ::testing::AssertionResult AssertExecStdOut(const char* prog_name_expr,
194
194
// @param output expected output of the program
195
195
#define ASSERT_EXECEQ (prog_name, input, output ) \
196
196
EXPECT_PRED_FORMAT3 (AssertExecStdOut, prog_name, input, output)
197
-
197
+
198
198
template <typename T>
199
199
::testing::AssertionResult AssertExecMatcher(const char * prog_name_expr,
200
200
const char * prog_input_expr,
@@ -207,25 +207,25 @@ ::testing::AssertionResult AssertExecMatcher(const char* prog_name_expr,
207
207
<< " ': Make sure your executable file"
208
208
<< " is called '" << prog_name << " '" ;
209
209
}
210
-
210
+
211
211
std::string exec_output = exec_program (prog_name, prog_input);
212
212
// based on https://github.com/google/googletest/blob/fb49e6c164490a227bbb7cf5223b846c836a0305/googlemock/include/gmock/gmock-matchers.h#L1304
213
213
// create a predicate formatter that can be used to run the matcher
214
214
auto pred_formatter = ::testing::internal::MakePredicateFormatterFromMatcher (matcher);
215
215
// where: matcher_expr is the stringized matcher (e.g., StartsWith("Hello"))
216
216
// exec_output is the expected output, in this case the program output
217
- return pred_formatter (matcher_expr, exec_output) << " \n Input: "
217
+ return pred_formatter (matcher_expr, exec_output) << " \n Input: "
218
218
<< prog_input;
219
219
}
220
-
220
+
221
221
// This macro checks if the output of an executable follows the pattern defined
222
222
// in the gMock matcher
223
223
// @param prog_name name of the executable file
224
224
// @param input keyboard input sent to the program
225
225
// @param matcher gMock matcher used to test the executable's output
226
226
#define ASSERT_EXECTHAT (prog_name, input, matcher ) \
227
227
EXPECT_PRED_FORMAT3 (AssertExecMatcher, prog_name, input, matcher)
228
-
228
+
229
229
::testing::AssertionResult AssertExecExit(const char * prog_name_expr,
230
230
const char * prog_input_expr,
231
231
const char * prog_max_dur,
@@ -237,7 +237,7 @@ ::testing::AssertionResult AssertExecExit(const char* prog_name_expr,
237
237
<< " ': Make sure your executable file"
238
238
<< " is called '" << prog_name << " '" ;
239
239
}
240
-
240
+
241
241
std::promise<bool > completed;
242
242
auto stmt_future = completed.get_future ();
243
243
std::thread ([&](std::promise<bool >& completed) {
@@ -246,15 +246,15 @@ ::testing::AssertionResult AssertExecExit(const char* prog_name_expr,
246
246
}, std::ref (completed)).detach ();
247
247
if (stmt_future.wait_for (std::chrono::seconds (max_dur)) == std::future_status::timeout) {
248
248
return ::testing::AssertionFailure ()
249
- << " Test Input: " << prog_input
249
+ << " Input: " << prog_input
250
250
<< " \n the program took more than " << max_dur
251
251
<< " seconds to exit. Check for infinite loops or "
252
252
<< " unnecessary inputs." ;
253
253
} else {
254
254
return ::testing::AssertionSuccess ();
255
255
}
256
256
}
257
-
257
+
258
258
// This macro checks whether the executable program exits given the provided
259
259
// input.
260
260
//
@@ -265,8 +265,8 @@ ::testing::AssertionResult AssertExecExit(const char* prog_name_expr,
265
265
// input
266
266
#define ASSERT_EXECEXIT (prog_name, input, duration ) \
267
267
EXPECT_PRED_FORMAT3 (AssertExecExit, prog_name, input, duration)
268
-
269
-
268
+
269
+
270
270
// Version of ASSERT_EXECIO_EQ that uses google mock's matchers
271
271
//
272
272
// @param prog_name name of the executable file
@@ -278,7 +278,7 @@ ::testing::AssertionResult AssertExecExit(const char* prog_name_expr,
278
278
} \
279
279
ASSERT_THAT (main_output (prog_name, input), matcher) << " Input: " << input; \
280
280
}
281
-
281
+
282
282
// This macro asserts that the result of performing the statement
283
283
// is equal to the expected value in the standard output (cout)
284
284
//
@@ -295,7 +295,7 @@ ::testing::AssertionResult AssertExecExit(const char* prog_name_expr,
295
295
std::string your_output = output_ss.str (); \
296
296
ASSERT_EQ (your_output, expected); \
297
297
}
298
-
298
+
299
299
// Version of ASSERT_SIO_EQ that uses google mock's matchers
300
300
//
301
301
// @param expected expected string value
@@ -311,7 +311,7 @@ ::testing::AssertionResult AssertExecExit(const char* prog_name_expr,
311
311
std::string your_output = output_ss.str (); \
312
312
ASSERT_THAT (your_output, expected); \
313
313
}
314
-
314
+
315
315
// This macro checks whether a function executes within a given time
316
316
//
317
317
// A thread is created to run the statement and update the status of a promise
0 commit comments