Skip to content

Commit 658f848

Browse files
authored
[NFC][libc++][test] Refactor new ftm generator tests. (llvm#134490)
This uses the python unit test framework instead of just asserts. This improves the diagnostics when a test fails.
1 parent 4928093 commit 658f848

File tree

6 files changed

+208
-161
lines changed

6 files changed

+208
-161
lines changed

libcxx/test/libcxx/feature_test_macro/ftm_metadata.sh.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,48 @@
99
# RUN: %{python} %s %{libcxx-dir}/utils %{libcxx-dir}/test/libcxx/feature_test_macro/test_data.json
1010

1111
import sys
12+
import unittest
1213

13-
sys.path.append(sys.argv[1])
14+
UTILS = sys.argv[1]
15+
TEST_DATA = sys.argv[2]
16+
del sys.argv[1:3]
17+
18+
sys.path.append(UTILS)
1419
from generate_feature_test_macro_components import FeatureTestMacros, Metadata
1520

1621

17-
def test(output, expected):
18-
assert output == expected, f"expected\n{expected}\n\noutput\n{output}"
19-
20-
21-
ftm = FeatureTestMacros(sys.argv[2])
22-
23-
test(
24-
ftm.ftm_metadata,
25-
{
26-
"__cpp_lib_any": Metadata(
27-
headers=["any"], test_suite_guard=None, libcxx_guard=None
28-
),
29-
"__cpp_lib_barrier": Metadata(
30-
headers=["barrier"],
31-
test_suite_guard="!defined(_LIBCPP_VERSION) || (_LIBCPP_HAS_THREADS && _LIBCPP_AVAILABILITY_HAS_SYNC)",
32-
libcxx_guard="_LIBCPP_HAS_THREADS && _LIBCPP_AVAILABILITY_HAS_SYNC",
33-
),
34-
"__cpp_lib_format": Metadata(
35-
headers=["format"], test_suite_guard=None, libcxx_guard=None
36-
),
37-
"__cpp_lib_parallel_algorithm": Metadata(
38-
headers=["algorithm", "numeric"], test_suite_guard=None, libcxx_guard=None
39-
),
40-
"__cpp_lib_variant": Metadata(
41-
headers=["variant"], test_suite_guard=None, libcxx_guard=None
42-
),
43-
"__cpp_lib_missing_FTM_in_older_standard": Metadata(
44-
headers=[], test_suite_guard=None, libcxx_guard=None
45-
),
46-
},
47-
)
22+
class Test(unittest.TestCase):
23+
def setUp(self):
24+
self.ftm = FeatureTestMacros(TEST_DATA)
25+
self.maxDiff = None # This causes the diff to be printed when the test fails
26+
27+
def test_implementation(self):
28+
expected = {
29+
"__cpp_lib_any": Metadata(
30+
headers=["any"], test_suite_guard=None, libcxx_guard=None
31+
),
32+
"__cpp_lib_barrier": Metadata(
33+
headers=["barrier"],
34+
test_suite_guard="!defined(_LIBCPP_VERSION) || (_LIBCPP_HAS_THREADS && _LIBCPP_AVAILABILITY_HAS_SYNC)",
35+
libcxx_guard="_LIBCPP_HAS_THREADS && _LIBCPP_AVAILABILITY_HAS_SYNC",
36+
),
37+
"__cpp_lib_format": Metadata(
38+
headers=["format"], test_suite_guard=None, libcxx_guard=None
39+
),
40+
"__cpp_lib_parallel_algorithm": Metadata(
41+
headers=["algorithm", "numeric"],
42+
test_suite_guard=None,
43+
libcxx_guard=None,
44+
),
45+
"__cpp_lib_variant": Metadata(
46+
headers=["variant"], test_suite_guard=None, libcxx_guard=None
47+
),
48+
"__cpp_lib_missing_FTM_in_older_standard": Metadata(
49+
headers=[], test_suite_guard=None, libcxx_guard=None
50+
),
51+
}
52+
self.assertEqual(self.ftm.ftm_metadata, expected)
53+
54+
55+
if __name__ == "__main__":
56+
unittest.main()

libcxx/test/libcxx/feature_test_macro/implemented_ftms.sh.py

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,61 @@
99
# RUN: %{python} %s %{libcxx-dir}/utils %{libcxx-dir}/test/libcxx/feature_test_macro/test_data.json
1010

1111
import sys
12+
import unittest
1213

13-
sys.path.append(sys.argv[1])
14+
UTILS = sys.argv[1]
15+
TEST_DATA = sys.argv[2]
16+
del sys.argv[1:3]
17+
18+
sys.path.append(UTILS)
1419
from generate_feature_test_macro_components import FeatureTestMacros
1520

1621

17-
def test(output, expected):
18-
assert output == expected, f"expected\n{expected}\n\noutput\n{output}"
19-
20-
21-
ftm = FeatureTestMacros(sys.argv[2])
22-
test(
23-
ftm.implemented_ftms,
24-
{
25-
"__cpp_lib_any": {
26-
"c++17": "201606L",
27-
"c++20": "201606L",
28-
"c++23": "201606L",
29-
"c++26": "201606L",
30-
},
31-
"__cpp_lib_barrier": {
32-
"c++20": "201907L",
33-
"c++23": "201907L",
34-
"c++26": "299900L",
35-
},
36-
"__cpp_lib_format": {
37-
"c++20": None,
38-
"c++23": None,
39-
"c++26": None,
40-
},
41-
"__cpp_lib_parallel_algorithm": {
42-
"c++17": "201603L",
43-
"c++20": "201603L",
44-
"c++23": "201603L",
45-
"c++26": "201603L",
46-
},
47-
"__cpp_lib_variant": {
48-
"c++17": "202102L",
49-
"c++20": "202102L",
50-
"c++23": "202102L",
51-
"c++26": "202102L",
52-
},
53-
"__cpp_lib_missing_FTM_in_older_standard": {
54-
"c++17": None,
55-
"c++20": None,
56-
"c++26": None,
57-
},
58-
},
59-
)
22+
class Test(unittest.TestCase):
23+
def setUp(self):
24+
self.ftm = FeatureTestMacros(TEST_DATA)
25+
self.maxDiff = None # This causes the diff to be printed when the test fails
26+
27+
def test_implementation(self):
28+
29+
expected = {
30+
"__cpp_lib_any": {
31+
"c++17": "201606L",
32+
"c++20": "201606L",
33+
"c++23": "201606L",
34+
"c++26": "201606L",
35+
},
36+
"__cpp_lib_barrier": {
37+
"c++20": "201907L",
38+
"c++23": "201907L",
39+
"c++26": "299900L",
40+
},
41+
"__cpp_lib_format": {
42+
"c++20": None,
43+
"c++23": None,
44+
"c++26": None,
45+
},
46+
"__cpp_lib_parallel_algorithm": {
47+
"c++17": "201603L",
48+
"c++20": "201603L",
49+
"c++23": "201603L",
50+
"c++26": "201603L",
51+
},
52+
"__cpp_lib_variant": {
53+
"c++17": "202102L",
54+
"c++20": "202102L",
55+
"c++23": "202102L",
56+
"c++26": "202102L",
57+
},
58+
"__cpp_lib_missing_FTM_in_older_standard": {
59+
"c++17": None,
60+
"c++20": None,
61+
"c++26": None,
62+
},
63+
}
64+
65+
self.assertEqual(self.ftm.implemented_ftms, expected)
66+
67+
68+
if __name__ == "__main__":
69+
unittest.main()

libcxx/test/libcxx/feature_test_macro/standard_ftms.sh.py

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,61 @@
99
# RUN: %{python} %s %{libcxx-dir}/utils %{libcxx-dir}/test/libcxx/feature_test_macro/test_data.json
1010

1111
import sys
12+
import unittest
1213

13-
sys.path.append(sys.argv[1])
14+
UTILS = sys.argv[1]
15+
TEST_DATA = sys.argv[2]
16+
del sys.argv[1:3]
17+
18+
sys.path.append(UTILS)
1419
from generate_feature_test_macro_components import FeatureTestMacros
1520

1621

17-
def test(output, expected):
18-
assert output == expected, f"expected\n{expected}\n\noutput\n{output}"
19-
20-
21-
ftm = FeatureTestMacros(sys.argv[2])
22-
test(
23-
ftm.standard_ftms,
24-
{
25-
"__cpp_lib_any": {
26-
"c++17": "201606L",
27-
"c++20": "201606L",
28-
"c++23": "201606L",
29-
"c++26": "201606L",
30-
},
31-
"__cpp_lib_barrier": {
32-
"c++20": "201907L",
33-
"c++23": "201907L",
34-
"c++26": "299900L",
35-
},
36-
"__cpp_lib_format": {
37-
"c++20": "202110L",
38-
"c++23": "202207L",
39-
"c++26": "202311L",
40-
},
41-
"__cpp_lib_parallel_algorithm": {
42-
"c++17": "201603L",
43-
"c++20": "201603L",
44-
"c++23": "201603L",
45-
"c++26": "201603L",
46-
},
47-
"__cpp_lib_variant": {
48-
"c++17": "202102L",
49-
"c++20": "202106L",
50-
"c++23": "202106L",
51-
"c++26": "202306L",
52-
},
53-
"__cpp_lib_missing_FTM_in_older_standard": {
54-
"c++17": "2017L",
55-
"c++20": "2020L",
56-
"c++23": "2020L",
57-
"c++26": "2026L",
58-
},
59-
},
60-
)
22+
class Test(unittest.TestCase):
23+
def setUp(self):
24+
self.ftm = FeatureTestMacros(TEST_DATA)
25+
self.maxDiff = None # This causes the diff to be printed when the test fails
26+
27+
def test_implementation(self):
28+
expected = {
29+
"__cpp_lib_any": {
30+
"c++17": "201606L",
31+
"c++20": "201606L",
32+
"c++23": "201606L",
33+
"c++26": "201606L",
34+
},
35+
"__cpp_lib_barrier": {
36+
"c++20": "201907L",
37+
"c++23": "201907L",
38+
"c++26": "299900L",
39+
},
40+
"__cpp_lib_format": {
41+
"c++20": "202110L",
42+
"c++23": "202207L",
43+
"c++26": "202311L",
44+
},
45+
"__cpp_lib_parallel_algorithm": {
46+
"c++17": "201603L",
47+
"c++20": "201603L",
48+
"c++23": "201603L",
49+
"c++26": "201603L",
50+
},
51+
"__cpp_lib_variant": {
52+
"c++17": "202102L",
53+
"c++20": "202106L",
54+
"c++23": "202106L",
55+
"c++26": "202306L",
56+
},
57+
"__cpp_lib_missing_FTM_in_older_standard": {
58+
"c++17": "2017L",
59+
"c++20": "2020L",
60+
"c++23": "2020L",
61+
"c++26": "2026L",
62+
},
63+
}
64+
65+
self.assertEqual(self.ftm.standard_ftms, expected)
66+
67+
68+
if __name__ == "__main__":
69+
unittest.main()

libcxx/test/libcxx/feature_test_macro/std_dialects.sh.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,31 @@
99
# RUN: %{python} %s %{libcxx-dir}/utils %{libcxx-dir}/test/libcxx/feature_test_macro/test_data.json
1010

1111
import sys
12+
import unittest
1213

13-
sys.path.append(sys.argv[1])
14+
UTILS = sys.argv[1]
15+
TEST_DATA = sys.argv[2]
16+
del sys.argv[1:3]
17+
18+
sys.path.append(UTILS)
1419
from generate_feature_test_macro_components import FeatureTestMacros
1520

1621

17-
def test(output, expected):
18-
assert output == expected, f"expected\n{expected}\n\noutput\n{output}"
22+
class Test(unittest.TestCase):
23+
def setUp(self):
24+
self.ftm = FeatureTestMacros(TEST_DATA)
25+
self.maxDiff = None # This causes the diff to be printed when the test fails
26+
27+
def test_implementation(self):
28+
expected = [
29+
"c++17",
30+
"c++20",
31+
"c++23",
32+
"c++26",
33+
]
34+
35+
self.assertEqual(self.ftm.std_dialects, expected)
1936

2037

21-
ftm = FeatureTestMacros(sys.argv[2])
22-
test(
23-
ftm.std_dialects,
24-
[
25-
"c++17",
26-
"c++20",
27-
"c++23",
28-
"c++26",
29-
],
30-
)
38+
if __name__ == "__main__":
39+
unittest.main()

libcxx/test/libcxx/feature_test_macro/version_header.sh.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@
99
# RUN: %{python} %s %{libcxx-dir}/utils %{libcxx-dir}/test/libcxx/feature_test_macro/test_data.json
1010

1111
import sys
12+
import unittest
1213

13-
sys.path.append(sys.argv[1])
14-
from generate_feature_test_macro_components import FeatureTestMacros
14+
UTILS = sys.argv[1]
15+
TEST_DATA = sys.argv[2]
16+
del sys.argv[1:3]
1517

18+
sys.path.append(UTILS)
19+
from generate_feature_test_macro_components import FeatureTestMacros
1620

17-
def test(output, expected):
18-
assert output == expected, f"expected\n{expected}\n\noutput\n{output}"
1921

22+
class Test(unittest.TestCase):
23+
def setUp(self):
24+
self.ftm = FeatureTestMacros(TEST_DATA)
25+
self.maxDiff = None # This causes the diff to be printed when the test fails
2026

21-
ftm = FeatureTestMacros(sys.argv[2])
22-
test(
23-
ftm.version_header,
24-
"""// -*- C++ -*-
27+
def test_implementeation(self):
28+
expected = """// -*- C++ -*-
2529
//===----------------------------------------------------------------------===//
2630
//
2731
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -70,5 +74,9 @@ def test(output, expected):
7074
#endif // _LIBCPP_STD_VER >= 26
7175
7276
#endif // _LIBCPP_VERSIONH
73-
""",
74-
)
77+
"""
78+
self.assertEqual(self.ftm.version_header, expected)
79+
80+
81+
if __name__ == "__main__":
82+
unittest.main()

0 commit comments

Comments
 (0)