describe.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // describe.h
  3. //
  4. // Copyright (c) 2013 Stephen Mathieson
  5. // Copyright (c) 2015 Michael Phan-Ba
  6. // MIT licensed
  7. //
  8. #ifndef DESCRIBE_H
  9. #define DESCRIBE_H 1
  10. #include "console-colors.h"
  11. #include "assertion-macros.h"
  12. #define DESCRIBE_VERSION "1.1.0"
  13. #define DESCRIBE_OK "✓"
  14. #define DESCRIBE_FAIL "✖"
  15. void __describe_after_spec(const char *specification, int before) {
  16. if (assert_failures() == before) {
  17. cc_fprintf(
  18. CC_FG_DARK_GREEN
  19. , stdout
  20. , " %s"
  21. , DESCRIBE_OK
  22. );
  23. } else {
  24. cc_fprintf(
  25. CC_FG_DARK_RED
  26. , stdout
  27. , " %s"
  28. , DESCRIBE_FAIL
  29. );
  30. }
  31. cc_fprintf(
  32. CC_FG_GRAY
  33. , stdout
  34. , " %s\n"
  35. , specification
  36. );
  37. }
  38. /*
  39. * Describe `suite` with `title`
  40. */
  41. #define describe(title) for ( \
  42. int __run = 0; \
  43. __run++ == 0 && printf("\n %s\n", title); \
  44. printf("\n") \
  45. )
  46. /*
  47. * Describe `fn` with `specification`
  48. */
  49. #define it(specification) for ( \
  50. int __before = assert_failures(), __run = 0; \
  51. __run++ == 0; \
  52. __describe_after_spec(specification, __before) \
  53. )
  54. #endif