| 1 |
# Note: testcases generated via `python -m cxxheaderparser.gentest` |
| 2 |
|
| 3 |
from cxxheaderparser.errors import CxxParseError |
| 4 |
from cxxheaderparser.types import ( |
| 5 |
BaseClass, |
| 6 |
ClassDecl, |
| 7 |
Function, |
| 8 |
FundamentalSpecifier, |
| 9 |
NameSpecifier, |
| 10 |
PQName, |
| 11 |
Parameter, |
| 12 |
Token, |
| 13 |
Type, |
| 14 |
Value, |
| 15 |
Variable, |
| 16 |
) |
| 17 |
from cxxheaderparser.simple import ( |
| 18 |
ClassScope, |
| 19 |
Include, |
| 20 |
NamespaceScope, |
| 21 |
Pragma, |
| 22 |
parse_string, |
| 23 |
ParsedData, |
| 24 |
) |
| 25 |
|
| 26 |
import pytest |
| 27 |
|
| 28 |
# |
| 29 |
# minimal preprocessor support |
| 30 |
# |
| 31 |
|
| 32 |
|
| 33 |
def test_includes() -> None: |
| 34 |
content = """ |
| 35 |
#include <global.h> |
| 36 |
#include "local.h" |
| 37 |
# include "space.h" |
| 38 |
""" |
| 39 |
data = parse_string(content, cleandoc=True) |
| 40 |
|
| 41 |
assert data == ParsedData( |
| 42 |
includes=[Include("<global.h>"), Include('"local.h"'), Include('"space.h"')] |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
def test_pragma() -> None: |
| 47 |
content = """ |
| 48 |
|
| 49 |
#pragma once |
| 50 |
|
| 51 |
""" |
| 52 |
data = parse_string(content, cleandoc=True) |
| 53 |
|
| 54 |
assert data == ParsedData( |
| 55 |
pragmas=[Pragma(content=Value(tokens=[Token(value="once")]))] |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
def test_pragma_more() -> None: |
| 60 |
content = """ |
| 61 |
|
| 62 |
#pragma (some content here) |
| 63 |
#pragma (even \ |
| 64 |
more \ |
| 65 |
content here) |
| 66 |
|
| 67 |
""" |
| 68 |
data = parse_string(content, cleandoc=True) |
| 69 |
|
| 70 |
assert data == ParsedData( |
| 71 |
pragmas=[ |
| 72 |
Pragma( |
| 73 |
content=Value( |
| 74 |
tokens=[ |
| 75 |
Token(value="("), |
| 76 |
Token(value="some"), |
| 77 |
Token(value="content"), |
| 78 |
Token(value="here"), |
| 79 |
Token(value=")"), |
| 80 |
] |
| 81 |
) |
| 82 |
), |
| 83 |
Pragma( |
| 84 |
content=Value( |
| 85 |
tokens=[ |
| 86 |
Token(value="("), |
| 87 |
Token(value="even"), |
| 88 |
Token(value="more"), |
| 89 |
Token(value="content"), |
| 90 |
Token(value="here"), |
| 91 |
Token(value=")"), |
| 92 |
] |
| 93 |
) |
| 94 |
), |
| 95 |
] |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
def test_line_and_define() -> None: |
| 100 |
content = """ |
| 101 |
// this should work + change line number of error |
| 102 |
#line 40 "filename.h" |
| 103 |
// this should fail |
| 104 |
#define 1 |
| 105 |
""" |
| 106 |
with pytest.raises(CxxParseError) as e: |
| 107 |
parse_string(content, cleandoc=True) |
| 108 |
|
| 109 |
assert "filename.h:41" in str(e.value) |
| 110 |
|
| 111 |
|
| 112 |
# |
| 113 |
# extern "C" |
| 114 |
# |
| 115 |
|
| 116 |
|
| 117 |
def test_extern_c() -> None: |
| 118 |
content = """ |
| 119 |
extern "C" { |
| 120 |
int x; |
| 121 |
}; |
| 122 |
|
| 123 |
int y; |
| 124 |
""" |
| 125 |
data = parse_string(content, cleandoc=True) |
| 126 |
|
| 127 |
assert data == ParsedData( |
| 128 |
namespace=NamespaceScope( |
| 129 |
variables=[ |
| 130 |
Variable( |
| 131 |
name=PQName(segments=[NameSpecifier(name="x")]), |
| 132 |
type=Type( |
| 133 |
typename=PQName(segments=[FundamentalSpecifier(name="int")]) |
| 134 |
), |
| 135 |
), |
| 136 |
Variable( |
| 137 |
name=PQName(segments=[NameSpecifier(name="y")]), |
| 138 |
type=Type( |
| 139 |
typename=PQName(segments=[FundamentalSpecifier(name="int")]) |
| 140 |
), |
| 141 |
), |
| 142 |
] |
| 143 |
) |
| 144 |
) |
| 145 |
|
| 146 |
|
| 147 |
def test_misc_extern_inline() -> None: |
| 148 |
content = """ |
| 149 |
extern "C++" { |
| 150 |
inline HAL_Value HAL_GetSimValue(HAL_SimValueHandle handle) { |
| 151 |
HAL_Value v; |
| 152 |
return v; |
| 153 |
} |
| 154 |
} // extern "C++" |
| 155 |
""" |
| 156 |
data = parse_string(content, cleandoc=True) |
| 157 |
|
| 158 |
assert data == ParsedData( |
| 159 |
namespace=NamespaceScope( |
| 160 |
functions=[ |
| 161 |
Function( |
| 162 |
return_type=Type( |
| 163 |
typename=PQName(segments=[NameSpecifier(name="HAL_Value")]) |
| 164 |
), |
| 165 |
name=PQName(segments=[NameSpecifier(name="HAL_GetSimValue")]), |
| 166 |
parameters=[ |
| 167 |
Parameter( |
| 168 |
type=Type( |
| 169 |
typename=PQName( |
| 170 |
segments=[NameSpecifier(name="HAL_SimValueHandle")] |
| 171 |
) |
| 172 |
), |
| 173 |
name="handle", |
| 174 |
) |
| 175 |
], |
| 176 |
inline=True, |
| 177 |
has_body=True, |
| 178 |
) |
| 179 |
] |
| 180 |
) |
| 181 |
) |
| 182 |
|
| 183 |
|
| 184 |
# |
| 185 |
# Misc |
| 186 |
# |
| 187 |
|
| 188 |
|
| 189 |
def test_static_assert_1() -> None: |
| 190 |
# static_assert should be ignored |
| 191 |
content = """ |
| 192 |
static_assert(x == 1); |
| 193 |
""" |
| 194 |
data = parse_string(content, cleandoc=True) |
| 195 |
|
| 196 |
assert data == ParsedData() |
| 197 |
|
| 198 |
|
| 199 |
def test_static_assert_2() -> None: |
| 200 |
# static_assert should be ignored |
| 201 |
content = """ |
| 202 |
static_assert(sizeof(int) == 4, |
| 203 |
"integer size is wrong" |
| 204 |
"for some reason"); |
| 205 |
""" |
| 206 |
data = parse_string(content, cleandoc=True) |
| 207 |
|
| 208 |
assert data == ParsedData() |
| 209 |
|
| 210 |
|
| 211 |
def test_comment_eof() -> None: |
| 212 |
content = """ |
| 213 |
namespace a {} // namespace a""" |
| 214 |
data = parse_string(content, cleandoc=True) |
| 215 |
|
| 216 |
assert data == ParsedData( |
| 217 |
namespace=NamespaceScope(namespaces={"a": NamespaceScope(name="a")}) |
| 218 |
) |
| 219 |
|
| 220 |
|
| 221 |
def test_final() -> None: |
| 222 |
content = """ |
| 223 |
// ok here |
| 224 |
int fn(const int final); |
| 225 |
|
| 226 |
// ok here |
| 227 |
int final = 2; |
| 228 |
|
| 229 |
// but it's a keyword here |
| 230 |
struct B final : A {}; |
| 231 |
""" |
| 232 |
data = parse_string(content, cleandoc=True) |
| 233 |
|
| 234 |
assert data == ParsedData( |
| 235 |
namespace=NamespaceScope( |
| 236 |
classes=[ |
| 237 |
ClassScope( |
| 238 |
class_decl=ClassDecl( |
| 239 |
typename=PQName( |
| 240 |
segments=[NameSpecifier(name="B")], classkey="struct" |
| 241 |
), |
| 242 |
bases=[ |
| 243 |
BaseClass( |
| 244 |
access="public", |
| 245 |
typename=PQName(segments=[NameSpecifier(name="A")]), |
| 246 |
) |
| 247 |
], |
| 248 |
final=True, |
| 249 |
) |
| 250 |
) |
| 251 |
], |
| 252 |
functions=[ |
| 253 |
Function( |
| 254 |
return_type=Type( |
| 255 |
typename=PQName(segments=[FundamentalSpecifier(name="int")]) |
| 256 |
), |
| 257 |
name=PQName(segments=[NameSpecifier(name="fn")]), |
| 258 |
parameters=[ |
| 259 |
Parameter( |
| 260 |
type=Type( |
| 261 |
typename=PQName( |
| 262 |
segments=[FundamentalSpecifier(name="int")] |
| 263 |
), |
| 264 |
const=True, |
| 265 |
), |
| 266 |
name="final", |
| 267 |
) |
| 268 |
], |
| 269 |
) |
| 270 |
], |
| 271 |
variables=[ |
| 272 |
Variable( |
| 273 |
name=PQName(segments=[NameSpecifier(name="final")]), |
| 274 |
type=Type( |
| 275 |
typename=PQName(segments=[FundamentalSpecifier(name="int")]) |
| 276 |
), |
| 277 |
value=Value(tokens=[Token(value="2")]), |
| 278 |
) |
| 279 |
], |
| 280 |
) |
| 281 |
) |
| 282 |
|
| 283 |
|
| 284 |
# |
| 285 |
# User defined literals |
| 286 |
# |
| 287 |
|
| 288 |
|
| 289 |
def test_user_defined_literal() -> None: |
| 290 |
content = """ |
| 291 |
units::volt_t v = 1_V; |
| 292 |
""" |
| 293 |
data = parse_string(content, cleandoc=True) |
| 294 |
|
| 295 |
assert data == ParsedData( |
| 296 |
namespace=NamespaceScope( |
| 297 |
variables=[ |
| 298 |
Variable( |
| 299 |
name=PQName(segments=[NameSpecifier(name="v")]), |
| 300 |
type=Type( |
| 301 |
typename=PQName( |
| 302 |
segments=[ |
| 303 |
NameSpecifier(name="units"), |
| 304 |
NameSpecifier(name="volt_t"), |
| 305 |
] |
| 306 |
) |
| 307 |
), |
| 308 |
value=Value(tokens=[Token(value="1_V")]), |
| 309 |
) |
| 310 |
] |
| 311 |
) |
| 312 |
) |
| 313 |
|
| 314 |
|
| 315 |
# |
| 316 |
# Line continuation |
| 317 |
# |
| 318 |
|
| 319 |
|
| 320 |
def test_line_continuation() -> None: |
| 321 |
content = """ |
| 322 |
static int \ |
| 323 |
variable; |
| 324 |
""" |
| 325 |
data = parse_string(content, cleandoc=True) |
| 326 |
|
| 327 |
assert data == ParsedData( |
| 328 |
namespace=NamespaceScope( |
| 329 |
variables=[ |
| 330 |
Variable( |
| 331 |
name=PQName(segments=[NameSpecifier(name="variable")]), |
| 332 |
type=Type( |
| 333 |
typename=PQName(segments=[FundamentalSpecifier(name="int")]) |
| 334 |
), |
| 335 |
static=True, |
| 336 |
) |
| 337 |
] |
| 338 |
) |
| 339 |
) |
| 340 |
|
| 341 |
|
| 342 |
# |
| 343 |
# #warning (C++23) |
| 344 |
# |
| 345 |
|
| 346 |
|
| 347 |
def test_warning_directive() -> None: |
| 348 |
content = """ |
| 349 |
#warning "this is a warning" |
| 350 |
""" |
| 351 |
data = parse_string(content, cleandoc=True) |
| 352 |
|
| 353 |
assert data == ParsedData() |