enums.cpp 6.8 KB

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