forked from satya-das/cppparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute-specifier-sequence.cpp
More file actions
142 lines (112 loc) · 5.53 KB
/
attribute-specifier-sequence.cpp
File metadata and controls
142 lines (112 loc) · 5.53 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <catch/catch.hpp>
#include "cppparser/cppparser.h"
#include "embedded-snippet-test-base.h"
#include <string>
class CppAtributeTest : public EmbeddedSnippetTestBase
{
protected:
CppAtributeTest()
: EmbeddedSnippetTestBase(__FILE__)
{
}
};
TEST_CASE_METHOD(CppAtributeTest, "Attribute specifier sequence")
{
#if TEST_CASE_SNIPPET_STARTS_FROM_NEXT_LINE
struct [[xnet::ValidateData]] SignUpRequest
{
[[xnet::StringNotEmpty]] [[xnet::StringMinLength(2)]] std::string name;
};
class [[xnet::HttpController]] [[xnet::Route("/plakmp")]] PlakMpApiController
{
public:
[[xnet::HttpGet]] [[xnet::Route("/players")]] std::string GetPlakMpPlayers(std::string first_name);
[[xnet::HttpGet]] std::string GetHelloWorld();
[[xnet::HttpPost]] [[xnet::Route("/register")]] std::string CreateAccount(
[[xnet::FromBody]] [[xnet::EnsureValid]] SignUpRequest request);
[[xnet::HttpGet, xnet::Route("/entities"), xnet::Route("/entity")]] std::string GetEntities();
[[]] [[]] void UnannotatedHelper();
};
#endif
auto testSnippet = getTestSnippetParseStream(__LINE__ - 2);
cppparser::CppParser parser;
const auto ast = parser.parseStream(testSnippet.data(), testSnippet.size());
REQUIRE(ast != nullptr);
const auto members = GetAllOwnedEntities(*ast);
REQUIRE(members.size() == 2);
cppast::CppConstCompoundEPtr structDefn = members[0];
REQUIRE(structDefn);
const auto structAttribSeq = GetAllAttributeSpecifiers(*structDefn);
REQUIRE(structAttribSeq.size() == 1);
cppast::CppConstNameExprEPtr structAttrib = structAttribSeq.at(0);
REQUIRE(structAttrib);
CHECK((*structAttrib) == cppast::CppNameExpr("xnet::ValidateData"));
const auto structMembers = GetAllOwnedEntities(*structDefn);
REQUIRE(structMembers.size() == 1);
cppast::CppConstCompoundEPtr classDefn = members[1];
REQUIRE(classDefn);
const auto classAttribSeq = GetAllAttributeSpecifiers(*classDefn);
REQUIRE(classAttribSeq.size() == 2);
cppast::CppConstNameExprEPtr classAttrib0 = classAttribSeq.at(0);
REQUIRE(classAttrib0);
CHECK((*classAttrib0) == cppast::CppNameExpr("xnet::HttpController"));
cppast::CppConstFunctionCallExprEPtr classAttrib1 = classAttribSeq.at(1);
REQUIRE(classAttrib1);
REQUIRE(classAttrib1->numArgs() == 1);
cppast::CppConstStringLiteralExprEPtr classAttrib1Arg = &(classAttrib1->arg(0));
REQUIRE(classAttrib1Arg);
CHECK((*classAttrib1Arg) == cppast::CppStringLiteralExpr("\"/plakmp\""));
const auto classMembers = GetAllOwnedEntities(*classDefn);
REQUIRE(classMembers.size() == 6);
const cppast::CppConstFunctionEPtr methodGetPlakMpPlayers = classMembers[1];
REQUIRE(methodGetPlakMpPlayers);
const auto* returnTypeGetPlakMpPlayers = methodGetPlakMpPlayers->returnType();
REQUIRE(returnTypeGetPlakMpPlayers);
const auto attribSeqGetPlakMpPlayers = GetAllAttributeSpecifiers(*returnTypeGetPlakMpPlayers);
REQUIRE(attribSeqGetPlakMpPlayers.size() == 2);
cppast::CppConstNameExprEPtr methodAttrib0 = attribSeqGetPlakMpPlayers.at(0);
REQUIRE(methodAttrib0);
CHECK((*methodAttrib0) == cppast::CppNameExpr("xnet::HttpGet"));
cppast::CppConstFunctionCallExprEPtr methodAttrib1 = attribSeqGetPlakMpPlayers.at(1);
REQUIRE(methodAttrib1);
cppast::CppConstNameExprEPtr methodAttrib1Func = &(methodAttrib1->function());
REQUIRE(methodAttrib1Func);
CHECK((*methodAttrib1Func) == cppast::CppNameExpr("xnet::Route"));
REQUIRE(methodAttrib1->numArgs() == 1);
cppast::CppConstStringLiteralExprEPtr methodAttrib1Arg = &(methodAttrib1->arg(0));
REQUIRE(methodAttrib1Arg);
CHECK((*methodAttrib1Arg) == cppast::CppStringLiteralExpr("\"/players\""));
const cppast::CppConstFunctionEPtr methodGetEntities = classMembers[4];
REQUIRE(methodGetEntities);
const auto* returnTypeGetEntities = methodGetEntities->returnType();
REQUIRE(returnTypeGetEntities);
const auto attribSeqGetEntities = GetAllAttributeSpecifiers(*returnTypeGetEntities);
REQUIRE(attribSeqGetEntities.size() == 3);
cppast::CppConstNameExprEPtr method2Attrib0 = attribSeqGetEntities.at(0);
REQUIRE(method2Attrib0);
CHECK((*method2Attrib0) == cppast::CppNameExpr("xnet::HttpGet"));
cppast::CppConstFunctionCallExprEPtr method2Attrib1 = attribSeqGetEntities.at(1);
REQUIRE(method2Attrib1);
cppast::CppConstNameExprEPtr method2Attrib1Func = &(method2Attrib1->function());
REQUIRE(method2Attrib1Func);
CHECK((*method2Attrib1Func) == cppast::CppNameExpr("xnet::Route"));
REQUIRE(method2Attrib1->numArgs() == 1);
cppast::CppConstStringLiteralExprEPtr method2Attrib1Arg = &(method2Attrib1->arg(0));
REQUIRE(method2Attrib1Arg);
CHECK((*method2Attrib1Arg) == cppast::CppStringLiteralExpr("\"/entities\""));
cppast::CppConstFunctionCallExprEPtr method2Attrib2 = attribSeqGetEntities.at(2);
REQUIRE(method2Attrib2);
cppast::CppConstNameExprEPtr method2Attrib2Func = &(method2Attrib2->function());
REQUIRE(method2Attrib2Func);
CHECK((*method2Attrib2Func) == cppast::CppNameExpr("xnet::Route"));
REQUIRE(method2Attrib2->numArgs() == 1);
cppast::CppConstStringLiteralExprEPtr method2Attrib2Arg = &(method2Attrib2->arg(0));
REQUIRE(method2Attrib2Arg);
CHECK((*method2Attrib2Arg) == cppast::CppStringLiteralExpr("\"/entity\""));
const cppast::CppConstFunctionEPtr methodUnannotatedHelper = classMembers[5];
REQUIRE(methodUnannotatedHelper);
const auto* returnTypeUnannotatedHelper = methodUnannotatedHelper->returnType();
REQUIRE(returnTypeUnannotatedHelper);
const auto attribSeqUnannotatedHelper = GetAllAttributeSpecifiers(*returnTypeUnannotatedHelper);
REQUIRE(attribSeqUnannotatedHelper.size() == 0);
}