/
textparse_test.sma
253 lines (203 loc) · 7.99 KB
/
textparse_test.sma
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
/**
* vim: set ts=4 sw=4 tw=99 noet:
*
* AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
* Copyright (C) The AMX Mod X Development Team.
*
* This software is licensed under the GNU General Public License, version 3 or higher.
* Additional exceptions apply. For full license details, see LICENSE.txt or visit:
* https://alliedmods.net/amxmodx-license
*/
#include <amxmodx>
new SuccessCount;
new Trie:ExpectedKVData;
new bool:Debug;
public plugin_init()
{
register_concmd("textparse", "ConsoleCommand_TextParse");
Debug = !!(plugin_flags() & AMX_FLAG_DEBUG);
}
public ConsoleCommand_TextParse()
{
InitializeTextParseSMC();
InitializeTextParseINI();
}
/**
* SMC Config format
*/
InitializeTextParseSMC()
{
SuccessCount = 0;
server_print("Testing text parser with SMC config file format...");
new const configFile[] = "addons/amxmodx/scripting/testsuite/textparse_test.cfg";
ExpectedKVData = TrieCreate();
TrieSetString(ExpectedKVData, "Logging", "on");
TrieSetString(ExpectedKVData, "LogMode", "daily");
TrieSetString(ExpectedKVData, "ServerLang", "en");
TrieSetString(ExpectedKVData, "PublicChatTrigger", "!");
TrieSetString(ExpectedKVData, "SilentChatTrigger", "/");
TrieSetString(ExpectedKVData, "SilentFailSuppress", "no");
TrieSetString(ExpectedKVData, "PassInfoVar", "_password");
TrieSetString(ExpectedKVData, "MenuItemSound", "buttons/button14.wav");
TrieSetString(ExpectedKVData, "MenuExitSound", "buttons/combine_button7.wav");
TrieSetString(ExpectedKVData, "MenuExitBackSound", "buttons/combine_button7.wav");
TrieSetString(ExpectedKVData, "AllowClLanguageVar", "On");
TrieSetString(ExpectedKVData, "DisableAutoUpdate", "no");
TrieSetString(ExpectedKVData, "ForceRestartAfterUpdate", "no");
TrieSetString(ExpectedKVData, "AutoUpdateURL", "http://update.sourcemod.net/update/");
TrieSetString(ExpectedKVData, "DebugSpew", "no");
TrieSetString(ExpectedKVData, "SteamAuthstringValidation", "yes");
TrieSetString(ExpectedKVData, "BlockBadPlugins", "yes");
TrieSetString(ExpectedKVData, "SlowScriptTimeout", "8");
new const expectedSectionCount = 2; // Include start and end.
new const expectedStartEndCount = 2;
new const expectedKeyValueCount = TrieGetSize(ExpectedKVData);
new const expectedLineCount = file_size(configFile, .flag = 1) - 1;
new SMCParser:parser = SMC_CreateParser();
SMC_SetReaders(parser, "ReadCore_KeyValue", "ReadCore_NewSection", "ReadCore_EndSection");
SMC_SetParseStart(parser, "ReadCore_ParseStart");
SMC_SetRawLine(parser, "ReadCore_CurrentLine");
SMC_SetParseEnd(parser, "ReadCore_ParseEnd");
new line, col;
new SMCError:err = SMC_ParseFile(parser, configFile, line, col);
if (err != SMCError_Okay)
{
new buffer[64];
server_print("%s", SMC_GetErrorString(err, buffer, charsmax(buffer)) ? buffer : "Fatal parse error");
}
if (line == expectedLineCount + 1 && col == 2)
{
++SuccessCount;
}
server_print("^tTests successful: %d/%d", SuccessCount, expectedStartEndCount + expectedSectionCount + expectedKeyValueCount + expectedLineCount + 1);
SMC_DestroyParser(parser);
TrieDestroy(ExpectedKVData);
SuccessCount = 0;
}
public ReadCore_ParseStart(SMCParser:handle)
{
Debug && server_print("ReadCore_ParseStart");
++SuccessCount;
}
public ReadCore_NewSection(SMCParser:handle, const name[])
{
Debug && server_print("^tReadCore_NewSection - %s", name);
++SuccessCount;
}
public ReadCore_KeyValue(SMCParser:handle, const key[], const value[])
{
Debug && server_print("^t^tReadCore_KeyValue - %-32s %s", key, value);
new buffer[128];
if (TrieGetString(ExpectedKVData, key, buffer, charsmax(buffer)) && equal(value, buffer))
{
++SuccessCount;
}
}
public ReadCore_EndSection(SMCParser:handle)
{
Debug && server_print("^tReadCore_EndSection");
++SuccessCount;
}
public ReadCore_CurrentLine(SMCParser:handle, const line[], lineno)
{
//Debug && server_print("^t^tReadCore_CurrentLine - %s", line);
++SuccessCount;
}
public ReadCore_ParseEnd(SMCParser:handle, bool:halted, bool:failed)
{
Debug &&server_print("ReadCore_ParseEnd - halted: %s, failed: %s", halted ? "yes" : "no", failed ? "yes" : "no");
++SuccessCount;
}
/**
* INI Config format
*/
public InitializeTextParseINI()
{
SuccessCount = 0;
server_print("Testing text parser with INI config file format...");
new const configFile[] = "addons/amxmodx/scripting/testsuite/textparse_test.ini";
ExpectedKVData = TrieCreate();
TrieSetString(ExpectedKVData, "settings", "");
TrieSetString(ExpectedKVData, "enabled", "1");
TrieSetString(ExpectedKVData, "strip_weapons", "1");
TrieSetString(ExpectedKVData, "weapons_stay", "0");
TrieSetString(ExpectedKVData, "spawnmode", "preset");
TrieSetString(ExpectedKVData, "remove_bomb", "1");
TrieSetString(ExpectedKVData, "spawn_wait_time", "0.75");
TrieSetString(ExpectedKVData, "protection", "");
TrieSetString(ExpectedKVData, "colors", "0 255 0 200");
TrieSetString(ExpectedKVData, "time", "time");
TrieSetString(ExpectedKVData, "secondary", "");
TrieSetString(ExpectedKVData, "usp USP 1", "");
TrieSetString(ExpectedKVData, "glock18 Glock 1", "");
TrieSetString(ExpectedKVData, "deagle Deagle 1", "");
TrieSetString(ExpectedKVData, "botsecondary", "");
TrieSetString(ExpectedKVData, "deagle", "");
TrieSetString(ExpectedKVData, "usp", "");
new const expectedSectionCount = 4;
new const expectedStartEndCount = 2;
new const expectedKeyValueCount = TrieGetSize(ExpectedKVData) - expectedSectionCount;
new const expectedLineCount = TrieGetSize(ExpectedKVData); // This doesn't include blanck/comments line.
new INIParser:parser = INI_CreateParser();
INI_SetReaders(parser, "ReadCSDM_KeyValue", "ReadCSDM_NewSection");
INI_SetParseStart(parser, "ReadCSDM_ParseStart");
INI_SetRawLine(parser, "ReadCSDM_CurrentLine");
INI_SetParseEnd(parser, "ReadCSDM_ParseEnd");
new line, col;
new bool:result = INI_ParseFile(parser, configFile, line, col);
if (!result)
{
server_print("^tFatal parse error");
}
if (line == expectedLineCount + 1)
{
++SuccessCount;
}
server_print("^tTests successful: %d/%d", SuccessCount, expectedStartEndCount + expectedSectionCount + expectedKeyValueCount + expectedLineCount + 1);
INI_DestroyParser(parser);
TrieDestroy(ExpectedKVData);
}
public ReadCSDM_ParseStart(INIParser:handle)
{
Debug && server_print("ReadCSDM_ParseStart");
++SuccessCount;
}
public ReadCSDM_NewSection(INIParser:handle, const section[], bool:invalid_tokens, bool:close_bracket, bool:extra_tokens, curtok)
{
Debug && server_print("^tReadCSDM_NewSection - [%s] (invalid_tokens: '%s', close_bracked: '%s', extra_tokens: '%s')", section, invalid_tokens ? "yes" : "no", close_bracket ? "yes" : "no", extra_tokens ? "yes" : "no");
if (TrieKeyExists(ExpectedKVData, section))
{
if ((equal(section, "secondary") && !extra_tokens) ||
(equal(section, "botsecondary") && close_bracket))
{
return true;
}
++SuccessCount;
}
return true;
}
public bool:ReadCSDM_KeyValue(INIParser:handle, const key[], const value[], bool:invalid_tokens, bool:equal_token, bool:quotes, curtok)
{
Debug && server_print("^t^tReadCSDM_KeyValue - %-32s %s", key, value);
new buffer[128];
if (TrieGetString(ExpectedKVData, key, buffer, charsmax(buffer)) && equal(value, buffer))
{
if (equal(key, "colors") && !quotes)
{
return true;
}
++SuccessCount;
}
return true;
}
public bool:ReadCSDM_CurrentLine(INIParser:handle, const line[], curtok)
{
//Debug && server_print("^t^tReadCSDM_CurrentLine - %s", line);
++SuccessCount;
return true;
}
public ReadCSDM_ParseEnd(INIParser:handle, bool:halted, bool:failed)
{
Debug && server_print("ReadCSDM_ParseStart");
++SuccessCount;
}