enums.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "enums.h"
  2. #include "enums_with_header.h"
  3. #include <tools/enum_parser/parse_enum/ut/enums_with_header.h_serialized.h>
  4. #include "including_header.h"
  5. // just to test that generated stuff works
  6. #include <util/generic/serialized_enum.h>
  7. #include <library/cpp/testing/unittest/registar.h>
  8. #include <util/generic/ptr.h>
  9. #include <util/generic/singleton.h>
  10. void FunctionUsingEFwdEnum(EFwdEnum) {
  11. }
  12. class TEnumSerializationInitializer {
  13. public:
  14. TEnumSerializationInitializer() {
  15. UNIT_ASSERT_VALUES_EQUAL(ToString(EDestructionPriorityTest::first), "first");
  16. }
  17. ~TEnumSerializationInitializer() {
  18. UNIT_ASSERT_VALUES_EQUAL(ToString(EDestructionPriorityTest::second), "second");
  19. }
  20. };
  21. class TEnumSerializationInitializerHolder {
  22. public:
  23. TEnumSerializationInitializerHolder() {
  24. }
  25. ~TEnumSerializationInitializerHolder() {
  26. }
  27. void Init() { Ptr.Reset(new TEnumSerializationInitializer); }
  28. private:
  29. THolder<TEnumSerializationInitializer> Ptr;
  30. };
  31. Y_UNIT_TEST_SUITE(TEnumGeneratorTest) {
  32. template<typename T>
  33. void CheckToString(const T& value, const TString& strValue) {
  34. UNIT_ASSERT_VALUES_EQUAL(ToString(value), strValue);
  35. }
  36. Y_UNIT_TEST(ToStringTest) {
  37. // ESimple
  38. CheckToString(Http, "Http");
  39. CheckToString(Https, "Https");
  40. CheckToString(ItemCount, "ItemCount");
  41. // ESimpleWithComma
  42. CheckToString(ESimpleWithComma::Http, "Http");
  43. CheckToString(ESimpleWithComma::Https, "Https");
  44. CheckToString(ESimpleWithComma::Http2, "Http"); // Http2 is an alias for Http
  45. CheckToString(ESimpleWithComma::ItemCount, "ItemCount");
  46. // ECustomAliases
  47. CheckToString(CAHttp, "http");
  48. CheckToString(CAHttps, "https");
  49. CheckToString(CAItemCount, "CAItemCount");
  50. // EMultipleAliases
  51. CheckToString(MAHttp, "http://");
  52. CheckToString(MAHttps, "https://");
  53. CheckToString(MAItemCount, "MAItemCount");
  54. // EDuplicateKeys
  55. CheckToString(Key0, "Key0");
  56. CheckToString(Key0Second, "Key0"); // obtain FIRST encountered value with such integer key
  57. CheckToString(Key1, "Key1");
  58. CheckToString(Key2, "k2");
  59. CheckToString(Key3, "k2"); // we CANNOT obtain "k3" here (as Key3 == Key2)
  60. }
  61. template<typename T>
  62. void CheckFromString(const TString& strValue, const T& value) {
  63. UNIT_ASSERT_VALUES_EQUAL(static_cast<int>(FromString<T>(TStringBuf(strValue))), static_cast<int>(value));
  64. }
  65. template<typename T>
  66. void CheckFromStringFail(const TString& strValue) {
  67. UNIT_ASSERT_EXCEPTION(FromString<T>(TStringBuf(strValue)), yexception);
  68. }
  69. template<typename T>
  70. void CheckTryFromString(const TString& strValue, const T& value) {
  71. T x;
  72. UNIT_ASSERT_VALUES_EQUAL(TryFromString(TStringBuf(strValue), x), true);
  73. UNIT_ASSERT_VALUES_EQUAL(x, value);
  74. }
  75. template<typename T>
  76. void CheckTryFromStringFail(const TString& strValue) {
  77. T x = T(-666);
  78. UNIT_ASSERT_VALUES_EQUAL(TryFromString(TStringBuf(strValue), x), false);
  79. UNIT_ASSERT_VALUES_EQUAL(int(x), -666);
  80. }
  81. Y_UNIT_TEST(TryFromStringTest) {
  82. // ESimple
  83. CheckFromString("Http", Http);
  84. CheckFromString("Https", Https);
  85. CheckFromString("ItemCount", ItemCount);
  86. CheckFromStringFail<ESimple>("ItemC0unt");
  87. CheckTryFromString("Http", Http);
  88. CheckTryFromString("Https", Https);
  89. CheckTryFromString("ItemCount", ItemCount);
  90. CheckTryFromStringFail<ESimple>("ItemC0unt");
  91. // ESimpleWithComma
  92. CheckTryFromString("Http", ESimpleWithComma::Http);
  93. CheckTryFromString("Https", ESimpleWithComma::Https);
  94. CheckTryFromString("ItemCount", ESimpleWithComma::ItemCount);
  95. CheckTryFromStringFail<ESimpleWithComma>("");
  96. // ECustomAliases
  97. CheckTryFromString("http", CAHttp);
  98. CheckTryFromString("https", CAHttps);
  99. CheckTryFromString("CAItemCount", CAItemCount);
  100. // EDuplicateKeys
  101. CheckTryFromString("Key0", Key0);
  102. CheckTryFromString("Key0Second", Key0Second);
  103. CheckTryFromString("Key1", Key1);
  104. CheckTryFromString("k2", Key2);
  105. CheckTryFromString("k2.1", Key2);
  106. CheckTryFromString("k3", Key3);
  107. }
  108. Y_UNIT_TEST(AllNamesValuesTest) {
  109. {
  110. auto allNames = GetEnumAllCppNames<EDuplicateKeys>();
  111. UNIT_ASSERT(!!allNames);
  112. UNIT_ASSERT_VALUES_EQUAL(allNames.size(), 5u);
  113. UNIT_ASSERT_VALUES_EQUAL(allNames[4], "Key3");
  114. }
  115. {
  116. auto allNames = GetEnumAllCppNames<ESimpleWithComma>();
  117. UNIT_ASSERT(!!allNames);
  118. UNIT_ASSERT_VALUES_EQUAL(allNames.size(), 4u);
  119. UNIT_ASSERT_VALUES_EQUAL(allNames[1], "ESimpleWithComma::Http2");
  120. }
  121. }
  122. Y_UNIT_TEST(EnumWithHeaderTest) {
  123. UNIT_ASSERT_VALUES_EQUAL(GetEnumItemsCount<EWithHeader>(), 3);
  124. }
  125. Y_UNIT_TEST(AllNamesValuesWithHeaderTest) {
  126. {
  127. auto allNames = GetEnumAllCppNames<EWithHeader>();
  128. UNIT_ASSERT_VALUES_EQUAL(allNames.size(), 3u);
  129. UNIT_ASSERT_VALUES_EQUAL(allNames.at(2), "HThree");
  130. }
  131. {
  132. UNIT_ASSERT_VALUES_EQUAL(GetEnumAllNames<EWithHeader>(), "'one', 'HTwo', 'HThree'");
  133. }
  134. }
  135. Y_UNIT_TEST(AllValuesTest) {
  136. const auto& allNames = GetEnumNames<EWithHeader>();
  137. const auto& allValues = GetEnumAllValues<EWithHeader>();
  138. UNIT_ASSERT_VALUES_EQUAL(allValues.size(), 3u);
  139. UNIT_ASSERT_VALUES_EQUAL(allValues[2], HThree);
  140. size_t size = 0;
  141. for (const EWithHeader value : GetEnumAllValues<EWithHeader>()) {
  142. size += 1;
  143. UNIT_ASSERT_VALUES_EQUAL(allNames.contains(value), true);
  144. }
  145. UNIT_ASSERT_VALUES_EQUAL(size, 3u);
  146. }
  147. Y_UNIT_TEST(EnumNamesTest) {
  148. const auto& names = GetEnumNames<EWithHeader>();
  149. UNIT_ASSERT_VALUES_EQUAL(names.size(), 3u);
  150. UNIT_ASSERT(names.contains(HOne));
  151. UNIT_ASSERT_VALUES_EQUAL(names.at(HOne), "one");
  152. UNIT_ASSERT(names.contains(HTwo));
  153. UNIT_ASSERT_VALUES_EQUAL(names.at(HTwo), "HTwo");
  154. UNIT_ASSERT(names.contains(HThree));
  155. UNIT_ASSERT_VALUES_EQUAL(names.at(HThree), "HThree");
  156. }
  157. Y_UNIT_TEST(ToStringBufHeaderTest) {
  158. UNIT_ASSERT_VALUES_EQUAL(ToStringBuf(HOne), "one"sv);
  159. UNIT_ASSERT_VALUES_EQUAL(ToStringBuf(HTwo), "HTwo"sv);
  160. }
  161. Y_UNIT_TEST(EnumSerializerDestructionPriority) {
  162. Singleton<TEnumSerializationInitializerHolder>()->Init();
  163. }
  164. };