| 44 | } |
| 45 | |
| 46 | static void do_test(const char *test_name) |
| 47 | { |
| 48 | char *expected = NULL; |
| 49 | char *actual = NULL; |
| 50 | cJSON *tree = NULL; |
| 51 | |
| 52 | size_t test_name_length = 0; |
| 53 | /* path of the test input */ |
| 54 | char *test_path = NULL; |
| 55 | /* path of the expected output */ |
| 56 | char *expected_path = NULL; |
| 57 | |
| 58 | test_name_length = strlen(test_name); |
| 59 | |
| 60 | /* allocate file paths */ |
| 61 | #define TEST_DIR_PATH "inputs/" |
| 62 | test_path = (char*)malloc(sizeof(TEST_DIR_PATH) + test_name_length); |
| 63 | TEST_ASSERT_NOT_NULL_MESSAGE(test_path, "Failed to allocate test_path buffer."); |
| 64 | expected_path = (char*)malloc(sizeof(TEST_DIR_PATH) + test_name_length + sizeof(".expected")); |
| 65 | TEST_ASSERT_NOT_NULL_MESSAGE(expected_path, "Failed to allocate expected_path buffer."); |
| 66 | |
| 67 | /* create file paths */ |
| 68 | sprintf(test_path, TEST_DIR_PATH"%s", test_name); |
| 69 | sprintf(expected_path, TEST_DIR_PATH"%s.expected", test_name); |
| 70 | |
| 71 | /* read expected output */ |
| 72 | expected = read_file(expected_path); |
| 73 | TEST_ASSERT_NOT_NULL_MESSAGE(expected, "Failed to read expected output."); |
| 74 | |
| 75 | /* read and parse test */ |
| 76 | tree = parse_file(test_path); |
| 77 | TEST_ASSERT_NOT_NULL_MESSAGE(tree, "Failed to read of parse test."); |
| 78 | |
| 79 | /* print the parsed tree */ |
| 80 | actual = cJSON_Print(tree); |
| 81 | TEST_ASSERT_NOT_NULL_MESSAGE(actual, "Failed to print tree back to JSON."); |
| 82 | |
| 83 | |
| 84 | TEST_ASSERT_EQUAL_STRING(expected, actual); |
| 85 | |
| 86 | /* cleanup resources */ |
| 87 | if (expected != NULL) |
| 88 | { |
| 89 | free(expected); |
| 90 | } |
| 91 | if (tree != NULL) |
| 92 | { |
| 93 | cJSON_Delete(tree); |
| 94 | } |
| 95 | if (actual != NULL) |
| 96 | { |
| 97 | free(actual); |
| 98 | } |
| 99 | if (test_path != NULL) |
| 100 | { |
| 101 | free(test_path); |
| 102 | } |
| 103 | if (expected_path != NULL) |
no test coverage detected
searching dependent graphs…