regulators: Add toString() method to Action class
Add a toString() method to the Action class and all child classes.
This method returns a string description of the action. The description
will be used in journal entries and error logs when an action fails
(such as due to an I2C error).
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I083496d23bf9c3df5be1b082650a9def24be1b00
diff --git a/phosphor-regulators/test/actions/and_action_tests.cpp b/phosphor-regulators/test/actions/and_action_tests.cpp
index 65f91e6..3b62273 100644
--- a/phosphor-regulators/test/actions/and_action_tests.cpp
+++ b/phosphor-regulators/test/actions/and_action_tests.cpp
@@ -162,3 +162,12 @@
EXPECT_EQ(andAction.getActions()[0].get(), action1);
EXPECT_EQ(andAction.getActions()[1].get(), action2);
}
+
+TEST(AndActionTests, ToString)
+{
+ std::vector<std::unique_ptr<Action>> actions{};
+ actions.push_back(std::make_unique<MockAction>());
+
+ AndAction andAction{std::move(actions)};
+ EXPECT_EQ(andAction.toString(), "and: [ ... ]");
+}
diff --git a/phosphor-regulators/test/actions/if_action_tests.cpp b/phosphor-regulators/test/actions/if_action_tests.cpp
index 1f67f27..87f2f9e 100644
--- a/phosphor-regulators/test/actions/if_action_tests.cpp
+++ b/phosphor-regulators/test/actions/if_action_tests.cpp
@@ -298,3 +298,36 @@
EXPECT_EQ(ifAction.getElseActions()[0].get(), elseAction1);
EXPECT_EQ(ifAction.getElseActions()[1].get(), elseAction2);
}
+
+TEST(IfActionTests, ToString)
+{
+ // Test where else clause is not specified
+ {
+ std::unique_ptr<Action> conditionAction =
+ std::make_unique<MockAction>();
+
+ std::vector<std::unique_ptr<Action>> thenActions{};
+ thenActions.push_back(std::make_unique<MockAction>());
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions)};
+ EXPECT_EQ(ifAction.toString(),
+ "if: { condition: { ... }, then: [ ... ] }");
+ }
+
+ // Test where else clause is specified
+ {
+ std::unique_ptr<Action> conditionAction =
+ std::make_unique<MockAction>();
+
+ std::vector<std::unique_ptr<Action>> thenActions{};
+ thenActions.push_back(std::make_unique<MockAction>());
+
+ std::vector<std::unique_ptr<Action>> elseActions{};
+ elseActions.push_back(std::make_unique<MockAction>());
+
+ IfAction ifAction{std::move(conditionAction), std::move(thenActions),
+ std::move(elseActions)};
+ EXPECT_EQ(ifAction.toString(),
+ "if: { condition: { ... }, then: [ ... ], else: [ ... ] }");
+ }
+}
diff --git a/phosphor-regulators/test/actions/mock_action.hpp b/phosphor-regulators/test/actions/mock_action.hpp
index 84455dd..bb48960 100644
--- a/phosphor-regulators/test/actions/mock_action.hpp
+++ b/phosphor-regulators/test/actions/mock_action.hpp
@@ -18,6 +18,8 @@
#include "action.hpp"
#include "action_environment.hpp"
+#include <string>
+
#include <gmock/gmock.h>
namespace phosphor::power::regulators
@@ -35,6 +37,7 @@
virtual ~MockAction() = default;
MOCK_METHOD(bool, execute, (ActionEnvironment & environment), (override));
+ MOCK_METHOD(std::string, toString, (), (const, override));
};
} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/actions/not_action_tests.cpp b/phosphor-regulators/test/actions/not_action_tests.cpp
index c53df7f..8e89862 100644
--- a/phosphor-regulators/test/actions/not_action_tests.cpp
+++ b/phosphor-regulators/test/actions/not_action_tests.cpp
@@ -96,3 +96,9 @@
NotAction notAction{std::unique_ptr<Action>{action}};
EXPECT_EQ(notAction.getAction().get(), action);
}
+
+TEST(NotActionTests, ToString)
+{
+ NotAction notAction{std::make_unique<MockAction>()};
+ EXPECT_EQ(notAction.toString(), "not: { ... }");
+}
diff --git a/phosphor-regulators/test/actions/or_action_tests.cpp b/phosphor-regulators/test/actions/or_action_tests.cpp
index 6e33d69..b2c89d1 100644
--- a/phosphor-regulators/test/actions/or_action_tests.cpp
+++ b/phosphor-regulators/test/actions/or_action_tests.cpp
@@ -162,3 +162,12 @@
EXPECT_EQ(orAction.getActions()[0].get(), action1);
EXPECT_EQ(orAction.getActions()[1].get(), action2);
}
+
+TEST(OrActionTests, ToString)
+{
+ std::vector<std::unique_ptr<Action>> actions{};
+ actions.push_back(std::make_unique<MockAction>());
+
+ OrAction orAction{std::move(actions)};
+ EXPECT_EQ(orAction.toString(), "or: [ ... ]");
+}
diff --git a/phosphor-regulators/test/actions/run_rule_action_tests.cpp b/phosphor-regulators/test/actions/run_rule_action_tests.cpp
index be94e52..b688a78 100644
--- a/phosphor-regulators/test/actions/run_rule_action_tests.cpp
+++ b/phosphor-regulators/test/actions/run_rule_action_tests.cpp
@@ -187,3 +187,9 @@
RunRuleAction action{"read_sensors_rule"};
EXPECT_EQ(action.getRuleID(), "read_sensors_rule");
}
+
+TEST(RunRuleActionTests, ToString)
+{
+ RunRuleAction action{"set_voltage_rule"};
+ EXPECT_EQ(action.toString(), "run_rule: set_voltage_rule");
+}
diff --git a/phosphor-regulators/test/actions/set_device_action_tests.cpp b/phosphor-regulators/test/actions/set_device_action_tests.cpp
index 1aa6cfc..c39b9b0 100644
--- a/phosphor-regulators/test/actions/set_device_action_tests.cpp
+++ b/phosphor-regulators/test/actions/set_device_action_tests.cpp
@@ -77,3 +77,9 @@
SetDeviceAction action{"io_expander_0"};
EXPECT_EQ(action.getDeviceID(), "io_expander_0");
}
+
+TEST(SetDeviceActionTests, ToString)
+{
+ SetDeviceAction action{"regulator1"};
+ EXPECT_EQ(action.toString(), "set_device: regulator1");
+}