-
Notifications
You must be signed in to change notification settings - Fork 0
/
findtest_names.c
107 lines (104 loc) · 4.08 KB
/
findtest_names.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// vim: ts=2 noet spell foldmethod=syntax :
#include "ut_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#define UNIT_TEST
#include "suite_and_test_list_wrapper.h"
#undef UNIT_TEST
#ifdef __CYGWIN__
#define COMMAND "nm %s |grep UnitTest_a_test_to_run_|cut -d' ' -f 3|grep -E \"^_UnitTest_a_test_to_run_\""
#define SETUP "nm %s |grep UnitTest_a_unit_test_suite_setup_func_|cut -d' ' -f 3|grep -E \"^_UnitTest_a_unit_test_suite_setup_func_\""
#define DESTORY "nm %s |grep UnitTest_a_unit_test_suite_destroy_func_|cut -d' ' -f 3|grep -E \"^_UnitTest_a_unit_test_suite_destroy_\""
#else
#define COMMAND "nm %s |grep UnitTest_a_test_to_run_|cut -d' ' -f 3|grep -E \"^UnitTest_a_test_to_run_\""
#define SETUP "nm %s |grep UnitTest_a_unit_test_suite_setup_func_|cut -d' ' -f 3|grep -E \"^UnitTest_a_unit_test_suite_setup_func_\""
#define DESTORY "nm %s |grep UnitTest_a_unit_test_suite_destroy_func_|cut -d' ' -f 3|grep -E \"^UnitTest_a_unit_test_suite_destroy_\""
#endif
void chomp(char *str) {
while ('\0' != *str ) {
if (*str == '\n')
*str='\0';
str++;
}
}
static int isSuite(ListNode_t * nodep, void * datap) {
char * suite_name = (char *) datap;
test_suite_element_t *sp = NODE_TO_ENTRY(test_suite_element_t,link,nodep);
return 0==strcmp(suite_name, sp->suite.suite_name);
}
static void addTestToSuites(test_element_t *e,ListNode_t * test_suites_list_headp) {
ListNode_t * listp = ListFind(test_suites_list_headp,isSuite, (char *)e->test.suite_name);
if (NULL == listp) {
test_suite_element_t *sp = create_test_suite_element( (char *)e->test.suite_name);
ListAddEnd(test_suites_list_headp, &sp->link);
listp = &sp->link;
}
test_suite_element_t *sp= NODE_TO_ENTRY(test_suite_element_t,link,listp);
ListAddEnd(&sp->test_list_head, &e->link);
}
void loadTestStruct(ListNode_t *test_suites_list_headp, char *line,ut_configuration_t *configp) {
test_results_t *trp;
trp = dlsym(configp->dynlibraryp, line);
if (trp == NULL) {
printf("failed to load test: %s: %s\n",line, dlerror());
return;
}
test_element_t * e = create_test_element(trp);
addTestToSuites(e, test_suites_list_headp);
}
void loadSuiteSetup(ListNode_t *test_suites_list_headp, char *line,ut_configuration_t *configp) {
test_suite_t *trp;
trp = dlsym(configp->dynlibraryp, line);
ListNode_t * listp = ListFind(test_suites_list_headp,isSuite, (char *)trp->suite_name);
if (NULL == listp) {
return;
}
test_suite_element_t *sp= NODE_TO_ENTRY(test_suite_element_t,link,listp);
memcpy(&sp->setup, trp, sizeof(test_suite_t));
}
void loadSuiteDestroy(ListNode_t *test_suites_list_headp, char *line,ut_configuration_t *configp) {
test_suite_t *trp;
trp = dlsym(configp->dynlibraryp, line);
ListNode_t * listp = ListFind(test_suites_list_headp,isSuite, (char *)trp->suite_name);
if (NULL == listp) {
return;
}
test_suite_element_t *sp= NODE_TO_ENTRY(test_suite_element_t,link,listp);
memcpy(&sp->destroy, trp, sizeof(test_suite_t));
}
#ifdef __CYGWIN__
void removeLeadingUnderscore (char *line) {
if (line[0] != '_')
return;
int i;
for (i=0; line[i] != '\0'; i++) {
line[i] = line[i+1];
}
}
#endif
void loadTests(ut_configuration_t *configp,ListNode_t *test_suites_list_headp, const char *command, void func(ListNode_t *test_suites_list_headp, char *line,ut_configuration_t *configp)) {
char command_line[2024];
snprintf(command_line, sizeof(command_line) -1, command, configp->program_name);
FILE * fd = popen (command_line, "r");
while (!feof(fd)) {
char line[300];
if (0 != fgets(line,299, fd)) {
chomp(line);
#if __CYGWIN__
removeLeadingUnderscore (line);
func( test_suites_list_headp, line,configp);
#else
func( test_suites_list_headp, line,configp);
#endif
}
}
pclose(fd);
}
char * getUnitTestListAsString(ut_configuration_t *configp,ListNode_t *test_suites_list_headp) {
loadTests(configp, test_suites_list_headp, COMMAND,loadTestStruct);
loadTests(configp, test_suites_list_headp, SETUP,loadSuiteSetup);
loadTests(configp, test_suites_list_headp, DESTORY,loadSuiteDestroy);
return "";
}