-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
375 lines (320 loc) · 11 KB
/
test.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "stdafx.h"
#include "resource.h"
#include "count.h"
#include "registry.h"
#include "settings.h"
#include "CharacterCount.h"
#include "test.h"
#ifdef _test
#define EE_EXTERN_ONLY
#define ETL_FRAME_CLASS_NAME CMyFrame
#include "template/etlframe.h"
#include "CMyFrame.h"
using namespace std::string_literals;
// To test CharacterCount.
namespace test {
// Test deleting and writing to registry
std::wstring testRegistry(HWND hwnd, HWND editor) {
const std::wstring failedMsg = L"Registry test failed.\n"s;
settings::deleteSettings();
HKEY key;
if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\EmSoft\\EmEditorPlugIns\\CharacterCount",
0, KEY_SET_VALUE, &key) == ERROR_SUCCESS)
return failedMsg + L"\tRegistry values were not deleted."s;
settings::writeSettings(editor, settings::defaultSettings);
charCount::Window charcount(hwnd, editor, false);
if (charcount.settings != settings::defaultSettings)
return failedMsg + L"\tRegistry values are incorrect.";
return L"Registry test passed."s;
}
std::wstring convertCountsToOutput(const std::array<int, count::countsSize>& counts) {
return
L"Characters: "s + std::to_wstring(counts[0]) +
L"\nLogical lines: " + std::to_wstring(counts[1]) +
L"\nView lines: " + std::to_wstring(counts[2]) +
L"\nLF: " + std::to_wstring(counts[3]) +
L"\nCR: " + std::to_wstring(counts[4]) +
L"\nCR+LF: " + std::to_wstring(counts[5]) +
L"\nWidth: " + std::to_wstring(counts[6]) +
L"\nHalfwidth character: " + std::to_wstring(counts[7]) +
L"\nFullwidth character: " + std::to_wstring(counts[8]) +
L"\nSpace: " + std::to_wstring(counts[9]) +
L"\nIdeographic space: " + std::to_wstring(counts[10]) +
L"\nTab character: " + std::to_wstring(counts[11]) +
L"\nControl character: " + std::to_wstring(counts[12]) +
L"\nCJK Unified Ideograph: " + std::to_wstring(counts[13]) +
L"\nHiragana: " + std::to_wstring(counts[14]) +
L"\nKatakana: " + std::to_wstring(counts[15]) +
L"\nHalfwidth katakana: " + std::to_wstring(counts[16]) + L"\n";
}
// [Files,DifferentSettings,(setOfSettings,output)]
std::vector<std::vector<std::tuple<std::array<unsigned char, settings::settingsSize>, std::wstring>>>
getSettingsAndOutputs() {
using namespace settings;
std::vector<std::vector<std::tuple<std::array<unsigned char, settings::settingsSize>,
std::array<int, count::countsSize>>>>
settingsAndCounts
{
// Empty document
{
{settings::defaultSettings,
{{0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}},
},
// LF
{
{settings::defaultSettings,
{{0,3,3,2,0,0,0,0,0,0,0,0,2,0,0,0,0}}}
},
// CR
{
{settings::defaultSettings,
{{0,3,3,0,2,0,0,0,0,0,0,0,2,0,0,0,0}}}
},
// CR+LF
{
{settings::defaultSettings,
{{0,3,3,0,0,2,0,0,0,0,0,0,4,0,0,0,0}}}
},
/*
testDocument.txt
àあアア一
、。゛゙ーー「・〱ゝ々〆〇
Under default settings:
à = halfwidth character
あ = fullwidth hiragana
ア = fullwidth katakana
ア = halfwidth katakana
一 = fullwidth CJK
= halfwidth tab control
= halfwidth space
= fullwidth ideographic space
= 1 CR+LF, 1 line, 2 controls
、 = fullwidth
。 = halfwidth katakana
゛ = fullwidth hiragana
゙ = halfwidth katakana
ー = fullwidth katakana
ー = halfwidth katakana
「 = halfwidth katakana
・ = fullwidth katakana
〱 = fullwidth
ゝ = fullwidth hiragana
々 = fullwidth
〆 = fullwidth
〇 = fullwidth
*/
{
// 00. default
{
settings::defaultSettings,
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,3,3,5}},
},
// 01. eol counted as character
{
{{one,neither,halfkana,hiragana,halfkana,katakana,halfkana,halfkana,katahalf,neither,
hirakata,neither,neither,neither}},
{{22,2,2,0,0,1,34,8,13,1,1,1,3,1,3,3,5}},
},
// 02. comma becomes hiragana
{
{{zero,hiragana,halfkana,hiragana,halfkana,katakana,halfkana,halfkana,katahalf,neither,
hirakata,neither,neither,neither}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,4,3,5}},
},
// 03. comma becomes katakana
{
{{zero,katakana,halfkana,hiragana,halfkana,katakana,halfkana,halfkana,katahalf,neither,
hirakata,neither,neither,neither}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,3,4,5}},
},
// 04. halfwidth stop is neither
{
{{zero,neither,neither,hiragana,halfkana,katakana,halfkana,halfkana,katahalf,neither,
hirakata,neither,neither,neither}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,3,3,4}},
},
// 05. halfwidth stop is hiragana
{
{{zero,neither,hiragana,hiragana,halfkana,katakana,halfkana,halfkana,katahalf,neither,
hirakata,neither,neither,neither}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,4,3,4}},
},
// 06. voiced sound mark is neither
{
{{zero,neither,halfkana,neither,halfkana,katakana,halfkana,halfkana,katahalf,neither,
hirakata,neither,neither,neither}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,2,3,5}},
},
// 07. halfwidth voiced sound mark is katakana
{
{{zero,neither,halfkana,hiragana,katakana,katakana,halfkana,halfkana,katahalf,neither,
hirakata,neither,neither,neither}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,3,4,4}},
},
// 08. prolonged sound mark is hiragana
{
{{zero,neither,halfkana,hiragana,halfkana,hiragana,halfkana,halfkana,katahalf,neither,
hirakata,neither,neither,neither}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,4,2,5}},
},
// 09. halfwidth prolonged sound mark is hiragana
{
{{zero,neither,halfkana,hiragana,halfkana,katakana,hiragana,halfkana,katahalf,neither,
hirakata,neither,neither,neither}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,4,3,4}},
},
// 10. halfwidth corner brackets is neither
{
{{zero,neither,halfkana,hiragana,halfkana,katakana,halfkana,neither,katahalf,neither,
hirakata,neither,neither,neither}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,3,3,4}},
},
// 11. katakana middle dot is neither
{
{{zero,neither,halfkana,hiragana,halfkana,katakana,halfkana,halfkana,neither,neither,
hirakata,neither,neither,neither}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,1,3,2,5}},
},
// 12. the three other options are all CJK
{
{{zero,neither,halfkana,hiragana,halfkana,katakana,halfkana,halfkana,katahalf,neither,
hirakata,cjk,cjk,cjk}},
{{21,2,2,0,0,1,34,8,13,1,1,1,3,4,3,3,5}},
},
/*
13. eol is 1 char, comma is hiragana, halfwidth full stop is katakana, voiced sound mark
is neither, halfwidth voiced sound mark is hiragana, halfwidth corner brackets is neither
-> (+1 char; +1 hiragana; -1 halfwidth katakana, +1 katakana; -1 hiragana; -1 halfwidth
katakana, +1 hiragana; -1 halfwidth katakana)
-> (+1 char, +1 hiragana, +1 katakana, -3 halfwidth katakana)
*/
{
{{one,hiragana,katakana,neither,hiragana,katakana,halfkana,neither,katahalf,neither,
hirakata,neither,neither,neither}},
{{22,2,2,0,0,1,34,8,13,1,1,1,3,1,4,4,2}},
}
}
};
std::vector<std::vector<std::tuple<std::array<unsigned char, settings::settingsSize>, std::wstring>>>
returnVec(settingsAndCounts.size());
for (int fileI = 0; fileI < settingsAndCounts.size(); ++fileI) {
returnVec[fileI] =
std::vector<std::tuple<std::array<unsigned char, settings::settingsSize>, std::wstring>>(
settingsAndCounts[fileI].size());
for (int settingI = 0; settingI < settingsAndCounts[fileI].size(); ++settingI) {
returnVec[fileI][settingI]
= std::make_tuple(
std::get<0>(settingsAndCounts[fileI][settingI]),
convertCountsToOutput(std::get<1>(settingsAndCounts[fileI][settingI]))
);
}
}
return returnVec;
}
std::wstring docPath() {
wchar_t dirChar[300];
// Get directory to DLL, including filename
GetModuleFileName(EEGetLocaleInstanceHandle(), dirChar, 300);
std::wstring docPath(dirChar);
return docPath.substr(0, docPath.find(L"build\\x64\\Debug"s))
+ L"testDocument.txt"s;
}
std::wstring testSettings(
std::vector<std::tuple<std::array<unsigned char, settings::settingsSize>, std::wstring>>
settingsAndOutputs,
HWND hwnd,
HWND editor
) {
std::wstring errors;
int i = 0;
for (auto settingsAndOutput : settingsAndOutputs) {
settings::writeSettings(editor, std::get<0>(settingsAndOutput));
charCount::Window charcount(hwnd, editor, false);
if (charcount.textOutput != std::get<1>(settingsAndOutput))
errors += L"\t\tSetting "s + std::to_wstring(i)
+ L" resulted in incorrect values.\n"s;
++i;
}
return errors;
}
// Delete all and insert text.
void replace(HWND editor, LPCWSTR text) {
Editor_ExecCommand(editor, EEID_EDIT_SELECT_ALL);
Editor_ExecCommand(editor, EEID_DELETE);
Editor_InsertW(editor, text);
}
// Test counting with various settings on various documents.
std::wstring countTest(HWND hwnd, HWND editor) {
auto settingsAndOutputs = getSettingsAndOutputs();
auto settingsAndOutputsIter = settingsAndOutputs.begin();
std::wstring output;
// Empty file test
Editor_EditTemp(editor, L"", L"test", NULL, NULL, 65001/*UTF-8*/);
std::wstring errors1 = testSettings(*settingsAndOutputsIter++, hwnd, editor);
if (errors1.empty())
output += L"Empty file test passed.\n";
else
output += L"Empty file test failed.\n" + errors1;
// LR test
replace(editor, L"\n\n");
std::wstring errors2 = testSettings(*settingsAndOutputsIter++, hwnd, editor);
if (errors2.empty())
output += L"LR test passed.\n";
else
output += L"LR test failed.\n" + errors2;
// CR test
replace(editor, L"\r\r");
std::wstring errors3 = testSettings(*settingsAndOutputsIter++, hwnd, editor);
if (errors3.empty())
output += L"CR test passed.\n";
else
output += L"CR test failed.\n" + errors3;
// CR+LF test
replace(editor, L"\r\n\r\n");
std::wstring errors4 = testSettings(*settingsAndOutputsIter++, hwnd, editor);
if (errors4.empty())
output += L"CR+LF test passed.\n";
else
output += L"CR+LF test failed.\n" + errors4;
// Document test.
std::wstring docPath_ = docPath();
if (Editor_LoadFileW(editor, reinterpret_cast<LOAD_FILE_INFO_EX*>(NULL), docPath_.data())) {
std::wstring errors5 = testSettings(*settingsAndOutputsIter++, hwnd, editor);
if (errors5.empty())
output += L"testDocument test passed.\n";
else
output += L"testDocument test failed.\n" + errors5;
} else {
output += L"testDocument test failed.\n\ttestDocument.txt doesn't exist under \""s
+ docPath_ + L"\"."s;
}
return output;
}
std::wstring test(HWND hwnd, HWND editor) {
return L"CharacterCount testing results:\n"s
+ testRegistry(hwnd, editor) + L"\n"s
+ countTest(hwnd, editor);
/*
To check by hand:
All controls show correct text on the interface and are working
Keyboard shortcuts are working
GUI looks the same in any DPI
Page wrapping results in higher view lines count
*/
}
}
void CMyFrame::OnCommand(HWND hwnd) {
std::wstring output = test::test(hwnd, m_hWnd);
Editor_EditTemp(hwnd, L"", L"CharacterCount unit test", NULL, NULL, 65001);
Editor_InsertW(hwnd, output.c_str());
}
BOOL CMyFrame::SetUninstall(HWND /*hwnd*/, LPTSTR /*command*/, LPTSTR /*param*/) {
settings::deleteSettings();
return UNINSTALL_SIMPLE_DELETE;
}
BOOL CMyFrame::SetProperties(HWND hwnd) {
settings::Window settingsWindow(m_hWnd, settings::readSettings(m_hWnd), false);
settingsWindow.openSettingsWindow(hwnd);
return TRUE;
}
#endif