Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 1 | #include "utils/conversion.hpp" |
| 2 | |
| 3 | #include <gmock/gmock.h> |
| 4 | |
| 5 | using namespace testing; |
| 6 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 7 | class TestConversion : public Test |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 8 | { |
| 9 | public: |
| 10 | enum class Enum |
| 11 | { |
| 12 | zero = 0, |
| 13 | one, |
| 14 | two |
| 15 | }; |
| 16 | |
| 17 | Enum toEnum(int x) |
| 18 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 19 | return utils::toEnum<Enum, utils::minEnumValue(convDataEnum), |
| 20 | utils::maxEnumValue(convDataEnum)>(x); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 21 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 22 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 23 | Enum toEnum(const std::string& value) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 24 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 25 | return utils::toEnum(convDataEnum, value); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | std::string enumToString(Enum value) |
| 29 | { |
| 30 | return std::string(utils::enumToString(convDataEnum, value)); |
| 31 | } |
| 32 | |
| 33 | static constexpr std::array<std::pair<std::string_view, Enum>, 3> |
| 34 | convDataEnum = { |
| 35 | {std::make_pair<std::string_view, Enum>("zero", Enum::zero), |
| 36 | std::make_pair<std::string_view, Enum>("one", Enum::one), |
| 37 | std::make_pair<std::string_view, Enum>("two", Enum::two)}}; |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 40 | TEST_F(TestConversion, passValueInRangeExpectToGetValidOutput) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 41 | { |
| 42 | EXPECT_EQ(toEnum(0), Enum::zero); |
| 43 | EXPECT_EQ(toEnum(2), Enum::two); |
| 44 | } |
| 45 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 46 | TEST_F(TestConversion, passInvalidValueExpectToThrowException) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 47 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 48 | EXPECT_THROW(toEnum(-1), sdbusplus::exception::SdBusError); |
| 49 | EXPECT_THROW(toEnum(3), sdbusplus::exception::SdBusError); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 50 | } |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 51 | |
| 52 | TEST_F(TestConversion, convertsToUnderlyingType) |
| 53 | { |
| 54 | EXPECT_THAT(utils::toUnderlying(Enum::one), Eq(1)); |
| 55 | EXPECT_THAT(utils::toUnderlying(Enum::two), Eq(2)); |
| 56 | EXPECT_THAT(utils::toUnderlying(Enum::zero), Eq(0)); |
| 57 | } |
| 58 | |
| 59 | TEST_F(TestConversion, convertsEnumToString) |
| 60 | { |
| 61 | EXPECT_THAT(enumToString(Enum::one), Eq("one")); |
| 62 | EXPECT_THAT(enumToString(Enum::two), Eq("two")); |
| 63 | EXPECT_THAT(enumToString(Enum::zero), Eq("zero")); |
| 64 | } |
| 65 | |
| 66 | TEST_F(TestConversion, convertsStringToEnum) |
| 67 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 68 | EXPECT_THAT(toEnum("one"), Eq(Enum::one)); |
| 69 | EXPECT_THAT(toEnum("two"), Eq(Enum::two)); |
| 70 | EXPECT_THAT(toEnum("zero"), Eq(Enum::zero)); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST_F(TestConversion, enumToStringThrowsWhenUknownEnumPassed) |
| 74 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 75 | EXPECT_THROW(enumToString(static_cast<Enum>(77)), |
| 76 | sdbusplus::exception::SdBusError); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 79 | TEST_F(TestConversion, toEnumThrowsWhenUknownStringPassed) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 80 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 81 | EXPECT_THROW(toEnum("four"), sdbusplus::exception::SdBusError); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 82 | } |