Skip to content

Commit 546ae16

Browse files
committed
feat: add comprehensive test suite for mjsonrpc with various test cases
1 parent 40ebd01 commit 546ae16

File tree

8 files changed

+451
-286
lines changed

8 files changed

+451
-286
lines changed

test/CMakeLists.txt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
project(mjsonrpc_tests)
22

3-
add_executable(basic_test basic_test.c)
4-
target_link_libraries(basic_test PRIVATE unity mjsonrpc)
3+
add_executable(client_test client_test.c)
4+
target_link_libraries(client_test PRIVATE unity mjsonrpc)
55

6-
add_executable(extra_test extra_test.c)
7-
target_link_libraries(extra_test PRIVATE unity mjsonrpc)
6+
add_executable(arry_test arry_test.c)
7+
target_link_libraries(arry_test PRIVATE unity mjsonrpc)
88

9-
add_test(NAME basic_test COMMAND basic_test)
10-
add_test(NAME extra_test COMMAND extra_test)
9+
add_executable(err_test err_test.c)
10+
target_link_libraries(err_test PRIVATE unity mjsonrpc)
11+
12+
add_executable(obj_test obj_test.c)
13+
target_link_libraries(obj_test PRIVATE unity mjsonrpc)
14+
15+
add_executable(mem_test mem_test.c)
16+
target_link_libraries(mem_test PRIVATE unity mjsonrpc)
17+
18+
add_test(NAME client_test COMMAND client_test)
19+
add_test(NAME arry_test COMMAND arry_test)
20+
add_test(NAME err_test COMMAND err_test)
21+
add_test(NAME obj_test COMMAND obj_test)
22+
add_test(NAME mem_test COMMAND mem_test)

test/arry_test.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// mjsonrpc 数组参数与返回值相关单元测试
2+
#include "unity.h"
3+
#include "mjsonrpc.h"
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
void setUp(void) {}
8+
void tearDown(void) {}
9+
10+
// 测试数组参数的加法
11+
static cJSON* sum_array_func(mjrpc_func_ctx_t* ctx, cJSON* params, cJSON* id)
12+
{
13+
if (!params || !cJSON_IsArray(params))
14+
{
15+
ctx->error_code = JSON_RPC_CODE_INVALID_PARAMS;
16+
ctx->error_message = strdup("params must be array");
17+
return NULL;
18+
}
19+
int sum = 0;
20+
int n = cJSON_GetArraySize(params);
21+
for (int i = 0; i < n; ++i)
22+
{
23+
cJSON* item = cJSON_GetArrayItem(params, i);
24+
if (!cJSON_IsNumber(item))
25+
{
26+
ctx->error_code = JSON_RPC_CODE_INVALID_PARAMS;
27+
ctx->error_message = strdup("array element not number");
28+
return NULL;
29+
}
30+
sum += item->valueint;
31+
}
32+
return cJSON_CreateNumber(sum);
33+
}
34+
35+
void test_sum_array(void)
36+
{
37+
mjrpc_handle_t* h = mjrpc_create_handle(8);
38+
mjrpc_add_method(h, sum_array_func, "sum_array", NULL);
39+
// 正常数组
40+
cJSON* arr = cJSON_CreateIntArray((int[]) {1, 2, 3, 4}, 4);
41+
cJSON* id = cJSON_CreateNumber(1);
42+
cJSON* req = mjrpc_request_cjson("sum_array", arr, id);
43+
int code = -1;
44+
cJSON* resp = mjrpc_process_cjson(h, req, &code);
45+
TEST_ASSERT_NOT_NULL(resp);
46+
cJSON* result = cJSON_GetObjectItem(resp, "result");
47+
TEST_ASSERT_TRUE(cJSON_IsNumber(result));
48+
TEST_ASSERT_EQUAL_INT(10, result->valueint);
49+
cJSON_Delete(resp);
50+
mjrpc_destroy_handle(h);
51+
}
52+
53+
void test_sum_array_empty(void)
54+
{
55+
mjrpc_handle_t* h = mjrpc_create_handle(8);
56+
mjrpc_add_method(h, sum_array_func, "sum_array", NULL);
57+
cJSON* arr = cJSON_CreateArray();
58+
cJSON* id = cJSON_CreateNumber(2);
59+
cJSON* req = mjrpc_request_cjson("sum_array", arr, id);
60+
int code = -1;
61+
cJSON* resp = mjrpc_process_cjson(h, req, &code);
62+
TEST_ASSERT_NOT_NULL(resp);
63+
cJSON* result = cJSON_GetObjectItem(resp, "result");
64+
TEST_ASSERT_TRUE(cJSON_IsNumber(result));
65+
TEST_ASSERT_EQUAL_INT(0, result->valueint);
66+
cJSON_Delete(resp);
67+
mjrpc_destroy_handle(h);
68+
}
69+
70+
void test_sum_array_type_error(void)
71+
{
72+
mjrpc_handle_t* h = mjrpc_create_handle(8);
73+
mjrpc_add_method(h, sum_array_func, "sum_array", NULL);
74+
cJSON* arr = cJSON_CreateArray();
75+
cJSON_AddItemToArray(arr, cJSON_CreateString("notnum"));
76+
cJSON* id = cJSON_CreateNumber(3);
77+
cJSON* req = mjrpc_request_cjson("sum_array", arr, id);
78+
int code = -1;
79+
cJSON* resp = mjrpc_process_cjson(h, req, &code);
80+
TEST_ASSERT_NOT_NULL(resp);
81+
cJSON* error = cJSON_GetObjectItem(resp, "error");
82+
TEST_ASSERT_NOT_NULL(error);
83+
cJSON* code_item = cJSON_GetObjectItem(error, "code");
84+
TEST_ASSERT_EQUAL_INT(JSON_RPC_CODE_INVALID_PARAMS, code_item->valueint);
85+
cJSON_Delete(resp);
86+
mjrpc_destroy_handle(h);
87+
}
88+
89+
int main(void)
90+
{
91+
UNITY_BEGIN();
92+
RUN_TEST(test_sum_array);
93+
RUN_TEST(test_sum_array_empty);
94+
RUN_TEST(test_sum_array_type_error);
95+
return UNITY_END();
96+
}

test/basic_test.c

Lines changed: 0 additions & 183 deletions
This file was deleted.

0 commit comments

Comments
 (0)