vcpe/unit_test/s2j/s2j_test.cpp

59 lines
1.9 KiB
C++

//
// Created by dongwenzhe on 2023/3/17.
//
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "s2j/s2j.h"
typedef struct {
char info[16];
} Message;
typedef struct {
int id;
char name[16];
Message msg;
} Service;
TEST_SUITE("S2J_TEST") {
TEST_CASE("S2J") {
Service origin_Server;
origin_Server.id = 3;
strcpy(origin_Server.name, "dhcpServer");
strcpy(origin_Server.msg.info, "opendhcpd");
Service *pServer = &origin_Server;
s2j_create_json_obj(testServer);
s2j_json_set_basic_element(testServer, pServer, int, id);
s2j_json_set_basic_element(testServer, pServer, string, name);
s2j_json_set_struct_element(testInfo, testServer, pMsg, pServer, Message, msg);
s2j_json_set_basic_element(testInfo, pMsg, string, info);
int i;
CHECK_EQ(testServer->child->valueint, 3);
for(i = 0; i < strlen(origin_Server.name); i++) {
CHECK_EQ(origin_Server.name[i], testServer->child->next->valuestring[i]);
}
for(i = 0; i < strlen(origin_Server.msg.info); i++) {
CHECK_EQ(origin_Server.msg.info[i], testServer->child->next->next->child->valuestring[i]);
}
cJSON_Delete(testServer);
}
TEST_CASE("J2S") {
char originServer[] = R"({"id":3, "name":"dhcpServer", "message":{"info":"opendhcpd"}})";
cJSON *testServer = cJSON_Parse(originServer);
s2j_create_struct_obj(pServer, Service);
s2j_struct_get_basic_element(pServer, testServer, int, id);
s2j_struct_get_basic_element(pServer, testServer, string, name);
s2j_struct_get_struct_element(pMsg, pServer, testInfo, testServer, Message, msg);
s2j_struct_get_basic_element(pMsg, testInfo, string, info);
CHECK_EQ(pServer->id, 3);
cJSON_Delete(testServer);
free(pServer);
}
}