1 | // |
2 | // CppParserTest.cpp |
3 | // |
4 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "CppParserTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/CppParser/Utility.h" |
15 | #include "Poco/CppParser/Symbol.h" |
16 | #include "Poco/CppParser/CppToken.h" |
17 | #include <sstream> |
18 | #include <iostream> |
19 | |
20 | |
21 | using namespace Poco::CppParser; |
22 | |
23 | |
24 | std::string linker("cl" ); |
25 | std::string options("/I \"C:\\Program Files\\Microsoft Visual Studio 8\\VC\\INCLUDE\", " |
26 | "/I \"C:\\Program Files\\Microsoft Visual Studio 8\\VC\\PlatformSDK\\include\", " |
27 | "/I \"p:\\poco\\Foundation\\include\", " |
28 | "/I \"p:\\poco\\XML\\include\", " |
29 | "/I \"p:\\poco\\Util\\include\", " |
30 | "/I \"p:\\poco\\Net\\include\", " |
31 | "/D \"WIN32\", " |
32 | "/D \"_DEBUG\", " |
33 | "/D \"_WINDOWS\", " |
34 | "/D \"_MBCS\", " |
35 | "/C, /P, /TP" ); |
36 | std::string path("C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE;C:\\Program Files\\Microsoft Visual Studio 8\\VC\\BIN;C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\Tools;;C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\Tools\\bin" ); |
37 | |
38 | |
39 | CppParserTest::CppParserTest(const std::string& name): CppUnit::TestCase(name) |
40 | { |
41 | } |
42 | |
43 | |
44 | CppParserTest::~CppParserTest() |
45 | { |
46 | } |
47 | |
48 | |
49 | void CppParserTest::testParseDir() |
50 | { |
51 | //FIXME: implement some proper tests |
52 | return; |
53 | NameSpace::SymbolTable st; |
54 | std::vector<std::string> inc; |
55 | inc.push_back("./*.h" ); |
56 | std::vector<std::string> exc; |
57 | Utility::parseDir(inc, exc, st, linker, options, path); |
58 | |
59 | NameSpace::SymbolTable::const_iterator it = st.begin(); |
60 | NameSpace::SymbolTable::const_iterator itEnd = st.end(); |
61 | for (; it != itEnd; ++it) |
62 | { |
63 | std::cout << it->first << ": " ; |
64 | Symbol* pSym = it->second; |
65 | std::cout << pSym->name() << ", " << pSym->getDocumentation() << "\n" ; |
66 | |
67 | } |
68 | } |
69 | |
70 | |
71 | void CppParserTest::() |
72 | { |
73 | std::string decl("int _var" ); |
74 | std::string name = Symbol::extractName(decl); |
75 | assertTrue (name == "_var" ); |
76 | |
77 | decl = "void func(int arg1, int arg2)" ; |
78 | name = Symbol::extractName(decl); |
79 | assertTrue (name == "func" ); |
80 | |
81 | decl = "const std::vector<NS::MyType>* var" ; |
82 | name = Symbol::extractName(decl); |
83 | assertTrue (name == "var" ); |
84 | |
85 | decl = "const std::vector<NS::MyType>* func(int arg) = 0" ; |
86 | name = Symbol::extractName(decl); |
87 | assertTrue (name == "func" ); |
88 | |
89 | decl = "int (*func)(int, const std::string&)" ; |
90 | name = Symbol::extractName(decl); |
91 | assertTrue (name == "func" ); |
92 | |
93 | decl = "int ( * func )(int, const std::string&)" ; |
94 | name = Symbol::extractName(decl); |
95 | assertTrue (name == "func" ); |
96 | |
97 | decl = "template <typename A, typename B> B func(A a, B b)" ; |
98 | name = Symbol::extractName(decl); |
99 | assertTrue (name == "func" ); |
100 | |
101 | decl = "template <typename A, typename B> class Class" ; |
102 | name = Symbol::extractName(decl); |
103 | assertTrue (name == "Class" ); |
104 | |
105 | decl = "template <> class Class<int, std::string>" ; |
106 | name = Symbol::extractName(decl); |
107 | assertTrue (name == "Class" ); |
108 | |
109 | decl = "template <> class Class <int, std::string>" ; |
110 | name = Symbol::extractName(decl); |
111 | assertTrue (name == "Class" ); |
112 | |
113 | decl = "template <> class Class<int, MyTemplate<int> >" ; |
114 | name = Symbol::extractName(decl); |
115 | assertTrue (name == "Class" ); |
116 | } |
117 | |
118 | |
119 | void CppParserTest::testNumberLiterals() |
120 | { |
121 | testNumberLiteral("123" ); |
122 | testNumberLiteral("0123" ); |
123 | testNumberLiteral("0x123" ); |
124 | testNumberLiteral("0XABC" ); |
125 | testNumberLiteral("0b010101" ); |
126 | testNumberLiteral("0B101010" ); |
127 | |
128 | testNumberLiteral("123L" ); |
129 | testNumberLiteral("0123L" ); |
130 | testNumberLiteral("0x123L" ); |
131 | testNumberLiteral("0XABCL" ); |
132 | testNumberLiteral("0b010101L" ); |
133 | testNumberLiteral("0B101010L" ); |
134 | |
135 | testNumberLiteral("123LL" ); |
136 | testNumberLiteral("0123LL" ); |
137 | testNumberLiteral("0x123LL" ); |
138 | testNumberLiteral("0XABCLL" ); |
139 | testNumberLiteral("0b010101LL" ); |
140 | testNumberLiteral("0B101010LL" ); |
141 | |
142 | testNumberLiteral("123U" ); |
143 | testNumberLiteral("0123U" ); |
144 | testNumberLiteral("0x123U" ); |
145 | testNumberLiteral("0XABCU" ); |
146 | testNumberLiteral("0b010101U" ); |
147 | testNumberLiteral("0B101010U" ); |
148 | |
149 | testNumberLiteral("123UL" ); |
150 | testNumberLiteral("0123UL" ); |
151 | testNumberLiteral("0x123UL" ); |
152 | testNumberLiteral("0XABCUL" ); |
153 | testNumberLiteral("0b010101UL" ); |
154 | testNumberLiteral("0B101010UL" ); |
155 | |
156 | testNumberLiteral("1.23" ); |
157 | testNumberLiteral(".123" ); |
158 | testNumberLiteral("1.23e+3" ); |
159 | testNumberLiteral("1.23E-3" ); |
160 | |
161 | testNumberLiteral("0x1fp1" ); |
162 | testNumberLiteral("0x1f.e3p+12" ); |
163 | testNumberLiteral("0x1f.e3p-12" ); |
164 | |
165 | testNumberLiteral("123'456" ); |
166 | testNumberLiteral("1'234'567" ); |
167 | testNumberLiteral("0x1234'5678" ); |
168 | testNumberLiteral("0xFFFF'FFFFULL" ); |
169 | } |
170 | |
171 | |
172 | void CppParserTest::testNumberLiteral(const std::string& literal) |
173 | { |
174 | NumberLiteralToken tok; |
175 | |
176 | std::istringstream istr(literal); |
177 | assertTrue (tok.start(istr.get(), istr)); |
178 | tok.finish(istr); |
179 | assertEquals (tok.tokenString(), literal); |
180 | } |
181 | |
182 | |
183 | void CppParserTest::setUp() |
184 | { |
185 | } |
186 | |
187 | |
188 | void CppParserTest::tearDown() |
189 | { |
190 | } |
191 | |
192 | |
193 | CppUnit::Test* CppParserTest::suite() |
194 | { |
195 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("CppParserTest" ); |
196 | |
197 | CppUnit_addTest(pSuite, CppParserTest, testParseDir); |
198 | CppUnit_addTest(pSuite, CppParserTest, testExtractName); |
199 | CppUnit_addTest(pSuite, CppParserTest, testNumberLiterals); |
200 | |
201 | return pSuite; |
202 | } |
203 | |