Permalink
Browse files

added the option to disable color

  • Loading branch information...
mortie committed Jan 24, 2018
1 parent e138fe2 commit c41d869c613a3a587279c6f833f74c609cb3bbf5
Showing with 88 additions and 40 deletions.
  1. +1 −1 Makefile
  2. +14 −8 README.md
  3. +2 −3 example.c
  4. +71 −28 snow/snow.h
View
@@ -4,7 +4,7 @@ example: example.c snow/snow.h
$(CC) $(CFLAGS) -g -Wall -o $@ $<
test: example
valgrind $(VGFLAGS) ./example
@valgrind $(VGFLAGS) ./example $(ARGS)
clean:
rm -f example
View
@@ -24,6 +24,14 @@ Some miscellaneous points:
ISO C compatible compilers. It's confirmed to work with at least GCC and
Clang.
## Arguments
Since you're not supposed to make your own main function, and instead use the
`snow_main` macro, your binary will take these arguments:
* **--color**: Force the use of color, even when stdout is not a TTY.
* **--no-color**: Force colors to be disabled, evern when stdout is a TTY.
## Example
Here's a simple example which tests a couple of filesystem functions, and has a
@@ -68,10 +76,9 @@ example in [example.c](https://github.com/mortie/snow/blob/master/example.c).
});
});
int main() {
snow_main({
test_files();
done();
}
});
## Structure Macros
@@ -102,12 +109,11 @@ reverse order of their definitions (i.e `defer(printf("World"));
defer(printf("Hello "));` will print "Hello World"). If the test case fails,
only deferred expressions defined before the point of failure will be executed.
### done()
### snow\_main(block)
Must be called at the end of the main function. It will print the total count
of total and successful tests (if there's more than one top-level description),
and return with an appropriate exit code (0 if no tests failed, 1 if tests
failed).
This macro expands to a main function which handless stuff like parsing
arguments and freeing memory allocated by Snow. The `block` should just be
calling the various functions defined by `describe`.
## Assert Macros
View
@@ -61,7 +61,6 @@ describe(files, {
});
});
int main() {
snow_main({
test_files();
done();
}
});
View
@@ -9,6 +9,7 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifndef SNOW_COLOR_SUCCESS
#define SNOW_COLOR_SUCCESS "\033[32m"
@@ -31,6 +32,8 @@ static int _snow_global_total = 0;
static int _snow_global_successes = 0;
static int _snow_num_defines = 0;
static int _snow_opt_color = 1;
struct {
void **labels;
size_t size;
@@ -40,14 +43,20 @@ struct {
#define _snow_fail(desc, spaces, name, file, ...) \
do { \
_snow_exit_code = 1; \
fprintf(stderr, \
_SNOW_COLOR_BOLD SNOW_COLOR_FAIL "%s" \
_SNOW_COLOR_RESET SNOW_COLOR_FAIL "Failed: " \
_SNOW_COLOR_RESET SNOW_COLOR_DESC "%s" \
_SNOW_COLOR_RESET ":\n%s ", \
spaces, desc, spaces); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, \
if (_snow_opt_color) { \
fprintf(stdout, \
_SNOW_COLOR_BOLD SNOW_COLOR_FAIL "%s" \
_SNOW_COLOR_RESET SNOW_COLOR_FAIL "Failed: " \
_SNOW_COLOR_RESET SNOW_COLOR_DESC "%s" \
_SNOW_COLOR_RESET ":\n%s ", \
spaces, desc, spaces); \
} else { \
fprintf(stdout, \
"%s✕ Failed: %s:\n%s ", \
spaces, desc, spaces); \
} \
fprintf(stdout, __VA_ARGS__); \
fprintf(stdout, \
"\n%s in %s:%s\n", spaces, file, name); \
} while (0)
@@ -182,30 +191,49 @@ static int __attribute__((unused)) _snow_assertneq_str(
#define _snow_print_success() \
do { \
fprintf(stderr, \
_SNOW_COLOR_BOLD SNOW_COLOR_SUCCESS "%s" \
_SNOW_COLOR_RESET SNOW_COLOR_SUCCESS "Success: " \
_SNOW_COLOR_RESET SNOW_COLOR_DESC "%s" \
_SNOW_COLOR_RESET "\n", \
_snow_spaces, _snow_desc); \
if (_snow_opt_color) { \
fprintf(stdout, \
_SNOW_COLOR_BOLD SNOW_COLOR_SUCCESS "%s" \
_SNOW_COLOR_RESET SNOW_COLOR_SUCCESS "Success: " \
_SNOW_COLOR_RESET SNOW_COLOR_DESC "%s" \
_SNOW_COLOR_RESET "\n", \
_snow_spaces, _snow_desc); \
} else { \
fprintf(stdout, \
"%s✓ Success: %s\n", \
_snow_spaces, _snow_desc); \
} \
} while (0)
#define _snow_print_run() \
do { \
if (_snow_depth > 0 || _snow_first_define) { \
fprintf(stderr, "\n"); \
fprintf(stdout, "\n"); \
_snow_first_define = 0; \
} \
fprintf(stderr, _SNOW_COLOR_BOLD "%sTesting %s:" _SNOW_COLOR_RESET "\n", \
_snow_spaces, _snow_name); \
if (_snow_opt_color) { \
fprintf(stdout, \
_SNOW_COLOR_BOLD "%sTesting %s:" _SNOW_COLOR_RESET "\n", \
_snow_spaces, _snow_name); \
} else { \
fprintf(stdout, \
"%sTesting %s:\n", \
_snow_spaces, _snow_name); \
} \
} while (0)
#define _snow_print_done() \
do { \
fprintf(stderr, \
_SNOW_COLOR_BOLD "%s%s: Passed %i/%i tests." \
_SNOW_COLOR_RESET "\n\n", \
_snow_spaces, _snow_name, _snow_successes, _snow_total); \
if (_snow_opt_color) { \
fprintf(stdout, \
_SNOW_COLOR_BOLD "%s%s: Passed %i/%i tests." \
_SNOW_COLOR_RESET "\n\n", \
_snow_spaces, _snow_name, _snow_successes, _snow_total); \
} else { \
fprintf(stdout, \
"%s%s: Passed %i/%i tests.\n\n", \
_snow_spaces, _snow_name, _snow_successes, _snow_total); \
} \
} while (0)
#define defer(expr) \
@@ -293,16 +321,31 @@ static int __attribute__((unused)) _snow_assertneq_str(
_snow_global_total += _snow_total; \
}
#define done() \
do { \
#define snow_main(block) \
int main(int argc, char **argv) { \
if (!isatty(1)) \
_snow_opt_color = 0; \
for (int i = 1; i < argc; ++i) { \
if (strcmp(argv[i], "--color") == 0) \
_snow_opt_color = 1; \
else if (strcmp(argv[i], "--no-color") == 0) \
_snow_opt_color = 0; \
} \
block \
free(_snow_labels.labels); \
if (_snow_num_defines > 1) { \
fprintf(stderr, \
_SNOW_COLOR_BOLD "Total: Passed %i/%i tests.\n\n" \
_SNOW_COLOR_RESET, \
_snow_global_successes, _snow_global_total); \
if (_snow_opt_color) { \
fprintf(stdout, \
_SNOW_COLOR_BOLD "Total: Passed %i/%i tests.\n\n" \
_SNOW_COLOR_RESET, \
_snow_global_successes, _snow_global_total); \
} else { \
fprintf(stdout, \
"Total: Passed %i/%i tests.\n\n", \
_snow_global_successes, _snow_global_total); \
} \
} \
return _snow_exit_code; \
} while (0)
}
#endif

0 comments on commit c41d869

Please sign in to comment.