/* * author:yangwei * create time:2021-8-21 * */ #pragma GCC diagnostic ignored "-Wunused-parameter" #include #include #include #include #include #include "stellar/stellar.h" #include "cJSON.h" #include "gtest_lpip.h" struct gtest_json_result { cJSON *test_json_root; cJSON *expect_json_root; int result_count; }; static struct gtest_json_result *gtest_result_new(const char *expect_json_path) { struct gtest_json_result *para = (struct gtest_json_result *)calloc(1, sizeof(struct gtest_json_result)); if(expect_json_path==NULL) { para->expect_json_root=cJSON_CreateArray(); return para; } FILE *file = fopen(expect_json_path, "rb"); if(file) { fseek(file, 0, SEEK_END); long filesize = ftell(file); rewind(file); char *buffer = (char *)calloc(filesize + 1, 1); fread(buffer, 1, filesize, file); para->expect_json_root=cJSON_Parse(buffer); free(buffer); fclose(file); } para->result_count=1;//count start from 1 return para; } static int gtest_result_compare(struct gtest_json_result *para) { if(cJSON_GetArraySize(para->test_json_root)!=cJSON_GetArraySize(para->expect_json_root)) { char *load_json_str = cJSON_Print(para->expect_json_root); printf("LOAD Raw:\n%s\n", load_json_str); free(load_json_str); char *result_json_str = cJSON_Print(para->test_json_root); printf("TEST Raw:\n%s\n", result_json_str); free(result_json_str); return -1; } int compare_ret = cJSON_Compare(para->expect_json_root, para->test_json_root, 0); if (compare_ret != 1) { char *load_json_str = cJSON_Print(para->expect_json_root); printf("LOAD Raw:\n%s\n", load_json_str); free(load_json_str); char *result_json_str = cJSON_Print(para->test_json_root); printf("TEST Raw:\n%s\n", result_json_str); free(result_json_str); cJSON *t_load = para->expect_json_root->child, *t_test = para->test_json_root->child; while (t_load != NULL) { // print first diff item, then return; if(1 != cJSON_Compare(t_load, t_test, 0)) { load_json_str = cJSON_Print(t_load); printf("LOAD Diff:\n%s\n", load_json_str); free(load_json_str); result_json_str = cJSON_Print(t_test); printf("TEST Diff:\n%s\n", result_json_str); free(result_json_str); return -1; } t_load = t_load->next; t_test = t_test->next; } } return compare_ret; } static void gtest_result_free(struct gtest_json_result *para) { if(para) { if(para->test_json_root)cJSON_Delete(para->test_json_root); if(para->expect_json_root)cJSON_Delete(para->expect_json_root); free(para); } return; } /********************************************** * GTEST MAIN * **********************************************/ int main(int argc, char ** argv) { ::testing::InitGoogleTest(&argc, argv); //EXPECT_EQ(argc, 2); printf("Usage: ./[gtest_main] [/path/to/expect_json]\n"); char *expect_json_path=argv[1]; struct gtest_json_result *g_test_para = gtest_result_new(expect_json_path); struct stellar *st=stellar_new("./conf/stellar.toml"); stellar_test_result_setup(); EXPECT_TRUE(st!=NULL); stellar_run(st); stellar_free(st); char *test_result_json=stellar_test_result_json_export(); g_test_para->test_json_root=cJSON_Parse(test_result_json); free(test_result_json); EXPECT_TRUE(g_test_para->expect_json_root != NULL && g_test_para->test_json_root != NULL); EXPECT_EQ(gtest_result_compare(g_test_para), 1); gtest_result_free(g_test_para); stellar_test_result_cleanup(); return ::testing::Test::HasFailure() ? 1 : 0; }