|
| 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 | +} |
0 commit comments