| 1 | #include "test_item.h" |
| 2 | |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | #include <cgreen/internal/unit_implementation.h> |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | |
| 10 | |
| 11 | static const char *skip_to_separator(const char *start) { |
| 12 | return strstr(start, CGREEN_SEPARATOR); |
| 13 | } |
| 14 | |
| 15 | static const char *skip_over_separator(const char *start) { |
| 16 | return strstr(start, CGREEN_SEPARATOR)+strlen(CGREEN_SEPARATOR); |
| 17 | } |
| 18 | |
| 19 | static const char *skip_cgreen_spec_marker(const char *string) { |
| 20 | return &string[strlen(CGREEN_SPEC_PREFIX CGREEN_SEPARATOR)]; |
| 21 | } |
| 22 | |
| 23 | static const char *string_copy_of(const char *start, const char *end) { |
| 24 | char *string = strdup(start); |
| 25 | string[end-start] = '\0'; |
| 26 | return string; |
| 27 | } |
| 28 | |
| 29 | static const char *context_name_from(const char *specification_name) { |
| 30 | const char *start = skip_cgreen_spec_marker(specification_name); |
| 31 | const char *end = skip_to_separator(start); |
| 32 | return string_copy_of(start, end); |
| 33 | } |
| 34 | |
| 35 | static const char *test_name_from(const char *specification_name) { |
| 36 | const char *start = skip_cgreen_spec_marker(specification_name); |
| 37 | start = skip_over_separator(start); |
| 38 | const char *end = skip_to_separator(start); |
| 39 | return string_copy_of(start, end? end : &start[strlen(start)]); |
| 40 | } |
| 41 | |
| 42 | TestItem *create_test_item_from(const char *specification_name) { |
| 43 | TestItem *test_item = (TestItem *)malloc(sizeof(TestItem)); |
| 44 | |
| 45 | test_item->specification_name = strdup(specification_name); |
| 46 | test_item->context_name = context_name_from(specification_name); |
| 47 | test_item->test_name = test_name_from(specification_name); |
| 48 | |
| 49 | return test_item; |
| 50 | } |
| 51 | |
| 52 | void destroy_test_item(TestItem *item) { |
| 53 | free((void *)item->specification_name); |
| 54 | free((void *)item->context_name); |
| 55 | free((void *)item->test_name); |
| 56 | free((void *)item); |
| 57 | } |