Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 1 | #include "helpers.hpp" |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 2 | #include "utils/conversion.hpp" |
| 3 | |
| 4 | #include <gmock/gmock.h> |
| 5 | |
| 6 | using namespace testing; |
| 7 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 8 | class TestConversion : public Test |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 9 | { |
| 10 | public: |
| 11 | enum class Enum |
| 12 | { |
| 13 | zero = 0, |
| 14 | one, |
| 15 | two |
| 16 | }; |
| 17 | |
| 18 | Enum toEnum(int x) |
| 19 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 20 | return utils::toEnum<Enum, utils::minEnumValue(convDataEnum), |
| 21 | utils::maxEnumValue(convDataEnum)>(x); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 22 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 23 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 24 | Enum toEnum(const std::string& value) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 25 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 26 | return utils::toEnum(convDataEnum, value); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 27 | } |
| 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, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 41 | TEST_F(TestConversion, passValueInRangeExpectToGetValidOutput) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 42 | { |
| 43 | EXPECT_EQ(toEnum(0), Enum::zero); |
| 44 | EXPECT_EQ(toEnum(2), Enum::two); |
| 45 | } |
| 46 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 47 | TEST_F(TestConversion, passInvalidValueExpectToThrowException) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 48 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 49 | EXPECT_THROW(toEnum(-1), sdbusplus::exception::SdBusError); |
| 50 | EXPECT_THROW(toEnum(3), sdbusplus::exception::SdBusError); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 51 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 52 | |
| 53 | TEST_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 | |
| 60 | TEST_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 | |
| 67 | TEST_F(TestConversion, convertsStringToEnum) |
| 68 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 69 | EXPECT_THAT(toEnum("one"), Eq(Enum::one)); |
| 70 | EXPECT_THAT(toEnum("two"), Eq(Enum::two)); |
| 71 | EXPECT_THAT(toEnum("zero"), Eq(Enum::zero)); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TEST_F(TestConversion, enumToStringThrowsWhenUknownEnumPassed) |
| 75 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 76 | EXPECT_THROW(enumToString(static_cast<Enum>(77)), |
| 77 | sdbusplus::exception::SdBusError); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 80 | TEST_F(TestConversion, toEnumThrowsWhenUknownStringPassed) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 81 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 82 | EXPECT_THROW(toEnum("four"), sdbusplus::exception::SdBusError); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 83 | } |