File: test_highlighter_main.cpp

package info (click to toggle)
source-highlight 3.1.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,332 kB
  • ctags: 5,233
  • sloc: sh: 11,270; cpp: 10,206; ansic: 9,515; makefile: 1,865; lex: 1,200; yacc: 1,021; php: 213; perl: 211; awk: 98; erlang: 94; lisp: 90; java: 75; ruby: 69; python: 61; asm: 43; ml: 38; ada: 36; haskell: 27; xml: 23; cs: 11; sql: 8; tcl: 6; sed: 4
file content (349 lines) | stat: -rw-r--r-- 11,386 bytes parent folder | download | duplicates (6)
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <iostream>

#include "logformatter.h"
#include "srchilite/sourcehighlighter.h"
#include "srchilite/formattermanager.h"
#include "srchilite/formatterparams.h"
#include "srchilite/regexrulefactory.h"
#include "srchilite/regexhighlightrule.h"
#include "asserttestexit.h"

using namespace std;
using namespace srchilite;

static void check_log_entry(const FormatterLogEntry &e,
        const std::string &elem, const std::string &s, const int start = -1);

void check_log_entry(const FormatterLogEntry &e, const std::string &elem,
        const std::string &s, const int start) {
    assertEquals(elem, e.first);
    assertEquals(s, e.second);
    if (start != -1) {
        assertEquals(start, e.start);
    }
}

int main() {
    FormatterLog log;
    RegexRuleFactory factory;
    FormatterParams params;

    FormatterPtr normalFormatter =
            FormatterPtr(new LogFormatter(log, "normal"));
    FormatterPtr keywordFormatter = FormatterPtr(new LogFormatter(log,
            "keyword"));
    FormatterPtr commentFormatter = FormatterPtr(new LogFormatter(log,
            "comment"));

    FormatterManager formatterManager(normalFormatter);

    HighlightStatePtr highlightState = HighlightStatePtr(new HighlightState);

    SourceHighlighter sourceHighlighter(highlightState);
    sourceHighlighter.setFormatterManager(&formatterManager);
    sourceHighlighter.setFormatterParams(&params);

    // now the state has no rules, so we should see only "normal" formatting
    sourceHighlighter.highlightParagraph("this is normal");

    showLog(log);

    assertEquals((size_t) 1, log.size());
    assertEquals("normal", log[0].first);
    assertEquals("this is normal", log[0].second);

    // now we add some rules
    log.clear();

    WordList keywords;
    keywords.push_back("class");
    keywords.push_back("public");
    highlightState->addRule(HighlightRulePtr(factory.createWordListRule(
            "keyword", keywords)));

    sourceHighlighter.highlightParagraph("this class is public and normal");

    // however the formatter for keyword is not set, so we will get
    // tokenized strings all as normal

    showLog(log);

    check_log_entry(log[0], "normal", "this ");
    check_log_entry(log[1], "normal", "class");
    check_log_entry(log[2], "normal", " is ");
    check_log_entry(log[3], "normal", "public");
    check_log_entry(log[4], "normal", " and normal");

    log.clear();

    // now we add the formatter for keyword

    formatterManager.addFormatter("keyword", keywordFormatter);

    sourceHighlighter.highlightParagraph("this class is public and normal");

    // and this time we will have also keywords logged

    showLog(log);

    check_log_entry(log[0], "normal", "this ", 0);
    check_log_entry(log[1], "keyword", "class", 5);
    check_log_entry(log[2], "normal", " is ", 10);
    check_log_entry(log[3], "keyword", "public", 14);
    check_log_entry(log[4], "normal", " and normal", 20);

    // test environments

    log.clear();

    HighlightRulePtr multiLineCommentRule = HighlightRulePtr(
            factory.createMultiLineRule("comment", "/\\*", "\\*/", "", false));
    HighlightStatePtr multiLineCommentState =
            multiLineCommentRule->getNextState();

    assertTrue(multiLineCommentState.get());

    highlightState->addRule(multiLineCommentRule);

    formatterManager.addFormatter("comment", commentFormatter);

    sourceHighlighter.highlightParagraph("this /* is a comment */ foo");

    showLog(log);

    // since we didn't set the default element of multiLineCommentState, then
    // the string inside the comment is formatted as normal

    check_log_entry(log[0], "normal", "this ");
    check_log_entry(log[1], "comment", "/*");
    check_log_entry(log[2], "normal", " is a comment ");
    check_log_entry(log[3], "comment", "*/");
    check_log_entry(log[4], "normal", " foo");

    log.clear();

    // now we set its default element (to comment)
    multiLineCommentState->setDefaultElement("comment");

    sourceHighlighter.highlightParagraph("this /* is a comment */ foo");

    showLog(log);

    // since now we set the default element of multiLineCommentState, then
    // the string inside the comment is formatted as a comment

    check_log_entry(log[0], "normal", "this ");
    check_log_entry(log[1], "comment", "/*");
    check_log_entry(log[2], "comment", " is a comment ");
    check_log_entry(log[3], "comment", "*/");
    check_log_entry(log[4], "normal", " foo");

    // test nested environments

    log.clear();

    HighlightRulePtr multiLineNestedCommentRule = HighlightRulePtr(
            factory.createMultiLineRule("comment", "<<", ">>", "", true));
    HighlightStatePtr multiLineNestedCommentState =
            multiLineNestedCommentRule->getNextState();

    assertTrue(multiLineNestedCommentState.get());

    multiLineNestedCommentState->setDefaultElement("comment");
    highlightState->addRule(multiLineNestedCommentRule);

    sourceHighlighter.highlightParagraph(
            "this << is << a << nested >> comment >> >> foo");

    showLog(log);

    check_log_entry(log[0], "normal", "this ");
    check_log_entry(log[1], "comment", "<<");
    check_log_entry(log[2], "comment", " is ");
    check_log_entry(log[3], "comment", "<<");
    check_log_entry(log[4], "comment", " a ");
    check_log_entry(log[5], "comment", "<<");
    check_log_entry(log[6], "comment", " nested ");
    check_log_entry(log[7], "comment", ">>");
    check_log_entry(log[8], "comment", " comment ");
    check_log_entry(log[9], "comment", ">>");
    check_log_entry(log[10], "comment", " ");
    check_log_entry(log[11], "comment", ">>");
    check_log_entry(log[12], "normal", " foo");

    // test for exit all

    log.clear();

    HighlightRulePtr exitAllRule = HighlightRulePtr(factory.createSimpleRule(
            "comment", "!"));
    exitAllRule->setExitLevel(-1);
    multiLineNestedCommentState->addRule(exitAllRule);

    sourceHighlighter.highlightParagraph(
            "this << is << a << ! exit >> comment >> >> foo");

    showLog(log);

    // the ! makes exit all states and go back to the initial state

    check_log_entry(log[0], "normal", "this ");
    check_log_entry(log[1], "comment", "<<");
    check_log_entry(log[2], "comment", " is ");
    check_log_entry(log[3], "comment", "<<");
    check_log_entry(log[4], "comment", " a ");
    check_log_entry(log[5], "comment", "<<");
    check_log_entry(log[6], "comment", " ");
    check_log_entry(log[7], "comment", "!");
    check_log_entry(log[8], "normal", " exit >> comment >> >> foo");

    // test for optmized buffering
    log.clear();

    sourceHighlighter.setOptimize();
    sourceHighlighter.highlightParagraph(
            "this << is << a << ! exit >> comment >> >> foo");

    showLog(log);

    check_log_entry(log[0], "normal", "this ");
    check_log_entry(log[1], "comment", "<< is << a << !");
    check_log_entry(log[2], "normal", " exit >> comment >> >> foo");

    // check for final flush in optimization
    log.clear();

    sourceHighlighter.setOptimize();
    sourceHighlighter.highlightParagraph("this << is << a << !");

    showLog(log);

    check_log_entry(log[0], "normal", "this ");
    check_log_entry(log[1], "comment", "<< is << a << !");

    sourceHighlighter.setOptimize(false);

    // check for final current state
    log.clear();

    sourceHighlighter.highlightParagraph("this <<");

    showLog(log);

    check_log_entry(log[0], "normal", "this ");
    check_log_entry(log[1], "comment", "<<");

    assertEquals("comment",
            sourceHighlighter.getCurrentState()->getDefaultElement());

    log.clear();

    // bring it back to the initial state
    sourceHighlighter.highlightParagraph("!");

    showLog(log);

    check_log_entry(log[0], "comment", "!");

    assertEquals("normal",
            sourceHighlighter.getCurrentState()->getDefaultElement());

    // test for rule with \z
    log.clear();

    HighlightRulePtr ruleWithEOL = HighlightRulePtr(factory.createLineRule(
            "comment", "#", "", "\\\\"));
    highlightState->addRule(ruleWithEOL);
    ruleWithEOL->getNextState()->setDefaultElement("comment");

    sourceHighlighter.highlightParagraph("before # this is a comment");
    sourceHighlighter.highlightParagraph("while this is class");

    showLog(log);

    int i = 0;
    check_log_entry(log[i++], "normal", "before ", 0);
    check_log_entry(log[i++], "comment", "#", 7);
    check_log_entry(log[i++], "comment", " this is a comment", 8);
    //check_log_entry(log[i++], "comment", ""); // match the end of line
    check_log_entry(log[i++], "normal", "while this is ", 0);
    check_log_entry(log[i++], "keyword", "class", 14);

    // test for suspended
    log.clear();

    sourceHighlighter.setSuspended(true);
    assertEquals((size_t) 0, log.size());
    sourceHighlighter.highlightParagraph("foo");
    sourceHighlighter.setSuspended(false);
    sourceHighlighter.highlightParagraph("bar");
    check_log_entry(log[0], "normal", "bar");

    // test for compound rule: class <classname>
    log.clear();

    FormatterPtr typeFormatter = FormatterPtr(new LogFormatter(log, "type"));
    formatterManager.addFormatter("type", typeFormatter);

    ElemNameList nameList;
    nameList.push_back("keyword");
    nameList.push_back("normal");
    nameList.push_back("type");
    HighlightRulePtr compoundRule = HighlightRulePtr(
            factory.createCompoundRule(nameList,
                    "(myclass)([[:blank:]]+)([[:word:]]+)"));
    highlightState->addRule(compoundRule);

    sourceHighlighter.highlightParagraph("fo() myclass Foo {");

    showLog(log);

    i = 0;
    check_log_entry(log[i++], "normal", "fo() ", 0);
    check_log_entry(log[i++], "keyword", "myclass", 5);
    check_log_entry(log[i++], "normal", " ", 12);
    check_log_entry(log[i++], "type", "Foo", 13);
    check_log_entry(log[i++], "normal", " {", 16);

    // check explicit setting of current state and stack
    log.clear();
    sourceHighlighter.highlightParagraph("<< comment");
    HighlightStatePtr prevCommentState = sourceHighlighter.getCurrentState();
    HighlightStateStackPtr prevStack = sourceHighlighter.getStateStack();
    // now the stack should contain one state
    assertEquals((size_t)1, sourceHighlighter.getStateStack()->size());
    showLog(log);
    i = 0;
    check_log_entry(log[i++], "comment", "<<");
    check_log_entry(log[i++], "comment", " comment");

    log.clear();
    sourceHighlighter.setCurrentState(sourceHighlighter.getMainState());
    sourceHighlighter.setStateStack(HighlightStateStackPtr(new HighlightStateStack));
    sourceHighlighter.highlightParagraph("not in comment");
    showLog(log);
    i = 0;
    check_log_entry(log[i++], "normal", "not in comment");

    log.clear();
    sourceHighlighter.setCurrentState(prevCommentState);
    sourceHighlighter.setStateStack(prevStack);
    sourceHighlighter.highlightParagraph("end comment >>");
    showLog(log);
    i = 0;
    check_log_entry(log[i++], "comment", "end comment ");
    check_log_entry(log[i++], "comment", ">>");

    // now the stack should be empty
    assertEquals((size_t)0, sourceHighlighter.getStateStack()->size());
    // also our copy
    assertEquals((size_t)0, prevStack->size());

    cout << "test_highlighter: SUCCESS" << endl;

    return 0;
}