Distinguish Rust paths and embedded markup in language detection

This commit is contained in:
evgeny committed 2026-11-02 23:03:30 +01:00
1 parent 032b084a15
commit 99b4bea3f7
2 files changed
+27 -14

No files matched your search

+11 -4
View File
@@ -20,12 +20,15 @@ private slots:
QTest::newRow("java") << "package example;\npublic class Hello { public static void main(String[] args) {} }" << "Java";
QTest::newRow("kotlin") << "fun main() { println(\"hello\") }" << "Kotlin";
QTest::newRow("rust") << "fn main() { println!(\"hello\"); }" << "Rust";
QTest::newRow("rust-std") << "use std::{borrow::Cow, collections::BTreeSet};" << "Rust";
QTest::newRow("c-macros") << "#include \"local.h\"\n#define COUNT 4\nunsigned long value;" << "C";
QTest::newRow("php") << "<?php\necho 'hello';" << "PHP/PHP";
QTest::newRow("php-fragment") << "function hello($name) { $message = $name; return $message; }" << "PHP/PHP";
QTest::newRow("javascript") << "const greet = (name) => console.log(name);" << "JavaScript";
QTest::newRow("css") << ".hello { color: red; margin: 1px; }" << "CSS";
QTest::newRow("html") << "<!doctype html>\n<html><script>const f = () => 1;</script></html>" << "HTML";
QTest::newRow("html-fragment") << "<div class=\"hello\"><span>Hello</span></div>" << "HTML";
QTest::newRow("html-css") << "<style>.x { color: red; margin: 1px; }</style><p>Hello</p>" << "HTML";
QTest::newRow("empty") << "" << "";
QTest::newRow("ambiguous") << "int x = 42;" << "";
QTest::newRow("prose") << "Java and Kotlin have class and function declarations; use CSS for color." << "";
@@ -54,7 +57,7 @@ private slots:
const auto bytes = source.readAll();
QCOMPARE(QString::fromLatin1(QCryptographicHash::hash(bytes, QCryptographicHash::Sha256).toHex()), entry["sha256"].toString());
const QString text = QString::fromUtf8(bytes), expected = entry["language"].toString();
const auto check = [&](const QString& code, const char* kind, Counts& counts) {
const auto check = [&](const QString& code, const QString& expected, const char* kind, Counts& counts) {
const QString actual = detectCodeLanguage(code);
if (actual == expected) ++counts.correct;
else if (actual.isEmpty()) ++counts.unknown;
@@ -62,11 +65,15 @@ private slots:
GUI_INFO("corpus: file=%s kind=%s expected=%s actual=%s", qPrintable(path), kind,
qPrintable(expected), actual.isEmpty() ? "unknown" : qPrintable(actual));
};
check(text, "full", full);
check(text, expected, "full", full);
const auto lines = text.split('\n');
// Фиксированные окна, не выбираемые по результату: начало, середина, конец по 20 строк.
for (int start : {0, qMax(0, int(lines.size()) / 2 - 10), qMax(0, int(lines.size()) - 20)})
check(lines.mid(start, 20).join('\n'), "snippet", snippets);
int window = 0;
const auto labels = entry["snippets"].toArray();
for (int start : {0, qMax(0, int(lines.size()) / 2 - 10), qMax(0, int(lines.size()) - 20)}) {
const QString label = labels.isEmpty() ? expected : labels[window].toString();
check(lines.mid(start, 20).join('\n'), label, "snippet", snippets); ++window;
}
}
GUI_INFO("corpus totals: full correct=%d unknown=%d wrong=%d; snippets correct=%d unknown=%d wrong=%d",
full.correct, full.unknown, full.wrong, snippets.correct, snippets.unknown, snippets.wrong);