Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
redirect.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright © 2015-2016 Franklin "Snaipe" Mathieu <http://snai.pe/>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24/**
25 * @file
26 * @brief Redirect functions and file asserts
27 *****************************************************************************/
28#ifndef CRITERION_REDIRECT_H_
29#define CRITERION_REDIRECT_H_
30
31#include "internal/common.h"
32
33#ifdef __cplusplus
34# include <cstdio>
35#else
36# include <stdio.h>
37#endif
38
40
41/**
42 * Redirect stdout for testing.
43 */
45
46/**
47 * Redirect stderr for testing.
48 */
50
51/**
52 * Redirect stdin for testing.
53 * This is implicitely called before each test.
54 */
56
57/**
58 * Get a file handle representing the read-end of the redirected stdout.
59 *
60 * @returns the file handle.
61 */
63
64/**
65 * Get a file handle representing the read-end of the redirected stderr.
66 *
67 * @returns the file handle.
68 */
70
71/**
72 * Get a file handle representing the write-end of the redirected stdin.
73 *
74 * @returns the file handle.
75 */
77
78/**
79 * Compare the contents of a file with a string.
80 *
81 * @param[in] f The file to compare the contents to.
82 * @param[in] str The string to compare the contents to.
83 * @returns 1 if the contents of the file is equal to the string, 0 otherwise.
84 */
85CR_API int cr_file_match_str(CR_STDN FILE *f, const char *str);
86
87/**
88 * Compare the contents of a file with the contents of another file.
89 *
90 * @param[in] f The first file to compare the contents to.
91 * @param[in] ref The second file to compare the contents to.
92 * @returns 1 if the contents of the files are equal, 0 otherwise.
93 */
94CR_API int cr_file_match_file(CR_STDN FILE *f, CR_STDN FILE *ref);
95
96/**
97 * Create a file mock.
98 *
99 * @param[in] max_size The maximum size in bytes of the file mock.
100 * @returns the file handle representing the mock.
101 */
102CR_API CR_STDN FILE *cr_mock_file_size(size_t max_size);
103
105
106/**
107 * @defgroup FileAsserts File content assertions
108 * @{
109 */
110
111/**
112 * Passes if the contents of \c File are equal to the string \c ExpectedContents
113 *
114 * Passes if the contents of \c File are equal to the string
115 * \c ExpectedContents.
116 * Otherwise the test is marked as failure and the execution of the function
117 * is aborted.
118 *
119 * The optional string is printed on failure.
120 *
121 * @param[in] File Pointer to a FILE object that specifies an input stream
122 * @param[in] ExpectedContents C string with the ExpectedContents
123 * @param[in] FormatString (optional) printf-like format string
124 * @param[in] ... (optional) format string parameters
125 *
126 *****************************************************************************/
127#define cr_assert_file_contents_eq_str(File, ExpectedContents, FormatString, ...) internal
128
129/**
130 * Passes if the contents of \c File are equal to the string \c ExpectedContents
131 *
132 * Passes if the contents of \c File are equal to the string
133 * \c ExpectedContents.
134 * Otherwise the test is marked as failure but the execution will continue.
135 *
136 * The optional string is printed on failure.
137 *
138 * @param[in] File Pointer to a FILE object that specifies an input stream
139 * @param[in] ExpectedContents C string with the ExpectedContents
140 * @param[in] FormatString (optional) printf-like format string
141 * @param[in] ... (optional) format string parameters
142 *
143 *****************************************************************************/
144#define cr_expect_file_contents_eq_str(File, ExpectedContents, FormatString, ...) internal
145
146/**
147 * Passes if the contents of \c File are not equal to the string
148 * \c UnexpectedContents
149 *
150 * Passes if the contents of \c File are not equal to the string
151 * \c UnexpectedContents.
152 * Otherwise the test is marked as failure and the execution of the function
153 * is aborted.
154 *
155 * The optional string is printed on failure.
156 *
157 * @param[in] File Pointer to a FILE object that specifies an input stream
158 * @param[in] UnexpectedContents C string with the UnexpectedContents
159 * @param[in] FormatString (optional) printf-like format string
160 * @param[in] ... (optional) format string parameters
161 *
162 *****************************************************************************/
163#define cr_assert_file_contents_neq_str(File, UnexpectedContents, FormatString, ...) internal
164
165/**
166 * Passes if the contents of \c File are not equal to the string
167 * \c UnexpectedContents
168 *
169 * Passes if the contents of \c File are not equal to the string
170 * \c UnexpectedContents.
171 * Otherwise the test is marked as failure but the execution will continue.
172 *
173 * The optional string is printed on failure.
174 *
175 * @param[in] File Pointer to a FILE object that specifies an input stream
176 * @param[in] UnexpectedContents C string with the UnexpectedContents
177 * @param[in] FormatString (optional) printf-like format string
178 * @param[in] ... (optional) format string parameters
179 *
180 *****************************************************************************/
181#define cr_expect_file_contents_neq_str(File, UnexpectedContents, FormatString, ...) internal
182
183/**
184 * Passes if the contents of \c File are equal to the contents of \c RefFile
185 *
186 * Passes if the contents of \c File are equal to the contents of \c RefFile.
187 * Otherwise the test is marked as failure and the execution of the function
188 * is aborted.
189 *
190 * The optional string is printed on failure.
191 *
192 * @param[in] File Pointer to a FILE object that specifies an input stream
193 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
194 * @param[in] FormatString (optional) printf-like format string
195 * @param[in] ... (optional) format string parameters
196 *
197 *****************************************************************************/
198#define cr_assert_file_contents_eq(File, RefFile, FormatString, ...) internal
199
200/**
201 * Passes if the contents of \c File are equal to the contents of \c RefFile
202 *
203 * Passes if the contents of \c File are equal to the contents of \c RefFile.
204 * Otherwise the test is marked as failure but the execution will continue.
205 *
206 * The optional string is printed on failure.
207 *
208 * @param[in] File Pointer to a FILE object that specifies an input stream
209 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
210 * @param[in] FormatString (optional) printf-like format string
211 * @param[in] ... (optional) format string parameters
212 *
213 *****************************************************************************/
214#define cr_expect_file_contents_eq(File, RefFile, FormatString, ...) internal
215
216/**
217 * Passes if the contents of \c File are not equal to the contents of
218 * \c RefFile
219 *
220 * Passes if the contents of \c File are not equal to the contents of
221 * \c RefFile.
222 * Otherwise the test is marked as failure and the execution of the function
223 * is aborted.
224 *
225 * The optional string is printed on failure.
226 *
227 * @param[in] File Pointer to a FILE object that specifies an input stream
228 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
229 * @param[in] FormatString (optional) printf-like format string
230 * @param[in] ... (optional) format string parameters
231 *
232 *****************************************************************************/
233#define cr_assert_file_contents_neq(File, RefFile, FormatString, ...) internal
234
235/**
236 * Passes if the contents of \c File are not equal to the contents of
237 * \c RefFile
238 *
239 * Passes if the contents of \c File are not equal to the contents of
240 * \c RefFile.
241 * Otherwise the test is marked as failure but the execution will continue.
242 *
243 * The optional string is printed on failure.
244 *
245 * @param[in] File Pointer to a FILE object that specifies an input stream
246 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
247 * @param[in] FormatString (optional) printf-like format string
248 * @param[in] ... (optional) format string parameters
249 *
250 *****************************************************************************/
251#define cr_expect_file_contents_neq(File, RefFile, FormatString, ...) internal
252
253/**@}*/
254
255/**
256 * @defgroup StreamAsserts Standard stream assertions
257 * @{
258 */
259
260/**
261 * Passes if the contents of \c stdout are equal to the contents of the string
262 * \c ExpectedContents
263 *
264 * Passes if the contents of \c stdout are equal to the contents of the string
265 * \c ExpectedContents.
266 * Otherwise the test is marked as failure and the execution of the function
267 * is aborted.
268 *
269 * The optional string is printed on failure.
270 *
271 * @param[in] ExpectedContents C string with the ExpectedContents
272 * @param[in] FormatString (optional) printf-like format string
273 * @param[in] ... (optional) format string parameters
274 *
275 *****************************************************************************/
276#define cr_assert_stdout_eq_str(ExpectedContents, FormatString, ...) internal
277
278/**
279 * Passes if the contents of \c stdout are equal to the contents of the string
280 * \c ExpectedContents
281 *
282 * Passes if the contents of \c stdout are equal to the contents of the string
283 * \c ExpectedContents.
284 * Otherwise the test is marked as failure but the execution will continue.
285 *
286 * The optional string is printed on failure.
287 *
288 * @param[in] ExpectedContents C string with the ExpectedContents
289 * @param[in] FormatString (optional) printf-like format string
290 * @param[in] ... (optional) format string parameters
291 *
292 *****************************************************************************/
293#define cr_expect_stdout_eq_str(ExpectedContents, FormatString, ...) internal
294
295/**
296 * Passes if the contents of \c stdout are not equal to the contents of the
297 * string \c UnexpectedContents
298 *
299 * Passes if the contents of \c stdout are not equal to the contents of the
300 * string \c UnexpectedContents.
301 * Otherwise the test is marked as failure and the execution of the function
302 * is aborted.
303 *
304 * The optional string is printed on failure.
305 *
306 * @param[in] UnexpectedContents C string with the UnexpectedContents
307 * @param[in] FormatString (optional) printf-like format string
308 * @param[in] ... (optional) format string parameters
309 *
310 *****************************************************************************/
311#define cr_assert_stdout_neq_str(UnexpectedContents, FormatString, ...) internal
312
313/**
314 * Passes if the contents of \c stdout are not equal to the contents of the
315 * string \c UnexpectedContents
316 *
317 * Passes if the contents of \c stdout are not equal to the contents of the
318 * string \c UnexpectedContents.
319 * Otherwise the test is marked as failure but the execution will continue.
320 *
321 * The optional string is printed on failure.
322 *
323 * @param[in] UnexpectedContents C string with the UnexpectedContents
324 * @param[in] FormatString (optional) printf-like format string
325 * @param[in] ... (optional) format string parameters
326 *
327 *****************************************************************************/
328#define cr_expect_stdout_neq_str(UnexpectedContents, FormatString, ...) internal
329
330/**
331 * Passes if the contents of \c stderr are equal to the contents of the string
332 * \c ExpectedContents
333 *
334 * Passes if the contents of \c stderr are equal to the contents of the string
335 * \c ExpectedContents.
336 * Otherwise the test is marked as failure and the execution of the function
337 * is aborted.
338 *
339 * The optional string is printed on failure.
340 *
341 * @param[in] ExpectedContents C string with the ExpectedContents
342 * @param[in] FormatString (optional) printf-like format string
343 * @param[in] ... (optional) format string parameters
344 *
345 *****************************************************************************/
346#define cr_assert_stderr_eq_str(ExpectedContents, FormatString, ...) internal
347
348/**
349 * Passes if the contents of \c stderr are equal to the contents of the string
350 * \c ExpectedContents
351 *
352 * Passes if the contents of \c stderr are equal to the contents of the string
353 * \c ExpectedContents.
354 * Otherwise the test is marked as failure but the execution will continue.
355 *
356 * The optional string is printed on failure.
357 *
358 * @param[in] ExpectedContents C string with the ExpectedContents
359 * @param[in] FormatString (optional) printf-like format string
360 * @param[in] ... (optional) format string parameters
361 *
362 *****************************************************************************/
363#define cr_expect_stderr_eq_str(ExpectedContents, FormatString, ...) internal
364
365/**
366 * Passes if the contents of \c stderr are not equal to the contents of the
367 * string \c UnexpectedContents
368 *
369 * Passes if the contents of \c stderr are not equal to the contents of the
370 * string \c UnexpectedContents.
371 * Otherwise the test is marked as failure and the execution of the function
372 * is aborted.
373 *
374 * The optional string is printed on failure.
375 *
376 * @param[in] UnexpectedContents C string with the UnexpectedContents
377 * @param[in] FormatString (optional) printf-like format string
378 * @param[in] ... (optional) format string parameters
379 *
380 *****************************************************************************/
381#define cr_assert_stderr_neq_str(UnexpectedContents, FormatString, ...) internal
382
383/**
384 * Passes if the contents of \c stderr are not equal to the contents of the
385 * string \c UnexpectedContents
386 *
387 * Passes if the contents of \c stderr are not equal to the contents of the
388 * string \c UnexpectedContents.
389 * Otherwise the test is marked as failure but the execution will continue.
390 *
391 * The optional string is printed on failure.
392 *
393 * @param[in] UnexpectedContents C string with the UnexpectedContents
394 * @param[in] FormatString (optional) printf-like format string
395 * @param[in] ... (optional) format string parameters
396 *
397 *****************************************************************************/
398#define cr_expect_stderr_neq_str(UnexpectedContents, FormatString, ...) internal
399
400/**
401 * Passes if the contents of \c stdout are equal to the contents of \c RefFile
402 *
403 * Passes if the contents of \c stdout are equal to the contents of \c RefFile.
404 * Otherwise the test is marked as failure and the execution of the function
405 * is aborted.
406 *
407 * The optional string is printed on failure.
408 *
409 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
410 * @param[in] FormatString (optional) printf-like format string
411 * @param[in] ... (optional) format string parameters
412 *
413 *****************************************************************************/
414#define cr_assert_stdout_eq(RefFile, FormatString, ...) internal
415
416/**
417 * Passes if the contents of \c stdout are equal to the contents of \c RefFile
418 *
419 * Passes if the contents of \c stdout are equal to the contents of \c RefFile.
420 * Otherwise the test is marked as failure but the execution will continue.
421 *
422 * The optional string is printed on failure.
423 *
424 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
425 * @param[in] FormatString (optional) printf-like format string
426 * @param[in] ... (optional) format string parameters
427 *
428 *****************************************************************************/
429#define cr_expect_stdout_eq(RefFile, FormatString, ...) internal
430
431/**
432 * Passes if the contents of \c stdout are not equal to the contents of \c
433 * RefFile
434 *
435 * Passes if the contents of \c stdout are not equal to the contents of \c
436 * RefFile.
437 * Otherwise the test is marked as failure and the execution of the function
438 * is aborted.
439 *
440 * The optional string is printed on failure.
441 *
442 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
443 * @param[in] FormatString (optional) printf-like format string
444 * @param[in] ... (optional) format string parameters
445 *
446 *****************************************************************************/
447#define cr_assert_stdout_neq(RefFile, FormatString, ...) internal
448
449/**
450 * Passes if the contents of \c stdout are not equal to the contents of \c
451 * RefFile
452 *
453 * Passes if the contents of \c stdout are not equal to the contents of \c
454 * RefFile.
455 * Otherwise the test is marked as failure but the execution will continue.
456 *
457 * The optional string is printed on failure.
458 *
459 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
460 * @param[in] FormatString (optional) printf-like format string
461 * @param[in] ... (optional) format string parameters
462 *
463 *****************************************************************************/
464#define cr_expect_stdout_neq(RefFile, FormatString, ...) internal
465
466/**
467 * Passes if the contents of \c stderr are equal to the contents of \c RefFile
468 *
469 * Passes if the contents of \c stderr are equal to the contents of \c RefFile.
470 * Otherwise the test is marked as failure and the execution of the function
471 * is aborted.
472 *
473 * The optional string is printed on failure.
474 *
475 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
476 * @param[in] FormatString (optional) printf-like format string
477 * @param[in] ... (optional) format string parameters
478 *
479 *****************************************************************************/
480#define cr_assert_stderr_eq(RefFile, FormatString, ...) internal
481
482/**
483 * Passes if the contents of \c stderr are equal to the contents of \c RefFile
484 *
485 * Passes if the contents of \c stderr are equal to the contents of \c RefFile.
486 * Otherwise the test is marked as failure but the execution will continue.
487 *
488 * The optional string is printed on failure.
489 *
490 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
491 * @param[in] FormatString (optional) printf-like format string
492 * @param[in] ... (optional) format string parameters
493 *
494 *****************************************************************************/
495#define cr_expect_stderr_eq(RefFile, FormatString, ...) internal
496
497/**
498 * Passes if the contents of \c stderr are not equal to the contents of \c
499 * RefFile
500 *
501 * Passes if the contents of \c stderr are not equal to the contents of \c
502 * RefFile.
503 * Otherwise the test is marked as failure and the execution of the function
504 * is aborted.
505 *
506 * The optional string is printed on failure.
507 *
508 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
509 * @param[in] FormatString (optional) printf-like format string
510 * @param[in] ... (optional) format string parameters
511 *
512 *****************************************************************************/
513#define cr_assert_stderr_neq(RefFile, FormatString, ...) internal
514
515/**
516 * Passes if the contents of \c stderr are not equal to the contents of \c
517 * RefFile
518 *
519 * Passes if the contents of \c stderr are not equal to the contents of \c
520 * RefFile.
521 * Otherwise the test is marked as failure but the execution will continue.
522 *
523 * The optional string is printed on failure.
524 *
525 * @param[in] RefFile Pointer to a FILE object that specifies an input stream
526 * @param[in] FormatString (optional) printf-like format string
527 * @param[in] ... (optional) format string parameters
528 *
529 *****************************************************************************/
530#define cr_expect_stderr_neq(RefFile, FormatString, ...) internal
531
532/**@}*/
533
534#ifdef __cplusplus
535# include "internal/stream.hxx"
536
537namespace criterion
538{
539typedef internal::basic_ofstream<char> ofstream;
540typedef internal::basic_ifstream<char> ifstream;
541typedef internal::basic_fstream<char> fstream;
542
543static inline ofstream &get_redirected_cin(void)
544{
545 return internal::get_redirected_out_stream_::call(cr_get_redirected_stdin());
546}
547
548static inline ifstream &get_redirected_cout(void)
549{
550 return internal::get_redirected_in_stream_::call(cr_get_redirected_stdout());
551}
552
553static inline ifstream &get_redirected_cerr(void)
554{
555 return internal::get_redirected_in_stream_::call(cr_get_redirected_stderr());
556}
557
558# if __GNUC__ >= 5
559static inline fstream mock_file(size_t max_size)
560{
561 return fstream(cr_mock_file_size(max_size));
562}
563
564static inline fstream mock_file(void)
565{
566 return mock_file(4096);
567}
568# endif
569}
570#endif
571
572#include "internal/redirect.h"
573
574#endif /* !CRITERION_REDIRECT_H_ */
#define CR_BEGIN_C_API
Definition common.h:54
#define CR_API
Definition common.h:128
#define CR_END_C_API
Definition common.h:55
#define CR_STDN
Definition common.h:136
CR_API void cr_redirect_stdin(void)
CR_BEGIN_C_API CR_API void cr_redirect_stdout(void)
CR_API int cr_file_match_file(CR_STDN FILE *f, CR_STDN FILE *ref)
CR_API CR_STDN FILE * cr_get_redirected_stdout(void)
CR_API CR_STDN FILE * cr_mock_file_size(size_t max_size)
CR_API CR_STDN FILE * cr_get_redirected_stdin(void)
CR_API void cr_redirect_stderr(void)
CR_API CR_STDN FILE * cr_get_redirected_stderr(void)
CR_API int cr_file_match_str(CR_STDN FILE *f, const char *str)