PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/common/messaging/MessagePrinterTest.cpp

https://code.google.com/
C++ | 167 lines | 108 code | 29 blank | 30 comment | 0 complexity | e72706cfd53fccdc0d597c86e4017f09 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Library General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * MessagePrinterTest.cpp
  17. * Test fixture for the MessagePrinter class.
  18. * Copyright (C) 2011 Simon Newton
  19. */
  20. #include <cppunit/extensions/HelperMacros.h>
  21. #include <string>
  22. #include <vector>
  23. #include "ola/messaging/Descriptor.h"
  24. #include "ola/messaging/Message.h"
  25. #include "ola/messaging/MessagePrinter.h"
  26. using std::string;
  27. using std::vector;
  28. using ola::messaging::BoolFieldDescriptor;
  29. using ola::messaging::BoolMessageField;
  30. using ola::messaging::FieldDescriptor;
  31. using ola::messaging::FieldDescriptorGroup;
  32. using ola::messaging::GroupMessageField;
  33. using ola::messaging::IPV4FieldDescriptor;
  34. using ola::messaging::IPV4MessageField;
  35. using ola::messaging::Int8FieldDescriptor;
  36. using ola::messaging::Int16FieldDescriptor;
  37. using ola::messaging::Int8MessageField;
  38. using ola::messaging::Int16MessageField;
  39. using ola::messaging::Message;
  40. using ola::messaging::MessageFieldInterface;
  41. using ola::messaging::GenericMessagePrinter;
  42. using ola::messaging::StringFieldDescriptor;
  43. using ola::messaging::StringMessageField;
  44. using ola::messaging::UInt32FieldDescriptor;
  45. using ola::messaging::UInt32MessageField;
  46. using ola::messaging::UInt8FieldDescriptor;
  47. using ola::messaging::UInt8MessageField;
  48. class GenericMessagePrinterTest: public CppUnit::TestFixture {
  49. CPPUNIT_TEST_SUITE(GenericMessagePrinterTest);
  50. CPPUNIT_TEST(testSimplePrinter);
  51. CPPUNIT_TEST(testLabeledPrinter);
  52. CPPUNIT_TEST(testNestedPrinter);
  53. CPPUNIT_TEST_SUITE_END();
  54. public:
  55. GenericMessagePrinterTest() {}
  56. void testSimplePrinter();
  57. void testLabeledPrinter();
  58. void testNestedPrinter();
  59. private:
  60. GenericMessagePrinter m_printer;
  61. };
  62. CPPUNIT_TEST_SUITE_REGISTRATION(GenericMessagePrinterTest);
  63. /*
  64. * Test the MessagePrinter
  65. */
  66. void GenericMessagePrinterTest::testSimplePrinter() {
  67. // setup some fields
  68. BoolFieldDescriptor bool_descriptor("On/Off");
  69. IPV4FieldDescriptor ipv4_descriptor("ip");
  70. StringFieldDescriptor string_descriptor("Name", 0, 32);
  71. UInt32FieldDescriptor uint32_descriptor("Id");
  72. UInt8FieldDescriptor uint8_descriptor("Count", false, -3);
  73. Int8FieldDescriptor int8_descriptor("Delta", false, 1);
  74. Int16FieldDescriptor int16_descriptor("Rate", false, -1);
  75. // try a simple print first
  76. vector<const ola::messaging::MessageFieldInterface*> fields;
  77. fields.push_back(new BoolMessageField(&bool_descriptor, false));
  78. fields.push_back(new IPV4MessageField(&ipv4_descriptor, 0x0100000a));
  79. fields.push_back(new StringMessageField(&string_descriptor, "foobar"));
  80. fields.push_back(new UInt32MessageField(&uint32_descriptor, 42));
  81. fields.push_back(new UInt8MessageField(&uint8_descriptor, 4));
  82. fields.push_back(new Int8MessageField(&int8_descriptor, 10));
  83. fields.push_back(new Int16MessageField(&int16_descriptor, 10));
  84. Message message(fields);
  85. string expected = (
  86. "On/Off: false\nip: 10.0.0.1\nName: foobar\nId: 42\n"
  87. "Count: 4 x 10 ^ -3\nDelta: 10 x 10 ^ 1\nRate: 10 x 10 ^ -1\n");
  88. CPPUNIT_ASSERT_EQUAL(expected, m_printer.AsString(&message));
  89. }
  90. /**
  91. * Check that labels are added
  92. */
  93. void GenericMessagePrinterTest::testLabeledPrinter() {
  94. UInt8FieldDescriptor::IntervalVector intervals;
  95. intervals.push_back(UInt8FieldDescriptor::Interval(0, 2));
  96. UInt8FieldDescriptor::LabeledValues labels;
  97. labels["off"] = 0;
  98. labels["on"] = 1;
  99. labels["auto"] = 2;
  100. UInt8FieldDescriptor uint8_descriptor("State", intervals, labels);
  101. vector<const ola::messaging::MessageFieldInterface*> fields;
  102. fields.push_back(new UInt8MessageField(&uint8_descriptor, 0));
  103. fields.push_back(new UInt8MessageField(&uint8_descriptor, 1));
  104. fields.push_back(new UInt8MessageField(&uint8_descriptor, 2));
  105. Message message(fields);
  106. string expected = "State: off\nState: on\nState: auto\n";
  107. CPPUNIT_ASSERT_EQUAL(expected, m_printer.AsString(&message));
  108. }
  109. void GenericMessagePrinterTest::testNestedPrinter() {
  110. // this holds some information on people
  111. StringFieldDescriptor *string_descriptor = new StringFieldDescriptor(
  112. "Name", 0, 32);
  113. BoolFieldDescriptor *bool_descriptor = new BoolFieldDescriptor("Female");
  114. UInt8FieldDescriptor *uint8_descriptor = new UInt8FieldDescriptor("Age");
  115. vector<const FieldDescriptor*> person_fields;
  116. person_fields.push_back(string_descriptor);
  117. person_fields.push_back(bool_descriptor);
  118. person_fields.push_back(uint8_descriptor);
  119. FieldDescriptorGroup group_descriptor("Person", person_fields, 0, 10);
  120. // setup the first person
  121. vector<const MessageFieldInterface*> person1;
  122. person1.push_back(new StringMessageField(string_descriptor, "Lisa"));
  123. person1.push_back(new BoolMessageField(bool_descriptor, true));
  124. person1.push_back(new UInt8MessageField(uint8_descriptor, 21));
  125. // setup the second person
  126. vector<const MessageFieldInterface*> person2;
  127. person2.push_back(new StringMessageField(string_descriptor, "Simon"));
  128. person2.push_back(new BoolMessageField(bool_descriptor, false));
  129. person2.push_back(new UInt8MessageField(uint8_descriptor, 26));
  130. vector<const ola::messaging::MessageFieldInterface*> messages;
  131. messages.push_back(new GroupMessageField(&group_descriptor, person1));
  132. messages.push_back(new GroupMessageField(&group_descriptor, person2));
  133. Message message(messages);
  134. string expected = (
  135. "Person {\n Name: Lisa\n Female: true\n Age: 21\n}\n"
  136. "Person {\n Name: Simon\n Female: false\n Age: 26\n}\n");
  137. CPPUNIT_ASSERT_EQUAL(expected, m_printer.AsString(&message));
  138. }