[Issue #7] Merge branch 'features/more-args' into bleeding
This commit is contained in:
commit
6a7161598c
5 changed files with 117 additions and 4 deletions
|
@ -32,6 +32,9 @@ struct criterion_options {
|
|||
bool enable_tap_format;
|
||||
bool no_early_exit;
|
||||
bool always_succeed;
|
||||
bool use_ascii;
|
||||
bool fail_fast;
|
||||
const char *pattern;
|
||||
};
|
||||
|
||||
extern struct criterion_options criterion_options;
|
||||
|
|
95
src/main.c
95
src/main.c
|
@ -1,8 +1,34 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright © 2015 Franklin "Snaipe" Mathieu <http://snai.pe/>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include <criterion/criterion.h>
|
||||
#include <criterion/options.h>
|
||||
#include <criterion/ordered-set.h>
|
||||
#include <stdio.h>
|
||||
#include <getopt.h>
|
||||
#include <csptr/smart_ptr.h>
|
||||
#include "runner.h"
|
||||
|
||||
# define VERSION "v1.0.0"
|
||||
# define VERSION_MSG "Tests compiled with Criterion " VERSION "\n"
|
||||
|
@ -14,6 +40,12 @@
|
|||
" -h or --help: prints this message\n" \
|
||||
" -v or --version: prints the version of criterion " \
|
||||
"these tests have been linked against\n" \
|
||||
" -l or --list: prints all the tests in a list\n" \
|
||||
" -f or --fail-fast: exit after the first failure\n" \
|
||||
" --ascii: don't use fancy unicode symbols " \
|
||||
"or colors in the output\n" \
|
||||
" --pattern [PATTERN]: run tests matching the " \
|
||||
"given pattern\n" \
|
||||
" --tap: enables TAP formatting\n" \
|
||||
" --always-succeed: always exit with 0\n" \
|
||||
" --no-early-exit: do not exit the test worker " \
|
||||
|
@ -31,12 +63,56 @@ int print_version(void) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
# define UTF8_TREE_NODE "├"
|
||||
# define UTF8_TREE_END "└"
|
||||
# define UTF8_TREE_JOIN "──"
|
||||
|
||||
# define ASCII_TREE_NODE "|"
|
||||
# define ASCII_TREE_END "`"
|
||||
# define ASCII_TREE_JOIN "--"
|
||||
|
||||
bool is_disabled(struct criterion_suite *s, struct criterion_test *t) {
|
||||
return (s->data && s->data->disabled) || t->data->disabled;
|
||||
}
|
||||
|
||||
int list_tests(bool unicode) {
|
||||
smart struct criterion_test_set *set = criterion_init();
|
||||
|
||||
const char *node = unicode ? UTF8_TREE_NODE : ASCII_TREE_NODE;
|
||||
const char *join = unicode ? UTF8_TREE_JOIN : ASCII_TREE_JOIN;
|
||||
const char *end = unicode ? UTF8_TREE_END : ASCII_TREE_END;
|
||||
|
||||
FOREACH_SET(struct criterion_suite_set *s, set->suites) {
|
||||
size_t tests = s->tests ? s->tests->size : 0;
|
||||
if (!tests)
|
||||
continue;
|
||||
|
||||
printf("%s: " SIZE_T_FORMAT " test%s\n",
|
||||
s->suite.name,
|
||||
tests,
|
||||
tests == 1 ? "" : "s");
|
||||
|
||||
FOREACH_SET(struct criterion_test *t, s->tests) {
|
||||
printf("%s%s %s%s\n",
|
||||
--tests == 0 ? end : node,
|
||||
join,
|
||||
t->name,
|
||||
is_disabled(&s->suite, t) ? "(disabled)" : "");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
static struct option opts[] = {
|
||||
{"verbose", optional_argument, 0, 'b'},
|
||||
{"version", no_argument, 0, 'v'},
|
||||
{"tap", no_argument, 0, 't'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"list", no_argument, 0, 'l'},
|
||||
{"ascii", no_argument, 0, 'k'},
|
||||
{"fail-fast", no_argument, 0, 'f'},
|
||||
{"pattern", required_argument, 0, 'p'},
|
||||
{"always-succeed", no_argument, 0, 'y'},
|
||||
{"no-early-exit", no_argument, 0, 'z'},
|
||||
{0, 0, 0, 0 }
|
||||
|
@ -49,17 +125,30 @@ int main(int argc, char *argv[]) {
|
|||
.logging_threshold = atoi(getenv("CRITERION_VERBOSITY_LEVEL") ?: "2"),
|
||||
};
|
||||
|
||||
for (int c; (c = getopt_long(argc, argv, "hv", opts, NULL)) != -1;) {
|
||||
bool do_list_tests = false;
|
||||
bool do_print_version = false;
|
||||
bool do_print_usage = false;
|
||||
for (int c; (c = getopt_long(argc, argv, "hvlf", opts, NULL)) != -1;) {
|
||||
switch (c) {
|
||||
case 'b': criterion_options.logging_threshold = atoi(optarg ?: "1"); break;
|
||||
case 't': criterion_options.enable_tap_format = true; break;
|
||||
case 'y': criterion_options.always_succeed = true; break;
|
||||
case 'z': criterion_options.no_early_exit = true; break;
|
||||
case 'v': return print_version();
|
||||
case 'k': criterion_options.use_ascii = true; break;
|
||||
case 'f': criterion_options.fail_fast = true; break;
|
||||
case 'p': criterion_options.pattern = optarg; break;
|
||||
case 'l': do_list_tests = true; break;
|
||||
case 'v': do_print_version = true; break;
|
||||
case 'h':
|
||||
default : return print_usage(argv[0]);
|
||||
default : do_print_usage = true; break;
|
||||
}
|
||||
}
|
||||
if (do_print_usage)
|
||||
return print_usage(argv[0]);
|
||||
if (do_print_version)
|
||||
return print_version();
|
||||
if (do_list_tests)
|
||||
return list_tests(!criterion_options.use_ascii);
|
||||
|
||||
return !criterion_run_all_tests();
|
||||
}
|
||||
|
|
17
src/report.c
17
src/report.c
|
@ -21,7 +21,9 @@
|
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <fnmatch.h>
|
||||
#include "criterion/criterion.h"
|
||||
#include "criterion/stats.h"
|
||||
#include "criterion/logging.h"
|
||||
|
@ -95,6 +97,21 @@ ReportHook(PRE_TEST)() {}
|
|||
ReportHook(POST_FINI)() {}
|
||||
|
||||
ReportHook(PRE_ALL)(struct criterion_test_set *set) {
|
||||
if (criterion_options.pattern) {
|
||||
FOREACH_SET(struct criterion_suite_set *s, set->suites) {
|
||||
if ((s->suite.data && s->suite.data->disabled) || !s->tests)
|
||||
continue;
|
||||
|
||||
FOREACH_SET(struct criterion_test *test, s->tests) {
|
||||
char *name = NULL;
|
||||
asprintf(&name, "%s/%s", s->suite.name, test->name);
|
||||
if (fnmatch(criterion_options.pattern, name, 0))
|
||||
test->data->disabled = true;
|
||||
|
||||
free(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (criterion_options.enable_tap_format) {
|
||||
size_t enabled_count = 0;
|
||||
FOREACH_SET(struct criterion_suite_set *s, set->suites) {
|
||||
|
|
|
@ -59,7 +59,7 @@ static void dtor_test_set(void *ptr, UNUSED void *meta) {
|
|||
sfree(t->suites);
|
||||
}
|
||||
|
||||
static struct criterion_test_set *criterion_init(void) {
|
||||
struct criterion_test_set *criterion_init(void) {
|
||||
struct criterion_ordered_set *suites = new_ordered_set(cmp_suite, dtor_suite_set);
|
||||
|
||||
FOREACH_SUITE_SEC(s) {
|
||||
|
@ -98,6 +98,8 @@ static void map_tests(struct criterion_test_set *set, struct criterion_global_st
|
|||
|
||||
FOREACH_SET(struct criterion_test *t, s->tests) {
|
||||
fun(stats, t, &s->suite);
|
||||
if (criterion_options.fail_fast && stats->tests_failed > 0)
|
||||
return;
|
||||
if (!is_runner())
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,8 @@
|
|||
DECL_SECTION_LIMITS(struct criterion_test, criterion_tests);
|
||||
DECL_SECTION_LIMITS(struct criterion_suite, crit_suites);
|
||||
|
||||
struct criterion_test_set *criterion_init(void);
|
||||
|
||||
# define FOREACH_TEST_SEC(Test) \
|
||||
for (struct criterion_test *Test = SECTION_START(criterion_tests); \
|
||||
Test < SECTION_END(criterion_tests); \
|
||||
|
|
Loading…
Add table
Reference in a new issue