snoop.h/main.cpp: Add first tests for PostReporter class
Add unit tests for PostReporter DBus object path, reading, and updating.
Move the class definition to the header file such that the
tests can access it.
Tested: unit tests pass.
Change-Id: I4d3571ccd5455297d6b02ed74ed789fe05e40979
Signed-off-by: Kun Yi <kunyi731@gmail.com>
diff --git a/test/post_reporter_test.cpp b/test/post_reporter_test.cpp
index 738982d..d1a158e 100644
--- a/test/post_reporter_test.cpp
+++ b/test/post_reporter_test.cpp
@@ -1,10 +1,82 @@
+#include <gmock/gmock.h>
#include <gtest/gtest.h>
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/test/sdbus_mock.hpp>
+#include "lpcsnoop/snoop.hpp"
+
+using ::testing::IsNull;
+using ::testing::NiceMock;
+using ::testing::Return;
+using ::testing::StrEq;
+using ::testing::_;
+
+namespace
+{
+
+// Fixture for testing class PostReporter
class PostReporterTest : public ::testing::Test
{
+ protected:
+ PostReporterTest() : bus_mock(), bus(sdbusplus::get_mocked_new(&bus_mock))
+ {
+ }
+
+ ~PostReporterTest()
+ {
+ }
+
+ NiceMock<sdbusplus::SdBusMock> bus_mock;
+ sdbusplus::bus::bus bus;
};
-TEST(PostReporterTest, DummyTest)
+TEST_F(PostReporterTest, EmitsObjectsOnExpectedDbusPath)
{
- EXPECT_EQ(1, 1);
+
+ EXPECT_CALL(bus_mock,
+ sd_bus_emit_object_added(IsNull(), StrEq(SNOOP_OBJECTPATH)))
+ .WillOnce(Return(0));
+
+ PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
+ testReporter.emit_object_added();
}
+
+TEST_F(PostReporterTest, AddsObjectWithExpectedName)
+{
+ EXPECT_CALL(bus_mock,
+ sd_bus_add_object_vtable(IsNull(), _, StrEq(SNOOP_OBJECTPATH),
+ StrEq(SNOOP_BUSNAME), _, _))
+ .WillOnce(Return(0));
+
+ PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
+}
+
+TEST_F(PostReporterTest, ValueReadsDefaultToZero)
+{
+ PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
+ EXPECT_EQ(0, testReporter.value());
+}
+
+TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
+{
+ PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
+ testReporter.value(65537);
+ EXPECT_EQ(65537, testReporter.value());
+}
+
+TEST_F(PostReporterTest, SetValueMultipleTimesWorks)
+{
+ PostReporter testReporter(bus, SNOOP_OBJECTPATH, true);
+ testReporter.value(123);
+ EXPECT_EQ(123, testReporter.value());
+ testReporter.value(456);
+ EXPECT_EQ(456, testReporter.value());
+ testReporter.value(0);
+ EXPECT_EQ(0, testReporter.value());
+ testReporter.value(456);
+ EXPECT_EQ(456, testReporter.value());
+ testReporter.value(456);
+ EXPECT_EQ(456, testReporter.value());
+}
+
+} // namespace