1 /* Copyright 2015-present Facebook, Inc. 2 * Licensed under the Apache License, Version 2.0 */ 3 4 #include "watchman_system.h" 5 6 #include "thirdparty/jansson/jansson.h" 7 #include "thirdparty/wildmatch/wildmatch.h" 8 #include "thirdparty/tap.h" test_integrals()9 10 #define WILDMATCH_TEST_JSON_FILE "tests/wildmatch_test.json" 11 12 static void run_test(json_t *test_case_data) 13 { 14 int wildmatch_should_succeed; 15 int wildmatch_flags; 16 char *text_to_match; 17 char *pattern_to_use; 18 int wildmatch_succeeded; 19 20 json_error_t error; 21 if (json_unpack_ex( 22 test_case_data, 23 &error, 24 0, 25 "[b,i,s,s]", 26 &wildmatch_should_succeed, 27 &wildmatch_flags, 28 &text_to_match, 29 &pattern_to_use) == -1) { 30 fail( 31 "Error decoding JSON: %s (source=%s, line=%d, col=%d)\n", 32 error.text, 33 error.source, 34 error.line, 35 error.column); 36 return; 37 } 38 39 wildmatch_succeeded = 40 wildmatch(pattern_to_use, text_to_match, wildmatch_flags, 0) == WM_MATCH; 41 if (wildmatch_should_succeed) { 42 ok( 43 wildmatch_succeeded, 44 "Pattern [%s] should match text [%s] with flags %d", 45 pattern_to_use, 46 text_to_match, 47 wildmatch_flags); 48 } else { 49 ok( 50 !wildmatch_succeeded, 51 "Pattern [%s] should not match text [%s] with flags %d", 52 pattern_to_use, 53 text_to_match, 54 wildmatch_flags); 55 } 56 } 57 58 int main(int, char**) { 59 FILE *test_cases_file; 60 json_error_t error; 61 size_t num_tests; 62 size_t index; 63 64 test_cases_file = fopen(WILDMATCH_TEST_JSON_FILE, "r"); 65 if (!test_cases_file) { 66 test_cases_file = fopen("watchman/" WILDMATCH_TEST_JSON_FILE, "r"); 67 } 68 if (!test_cases_file) { 69 diag("Couldn't open %s: %s\n", WILDMATCH_TEST_JSON_FILE, strerror(errno)); 70 abort(); 71 } 72 auto test_cases = json_loadf(test_cases_file, 0, &error); 73 if (!test_cases) { 74 diag( 75 "Error decoding JSON: %s (source=%s, line=%d, col=%d)\n", 76 error.text, 77 error.source, 78 error.line, 79 error.column); test_pointers()80 abort(); 81 } 82 if (fclose(test_cases_file) != 0) { 83 diag("Error closing %s: %s\n", WILDMATCH_TEST_JSON_FILE, strerror(errno)); 84 abort(); 85 } 86 if (!json_is_array(test_cases)) { 87 diag("Expected JSON in %s to be an array\n", WILDMATCH_TEST_JSON_FILE); 88 abort(); 89 } 90 num_tests = json_array_size(test_cases); 91 plan_tests((unsigned int)num_tests); 92 for (index = 0; index < num_tests; index++) { 93 auto test_case_data = json_array_get(test_cases, index); 94 run_test(test_case_data); 95 } 96 return exit_status(); 97 } 98