hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
main.cpp
Go to the documentation of this file.
1 
14 #include <Poco/SharedPtr.h>
15 #include <gtest/gtest.h>
16 #include <sstream>
17 #include <iostream>
18 
19 #include "LoggerStream.hpp"
20 #include "RealLoggable.hpp"
21 #include "CustomMessage.hpp"
22 
23 // list all messages need to functional objects
24 std::vector<std::pair<unsigned int, std::string> > messages =
25 {
26  {0, "TEST START"},
27  {1, "TEST DATA"},
28  {2, "TEST MSG"},
29  {3, "TEST STOP"}
30 };
31 
32 Poco::SharedPtr<HCE::MessagesCollection> pMessagesCollection;
33 Poco::SharedPtr<HCE::CustomMessage> pCustomMessage;
34 
35 class CustomMessageTest : public ::testing::Test
36 {
37 protected:
38  static void SetUpTestCase(void)
39  {
40  pMessagesCollection = Poco::SharedPtr<HCE::MessagesCollection>(new HCE::MessagesCollection(messages));
41  pCustomMessage = Poco::SharedPtr<HCE::CustomMessage>(new HCE::CustomMessage(*pMessagesCollection));
42  }
43  static void TearDownTestCase(void)
44  {
45  pMessagesCollection = nullptr;
46  pCustomMessage = nullptr;
47  }
48 };
49 
50 TEST(LoggerStreamTest, defaultModeWriteToBuffer)
51 {
52  HCE::LoggerStream logger;
53 
54  // default mode write to buffer
55  logger << "test 1";
56  ASSERT_EQ(logger.logMsg(), "test 1");
57 
58  logger << "TEST 2" << std::endl;
59  ASSERT_EQ(logger.logMsg(), "TEST 2\n");
60 }
61 
62 TEST(LoggerStreamTest, modeWriteToStream)
63 {
64  HCE::LoggerStream logger;
65  std::stringstream str;
66 
67  // below mode write to stream
68  logger.setLogStream(str); // turn on mode write to stream
69  logger.log() << "TEST CONSOLE" << std::endl;
70  ASSERT_EQ(str.str(), "TEST CONSOLE\n");
71 
72  logger.resetLogStream(); // turn off mode write to stream
73  logger.log() << "THIS NEVER BEEN TO STREAM" << std::endl; // message will be write to buffer
74 
75  // again mode write to buffer
76  logger.log() << "test 3" << std::endl;
77  // output buffer content (include "THIS NEVER BEEN TO STREAM")
78  ASSERT_EQ(logger.logMsg(), "THIS NEVER BEEN TO STREAM\ntest 3\n");
79 }
80 
81 TEST(LoggerStreamTest, useEndlMamipulator)
82 {
83  HCE::LoggerStream logger;
84  HCE::tests::RealLoggable realLoggable; // some user implemented class (child of interface class Loggable)
85 
86  // below used HCE::endl manipulator
87  logger << "test 4" << HCE::endl;
88  ASSERT_EQ(logger.logMsg(), "test 4\n");
89 
90  logger.setLoggable(realLoggable); // bind to class describing need behavior
91  logger << "test 5" << std::endl << "test 6" << HCE::endl;
92 
93  std::string msg = logger.logMsg();
94  ASSERT_TRUE(msg.find("test 5")!=std::string::npos && msg.find("test 6")!=std::string::npos);
95 
96  logger.resetLoggable(); // unbind any classes describing need behavior
97  logger << "test 7";
98  ASSERT_EQ(logger.logMsg(), "test 7");
99 }
100 
101 TEST_F(CustomMessageTest, defaultDelimiterTokens)
102 {
104  /* output messages with default delimiter tokens */
105  ASSERT_EQ(msg(0), "TEST START. ");
106  ASSERT_EQ(msg(1, 42), "TEST DATA: 42 ");
107  ASSERT_EQ(msg(2, "USER MESSAGE"), "TEST MSG: USER MESSAGE ");
108  ASSERT_EQ(msg(3), "TEST STOP. ");
109 }
110 
111 TEST_F(CustomMessageTest, customDelimiterTokens)
112 {
114  /* set custom delimiter tokens */
115  HCE::CustomMessage::MsgConst msgConst = { '*', ';', '-'};
116  msg.setMsgConst(msgConst);
117 
118  /* output messages with custom delimiter tokens */
119  ASSERT_EQ(msg(0), "TEST START;*");
120  ASSERT_EQ(msg(1, 42), "TEST DATA-*42*");
121  ASSERT_EQ(msg(2, "USER MESSAGE"), "TEST MSG-*USER MESSAGE*");
122  ASSERT_EQ(msg(3), "TEST STOP;*");
123 }
124 
125 int main (int argc, char** argv)
126 {
127  testing::InitGoogleTest(&argc, argv);
128  return RUN_ALL_TESTS();
129 }