Refactored CLI parameter handling in its own function for reusability

This commit is contained in:
Snaipe 2015-09-07 15:27:15 +02:00
parent 3028af859d
commit aca092b9d6
5 changed files with 40 additions and 7 deletions

View file

@ -99,6 +99,7 @@ set(SOURCE_FILES
src/posix-compat.c
src/theories.c
src/main.c
src/entry.c
)
if (PCRE_FOUND)

View file

@ -34,12 +34,35 @@ fail_fast bool True iff the test runner
pattern const char * The pattern of the tests that should be executed
=================== ================================== ==============================================================
if you want criterion to provide its own default CLI parameters and environment
variables handling, you can also call ``criterion_initialize(int argc, char *argv[], bool handle_unknown_arg)``
with the proper ``argc/argv``. ``handle_unknown_arg``, if set to true, is here
to tell criterion to print its usage when an unknown CLI parameter is encountered.
If you want to add your own parameters, you should set it to false.
The function returns 0 if the main should exit immediately, and 1 if it should
continue.
Starting the test runner
~~~~~~~~~~~~~~~~~~~~~~~~
The test runner can be called with ``criterion_run_all_tests``. The function
returns 0 if one test or more failed, 1 otherwise.
Example main
~~~~~~~~~~~~
.. code-block:: c
#include <criterion/criterion.h>
int main(int argc, char *argv[]) {
if (!criterion_initialize(argc, argv, true))
return 0;
return !criterion_run_all_tests();
}
Implementing your own output provider
-------------------------------------

View file

@ -79,6 +79,7 @@
CR_BEGIN_C_API
CR_API int criterion_run_all_tests(void);
CR_API int criterion_initialize(int argc, char *argv[], bool handle_unknown_arg);
CR_END_C_API

8
src/entry.c Normal file
View file

@ -0,0 +1,8 @@
#include "criterion/criterion.h"
CR_API int main(int argc, char *argv[]) {
if (!criterion_initialize(argc, argv, true))
return 0;
return !criterion_run_all_tests();
}

View file

@ -22,13 +22,13 @@
* THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <criterion/criterion.h>
#include <criterion/options.h>
#include <criterion/ordered-set.h>
#include <stdio.h>
#include <locale.h>
#include <getopt.h>
#include <csptr/smalloc.h>
#include "criterion/criterion.h"
#include "criterion/options.h"
#include "criterion/ordered-set.h"
#include "runner.h"
#include "config.h"
#include "common.h"
@ -120,7 +120,7 @@ int list_tests(bool unicode) {
return 0;
}
CR_API int main(int argc, char *argv[]) {
CR_API int criterion_initialize(int argc, char *argv[], bool handle_unknown_arg) {
static struct option opts[] = {
{"verbose", optional_argument, 0, 'b'},
{"version", no_argument, 0, 'v'},
@ -176,8 +176,8 @@ CR_API int main(int argc, char *argv[]) {
case 't': use_tap = true; break;
case 'l': do_list_tests = true; break;
case 'v': do_print_version = true; break;
case 'h':
default : do_print_usage = true; break;
case 'h': do_print_usage = true; break;
default : do_print_usage = handle_unknown_arg; break;
}
}
if (use_tap)
@ -189,5 +189,5 @@ CR_API int main(int argc, char *argv[]) {
if (do_list_tests)
return list_tests(!criterion_options.use_ascii);
return !criterion_run_all_tests();
return 1;
}