From 3e4f0e073ea47cc0dffc285dfeee06ad8cf359d8 Mon Sep 17 00:00:00 2001 From: lixiumei Date: Mon, 14 Feb 2022 17:00:04 +0800 Subject: [PATCH] Add unit test for getting neu_json_tty --- tests/CMakeLists.txt | 8 ++++++++ tests/json_tty_test.cc | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/json_tty_test.cc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 45a8328b..c63c73df 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -43,6 +43,13 @@ target_include_directories(json_group_config_test PRIVATE ) target_link_libraries(json_group_config_test neuron-base gtest_main gtest) +add_executable(json_tty_test json_tty_test.cc ${SRC_PARSER}) +target_include_directories(json_tty_test PRIVATE + ${CMAKE_SOURCE_DIR}/src + ${CMAKE_SOURCE_DIR}/include +) +target_link_libraries(json_tty_test neuron-base gtest_main gtest pthread jansson) + add_executable(variable_test variable_test.cc) target_include_directories(variable_test PRIVATE ${CMAKE_SOURCE_DIR}/src @@ -171,6 +178,7 @@ gtest_discover_tests(json_datatag_table_test) gtest_discover_tests(json_nodes_test) gtest_discover_tests(json_plugin_test) gtest_discover_tests(json_group_config_test) +gtest_discover_tests(json_tty_test) gtest_discover_tests(uuid_test) gtest_discover_tests(schema_valid_test) gtest_discover_tests(http_test) diff --git a/tests/json_tty_test.cc b/tests/json_tty_test.cc new file mode 100644 index 00000000..8193921c --- /dev/null +++ b/tests/json_tty_test.cc @@ -0,0 +1,45 @@ +#include + +#include + +#include "json/neu_json_fn.h" +#include "json/neu_json_tty.h" + +TEST(JsonTtytest, GetTtyEncode) +{ + char *buf = + (char *) "{\"ttys\": [\"/dev/tty0\", \"/dev/tty1\", \"/dev/tty2\", " + "\"/dev/tty3\", \"/dev/tty4\", \"/dev/tty5\", \"/dev/tty6\"]}"; + + char *result = NULL; + + neu_json_get_tty_resp_t resp = { + .n_tty = 7, + }; + + resp.ttys = (neu_json_get_tty_resp_tty_t *) calloc( + 7, sizeof(neu_json_get_tty_resp_tty_t)); + + resp.ttys[0] = strdup((char *) "/dev/tty0"); + resp.ttys[1] = strdup((char *) "/dev/tty1"); + resp.ttys[2] = strdup((char *) "/dev/tty2"); + resp.ttys[3] = strdup((char *) "/dev/tty3"); + resp.ttys[4] = strdup((char *) "/dev/tty4"); + resp.ttys[5] = strdup((char *) "/dev/tty5"); + resp.ttys[6] = strdup((char *) "/dev/tty6"); + + EXPECT_EQ( + 0, neu_json_encode_by_fn(&resp, neu_json_encode_get_tty_resp, &result)); + EXPECT_STREQ(buf, result); + + free(resp.ttys[0]); + free(resp.ttys[1]); + free(resp.ttys[2]); + free(resp.ttys[3]); + free(resp.ttys[4]); + free(resp.ttys[5]); + free(resp.ttys[6]); + free(resp.ttys); + + free(result); +} \ No newline at end of file