registar_ut.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #include <library/cpp/testing/unittest/registar.h>
  2. Y_UNIT_TEST_SUITE(TUnitTestMacroTest) {
  3. Y_UNIT_TEST(Assert) {
  4. auto unitAssert = [] {
  5. UNIT_ASSERT(false);
  6. };
  7. UNIT_ASSERT_TEST_FAILS(unitAssert());
  8. UNIT_ASSERT(true);
  9. }
  10. Y_UNIT_TEST(TypesEqual) {
  11. auto typesEqual = [] {
  12. UNIT_ASSERT_TYPES_EQUAL(int, long);
  13. };
  14. UNIT_ASSERT_TEST_FAILS(typesEqual());
  15. UNIT_ASSERT_TYPES_EQUAL(TString, TString);
  16. }
  17. Y_UNIT_TEST(DoublesEqual) {
  18. auto doublesEqual = [](double d1, double d2, double precision) {
  19. UNIT_ASSERT_DOUBLES_EQUAL(d1, d2, precision);
  20. };
  21. UNIT_ASSERT_TEST_FAILS(doublesEqual(0.0, 0.5, 0.1));
  22. UNIT_ASSERT_TEST_FAILS(doublesEqual(0.1, -0.1, 0.1));
  23. UNIT_ASSERT_DOUBLES_EQUAL(0.0, 0.01, 0.1);
  24. UNIT_ASSERT_DOUBLES_EQUAL(0.01, 0.0, 0.1);
  25. constexpr auto nan = std::numeric_limits<double>::quiet_NaN();
  26. UNIT_ASSERT_TEST_FAILS(doublesEqual(nan, 0.5, 0.1));
  27. UNIT_ASSERT_TEST_FAILS(doublesEqual(0.5, nan, 0.1));
  28. UNIT_ASSERT_DOUBLES_EQUAL(nan, nan, 0.1);
  29. }
  30. Y_UNIT_TEST(StringsEqual) {
  31. auto stringsEqual = [](auto s1, auto s2) {
  32. UNIT_ASSERT_STRINGS_EQUAL(s1, s2);
  33. };
  34. UNIT_ASSERT_TEST_FAILS(stringsEqual("q", "w"));
  35. UNIT_ASSERT_TEST_FAILS(stringsEqual("q", TString("w")));
  36. UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("q"), "w"));
  37. UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("a"), TString("b")));
  38. UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("a"), TStringBuf("b")));
  39. UNIT_ASSERT_TEST_FAILS(stringsEqual("a", TStringBuf("b")));
  40. UNIT_ASSERT_TEST_FAILS(stringsEqual(TStringBuf("a"), "b"));
  41. TString empty;
  42. TStringBuf emptyBuf;
  43. UNIT_ASSERT_STRINGS_EQUAL("", empty);
  44. UNIT_ASSERT_STRINGS_EQUAL(empty, emptyBuf);
  45. UNIT_ASSERT_STRINGS_EQUAL("", static_cast<const char*>(nullptr));
  46. }
  47. Y_UNIT_TEST(StringContains) {
  48. auto stringContains = [](auto s, auto substr) {
  49. UNIT_ASSERT_STRING_CONTAINS(s, substr);
  50. };
  51. UNIT_ASSERT_TEST_FAILS(stringContains("", "a"));
  52. UNIT_ASSERT_TEST_FAILS(stringContains("lurkmore", "moar"));
  53. UNIT_ASSERT_STRING_CONTAINS("", "");
  54. UNIT_ASSERT_STRING_CONTAINS("a", "");
  55. UNIT_ASSERT_STRING_CONTAINS("failure", "fail");
  56. UNIT_ASSERT_STRING_CONTAINS("lurkmore", "more");
  57. }
  58. Y_UNIT_TEST(NoDiff) {
  59. auto noDiff = [](auto s1, auto s2) {
  60. UNIT_ASSERT_NO_DIFF(s1, s2);
  61. };
  62. UNIT_ASSERT_TEST_FAILS(noDiff("q", "w"));
  63. UNIT_ASSERT_TEST_FAILS(noDiff("q", ""));
  64. UNIT_ASSERT_NO_DIFF("", "");
  65. UNIT_ASSERT_NO_DIFF("a", "a");
  66. }
  67. Y_UNIT_TEST(StringsUnequal) {
  68. auto stringsUnequal = [](auto s1, auto s2) {
  69. UNIT_ASSERT_STRINGS_UNEQUAL(s1, s2);
  70. };
  71. UNIT_ASSERT_TEST_FAILS(stringsUnequal("1", "1"));
  72. UNIT_ASSERT_TEST_FAILS(stringsUnequal("", ""));
  73. UNIT_ASSERT_TEST_FAILS(stringsUnequal("42", TString("42")));
  74. UNIT_ASSERT_TEST_FAILS(stringsUnequal(TString("4"), "4"));
  75. UNIT_ASSERT_TEST_FAILS(stringsUnequal("d", TStringBuf("d")));
  76. UNIT_ASSERT_TEST_FAILS(stringsUnequal(TStringBuf("yandex"), "yandex"));
  77. UNIT_ASSERT_TEST_FAILS(stringsUnequal(TStringBuf("index"), TString("index")));
  78. UNIT_ASSERT_TEST_FAILS(stringsUnequal(TString("diff"), TStringBuf("diff")));
  79. UNIT_ASSERT_STRINGS_UNEQUAL("1", "2");
  80. UNIT_ASSERT_STRINGS_UNEQUAL("", "3");
  81. UNIT_ASSERT_STRINGS_UNEQUAL("green", TStringBuf("red"));
  82. UNIT_ASSERT_STRINGS_UNEQUAL(TStringBuf("solomon"), "golovan");
  83. UNIT_ASSERT_STRINGS_UNEQUAL("d", TString("f"));
  84. UNIT_ASSERT_STRINGS_UNEQUAL(TString("yandex"), "index");
  85. UNIT_ASSERT_STRINGS_UNEQUAL(TString("mail"), TStringBuf("yandex"));
  86. UNIT_ASSERT_STRINGS_UNEQUAL(TStringBuf("C++"), TString("python"));
  87. }
  88. Y_UNIT_TEST(Equal) {
  89. auto equal = [](auto v1, auto v2) {
  90. UNIT_ASSERT_EQUAL(v1, v2);
  91. };
  92. UNIT_ASSERT_TEST_FAILS(equal("1", TString("2")));
  93. UNIT_ASSERT_TEST_FAILS(equal(1, 2));
  94. UNIT_ASSERT_TEST_FAILS(equal(42ul, static_cast<unsigned short>(24)));
  95. UNIT_ASSERT_EQUAL("abc", TString("abc"));
  96. UNIT_ASSERT_EQUAL(12l, 12);
  97. UNIT_ASSERT_EQUAL(55, 55);
  98. }
  99. Y_UNIT_TEST(Unequal) {
  100. auto unequal = [](auto v1, auto v2) {
  101. UNIT_ASSERT_UNEQUAL(v1, v2);
  102. };
  103. UNIT_ASSERT_TEST_FAILS(unequal("x", TString("x")));
  104. UNIT_ASSERT_TEST_FAILS(unequal(1, 1));
  105. UNIT_ASSERT_TEST_FAILS(unequal(static_cast<unsigned short>(42), 42ul));
  106. UNIT_ASSERT_UNEQUAL("abc", TString("cba"));
  107. UNIT_ASSERT_UNEQUAL(12l, 10);
  108. UNIT_ASSERT_UNEQUAL(33, 50);
  109. }
  110. Y_UNIT_TEST(LessThan) {
  111. auto lt = [](auto v1, auto v2) {
  112. UNIT_ASSERT_LT(v1, v2);
  113. };
  114. // less than
  115. UNIT_ASSERT_LT(TStringBuf("1"), "2");
  116. UNIT_ASSERT_LT("2", TString("3"));
  117. UNIT_ASSERT_LT("abc", TString("azz"));
  118. UNIT_ASSERT_LT(2, 4);
  119. UNIT_ASSERT_LT(42ul, static_cast<unsigned short>(48));
  120. // equals
  121. UNIT_ASSERT_TEST_FAILS(lt(TStringBuf("2"), "2"));
  122. UNIT_ASSERT_TEST_FAILS(lt("2", TString("2")));
  123. UNIT_ASSERT_TEST_FAILS(lt("abc", TString("abc")));
  124. UNIT_ASSERT_TEST_FAILS(lt(2, 2));
  125. UNIT_ASSERT_TEST_FAILS(lt(42ul, static_cast<unsigned short>(42)));
  126. // greater than
  127. UNIT_ASSERT_TEST_FAILS(lt(TStringBuf("2"), "1"));
  128. UNIT_ASSERT_TEST_FAILS(lt("3", TString("2")));
  129. UNIT_ASSERT_TEST_FAILS(lt("azz", TString("abc")));
  130. UNIT_ASSERT_TEST_FAILS(lt(5, 2));
  131. UNIT_ASSERT_TEST_FAILS(lt(100ul, static_cast<unsigned short>(42)));
  132. }
  133. Y_UNIT_TEST(LessOrEqual) {
  134. auto le = [](auto v1, auto v2) {
  135. UNIT_ASSERT_LE(v1, v2);
  136. };
  137. // less than
  138. UNIT_ASSERT_LE(TStringBuf("1"), "2");
  139. UNIT_ASSERT_LE("2", TString("3"));
  140. UNIT_ASSERT_LE("abc", TString("azz"));
  141. UNIT_ASSERT_LE(2, 4);
  142. UNIT_ASSERT_LE(42ul, static_cast<unsigned short>(48));
  143. // equals
  144. UNIT_ASSERT_LE(TStringBuf("2"), "2");
  145. UNIT_ASSERT_LE("2", TString("2"));
  146. UNIT_ASSERT_LE("abc", TString("abc"));
  147. UNIT_ASSERT_LE(2, 2);
  148. UNIT_ASSERT_LE(42ul, static_cast<unsigned short>(42));
  149. // greater than
  150. UNIT_ASSERT_TEST_FAILS(le(TStringBuf("2"), "1"));
  151. UNIT_ASSERT_TEST_FAILS(le("3", TString("2")));
  152. UNIT_ASSERT_TEST_FAILS(le("azz", TString("abc")));
  153. UNIT_ASSERT_TEST_FAILS(le(5, 2));
  154. UNIT_ASSERT_TEST_FAILS(le(100ul, static_cast<unsigned short>(42)));
  155. }
  156. Y_UNIT_TEST(GreaterThan) {
  157. auto gt = [](auto v1, auto v2) {
  158. UNIT_ASSERT_GT(v1, v2);
  159. };
  160. // less than
  161. UNIT_ASSERT_TEST_FAILS(gt(TStringBuf("1"), "2"));
  162. UNIT_ASSERT_TEST_FAILS(gt("2", TString("3")));
  163. UNIT_ASSERT_TEST_FAILS(gt("abc", TString("azz")));
  164. UNIT_ASSERT_TEST_FAILS(gt(2, 4));
  165. UNIT_ASSERT_TEST_FAILS(gt(42ul, static_cast<unsigned short>(48)));
  166. // equals
  167. UNIT_ASSERT_TEST_FAILS(gt(TStringBuf("2"), "2"));
  168. UNIT_ASSERT_TEST_FAILS(gt("2", TString("2")));
  169. UNIT_ASSERT_TEST_FAILS(gt("abc", TString("abc")));
  170. UNIT_ASSERT_TEST_FAILS(gt(2, 2));
  171. UNIT_ASSERT_TEST_FAILS(gt(42ul, static_cast<unsigned short>(42)));
  172. // greater than
  173. UNIT_ASSERT_GT(TStringBuf("2"), "1");
  174. UNIT_ASSERT_GT("3", TString("2"));
  175. UNIT_ASSERT_GT("azz", TString("abc"));
  176. UNIT_ASSERT_GT(5, 2);
  177. UNIT_ASSERT_GT(100ul, static_cast<unsigned short>(42));
  178. }
  179. Y_UNIT_TEST(GreaterOrEqual) {
  180. auto ge = [](auto v1, auto v2) {
  181. UNIT_ASSERT_GE(v1, v2);
  182. };
  183. // less than
  184. UNIT_ASSERT_TEST_FAILS(ge(TStringBuf("1"), "2"));
  185. UNIT_ASSERT_TEST_FAILS(ge("2", TString("3")));
  186. UNIT_ASSERT_TEST_FAILS(ge("abc", TString("azz")));
  187. UNIT_ASSERT_TEST_FAILS(ge(2, 4));
  188. UNIT_ASSERT_TEST_FAILS(ge(42ul, static_cast<unsigned short>(48)));
  189. // equals
  190. UNIT_ASSERT_GE(TStringBuf("2"), "2");
  191. UNIT_ASSERT_GE("2", TString("2"));
  192. UNIT_ASSERT_GE("abc", TString("abc"));
  193. UNIT_ASSERT_GE(2, 2);
  194. UNIT_ASSERT_GE(42ul, static_cast<unsigned short>(42));
  195. // greater than
  196. UNIT_ASSERT_GE(TStringBuf("2"), "1");
  197. UNIT_ASSERT_GE("3", TString("2"));
  198. UNIT_ASSERT_GE("azz", TString("abc"));
  199. UNIT_ASSERT_GE(5, 2);
  200. UNIT_ASSERT_GE(100ul, static_cast<unsigned short>(42));
  201. }
  202. Y_UNIT_TEST(ValuesEqual) {
  203. auto valuesEqual = [](auto v1, auto v2) {
  204. UNIT_ASSERT_VALUES_EQUAL(v1, v2);
  205. };
  206. UNIT_ASSERT_TEST_FAILS(valuesEqual(1, 2));
  207. UNIT_ASSERT_TEST_FAILS(valuesEqual(1l, static_cast<short>(2)));
  208. UNIT_ASSERT_VALUES_EQUAL("yandex", TString("yandex"));
  209. UNIT_ASSERT_VALUES_EQUAL(1.0, 1.0);
  210. }
  211. Y_UNIT_TEST(ValuesUnequal) {
  212. auto valuesUnequal = [](auto v1, auto v2) {
  213. UNIT_ASSERT_VALUES_UNEQUAL(v1, v2);
  214. };
  215. UNIT_ASSERT_TEST_FAILS(valuesUnequal(5, 5));
  216. UNIT_ASSERT_TEST_FAILS(valuesUnequal(static_cast<char>(5), 5l));
  217. TString test("test");
  218. UNIT_ASSERT_TEST_FAILS(valuesUnequal("test", test.data()));
  219. UNIT_ASSERT_VALUES_UNEQUAL("UNIT_ASSERT_VALUES_UNEQUAL", "UNIT_ASSERT_VALUES_EQUAL");
  220. UNIT_ASSERT_VALUES_UNEQUAL(1.0, 1.1);
  221. }
  222. class TTestException: public yexception {
  223. public:
  224. TTestException(const TString& text = "test exception", bool throwMe = true)
  225. : ThrowMe(throwMe)
  226. {
  227. *this << text;
  228. }
  229. virtual ~TTestException() = default;
  230. virtual void Throw() {
  231. if (ThrowMe) {
  232. throw *this;
  233. }
  234. }
  235. std::string ThrowStr() {
  236. if (ThrowMe) {
  237. throw *this;
  238. }
  239. return {};
  240. }
  241. void AssertNoException() {
  242. UNIT_ASSERT_NO_EXCEPTION(Throw());
  243. }
  244. void AssertNoExceptionRet() {
  245. const TString res = UNIT_ASSERT_NO_EXCEPTION_RESULT(ThrowStr());
  246. }
  247. template <class TExpectedException>
  248. void AssertException() {
  249. UNIT_ASSERT_EXCEPTION(Throw(), TExpectedException);
  250. }
  251. template <class TExpectedException, class T>
  252. void AssertExceptionContains(const T& substr) {
  253. UNIT_ASSERT_EXCEPTION_CONTAINS(Throw(), TExpectedException, substr);
  254. }
  255. template <class TExpectedException, class P>
  256. void AssertExceptionSatisfies(const P& predicate) {
  257. UNIT_ASSERT_EXCEPTION_SATISFIES(Throw(), TExpectedException, predicate);
  258. }
  259. int GetValue() const {
  260. return 5; // just some value for predicate testing
  261. }
  262. bool ThrowMe;
  263. };
  264. class TOtherTestException: public TTestException {
  265. public:
  266. using TTestException::TTestException;
  267. // Throws other type of exception
  268. void Throw() override {
  269. if (ThrowMe) {
  270. throw *this;
  271. }
  272. }
  273. };
  274. Y_UNIT_TEST(Exception) {
  275. UNIT_ASSERT_TEST_FAILS(TTestException("", false).AssertException<TTestException>());
  276. UNIT_ASSERT_TEST_FAILS(TTestException().AssertException<TOtherTestException>());
  277. UNIT_ASSERT_EXCEPTION(TOtherTestException().Throw(), TTestException);
  278. UNIT_ASSERT_EXCEPTION(TTestException().Throw(), TTestException);
  279. }
  280. Y_UNIT_TEST(ExceptionAssertionContainsOtherExceptionMessage) {
  281. NUnitTest::TUnitTestFailChecker checker;
  282. {
  283. auto guard = checker.InvokeGuard();
  284. TTestException("custom exception message").AssertException<TOtherTestException>();
  285. }
  286. UNIT_ASSERT(checker.Failed());
  287. UNIT_ASSERT_STRING_CONTAINS(checker.Msg(), "custom exception message");
  288. }
  289. Y_UNIT_TEST(NoException) {
  290. UNIT_ASSERT_TEST_FAILS(TTestException().AssertNoException());
  291. UNIT_ASSERT_TEST_FAILS(TTestException().AssertNoExceptionRet());
  292. UNIT_ASSERT_NO_EXCEPTION(TTestException("", false).Throw());
  293. }
  294. Y_UNIT_TEST(ExceptionContains) {
  295. UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>("cba"));
  296. UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TStringBuf("cba")));
  297. UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TString("cba")));
  298. UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TStringBuilder() << "cba"));
  299. UNIT_ASSERT_TEST_FAILS(TTestException("abc", false).AssertExceptionContains<TTestException>("bc"));
  300. UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TOtherTestException>("b"));
  301. UNIT_ASSERT_EXCEPTION_CONTAINS(TTestException("abc").Throw(), TTestException, "a");
  302. }
  303. Y_UNIT_TEST(ExceptionSatisfies) {
  304. const auto goodPredicate = [](const TTestException& e) { return e.GetValue() == 5; };
  305. const auto badPredicate = [](const TTestException& e) { return e.GetValue() != 5; };
  306. UNIT_ASSERT_NO_EXCEPTION(TTestException().AssertExceptionSatisfies<TTestException>(goodPredicate));
  307. UNIT_ASSERT_TEST_FAILS(TTestException().AssertExceptionSatisfies<TTestException>(badPredicate));
  308. UNIT_ASSERT_TEST_FAILS(TTestException().AssertExceptionSatisfies<TOtherTestException>(goodPredicate));
  309. }
  310. }