ST_engine  0.3-ALPHA
console_tests.cpp
1 /* This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #include <gtest/gtest.h>
11 #include "../../../ST_message_bus/include/message_bus.hpp"
12 #include <console.hpp>
13 
15 class console_test : public ::testing::Test {
16 
17 protected:
18  console *test_cnsl{};
19 
20  uint8_t get_log_level() {
21  return test_cnsl->log_level;
22  }
23 
24  void write(ST::log_type type, const std::string &text) {
25  test_cnsl->write(text, type);
26  }
27 
28  message_bus *msg_bus{};
29 
30  void SetUp() override {
31  msg_bus = new message_bus();
32  test_cnsl = new console(*msg_bus);
33  }
34 
35  void TearDown() override {
36  delete test_cnsl;
37  delete msg_bus;
38  }
39 };
40 
41 TEST_F(console_test, set_log_level) {
42  test_cnsl->set_log_level(ST::log_type::ERROR);
43  EXPECT_EQ(static_cast<uint8_t>(ST::log_type::ERROR), get_log_level());
44 
45  test_cnsl->set_log_level(ST::log_type::INFO);
46  EXPECT_EQ(static_cast<uint8_t>(ST::log_type::INFO), get_log_level());
47 
48  test_cnsl->set_log_level(ST::log_type::SUCCESS);
49  EXPECT_EQ(static_cast<uint8_t>(ST::log_type::SUCCESS), get_log_level());
50 
51  test_cnsl->set_log_level(ST::log_type::SUCCESS | ST::log_type::ERROR);
52  EXPECT_EQ(static_cast<uint8_t>(ST::log_type::SUCCESS | ST::log_type::ERROR), get_log_level());
53 
54  test_cnsl->set_log_level(ST::log_type::INFO | ST::log_type::ERROR);
55  EXPECT_EQ(static_cast<uint8_t>(ST::log_type::INFO | ST::log_type::ERROR), get_log_level());
56 
57  test_cnsl->set_log_level(ST::log_type::INFO | ST::log_type::SUCCESS);
58  EXPECT_EQ(static_cast<uint8_t>(ST::log_type::INFO | ST::log_type::SUCCESS), get_log_level());
59 
60  test_cnsl->set_log_level(ST::log_type::INFO | ST::log_type::SUCCESS | ST::log_type::ERROR);
61  EXPECT_EQ(static_cast<uint8_t>(ST::log_type::INFO | ST::log_type::SUCCESS | ST::log_type::ERROR), get_log_level());
62 }
63 
64 TEST_F(console_test, console_write_error) {
65  ::testing::internal::CaptureStderr();
66  ::testing::internal::CaptureStdout();
67  test_cnsl->set_log_level(ST::log_type::ERROR);
68  msg_bus->send_msg(new message(LOG_ERROR, make_data<std::string>("TEST_STRING")));
69  msg_bus->send_msg(new message(LOG_INFO, make_data<std::string>("TEST_STRING2")));
70  msg_bus->send_msg(new message(LOG_SUCCESS, make_data<std::string>("TEST_STRING3")));
71  test_cnsl->update();
72  ASSERT_EQ("TEST_STRING\n", testing::internal::GetCapturedStderr());
73  ASSERT_EQ("", testing::internal::GetCapturedStdout());
74 }
75 
76 TEST_F(console_test, console_write_info) {
77  ::testing::internal::CaptureStdout();
78  ::testing::internal::CaptureStderr();
79  test_cnsl->set_log_level(ST::log_type::INFO);
80  msg_bus->send_msg(new message(LOG_ERROR, make_data<std::string>("TEST_STRING")));
81  msg_bus->send_msg(new message(LOG_INFO, make_data<std::string>("TEST_STRING2")));
82  msg_bus->send_msg(new message(LOG_SUCCESS, make_data<std::string>("TEST_STRING3")));
83  test_cnsl->update();
84  ASSERT_EQ("TEST_STRING2\n", testing::internal::GetCapturedStdout());
85  ASSERT_EQ("", testing::internal::GetCapturedStderr());
86 }
87 
88 TEST_F(console_test, console_write_success) {
89  ::testing::internal::CaptureStdout();
90  ::testing::internal::CaptureStderr();
91  test_cnsl->set_log_level(ST::log_type::SUCCESS);
92  msg_bus->send_msg(new message(LOG_ERROR, make_data<std::string>("TEST_STRING")));
93  msg_bus->send_msg(new message(LOG_INFO, make_data<std::string>("TEST_STRING2")));
94  msg_bus->send_msg(new message(LOG_SUCCESS, make_data<std::string>("TEST_STRING3")));
95  test_cnsl->update();
96  ASSERT_EQ("TEST_STRING3\n", testing::internal::GetCapturedStdout());
97  ASSERT_EQ("", testing::internal::GetCapturedStderr());
98 }
99 
100 TEST_F(console_test, console_write_error_info) {
101  ::testing::internal::CaptureStdout();
102  ::testing::internal::CaptureStderr();
103  test_cnsl->set_log_level(ST::log_type::ERROR | ST::log_type::INFO);
104  msg_bus->send_msg(new message(LOG_ERROR, make_data<std::string>("TEST_STRING")));
105  msg_bus->send_msg(new message(LOG_INFO, make_data<std::string>("TEST_STRING2")));
106  msg_bus->send_msg(new message(LOG_SUCCESS, make_data<std::string>("TEST_STRING3")));
107  test_cnsl->update();
108  ASSERT_EQ("TEST_STRING2\n", testing::internal::GetCapturedStdout());
109  ASSERT_EQ("TEST_STRING\n", testing::internal::GetCapturedStderr());
110 }
111 
112 TEST_F(console_test, console_write_error_success) {
113  ::testing::internal::CaptureStdout();
114  ::testing::internal::CaptureStderr();
115  test_cnsl->set_log_level(ST::log_type::ERROR | ST::log_type::SUCCESS);
116  msg_bus->send_msg(new message(LOG_ERROR, make_data<std::string>("TEST_STRING")));
117  msg_bus->send_msg(new message(LOG_INFO, make_data<std::string>("TEST_STRING2")));
118  msg_bus->send_msg(new message(LOG_SUCCESS, make_data<std::string>("TEST_STRING3")));
119  test_cnsl->update();
120  ASSERT_EQ("TEST_STRING3\n", testing::internal::GetCapturedStdout());
121  ASSERT_EQ("TEST_STRING\n", testing::internal::GetCapturedStderr());
122 }
123 
124 TEST_F(console_test, console_write_info_success) {
125  ::testing::internal::CaptureStdout();
126  ::testing::internal::CaptureStderr();
127  test_cnsl->set_log_level(ST::log_type::INFO | ST::log_type::SUCCESS);
128  msg_bus->send_msg(new message(LOG_ERROR, make_data<std::string>("TEST_STRING")));
129  msg_bus->send_msg(new message(LOG_INFO, make_data<std::string>("TEST_STRING2")));
130  msg_bus->send_msg(new message(LOG_SUCCESS, make_data<std::string>("TEST_STRING3")));
131  test_cnsl->update();
132  ASSERT_EQ("TEST_STRING2\nTEST_STRING3\n", testing::internal::GetCapturedStdout());
133  ASSERT_EQ("", testing::internal::GetCapturedStderr());
134 }
135 
136 TEST_F(console_test, console_write_all) {
137  ::testing::internal::CaptureStdout();
138  ::testing::internal::CaptureStderr();
139 
140  test_cnsl->set_log_level(ST::log_type::SUCCESS | ST::log_type::INFO | ST::log_type::ERROR);
141 
142  write(ST::log_type::INFO, "TEST_STRING");
143  write(ST::log_type::ERROR, "TEST_STRING2");
144  write(ST::log_type::SUCCESS, "TEST_STRING3");
145 
146  ASSERT_EQ("TEST_STRING\nTEST_STRING3\n", testing::internal::GetCapturedStdout());
147  ASSERT_EQ("TEST_STRING2\n", testing::internal::GetCapturedStderr());
148 
149 }
150 
151 int main(int argc, char **argv) {
152  ::testing::InitGoogleTest(&argc, argv);
153  return RUN_ALL_TESTS();
154 }
Tests fixture for the console.
This object represents the console window.
Definition: console.hpp:17
The central messaging system of the engine. All subsystem make extensive use of it.
Definition: message_bus.hpp:29
A message object passed around in the message bus. Holds anything created with make_data<>().
Definition: message.hpp:21