[clang-tools-extra] [clang-tidy] Implement alphabetical order test (PR #166072)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 9 01:29:45 PST 2025
================
@@ -0,0 +1,283 @@
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+# To run these tests:
+# python3 check-alphabetical-order_test.py -v
+
+import io
+import os
+import tempfile
+import unittest
+from contextlib import redirect_stderr
+import importlib.util
+import importlib.machinery
+from typing import Any, cast
+
+
+def _load_script_module():
+ here = os.path.dirname(cast(str, __file__))
+ script_path = os.path.normpath(os.path.join(here, "check-alphabetical-order.py"))
+ loader = importlib.machinery.SourceFileLoader(
+ "check_alphabetical_order", cast(str, script_path)
+ )
+ spec = importlib.util.spec_from_loader(loader.name, loader)
+ if spec is None or spec.loader is None:
+ raise ImportError(f"Failed to load spec for {script_path}")
+ mod = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(mod)
+ return mod
+
+
+_mod = cast(Any, _load_script_module())
+
+
+class TestAlphabeticalOrderCheck(unittest.TestCase):
+ def test_normalize_list_rst_sorts_rows(self):
+ lines = [
+ "Header\n",
+ "------" "\n",
+ ".. csv-table:: Clang-Tidy checks\n",
+ ' :header: "Name", "Offers fixes"\n',
+ "\n",
+ ' :doc:`bugprone-virtual-near-miss <bugprone/virtual-near-miss>`, "Yes"\n',
+ " :doc:`cert-flp30-c <cert/flp30-c>`,\n",
+ ' :doc:`abseil-cleanup-ctad <abseil/cleanup-ctad>`, "Yes"\n',
+ " A non-doc row that should stay after docs\n",
+ "\n",
+ "Footer\n",
+ ]
+
+ out = _mod.normalize_list_rst(lines)
+ pos_abseil = out.find("abseil-cleanup-ctad")
+ pos_bugprone = out.find("bugprone-virtual-near-miss")
+ pos_cert = out.find("cert-flp30-c")
+ self.assertTrue(all(p != -1 for p in [pos_abseil, pos_bugprone, pos_cert]))
+ self.assertLess(pos_abseil, pos_bugprone)
+ self.assertLess(pos_bugprone, pos_cert)
+ # Non-doc row should remain after doc rows within the table region.
+ self.assertGreater(out.find("A non-doc row"), out.find("cert-flp30-c"))
----------------
vbvictor wrote:
Could you make other tests follow this idea.
https://github.com/llvm/llvm-project/pull/166072
More information about the cfe-commits
mailing list