/
DeclManager.cpp
344 lines (262 loc) · 11.6 KB
/
DeclManager.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "RadiantTest.h"
#include "ideclmanager.h"
#include "testutil/TemporaryFile.h"
namespace test
{
using DeclManagerTest = RadiantTest;
class TestDeclaration :
public decl::IDeclaration
{
private:
decl::Type _type;
std::string _name;
decl::DeclarationBlockSyntax _block;
public:
TestDeclaration(decl::Type type, const std::string& name) :
_type(type),
_name(name)
{}
const std::string& getDeclName() const override
{
return _name;
}
decl::Type getDeclType() const override
{
return _type;
}
const decl::DeclarationBlockSyntax& getBlockSyntax() const override
{
return _block;
}
void parseFromBlock(const decl::DeclarationBlockSyntax& block) override
{
_block = block;
}
};
class TestDeclarationCreator :
public decl::IDeclarationCreator
{
public:
bool processingDisabled = false;
decl::Type getDeclType() const override
{
return decl::Type::Material;
}
decl::IDeclaration::Ptr createDeclaration(const std::string& name) override
{
while (processingDisabled)
{
using namespace std::chrono_literals;
std::this_thread::sleep_for(20ms);
}
return std::make_shared<TestDeclaration>(getDeclType(), name);
}
};
class TestDeclaration2Creator :
public decl::IDeclarationCreator
{
public:
decl::Type getDeclType() const override
{
return decl::Type::Model;
}
decl::IDeclaration::Ptr createDeclaration(const std::string& name) override
{
return std::make_shared<TestDeclaration>(getDeclType(), name);
}
};
TEST_F(DeclManagerTest, DeclTypeRegistration)
{
auto creator = std::make_shared<TestDeclarationCreator>();
EXPECT_NO_THROW(GlobalDeclarationManager().registerDeclType("testdecl", creator));
// Registering the same type name twice should result in an exception
EXPECT_THROW(GlobalDeclarationManager().registerDeclType("testdecl", creator), std::logic_error);
// Passing a new creator instance doesn't help either
auto creator2 = std::make_shared<TestDeclarationCreator>();
EXPECT_THROW(GlobalDeclarationManager().registerDeclType("testdecl", creator2), std::logic_error);
}
TEST_F(DeclManagerTest, DeclTypeUnregistration)
{
auto creator = std::make_shared<TestDeclarationCreator>();
GlobalDeclarationManager().registerDeclType("testdecl", creator);
// Unregistering the creator should succeed
EXPECT_NO_THROW(GlobalDeclarationManager().unregisterDeclType("testdecl"));
// Trying to unregister it twice should result in an exception
EXPECT_THROW(GlobalDeclarationManager().unregisterDeclType("testdecl"), std::logic_error);
}
inline std::set<std::string> getAllDeclNames(decl::Type type)
{
// Iterate over all decls and collect the names
std::set<std::string> foundNames;
GlobalDeclarationManager().foreachDeclaration(type, [&](const decl::IDeclaration& declaration)
{
foundNames.insert(declaration.getDeclName());
});
return foundNames;
}
inline void checkKnownTestDeclNames()
{
auto foundNames = getAllDeclNames(decl::Type::Material);
// Check a few known ones
EXPECT_TRUE(foundNames.count("decl/exporttest/guisurf1") > 0);
EXPECT_TRUE(foundNames.count("decl/numbers/0") > 0);
EXPECT_TRUE(foundNames.count("decl/numbers/1") > 0);
EXPECT_TRUE(foundNames.count("decl/numbers/2") > 0);
// decltables should not be listed
EXPECT_FALSE(foundNames.count("decltable1") > 0);
EXPECT_FALSE(foundNames.count("decltable2") > 0);
EXPECT_FALSE(foundNames.count("decltable3") > 0);
}
inline void checkKnownTestDecl2Names()
{
auto foundNames = getAllDeclNames(decl::Type::Model);
// Assume testdecls are not listed
EXPECT_FALSE(foundNames.count("decl/exporttest/guisurf1") > 0);
EXPECT_FALSE(foundNames.count("decl/numbers/0") > 0);
EXPECT_FALSE(foundNames.count("decl/numbers/1") > 0);
EXPECT_FALSE(foundNames.count("decl/numbers/2") > 0);
// testdecl2 should be listed
EXPECT_TRUE(foundNames.count("decltable1") > 0);
EXPECT_TRUE(foundNames.count("decltable2") > 0);
EXPECT_TRUE(foundNames.count("decltable3") > 0);
}
TEST_F(DeclManagerTest, DeclFolderRegistration)
{
GlobalDeclarationManager().registerDeclType("testdecl", std::make_shared<TestDeclarationCreator>());
GlobalDeclarationManager().registerDeclFolder(decl::Type::Material, "testdecls/", "decl");
checkKnownTestDeclNames();
}
TEST_F(DeclManagerTest, DeclFolderRegistrationWithoutSlash)
{
GlobalDeclarationManager().registerDeclType("testdecl", std::make_shared<TestDeclarationCreator>());
// Omit the trailing slash, should work just fine
GlobalDeclarationManager().registerDeclFolder(decl::Type::Material, "testdecls", "decl");
checkKnownTestDeclNames();
}
TEST_F(DeclManagerTest, DeclFolderRegistrationWithExtensionDot)
{
GlobalDeclarationManager().registerDeclType("testdecl", std::make_shared<TestDeclarationCreator>());
// Add the dot to the file extension, should work just fine
GlobalDeclarationManager().registerDeclFolder(decl::Type::Material, "testdecls", ".decl");
checkKnownTestDeclNames();
}
// Test a second decl creator
TEST_F(DeclManagerTest, DeclTypeCreatorRegistration)
{
GlobalDeclarationManager().registerDeclType("testdecl", std::make_shared<TestDeclarationCreator>());
GlobalDeclarationManager().registerDeclType("testdecl2", std::make_shared<TestDeclaration2Creator>());
// Parse this folder, it contains decls of type testdecl and testdecl2 in the .decl files
GlobalDeclarationManager().registerDeclFolder(decl::Type::Material, "testdecls", ".decl");
checkKnownTestDeclNames();
checkKnownTestDecl2Names();
}
// Test that a creator coming late to the party is immediately fed with the buffered decl blocks
TEST_F(DeclManagerTest, LateCreatorRegistration)
{
auto creator = std::make_shared<TestDeclarationCreator>();
GlobalDeclarationManager().registerDeclType("testdecl", std::make_shared<TestDeclarationCreator>());
// Parse this folder, it contains decls of type testdecl and testdecl2 in the .decl files
GlobalDeclarationManager().registerDeclFolder(decl::Type::Material, "testdecls", ".decl");
// Let the testdecl creator finish its work
getAllDeclNames(decl::Type::Material);
auto foundTestDecl2Names = getAllDeclNames(decl::Type::Model);
EXPECT_FALSE(foundTestDecl2Names.count("decltable1") > 0);
EXPECT_FALSE(foundTestDecl2Names.count("decltable2") > 0);
EXPECT_FALSE(foundTestDecl2Names.count("decltable3") > 0);
// Register the testdecl2 creator now, it should be used by the decl manager to parse the missing pieces
GlobalDeclarationManager().registerDeclType("testdecl2", std::make_shared<TestDeclaration2Creator>());
// Everything should be registered now
checkKnownTestDecl2Names();
}
TEST_F(DeclManagerTest, CreatorRegistrationDuringRunningThread)
{
auto creator = std::make_shared<TestDeclarationCreator>();
// Hold back this creator until we let it go in this fixture
creator->processingDisabled = true;
GlobalDeclarationManager().registerDeclType("testdecl", creator);
// Parse this folder, it contains decls of type testdecl and testdecl2 in the .decl files
GlobalDeclarationManager().registerDeclFolder(decl::Type::Material, "testdecls", ".decl");
auto foundTestDecl2Names = getAllDeclNames(decl::Type::Model);
EXPECT_FALSE(foundTestDecl2Names.count("decltable1") > 0);
// Register the testdecl2 creator now, it should be used by the decl manager to parse the missing pieces
GlobalDeclarationManager().registerDeclType("testdecl2", std::make_shared<TestDeclaration2Creator>());
// The first thread is still running, so we didn't get any unrecognised decls reported
foundTestDecl2Names = getAllDeclNames(decl::Type::Model);
EXPECT_FALSE(foundTestDecl2Names.count("decltable1") > 0);
// Let the testdecl creator finish its work
creator->processingDisabled = false;
getAllDeclNames(decl::Type::Material);
// Everything should be registered now
checkKnownTestDecl2Names();
}
TEST_F(DeclManagerTest, DeclsReloadedSignal)
{
auto creator = std::make_shared<TestDeclarationCreator>();
GlobalDeclarationManager().registerDeclType("testdecl", creator);
bool materialSignalFired = false;
bool modelSignalFired = false;
GlobalDeclarationManager().signal_DeclsReloaded(decl::Type::Material).connect(
[&]() { materialSignalFired = true; }
);
GlobalDeclarationManager().signal_DeclsReloaded(decl::Type::Model).connect(
[&]() { modelSignalFired = true; }
);
// Parse this folder, it contains decls of type testdecl and testdecl2 in the .decl files
GlobalDeclarationManager().registerDeclFolder(decl::Type::Material, "testdecls", ".decl");
// Force the thread to be finished
GlobalDeclarationManager().foreachDeclaration(decl::Type::Material, [](const decl::IDeclaration&) {});
EXPECT_TRUE(materialSignalFired) << "Material signal should have fired by the time parsing has finished";
EXPECT_FALSE(modelSignalFired) << "Model-type Signal should not have been fired";
}
TEST_F(DeclManagerTest, FindDeclaration)
{
GlobalDeclarationManager().registerDeclType("testdecl", std::make_shared<TestDeclarationCreator>());
GlobalDeclarationManager().registerDeclFolder(decl::Type::Material, "testdecls", ".decl");
EXPECT_TRUE(GlobalDeclarationManager().findDeclaration(decl::Type::Material, "decl/exporttest/guisurf1"));
EXPECT_FALSE(GlobalDeclarationManager().findDeclaration(decl::Type::Material, "decl/nonexistent"));
}
TEST_F(DeclManagerTest, ReloadDeclarationWithChangedFile)
{
TemporaryFile tempFile(_context.getTestProjectPath() + "testdecls/temp_file.decl");
tempFile.setContents(R"(
decl/temporary/11
{
diffusemap textures/temporary/11
}
decl/temporary/12
{
diffusemap textures/temporary/12
}
)");
GlobalDeclarationManager().registerDeclType("testdecl", std::make_shared<TestDeclarationCreator>());
GlobalDeclarationManager().registerDeclFolder(decl::Type::Material, "testdecls", ".decl");
auto temp12 = GlobalDeclarationManager().findDeclaration(decl::Type::Material, "decl/temporary/12");
EXPECT_TRUE(temp12) << "Couldn't find the declaration decl/temporary/12";
auto temp11 = GlobalDeclarationManager().findDeclaration(decl::Type::Material, "decl/temporary/11");
EXPECT_TRUE(temp11) << "Couldn't find the declaration decl/temporary/11";
EXPECT_FALSE(GlobalDeclarationManager().findDeclaration(decl::Type::Material, "decl/temporary/13"))
<< "decl/temporary/13 should not be present";
EXPECT_NE(temp12->getBlockSyntax().contents.find("diffusemap textures/temporary/12"), std::string::npos) <<
"Didn't find the expected contents in the decl block";
// Change the file, change temp12, remove temp11 and add temp13 instead
tempFile.setContents(R"(
decl/temporary/12
{
diffusemap textures/changed_temporary/12
}
decl/temporary/13
{
diffusemap textures/temporary/13
}
)");
// Check the change sin temp12
temp12 = GlobalDeclarationManager().findDeclaration(decl::Type::Material, "decl/temporary/12");
EXPECT_TRUE(temp12) << "Couldn't find the declaration decl/temporary/12";
EXPECT_NE(temp12->getBlockSyntax().contents.find("diffusemap textures/changed_temporary/12"), std::string::npos) <<
"Couldn't find the changed contents in the decl block";
EXPECT_TRUE(GlobalDeclarationManager().findDeclaration(decl::Type::Material, "decl/temporary/13"))
<< "decl/temporary/13 should be present now";
EXPECT_FALSE(GlobalDeclarationManager().findDeclaration(decl::Type::Material, "decl/temporary/11"))
<< "decl/temporary/11 should be gone now";
}
}