dbg_output_ut.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <library/cpp/dbg_output/dump.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/stream/str.h>
  4. #include <util/string/builder.h>
  5. #include <util/string/escape.h>
  6. #include <util/generic/map.h>
  7. namespace {
  8. struct TX {
  9. inline TX() {
  10. N = this;
  11. }
  12. TX* N;
  13. };
  14. }
  15. template <>
  16. struct TDumper<TX> {
  17. template <class S>
  18. static inline void Dump(S& s, const TX& x) {
  19. s << DumpRaw("x") << x.N;
  20. }
  21. };
  22. namespace TMyNS {
  23. struct TMyStruct {
  24. int A, B;
  25. };
  26. }
  27. DEFINE_DUMPER(TMyNS::TMyStruct, A, B)
  28. Y_UNIT_TEST_SUITE(TContainerPrintersTest) {
  29. Y_UNIT_TEST(TestVectorInt) {
  30. TStringStream out;
  31. out << DbgDump(TVector<int>({1, 2, 3, 4, 5}));
  32. UNIT_ASSERT_STRINGS_EQUAL(out.Str(), "[1, 2, 3, 4, 5]");
  33. }
  34. Y_UNIT_TEST(TestMapCharToCharArray) {
  35. TStringStream out;
  36. TMap<char, const char*> m;
  37. m['a'] = "SMALL LETTER A";
  38. m['b'] = nullptr;
  39. out << DbgDump(m);
  40. UNIT_ASSERT_STRINGS_EQUAL(out.Str(), "{'a' -> \"SMALL LETTER A\", 'b' -> (empty)}");
  41. }
  42. Y_UNIT_TEST(TestVectorOfVectors) {
  43. TStringStream out;
  44. TVector<TVector<wchar16>> vec(2);
  45. vec[0].push_back(0);
  46. vec[1] = {wchar16('a')};
  47. out << DbgDump(vec);
  48. UNIT_ASSERT_STRINGS_EQUAL(out.Str(), "[[w'\\0'], [w'a']]");
  49. }
  50. Y_UNIT_TEST(TestInfinite) {
  51. UNIT_ASSERT(!!(TStringBuilder() << DbgDumpDeep(TX())));
  52. }
  53. Y_UNIT_TEST(TestLabeledDump) {
  54. TStringStream out;
  55. int a = 1, b = 2;
  56. out << LabeledDump(a, b, 1 + 2);
  57. UNIT_ASSERT_STRINGS_EQUAL(out.Str(), "{\"a\": 1, \"b\": 2, \"1 + 2\": 3}");
  58. }
  59. Y_UNIT_TEST(TestStructDumper) {
  60. TStringStream out;
  61. out << DbgDump(TMyNS::TMyStruct{3, 4});
  62. UNIT_ASSERT_STRINGS_EQUAL(out.Str(), "{\"A\": 3, \"B\": 4}");
  63. }
  64. Y_UNIT_TEST(TestColors) {
  65. using TComplex = TMap<TString, TMap<int, char>>;
  66. TComplex test;
  67. test["a"][1] = '7';
  68. test["b"][2] = '6';
  69. TStringStream out;
  70. out << DbgDump<TComplex, NDbgDump::NColorScheme::TEyebleed</* Enforce = */ true>>(test);
  71. UNIT_ASSERT_STRINGS_EQUAL(
  72. EscapeC(out.Str()),
  73. "\\x1B[1;32m{\\x1B[1;31m\\x1B[42m\\x1B[1;31m\\x1B[1;33m\\\"a\\\"\\x1B[22;39m\\x1B[22;39m"
  74. "\\x1B[49m\\x1B[1;32m -> \\x1B[44m\\x1B[1;31m\\x1B[1;32m{\\x1B[1;31m\\x1B[1;31m1"
  75. "\\x1B[22;39m\\x1B[1;32m -> \\x1B[1;31m'7'\\x1B[22;39m\\x1B[1;32m}"
  76. "\\x1B[22;39m\\x1B[22;39m\\x1B[49m\\x1B[1;32m, \\x1B[1;31m\\x1B[42m\\x1B[1;31m\\x1B[1;33m"
  77. "\\\"b\\\"\\x1B[22;39m\\x1B[22;39m\\x1B[49m\\x1B[1;32m -> "
  78. "\\x1B[44m\\x1B[1;31m\\x1B[1;32m{\\x1B[1;31m\\x1B[1;31m2\\x1B[22;39m\\x1B[1;32m -> "
  79. "\\x1B[1;31m'6'\\x1B[22;39m\\x1B[1;32m}\\x1B[22;39m\\x1B[22;39m\\x1B[49m\\x1B[1;32m}\\x1B[22;39m");
  80. }
  81. Y_UNIT_TEST(SmallIntOrChar) {
  82. char c = 'e';
  83. i8 i = -100;
  84. ui8 u = 10;
  85. UNIT_ASSERT_VALUES_EQUAL(TStringBuilder() << DbgDump(c), "'e'");
  86. UNIT_ASSERT_VALUES_EQUAL(TStringBuilder() << DbgDump(i), "-100");
  87. UNIT_ASSERT_VALUES_EQUAL(TStringBuilder() << DbgDump(u), "10");
  88. }
  89. }