108 lines
2.7 KiB
C
108 lines
2.7 KiB
C
#include "test_result_validator.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
struct test_result_validator
|
|
{
|
|
cJSON *test_json_root;
|
|
cJSON *expect_json_root;
|
|
int result_count;
|
|
};
|
|
|
|
struct test_result_validator *test_result_validator_new(const char *expect_json_path)
|
|
{
|
|
struct test_result_validator *para = (struct test_result_validator *)calloc(1, sizeof(struct test_result_validator));
|
|
para->test_json_root=cJSON_CreateArray();
|
|
|
|
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;
|
|
}
|
|
|
|
void test_result_validator_free(struct test_result_validator *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;
|
|
}
|
|
|
|
int test_result_validator_compare(struct test_result_validator *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;
|
|
}
|
|
|
|
|
|
int test_result_validator_add_actual(struct test_result_validator *param, cJSON *json)
|
|
{
|
|
if(param==NULL || json==NULL)
|
|
{
|
|
return -1;
|
|
}
|
|
cJSON_AddItemToArray(param->test_json_root, json);
|
|
return 0;
|
|
}
|
|
|