================
@@ -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:

`assertTrue`, `assertLess` is hard to understand, could we instead match the 
text as a whole?
E.g. given input
```
".. 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",
```

We expect output

```
".. csv-table:: Clang-Tidy checks\n",
'   :header: "Name", "Offers fixes"\n',
"\n",
'   :doc:`abseil-cleanup-ctad <abseil/cleanup-ctad>`, "Yes"\n',
'   :doc:`bugprone-virtual-near-miss <bugprone/virtual-near-miss>`, "Yes"\n',
"   :doc:`cert-flp30-c <cert/flp30-c>`,\n",
"   A non-doc row that should stay after docs\n",
```

And just compare two strings.

It will be good to make `normalize_list_rst` function either accept `list[str]` 
and return `list[str]` or accept `str` and return `str`.
It will be easy to test.

https://github.com/llvm/llvm-project/pull/166072
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to