blob: 6a69e39306729358465bd0296f88cfb37b97eb3d [file] [log] [blame]
Szymon Dompke1cdd7e42022-06-08 14:43:13 +02001#include "helpers.hpp"
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01002#include "utils/conversion.hpp"
3
4#include <gmock/gmock.h>
5
6using namespace testing;
7
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +00008class TestConversion : public Test
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +01009{
10 public:
11 enum class Enum
12 {
13 zero = 0,
14 one,
15 two
16 };
17
18 Enum toEnum(int x)
19 {
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010020 return utils::toEnum<Enum, utils::minEnumValue(convDataEnum),
21 utils::maxEnumValue(convDataEnum)>(x);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010022 }
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000023
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010024 Enum toEnum(const std::string& value)
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000025 {
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010026 return utils::toEnum(convDataEnum, value);
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000027 }
28
29 std::string enumToString(Enum value)
30 {
31 return std::string(utils::enumToString(convDataEnum, value));
32 }
33
34 static constexpr std::array<std::pair<std::string_view, Enum>, 3>
35 convDataEnum = {
36 {std::make_pair<std::string_view, Enum>("zero", Enum::zero),
37 std::make_pair<std::string_view, Enum>("one", Enum::one),
38 std::make_pair<std::string_view, Enum>("two", Enum::two)}};
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010039};
40
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000041TEST_F(TestConversion, passValueInRangeExpectToGetValidOutput)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010042{
43 EXPECT_EQ(toEnum(0), Enum::zero);
44 EXPECT_EQ(toEnum(2), Enum::two);
45}
46
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010047TEST_F(TestConversion, passInvalidValueExpectToThrowException)
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010048{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010049 EXPECT_THROW(toEnum(-1), sdbusplus::exception::SdBusError);
50 EXPECT_THROW(toEnum(3), sdbusplus::exception::SdBusError);
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +010051}
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000052
53TEST_F(TestConversion, convertsToUnderlyingType)
54{
55 EXPECT_THAT(utils::toUnderlying(Enum::one), Eq(1));
56 EXPECT_THAT(utils::toUnderlying(Enum::two), Eq(2));
57 EXPECT_THAT(utils::toUnderlying(Enum::zero), Eq(0));
58}
59
60TEST_F(TestConversion, convertsEnumToString)
61{
62 EXPECT_THAT(enumToString(Enum::one), Eq("one"));
63 EXPECT_THAT(enumToString(Enum::two), Eq("two"));
64 EXPECT_THAT(enumToString(Enum::zero), Eq("zero"));
65}
66
67TEST_F(TestConversion, convertsStringToEnum)
68{
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010069 EXPECT_THAT(toEnum("one"), Eq(Enum::one));
70 EXPECT_THAT(toEnum("two"), Eq(Enum::two));
71 EXPECT_THAT(toEnum("zero"), Eq(Enum::zero));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000072}
73
74TEST_F(TestConversion, enumToStringThrowsWhenUknownEnumPassed)
75{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010076 EXPECT_THROW(enumToString(static_cast<Enum>(77)),
77 sdbusplus::exception::SdBusError);
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000078}
79
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010080TEST_F(TestConversion, toEnumThrowsWhenUknownStringPassed)
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000081{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010082 EXPECT_THROW(toEnum("four"), sdbusplus::exception::SdBusError);
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000083}