From 40662b33aa333f82d5a12609cdbff6f42cf22288 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 30 Nov 2023 21:43:51 +0100 Subject: [PATCH] tcp_cmd add testing of stdout both if moc's in DEBUG mode or not --- tests/CMakeLists.txt | 1 + tests/tcp_cmd.cpp | 120 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0a9d66c..4fa5846 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable(tests) target_compile_definitions(tests PRIVATE "TESTING" + "DEBUG" ) target_sources(tests diff --git a/tests/tcp_cmd.cpp b/tests/tcp_cmd.cpp index 3885f46..5f95645 100644 --- a/tests/tcp_cmd.cpp +++ b/tests/tcp_cmd.cpp @@ -11,58 +11,178 @@ extern "C" { TEST(TCP_CMD, tcp_data_cb) { char* cmd = (char*)calloc(50, 1); + std::string output; pbuf p = {.next = NULL, .payload = (void*)cmd, .tot_len = 4, .len = 0, .type_internal = 0, .flags = 0, .if_idx = 0}; strcpy(cmd, "help"); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "help : shows a list of commands\r\n" + "text \"\" : puts text on the lcd\r\n" + "color : set the background color of the lcd\r\n" + "textColor : set the color of the text\r\n" + "listImages : shows a list with images in the filesystem\r\n" + "setImage : put an image on the screen\r\n" + "setGift : put a gif on the screen\r\n" + "exit : closes the connection\r\n\n$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\ntcp_write:\nhelp : shows a list of commands\r\n" + "text \"\" : puts text on the lcd\r\n" + "color : set the background color of the lcd\r\n" + "textColor : set the color of the text\r\n" + "listImages : shows a list with images in the filesystem\r\n" + "setImage : put an image on the screen\r\n" + "setGift : put a gif on the screen\r\n" + "exit : closes the connection\r\n\n" + "tcp_output\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "text \"This is printed on the display\""); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\nlcd_clear_text\nlcd_display_text @ 10 10 with color 0xFF000000 bg color 0xFF000000\nThis is printed on the display\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "color 0x555555"); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "bgcolor 0xAAAAAA"); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "text \"This is printed on the display\""); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\nlcd_clear_text\nlcd_display_text @ 10 10 with color 0xFF555555 bg color 0xFFAAAAAA\nThis is printed on the display\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "color 255 255 255"); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "bgcolor 255 255 255"); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "text \"This is printed on the display\""); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\nlcd_clear_text\nlcd_display_text @ 10 10 with color 0xFFFFFFFF bg color 0xFFFFFFFF\nThis is printed on the display\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "setImage \"test.bmp\""); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\nlcd_clear_images\nlcd_draw_img_from_fs\ntest.bmp @0 0\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "setImage \"test.gif\""); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "File is not a bmp\n\n$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\ntcp_write:\nFile is not a bmp\n\ntcp_output\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "setGif \"test.gif\""); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\nlcd_clear_images\nlcd_draw_gif_from_fs\ntest.gif @0 0\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "setGif \"test.bmp\""); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "File is not a gif\n\n$> \n"); + #else + EXPECT_EQ(output, "tcp_recved\ntcp_write:\nFile is not a gif\n\ntcp_output\ntcp_write:\n$> \ntcp_output\npbuf_free\n"); + #endif strcpy(cmd, "exit"); p.tot_len = (uint16_t)strlen(cmd); + + testing::internal::CaptureStdout(); tcp_cmd_recv(NULL, NULL, &p, ERR_OK); + output = testing::internal::GetCapturedStdout(); + #ifndef DEBUG + EXPECT_EQ(output, "Exiting...\n\n"); + #else + EXPECT_EQ(output, "tcp_recved\ntcp_write:\nExiting...\n\ntcp_output\nlcd_clear_images\nlcd_clear_text\ntcp_close\ntcp_arg\ntcp_sent\ntcp_recv\ntcp_close\npbuf_free\n"); + #endif free(cmd); } \ No newline at end of file