forked from standardese/cppast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_member_variable.cpp
More file actions
112 lines (102 loc) · 2.98 KB
/
Copy pathcpp_member_variable.cpp
File metadata and controls
112 lines (102 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2017-2018 Jonathan Müller <[email protected]>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include <cppast/cpp_member_variable.hpp>
#include "test_parser.hpp"
using namespace cppast;
TEST_CASE("cpp_member_variable")
{
auto code = R"(
struct foo
{
/// int a;
int a;
/// float b=3.14f;
float b = 3.14f;
/// mutable char c;
mutable char c;
};
)";
cpp_entity_index idx;
auto file = parse(idx, "cpp_member_variable.cpp", code);
auto count = test_visit<cpp_member_variable>(*file, [&](const cpp_member_variable& var) {
if (var.name() == "a")
{
auto type = cpp_builtin_type::build(cpp_int);
REQUIRE(equal_types(idx, var.type(), *type));
REQUIRE(!var.default_value());
REQUIRE(!var.is_mutable());
}
else if (var.name() == "b")
{
auto type = cpp_builtin_type::build(cpp_float);
REQUIRE(equal_types(idx, var.type(), *type));
// all initializers are unexposed
auto def = cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_float),
cpp_token_string::tokenize("3.14f"));
REQUIRE(var.default_value());
REQUIRE(equal_expressions(var.default_value().value(), *def));
REQUIRE(!var.is_mutable());
}
else if (var.name() == "c")
{
auto type = cpp_builtin_type::build(cpp_char);
REQUIRE(equal_types(idx, var.type(), *type));
REQUIRE(!var.default_value());
REQUIRE(var.is_mutable());
}
else
REQUIRE(false);
});
REQUIRE(count == 3u);
}
TEST_CASE("cpp_bitfield")
{
auto code = R"(
struct foo
{
/// char a:3;
char a : 3;
/// mutable char b:2;
mutable char b : 2;
/// char:0;
char : 0;
/// char c:3;
char c : 3;
/// char:4;
char : 4;
};
)";
auto file = parse({}, "cpp_bitfield.cpp", code);
auto count = test_visit<cpp_bitfield>(*file, [&](const cpp_bitfield& bf) {
REQUIRE(!bf.default_value());
REQUIRE(equal_types({}, bf.type(), *cpp_builtin_type::build(cpp_char)));
if (bf.name() == "a")
{
REQUIRE(bf.no_bits() == 3u);
REQUIRE(!bf.is_mutable());
}
else if (bf.name() == "b")
{
REQUIRE(bf.no_bits() == 2u);
REQUIRE(bf.is_mutable());
}
else if (bf.name() == "c")
{
REQUIRE(bf.no_bits() == 3u);
REQUIRE(!bf.is_mutable());
}
else if (bf.name() == "")
{
REQUIRE(!bf.is_mutable());
if (bf.no_bits() != 0u && bf.no_bits() != 4u)
{
INFO(bf.no_bits());
REQUIRE(false);
}
}
else
REQUIRE(false);
});
REQUIRE(count == 5u);
}