|
| 1 | +#include "cut.h" |
| 2 | + |
| 3 | +struct cut_runtime cut; |
| 4 | + |
| 5 | +static char suite_pattern[64]; |
| 6 | +static char case_pattern[64]; |
| 7 | + |
| 8 | +static void _filter(int argc, char **argv) |
| 9 | +{ |
| 10 | + int i = 0; |
| 11 | + struct cut_case *c = NULL; |
| 12 | + if (argc == 2 && 0 == strcmp(argv[1], "all")) { |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + for (i = 0; i < cut.ccnt_total; i++) { |
| 17 | + c = cut.clist[i]; |
| 18 | + if ((argc == 2 && (0 != strcmp(c->sname, argv[1]))) || |
| 19 | + (argc == 3 && (0 != strcmp(c->sname, argv[1]) || 0 != strcmp(c->cname, argv[2])))) { |
| 20 | + |
| 21 | + if (!(argc == 2 && strlen(suite_pattern) && strstr(c->sname, suite_pattern))) { |
| 22 | + cut.clist[i]->skip = 1; |
| 23 | + cut.ccnt_skip++; |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +static void _usage(const char *me) |
| 30 | +{ |
| 31 | + cut_printf("Usage: %s [OPTION] S-FILTER [C-FILTER]\n\n" \ |
| 32 | + "OPTION:\n" \ |
| 33 | + " --verbose: verbose when exec cases\n" \ |
| 34 | + " --list: list cases\n" \ |
| 35 | + " --count: print case count\n" \ |
| 36 | + "\n" \ |
| 37 | + "S-FILTER: suite name filter, e.g. '%s all' means run all suites\n" \ |
| 38 | + "C-FILTER: case name filter\n", me, me); |
| 39 | +} |
| 40 | + |
| 41 | +static int _verbose_opt = 0; |
| 42 | +static int _parse_arg(int argc, char **argv) |
| 43 | +{ |
| 44 | + if (argc >= 2) { |
| 45 | + if (0 == strcmp(argv[1], "--list")) { |
| 46 | + int i = 0; |
| 47 | + int cnt = 0; |
| 48 | + for (i = 0; i < cut.ccnt_total; i++) { |
| 49 | + struct cut_case *c = cut.clist[i]; |
| 50 | + if (argc == 2 || |
| 51 | + (argc == 3 && 0 == strcmp(argv[2], "all")) || |
| 52 | + (argc == 3 && 0 == strcmp(c->sname, argv[2])) || |
| 53 | + (argc == 3 && strlen(suite_pattern) && strstr(c->sname, suite_pattern)) || |
| 54 | + (argc == 4 && strlen(suite_pattern) && strlen(case_pattern) && strstr(c->sname, suite_pattern) |
| 55 | + && strstr(c->cname, case_pattern)) || |
| 56 | + (argc == 4 && 0 == strcmp(c->sname, argv[2]) && 0 == strcmp(c->cname, argv[3]))) { |
| 57 | + cut_printf(" [%02d] %s.%s\n", ++cnt, c->sname, c->cname); |
| 58 | + } |
| 59 | + } |
| 60 | + cut_printf("\n"); |
| 61 | + cut_printf("In total %d case(s), matched %d case(s)\n", cut.ccnt_total, cnt); |
| 62 | + cut_printf("\n"); |
| 63 | + return 0; |
| 64 | + } |
| 65 | + if (0 == strcmp(argv[1], "--count")) { |
| 66 | + cut_printf("total %d case(s).\n", cut.ccnt_total); |
| 67 | + return 0; |
| 68 | + } |
| 69 | + if (0 == strcmp(argv[1], "--help")) { |
| 70 | + _usage(argv[0]); |
| 71 | + return 0; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return 1; |
| 76 | +} |
| 77 | + |
| 78 | +int cut_main(int argc, char **argv) |
| 79 | +{ |
| 80 | + int i = 0, j = 0, cnt = 0; |
| 81 | + char tmpbuf[128]; |
| 82 | + char *pos; |
| 83 | + |
| 84 | + if (argc >= 2) { |
| 85 | + |
| 86 | + int idx = 1; |
| 87 | + char *q = NULL; |
| 88 | + |
| 89 | + if (!strcmp(argv[1], "--list") || !strncmp(argv[1], "--verbose", strlen("--verbose"))) { |
| 90 | + idx += 1; |
| 91 | + } |
| 92 | + |
| 93 | + if (idx < argc) { |
| 94 | + |
| 95 | + if ((q = strchr(argv[idx], '%')) != NULL) { |
| 96 | + strncpy(suite_pattern, argv[idx], q - argv[idx]); |
| 97 | + } |
| 98 | + idx += 1; |
| 99 | + if (idx < argc) { |
| 100 | + if ((q = strchr(argv[idx], '%')) != NULL) { |
| 101 | + strncpy(case_pattern, argv[idx], q - argv[idx]); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (0 == _parse_arg(argc, argv)) { |
| 108 | + return 0; |
| 109 | + } |
| 110 | + |
| 111 | + if (argc >= 2 && !strncmp(argv[1], "--verbose", strlen("--verbose"))) { |
| 112 | + _verbose_opt = 1; |
| 113 | + argc --; |
| 114 | + argv ++; |
| 115 | + } |
| 116 | + |
| 117 | + _filter(argc, argv); |
| 118 | + |
| 119 | + for (; i < cut.ccnt_total; i++) { |
| 120 | + pos = tmpbuf; |
| 121 | + |
| 122 | + cut.ccur = cut.clist[i]; |
| 123 | + if (cut.ccur->skip) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + memset(tmpbuf, 0, sizeof(tmpbuf)); |
| 128 | + pos += cut_snprintf(pos, |
| 129 | + sizeof(tmpbuf), |
| 130 | + "TEST [%02d/%02d] %s.%s ", |
| 131 | + ++cnt, |
| 132 | + cut.ccnt_total - cut.ccnt_skip, |
| 133 | + cut.ccur->sname, |
| 134 | + cut.ccur->cname); |
| 135 | + for (j = 80 - strlen(tmpbuf); j >= 0; --j) { |
| 136 | + pos += sprintf(pos, "%s", "."); |
| 137 | + } |
| 138 | + if (_verbose_opt) { |
| 139 | + pos += sprintf(pos, " [%sEXEC%s]\n", COL_YEL, COL_DEF); |
| 140 | + cut_printf("%s", tmpbuf); |
| 141 | + pos -= 19; |
| 142 | + } |
| 143 | + HAL_SleepMs(15000); |
| 144 | + TRY { |
| 145 | + if (cut.ccur->setup) |
| 146 | + { |
| 147 | + cut.ccur->setup(cut.ccur->data, cut.ccur); |
| 148 | + } |
| 149 | + cut.ccur->run(cut.ccur->data, (struct cut_case *)cut.ccur); |
| 150 | + if (cut.ccur->teardown) |
| 151 | + { |
| 152 | + cut.ccur->teardown(cut.ccur->data); |
| 153 | + } |
| 154 | + |
| 155 | + pos += sprintf(pos, " [%sSUCC%s]\n", COL_GRE, COL_DEF); |
| 156 | + cut_printf("%s", tmpbuf); |
| 157 | + |
| 158 | + cut.ccnt_pass++; |
| 159 | + continue; |
| 160 | + } |
| 161 | + EXCEPT { |
| 162 | + |
| 163 | + pos += sprintf(pos, " [%sFAIL%s]\n", COL_RED, COL_DEF); |
| 164 | + cut_printf("%s", tmpbuf); |
| 165 | + |
| 166 | + cut.ccnt_fail++; |
| 167 | + continue; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + cut_printf("\n"); |
| 172 | + cut_printf("===========================================================================\n"); |
| 173 | + if (cut.ccnt_fail > 0) { |
| 174 | + cut_printf("FAIL LIST:\n"); |
| 175 | + for (i = 0; i < cut.ccnt_fail; i++) { |
| 176 | + cut_printf(" [%02d] %s\n", i + 1, cut.cerrmsg[i]); |
| 177 | + cut_free(cut.cerrmsg[i]); |
| 178 | + } |
| 179 | + cut_printf("---------------------------------------------------------------------------\n"); |
| 180 | + } |
| 181 | + cut_printf("SUMMARY:\n" \ |
| 182 | + " TOTAL: %d\n" \ |
| 183 | + " SKIPPED: %d\n" \ |
| 184 | + " MATCHED: %d\n" \ |
| 185 | + " PASS: %d\n" \ |
| 186 | + " FAILED: %d\n", cut.ccnt_total, cut.ccnt_skip, |
| 187 | + cut.ccnt_total - cut.ccnt_skip, cut.ccnt_pass, cut.ccnt_fail); |
| 188 | + cut_printf("===========================================================================\n"); |
| 189 | + |
| 190 | + return cut.ccnt_fail; |
| 191 | + |
| 192 | +} |
| 193 | + |
0 commit comments