summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/highlight/sql_highlighter_ut.cpp
diff options
context:
space:
mode:
authorrobot-piglet <[email protected]>2025-05-12 13:53:24 +0300
committerrobot-piglet <[email protected]>2025-05-12 14:05:50 +0300
commit7a941ebd252fd7442b4d1d34d31d72e971ad20bf (patch)
tree70c132d1b611697ad23b90cf35215b035f247ec0 /yql/essentials/sql/v1/highlight/sql_highlighter_ut.cpp
parentbf1279129bcf6c1b1001e39c39a13d80737898d3 (diff)
Intermediate changes
commit_hash:3a624a323006078de71f50747f7b2e8cadba7ccd
Diffstat (limited to 'yql/essentials/sql/v1/highlight/sql_highlighter_ut.cpp')
-rw-r--r--yql/essentials/sql/v1/highlight/sql_highlighter_ut.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/yql/essentials/sql/v1/highlight/sql_highlighter_ut.cpp b/yql/essentials/sql/v1/highlight/sql_highlighter_ut.cpp
new file mode 100644
index 00000000000..5fcab7937fc
--- /dev/null
+++ b/yql/essentials/sql/v1/highlight/sql_highlighter_ut.cpp
@@ -0,0 +1,106 @@
+#include "sql_highlighter.h"
+
+#include <library/cpp/json/json_reader.h>
+#include <library/cpp/resource/resource.h>
+#include <library/cpp/testing/unittest/registar.h>
+
+#include <util/generic/string.h>
+#include <util/string/join.h>
+#include <util/string/escape.h>
+
+using namespace NSQLHighlight;
+
+struct TTest {
+ struct TCase {
+ TString Input;
+ TString Expected;
+ };
+
+ TString Name;
+ TVector<TCase> Cases;
+};
+
+TVector<TTest> LoadTestSuite() {
+ TString text;
+ Y_ENSURE(NResource::FindExact("suite.json", &text));
+ auto json = NJson::ReadJsonFastTree(text).GetMapSafe();
+
+ TVector<TTest> tests;
+ for (auto& [k, v] : json) {
+ TVector<TTest::TCase> cases;
+ for (auto& c : v.GetArraySafe()) {
+ cases.emplace_back(
+ std::move(c[0].GetStringSafe()),
+ std::move(c[1].GetStringSafe()));
+ }
+ tests.emplace_back(std::move(k), std::move(cases));
+ }
+ return tests;
+}
+
+char ToChar(EUnitKind kind) {
+ switch (kind) {
+ case EUnitKind::Keyword:
+ return 'K';
+ case EUnitKind::Punctuation:
+ return 'P';
+ case EUnitKind::Identifier:
+ return 'I';
+ case EUnitKind::QuotedIdentifier:
+ return 'Q';
+ case EUnitKind::BindParamterIdentifier:
+ return 'B';
+ case EUnitKind::TypeIdentifier:
+ return 'T';
+ case EUnitKind::FunctionIdentifier:
+ return 'F';
+ case EUnitKind::Literal:
+ return 'L';
+ case EUnitKind::StringLiteral:
+ return 'S';
+ case EUnitKind::Comment:
+ return 'C';
+ case EUnitKind::Whitespace:
+ return '_';
+ case EUnitKind::Error:
+ return 'E';
+ }
+}
+
+TString ToMask(const TVector<TToken>& tokens) {
+ TVector<TString> s;
+ for (const auto& t : tokens) {
+ s.emplace_back(TString(t.Length, ToChar(t.Kind)));
+ }
+ return JoinSeq("#", s);
+}
+
+TString Mask(IHighlighter::TPtr& h, TStringBuf text) {
+ return ToMask(Tokenize(*h, text));
+}
+
+Y_UNIT_TEST_SUITE(SqlHighlighterTests) {
+
+ Y_UNIT_TEST(Suite) {
+ auto h = MakeHighlighter(MakeHighlighting());
+ size_t count = 0;
+ Cerr << "{" << Endl;
+ for (const auto& test : LoadTestSuite()) {
+ Cerr << " \"" << test.Name << "\": [" << Endl;
+ for (size_t i = 0; i < test.Cases.size(); ++i) {
+ const auto& check = test.Cases[i];
+ const auto actual = Mask(h, check.Input);
+ Cerr << " [\"" << EscapeC(check.Input) << "\", \"" << actual << "\"]," << Endl;
+ UNIT_ASSERT_VALUES_EQUAL_C(
+ actual,
+ check.Expected,
+ test.Name << " #" << i << ": Input = '" << check.Input << "'");
+ count += 1;
+ }
+ Cerr << " ]," << Endl;
+ }
+ Cerr << "}" << Endl;
+ Cerr << "Test Cases Executed: " << count << Endl;
+ }
+
+} // Y_UNIT_TEST_SUITE(SqlHighlighterTests)