|
3 | 3 | # Distributed under the Boost Software License, Version 1.0.
|
4 | 4 | # See http://www.boost.org/LICENSE_1_0.txt
|
5 | 5 |
|
6 |
| -import unittest |
| 6 | +import pytest |
7 | 7 |
|
8 |
| -from . import parser_test_case |
| 8 | +from . import autoconfig |
9 | 9 |
|
10 | 10 | from pygccxml import parser
|
11 | 11 | from pygccxml import declarations
|
12 | 12 |
|
13 | 13 |
|
14 |
| -class Test(parser_test_case.parser_test_case_t): |
15 |
| - global_ns = None |
16 |
| - |
17 |
| - def __init__(self, *args): |
18 |
| - parser_test_case.parser_test_case_t.__init__(self, *args) |
19 |
| - self.header = "test_comments.hpp" |
20 |
| - self.global_ns = None |
21 |
| - self.config.castxml_epic_version = 1 |
22 |
| - |
23 |
| - def _check_comment_content(self, list, comment_decl): |
24 |
| - if comment_decl.text: |
25 |
| - self.assertEqual(list, comment_decl.text) |
26 |
| - else: |
27 |
| - print("No text in comment to check") |
28 |
| - |
29 |
| - def setUp(self): |
30 |
| - |
31 |
| - if not self.global_ns: |
32 |
| - decls = parser.parse([self.header], self.config) |
33 |
| - Test.global_ns = declarations.get_global_namespace(decls) |
34 |
| - Test.xml_generator_from_xml_file = \ |
35 |
| - self.config.xml_generator_from_xml_file |
36 |
| - self.xml_generator_from_xml_file = Test.xml_generator_from_xml_file |
37 |
| - |
38 |
| - self.global_ns = Test.global_ns |
39 |
| - |
40 |
| - def test(self): |
41 |
| - """ |
42 |
| - Check the comment parsing |
43 |
| - """ |
44 |
| - if self.config.castxml_epic_version != 1: |
45 |
| - # Run this test only with castxml epic version == 1 |
46 |
| - return |
47 |
| - tnamespace = self.global_ns.namespace("comment") |
48 |
| - |
49 |
| - self.assertIn("comment", dir(tnamespace)) |
50 |
| - self._check_comment_content(["//! Namespace Comment", |
51 |
| - "//! Across multiple lines"], |
52 |
| - tnamespace.comment) |
53 |
| - |
54 |
| - tenumeration = tnamespace.enumeration("com_enum") |
55 |
| - self.assertIn("comment", dir(tenumeration)) |
56 |
| - self._check_comment_content(['/// Outside Class enum comment'], |
57 |
| - tenumeration.comment) |
58 |
| - |
59 |
| - tclass = tnamespace.class_("test") |
60 |
| - self.assertIn("comment", dir(tclass)) |
61 |
| - self._check_comment_content(["/** class comment */"], tclass.comment) |
62 |
| - |
63 |
| - tcls_enumeration = tclass.enumeration("test_enum") |
64 |
| - self.assertIn("comment", dir(tcls_enumeration)) |
65 |
| - self._check_comment_content(['/// inside class enum comment'], |
66 |
| - tcls_enumeration.comment) |
67 |
| - |
68 |
| - tmethod = tclass.member_functions()[0] |
69 |
| - |
70 |
| - self.assertIn("comment", dir(tmethod)) |
71 |
| - self._check_comment_content(["/// cxx comment", |
72 |
| - "/// with multiple lines"], |
73 |
| - tmethod.comment) |
74 |
| - |
75 |
| - tconstructor = tclass.constructors()[0] |
76 |
| - |
77 |
| - self.assertIn("comment", dir(tconstructor)) |
78 |
| - self._check_comment_content(["/** doc comment */"], |
79 |
| - tconstructor.comment) |
80 |
| - |
81 |
| - for indx, cmt in enumerate(['//! mutable field comment', |
82 |
| - "/// bit field comment"]): |
83 |
| - tvariable = tclass.variables()[indx] |
84 |
| - self.assertIn("comment", dir(tvariable)) |
85 |
| - self._check_comment_content([cmt], tvariable.comment) |
86 |
| - |
87 |
| - |
88 |
| -def create_suite(): |
89 |
| - suite = unittest.TestSuite() |
90 |
| - suite.addTest( |
91 |
| - unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test)) |
92 |
| - return suite |
93 |
| - |
94 |
| - |
95 |
| -def run_suite(): |
96 |
| - unittest.TextTestRunner(verbosity=2).run(create_suite()) |
97 |
| - |
98 |
| - |
99 |
| -if __name__ == "__main__": |
100 |
| - run_suite() |
| 14 | +TEST_FILES = [ |
| 15 | + "test_comments.hpp", |
| 16 | +] |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def global_ns(): |
| 21 | + COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE |
| 22 | + INIT_OPTIMIZER = True |
| 23 | + config = autoconfig.cxx_parsers_cfg.config.clone() |
| 24 | + config.castxml_epic_version = 1 |
| 25 | + decls = parser.parse(TEST_FILES, config, COMPILATION_MODE) |
| 26 | + global_ns = declarations.get_global_namespace(decls) |
| 27 | + if INIT_OPTIMIZER: |
| 28 | + global_ns.init_optimizer() |
| 29 | + return global_ns |
| 30 | + |
| 31 | + |
| 32 | +def _check_comment_content(list, comment_decl): |
| 33 | + if comment_decl.text: |
| 34 | + assert list == comment_decl.text |
| 35 | + else: |
| 36 | + print("No text in comment to check") |
| 37 | + |
| 38 | + |
| 39 | +def test_comments(global_ns): |
| 40 | + """ |
| 41 | + Check the comment parsing |
| 42 | + """ |
| 43 | + tnamespace = global_ns.namespace("comment") |
| 44 | + |
| 45 | + assert "comment" in dir(tnamespace) |
| 46 | + _check_comment_content( |
| 47 | + [ |
| 48 | + "//! Namespace Comment", |
| 49 | + "//! Across multiple lines" |
| 50 | + ], |
| 51 | + tnamespace.comment |
| 52 | + ) |
| 53 | + |
| 54 | + tenumeration = tnamespace.enumeration("com_enum") |
| 55 | + assert "comment" in dir(tenumeration) |
| 56 | + _check_comment_content( |
| 57 | + ['/// Outside Class enum comment'], |
| 58 | + tenumeration.comment |
| 59 | + ) |
| 60 | + |
| 61 | + tclass = tnamespace.class_("test") |
| 62 | + assert "comment" in dir(tclass) |
| 63 | + _check_comment_content( |
| 64 | + ["/** class comment */"], |
| 65 | + tclass.comment |
| 66 | + ) |
| 67 | + |
| 68 | + tcls_enumeration = tclass.enumeration("test_enum") |
| 69 | + assert "comment" in dir(tcls_enumeration) |
| 70 | + _check_comment_content( |
| 71 | + ['/// inside class enum comment'], |
| 72 | + tcls_enumeration.comment |
| 73 | + ) |
| 74 | + |
| 75 | + tmethod = tclass.member_functions()[0] |
| 76 | + |
| 77 | + assert "comment" in dir(tmethod) |
| 78 | + _check_comment_content( |
| 79 | + ["/// cxx comment", "/// with multiple lines"], |
| 80 | + tmethod.comment |
| 81 | + ) |
| 82 | + |
| 83 | + tconstructor = tclass.constructors()[0] |
| 84 | + |
| 85 | + assert "comment" in dir(tconstructor) |
| 86 | + _check_comment_content( |
| 87 | + ["/** doc comment */"], |
| 88 | + tconstructor.comment |
| 89 | + ) |
| 90 | + |
| 91 | + for indx, cmt in enumerate( |
| 92 | + [ |
| 93 | + '//! mutable field comment', |
| 94 | + "/// bit field comment" |
| 95 | + ] |
| 96 | + ): |
| 97 | + tvariable = tclass.variables()[indx] |
| 98 | + assert "comment" in dir(tvariable) |
| 99 | + _check_comment_content([cmt], tvariable.comment) |
0 commit comments