0001 #include <cgreen/parameters.h>
0002 #include <cgreen/suite.h>
0003 #include <cgreen/unit.h>
0004 #include <cgreen/vector.h>
0005 #ifndef __cplusplus
0006 #include <stdbool.h>
0007 #endif
0008 #include <stddef.h>
0009 #include <stdlib.h>
0010 #include <string.h>
0011
0012
0013 #ifdef __cplusplus
0014 namespace cgreen {
0015 #endif
0016
0017
0018 CgreenContext defaultContext = {
0019 "",
0020 __FILE__,
0021 &do_nothing,
0022 &do_nothing
0023 };
0024
0025 bool has_test(TestSuite *suite, const char *name) {
0026 int i;
0027 for (i = 0; i < suite->size; i++) {
0028 if (suite->tests[i].type == test_function) {
0029 if (strcmp(suite->tests[i].name, name) == 0) {
0030 return true;
0031 }
0032 } else if (has_test(suite->tests[i].Runnable.suite, name)) {
0033 return true;
0034 }
0035 }
0036
0037 return false;
0038 }
0039
0040 bool has_setup(TestSuite *suite) {
0041 return (suite->setup != &do_nothing);
0042 }
0043
0044 bool has_teardown(TestSuite *suite) {
0045 return (suite->teardown != &do_nothing);
0046 }
0047
0048 void do_nothing() {
0049 }
0050
0051 TestSuite *create_named_test_suite_(const char *name, const char *filename, int line) {
0052 TestSuite *suite = (TestSuite *)malloc(sizeof(TestSuite));
0053 suite->name = name;
0054 suite->filename = filename;
0055 suite->line = line;
0056 suite->tests = NULL;
0057 suite->setup = &do_nothing;
0058 suite->teardown = &do_nothing;
0059 suite->size = 0;
0060 return suite;
0061 }
0062
0063 void destroy_test_suite(TestSuite *suiteToDestroy) {
0064 int i;
0065 for (i = 0; i < suiteToDestroy->size; i++) {
0066 UnitTest test = suiteToDestroy->tests[i];
0067 TestSuite* suite = test.Runnable.suite;
0068 if (test_suite == test.type && suite != NULL) {
0069 suiteToDestroy->tests[i].Runnable.suite = NULL;
0070 destroy_test_suite(suite);
0071 }
0072 }
0073
0074 if (suiteToDestroy->tests != NULL)
0075 free(suiteToDestroy->tests);
0076
0077 free(suiteToDestroy);
0078 }
0079
0080 void add_test_(TestSuite *suite, const char *name, CgreenTest *test) {
0081 suite->size++;
0082 suite->tests = (UnitTest *)realloc(suite->tests, sizeof(UnitTest) * suite->size);
0083 suite->tests[suite->size - 1].type = test_function;
0084 suite->tests[suite->size - 1].name = name;
0085 suite->tests[suite->size - 1].Runnable.test = test;
0086 }
0087
0088 void add_tests_(TestSuite *suite, const char *names, ...) {
0089 CgreenVector *test_names = create_vector_of_names(names);
0090 int i;
0091 va_list tests;
0092 va_start(tests, names);
0093 for (i = 0; i < cgreen_vector_size(test_names); i++) {
0094 add_test_(suite, (char *)(cgreen_vector_get(test_names, i)), va_arg(tests, CgreenTest *));
0095 }
0096 va_end(tests);
0097 destroy_cgreen_vector(test_names);
0098 }
0099
0100 void add_suite_(TestSuite *owner, const char *name, TestSuite *suite) {
0101 owner->size++;
0102 owner->tests = (UnitTest *)realloc(owner->tests, sizeof(UnitTest) * owner->size);
0103 owner->tests[owner->size - 1].type = test_suite;
0104 owner->tests[owner->size - 1].name = name;
0105 owner->tests[owner->size - 1].Runnable.suite = suite;
0106 }
0107
0108 void set_setup(TestSuite *suite, void (*set_up)()) {
0109 suite->setup = set_up;
0110 }
0111
0112 void set_teardown(TestSuite *suite, void (*tear_down)()) {
0113 suite->teardown = tear_down;
0114 }
0115
0116 int count_tests(TestSuite *suite) {
0117 int count = 0;
0118 int i;
0119 for (i = 0; i < suite->size; i++) {
0120 if (suite->tests[i].type == test_function) {
0121 count++;
0122 } else {
0123 count += count_tests(suite->tests[i].Runnable.suite);
0124 }
0125 }
0126 return count;
0127 }
0128
0129 #ifdef __cplusplus
0130 }
0131 #endif