sysfs: Refactor LED property parsing
Added new test for parsing led description from sysfs.
Since there are some edge cases that can happen, to make sure the
parsing happens as expected in all cases.
The edge cases primarily come from the different led properties that can
be present or absent in devicetree. I have tested some combinations
thereof and would prefer the label to be generated by led sysfs instead
of manually providing the 3-component label.
However for that to work phosphor-led-sysfs must be able to extract the
labels components in all cases.
This modifies the behavior slightly but it will stay the same
for led names that have 1 or 3 components.
Change-Id: I8def089e4c8dc5d3a341cf6f6b1d6356f5aefe48
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/test/test_led_description.cpp b/test/test_led_description.cpp
new file mode 100644
index 0000000..708794b
--- /dev/null
+++ b/test/test_led_description.cpp
@@ -0,0 +1,92 @@
+/**
+ * Copyright © 2024 9elements
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "interfaces/internal_interface.hpp"
+#include "sysfs.hpp"
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::led;
+
+static LedDescr runtest(const std::string& name)
+{
+ std::string path = devParent + name;
+ SysfsLed led(path);
+ return led.getLedDescr();
+}
+
+TEST(LEDDescr, Has4PartsInvalid)
+{
+ LedDescr d = runtest("devicename:color:function:part4");
+
+ ASSERT_EQ("devicename", d.devicename);
+ ASSERT_EQ("color", d.color);
+ ASSERT_EQ("function", d.function);
+}
+
+TEST(LEDDescr, Has3Parts)
+{
+ LedDescr d = runtest("devicename:color:function");
+
+ ASSERT_EQ("devicename", d.devicename);
+ ASSERT_EQ("color", d.color);
+ ASSERT_EQ("function", d.function);
+}
+
+TEST(LEDDescr, Has2PartsColorFunction)
+{
+ LedDescr d = runtest("red:fault");
+
+ ASSERT_EQ(std::nullopt, d.devicename);
+ ASSERT_EQ("red", d.color);
+ ASSERT_EQ("fault", d.function);
+}
+
+TEST(LEDDescr, Has2PartsDevicenameFunction)
+{
+ LedDescr d = runtest("input9::capslock");
+
+ ASSERT_EQ("input9", d.devicename);
+ ASSERT_EQ(std::nullopt, d.color);
+ ASSERT_EQ("capslock", d.function);
+}
+
+TEST(LEDDescr, Has1PartColor)
+{
+ LedDescr d = runtest("green:");
+
+ ASSERT_EQ(std::nullopt, d.devicename);
+ ASSERT_EQ("green", d.color);
+ ASSERT_EQ(std::nullopt, d.function);
+}
+
+TEST(LEDDescr, Has1PartFunction)
+{
+ LedDescr d = runtest(":boot");
+
+ ASSERT_EQ(std::nullopt, d.devicename);
+ ASSERT_EQ(std::nullopt, d.color);
+ ASSERT_EQ("boot", d.function);
+}
+
+TEST(LEDDescr, Has1PartLabel)
+{
+ LedDescr d = runtest("identify");
+
+ ASSERT_EQ("identify", d.devicename);
+ ASSERT_EQ(std::nullopt, d.color);
+ ASSERT_EQ(std::nullopt, d.function);
+}