yql_issue_ut.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "yql_issue.h"
  2. #include "yql_issue_message.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <yql/essentials/public/issue/protos/issue_message.pb.h>
  5. #include <yql/essentials/public/issue/yql_issue_message.h>
  6. #include <library/cpp/unicode/normalization/normalization.h>
  7. #include <util/charset/utf8.h>
  8. #include <util/charset/wide.h>
  9. #include <util/string/builder.h>
  10. #include <google/protobuf/message.h>
  11. #include <google/protobuf/descriptor.h>
  12. using namespace google::protobuf;
  13. using namespace NYql;
  14. Y_UNIT_TEST_SUITE(IssueTest) {
  15. Y_UNIT_TEST(Ascii) {
  16. TIssue issue1("тест abc");
  17. UNIT_ASSERT_VALUES_EQUAL(issue1.GetMessage(), "тест abc");
  18. TIssue issue2("\xFF abc");
  19. UNIT_ASSERT_VALUES_EQUAL(issue2.GetMessage(), "? abc");
  20. TIssue issue3("");
  21. UNIT_ASSERT_VALUES_EQUAL(issue3.GetMessage(), "");
  22. TIssue issue4("abc");
  23. UNIT_ASSERT_VALUES_EQUAL(issue4.GetMessage(), "abc");
  24. }
  25. }
  26. Y_UNIT_TEST_SUITE(TextWalkerTest) {
  27. Y_UNIT_TEST(BasicTest) {
  28. TPosition pos;
  29. pos.Row = 1;
  30. TTextWalker walker(pos, false);
  31. walker.Advance(TStringBuf("a\r\taa"));
  32. UNIT_ASSERT_VALUES_EQUAL(pos, TPosition(5, 1));
  33. walker.Advance(TStringBuf("\na"));
  34. UNIT_ASSERT_VALUES_EQUAL(pos, TPosition(1, 2));
  35. }
  36. Y_UNIT_TEST(CrLfTest) {
  37. TPosition pos;
  38. pos.Row = 1;
  39. TTextWalker walker(pos, false);
  40. walker.Advance(TStringBuf("a\raa\r"));
  41. UNIT_ASSERT_VALUES_EQUAL(pos, TPosition(4, 1));
  42. walker.Advance('\n');
  43. UNIT_ASSERT_VALUES_EQUAL(pos, TPosition(4, 1));
  44. walker.Advance(TStringBuf("\r\r\ra"));
  45. UNIT_ASSERT_VALUES_EQUAL(pos, TPosition(4, 2));
  46. walker.Advance('\r');
  47. UNIT_ASSERT_VALUES_EQUAL(pos, TPosition(4, 2));
  48. walker.Advance('\n');
  49. UNIT_ASSERT_VALUES_EQUAL(pos, TPosition(4, 2));
  50. walker.Advance('a');
  51. UNIT_ASSERT_VALUES_EQUAL(pos, TPosition(1, 3));
  52. }
  53. Y_UNIT_TEST(UnicodeTest) {
  54. {
  55. TPosition pos;
  56. pos.Row = 1;
  57. TTextWalker walker(pos, false);
  58. walker.Advance(TStringBuf("привет"));
  59. UNIT_ASSERT_VALUES_EQUAL(pos, TPosition(12, 1));
  60. }
  61. {
  62. TPosition pos;
  63. pos.Row = 1;
  64. TTextWalker walker(pos, true);
  65. walker.Advance(TStringBuf("привет"));
  66. UNIT_ASSERT_VALUES_EQUAL(pos, TPosition(6, 1));
  67. }
  68. }
  69. }
  70. Y_UNIT_TEST_SUITE(ToOneLineStringTest) {
  71. Y_UNIT_TEST(OneMessageTest) {
  72. TIssues issues;
  73. issues.AddIssue(TPosition(12, 34, "file.abc"), "error");
  74. UNIT_ASSERT_STRINGS_EQUAL(issues.ToOneLineString(), "{ file.abc:34:12: Error: error }");
  75. }
  76. Y_UNIT_TEST(SubIssuesTest) {
  77. TIssue issue(TPosition(12, 34, "file.abc"), "error");
  78. TIssue subissue("suberror");
  79. subissue.AddSubIssue(MakeIntrusive<TIssue>("subsuberror"));
  80. issue.AddSubIssue(MakeIntrusive<TIssue>(subissue));
  81. TIssues issues;
  82. issues.AddIssue(issue);
  83. UNIT_ASSERT_STRINGS_EQUAL(issues.ToOneLineString(), "{ file.abc:34:12: Error: error subissue: { <main>: Error: suberror subissue: { <main>: Error: subsuberror } } }");
  84. }
  85. Y_UNIT_TEST(ManyIssuesTest) {
  86. TIssue issue(TPosition(12, 34, "file.abc"), "error\n");
  87. issue.AddSubIssue(MakeIntrusive<TIssue>("suberror"));
  88. TIssues issues;
  89. issues.AddIssue(issue);
  90. issues.AddIssue(TPosition(100, 2, "abc.file"), "my\nmessage");
  91. UNIT_ASSERT_STRINGS_EQUAL(issues.ToOneLineString(), "[ { file.abc:34:12: Error: error subissue: { <main>: Error: suberror } } { abc.file:2:100: Error: my message } ]");
  92. }
  93. }
  94. Y_UNIT_TEST_SUITE(EscapeNonUtf8) {
  95. Y_UNIT_TEST(Escape) {
  96. const TString nonUtf8String = "\xfe\xfa\xf5\xc2";
  97. UNIT_ASSERT(!IsUtf(nonUtf8String));
  98. // Check that our escaping correctly processes unicode pairs
  99. const TString toNormalize = "Ёлка";
  100. const TString nfd = WideToUTF8(Normalize<NUnicode::ENormalization::NFD>(UTF8ToWide(toNormalize))); // dots over 'ё' will be separate unicode symbol
  101. const TString nfc = WideToUTF8(Normalize<NUnicode::ENormalization::NFC>(UTF8ToWide(toNormalize))); // dots over 'ё' will be with with their letter
  102. UNIT_ASSERT_STRINGS_UNEQUAL(nfc, nfd);
  103. std::pair<TString, TString> nonUtf8Messages[] = {
  104. { nonUtf8String, "????" },
  105. { TStringBuilder() << nonUtf8String << "Failed to parse file " << nonUtf8String << "עברית" << nonUtf8String, "????Failed to parse file ????עברית????" },
  106. { nfd, nfd },
  107. { nfc, nfc },
  108. { TStringBuilder() << nfc << nonUtf8String << nfd, TStringBuilder() << nfc << "????" << nfd },
  109. { TStringBuilder() << nfd << nonUtf8String << nfc, TStringBuilder() << nfd << "????" << nfc },
  110. };
  111. for (const auto& [src, dst] : nonUtf8Messages) {
  112. TIssue issue(src);
  113. UNIT_ASSERT_STRINGS_EQUAL(issue.GetMessage(), dst);
  114. }
  115. }
  116. }