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
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <iostream>
#include "srchilite/sourcehighlighter.h"
#include "srchilite/sourcefilehighlighter.h"
#include "srchilite/formattermanager.h"
#include "srchilite/regexrulefactory.h"
#include "srchilite/regexhighlightrule.h"
#include "asserttestexit.h"
#include <sstream>
#include "srchilite/bufferedoutput.h"
#include "srchilite/textstyleformatter.h"
#include "srchilite/chartranslator.h"
#include "srchilite/linenumgenerator.h"
#include "srchilite/lineranges.h"
#include "srchilite/regexranges.h"
/*
static void check_log_entry(const FormatterLogEntry &e,
const std::string &elem, const std::string &s);
void check_log_entry(const FormatterLogEntry &e, const std::string &elem,
const std::string &s) {
assertEquals(elem, e.first);
assertEquals(s, e.second);
}
*/
using namespace std;
using namespace srchilite;
int main() {
ostringstream os;
RegexRuleFactory factory;
BufferedOutput output(os);
FormatterPtr normalFormatter = FormatterPtr(new TextStyleFormatter("$text",
&output));
FormatterPtr keywordFormatter = FormatterPtr(new TextStyleFormatter(
"<K>$text</K>", &output));
FormatterPtr commentFormatter = FormatterPtr(new TextStyleFormatter(
"<C>$text</C>", &output));
FormatterPtr contextFormatter = FormatterPtr(new TextStyleFormatter(
"<CC>$text</CC>", &output));
FormatterManager formatterManager(normalFormatter);
formatterManager.addFormatter("keyword", keywordFormatter);
formatterManager.addFormatter("comment", commentFormatter);
HighlightStatePtr highlightState = HighlightStatePtr(new HighlightState);
SourceHighlighter sourceHighlighter(highlightState);
sourceHighlighter.setFormatterManager(&formatterManager);
SourceFileHighlighter highlighter("test", &sourceHighlighter, &output);
// now the state has no rules, so we should see only "normal" formatting
highlighter.highlight("this is a normal\nstring");
cout << "HIGHLIGHTED: " << os.str() << endl;
assertEquals("this is a normal\nstring", os.str());
// now we add some rules
os.str("");
WordList keywords;
keywords.push_back("class");
keywords.push_back("public");
highlightState->addRule(HighlightRulePtr(factory.createWordListRule(
"keyword", keywords)));
highlighter.highlight(
"this is a public class and\nhere's another class\nend");
cout << "HIGHLIGHTED: " << os.str() << endl;
assertEquals(
"this is a <K>public</K> <K>class</K> and\nhere's another <K>class</K>\nend",
os.str());
// test comments (environments)
os.str("");
HighlightRulePtr multiLineCommentRule = HighlightRulePtr(
factory.createMultiLineRule("comment", "/\\*", "\\*/", "", false));
HighlightStatePtr multiLineCommentState =
multiLineCommentRule->getNextState();
assertTrue(multiLineCommentState.get());
highlightState->addRule(multiLineCommentRule);
// now we set its default element (to comment)
multiLineCommentState->setDefaultElement("comment");
highlighter.highlight(
"this is /* a comment\nthat spans\nmore lines\n*/\na public class and\nhere's another class\nend");
cout << "HIGHLIGHTED: " << os.str() << endl;
assertEquals(
"this is <C>/*</C><C> a comment</C>\n\
<C>that spans</C>\n\
<C>more lines</C>\n\
<C>*/</C>\n\
a <K>public</K> <K>class</K> and\n\
here's another <K>class</K>\n\
end",
os.str());
// test optimizations (buffer adiacent elements)
os.str("");
sourceHighlighter.setOptimize(true);
highlighter.highlight(
"this is /* a comment\nthat spans\nmore lines\n*/ a public class end");
cout << "HIGHLIGHTED: " << os.str() << endl;
assertEquals(
"this is <C>/* a comment</C>\n\
<C>that spans</C>\n\
<C>more lines</C>\n\
<C>*/</C> a <K>public</K> <K>class</K> end",
os.str());
// test char translator for newlines
os.str("");
CharTranslator charTranslator;
charTranslator.set_translation("\n", "<NL>\n");
highlighter.setPreformatter(&charTranslator);
highlighter.highlight(
"this is /* a comment\nthat spans\nmore lines\n*/ a public class end");
cout << "HIGHLIGHTED: " << os.str() << endl;
assertEquals(
"this is <C>/* a comment</C><NL>\n\
<C>that spans</C><NL>\n\
<C>more lines</C><NL>\n\
<C>*/</C> a <K>public</K> <K>class</K> end",
os.str());
// test for line number generation
os.str("");
TextStyle lineStyle("<LINE>$text</LINE>");
LineNumGenerator lineNumGen(lineStyle, 5);
highlighter.setLineNumGenerator(&lineNumGen);
highlighter.highlight("this is /* a comment\nthat spans more lines\n*/ end");
cout << "HIGHLIGHTED: " << os.str() << endl;
assertEquals(
"<LINE>00001:</LINE> this is <C>/* a comment</C><NL>\n\
<LINE>00002:</LINE> <C>that spans more lines</C><NL>\n\
<LINE>00003:</LINE> <C>*/</C> end",
os.str());
// test for line prefix
os.str("");
highlighter.setLinePrefix(" PREFIX ");
highlighter.highlight("this is /* a comment\nthat spans more lines\n*/ end");
cout << "HIGHLIGHTED: " << os.str() << endl;
assertEquals(
" PREFIX <LINE>00001:</LINE> this is <C>/* a comment</C><NL>\n\
PREFIX <LINE>00002:</LINE> <C>that spans more lines</C><NL>\n\
PREFIX <LINE>00003:</LINE> <C>*/</C> end",
os.str());
// test for line ranges
os.str("");
LineRanges lineRanges;
lineRanges.addRange("2-3");
highlighter.setLineRanges(&lineRanges);
highlighter.highlight(
"this is /* a comment\nthat spans more lines\n*/ end\nend2");
cout << "HIGHLIGHTED: " << os.str() << endl;
// only line 2 and 3 must be output
assertEquals(
" PREFIX <LINE>00002:</LINE> <C>that spans more lines</C><NL>\n\
PREFIX <LINE>00003:</LINE> <C>*/</C> end<NL>\n",
os.str());
// do it again to check that lineRanges was correctly reset
os.str("");
highlighter.highlight(
"this is /* a comment\nthat spans more lines\n*/ end\nend2\nend3\nend4");
cout << "HIGHLIGHTED: " << os.str() << endl;
// only line 2 and 3 must be output
assertEquals(
" PREFIX <LINE>00002:</LINE> <C>that spans more lines</C><NL>\n\
PREFIX <LINE>00003:</LINE> <C>*/</C> end<NL>\n",
os.str());
// do it again to check context lines
os.str("");
highlighter.setContextFormatter(contextFormatter.get());
lineRanges.setContextLines(2);
highlighter.highlight(
"this is /* a comment\nthat spans more lines\n*/ end\nend2\nend3\nend4\nend5");
cout << "HIGHLIGHTED: " << os.str() << endl;
// only line 2 and 3 must be output, but line 1 and line 4 and 5 are printed as context
assertEquals(
" PREFIX <LINE>00001:</LINE> <CC>this is /* a comment</CC><NL>\n\
PREFIX <LINE>00002:</LINE> <C>that spans more lines</C><NL>\n\
PREFIX <LINE>00003:</LINE> <C>*/</C> end<NL>\n\
PREFIX <LINE>00004:</LINE> <CC>end2</CC><NL>\n\
PREFIX <LINE>00005:</LINE> <CC>end3</CC><NL>\n",
os.str());
// do it again to check range separators
os.str("");
highlighter.setContextFormatter(contextFormatter.get());
highlighter.setRangeSeparator("...");
lineRanges.addRange("7-8");
lineRanges.setContextLines(1);
highlighter.highlight(
"this is /* a comment\nthat spans more lines\n*/ end\nend2\nend3\nend4\nend5\nend6\nend7\nend8\nend9");
cout << "HIGHLIGHTED: " << os.str() << endl;
// only line 2 and 3 must be output, but line 1 and line 4 is printed as context
// then lines 7 and 8
// and also the separator is printed
assertEquals(
" PREFIX <LINE>00001:</LINE> <CC>this is /* a comment</CC><NL>\n\
PREFIX <LINE>00002:</LINE> <C>that spans more lines</C><NL>\n\
PREFIX <LINE>00003:</LINE> <C>*/</C> end<NL>\n\
PREFIX <LINE>00004:</LINE> <CC>end2</CC><NL>\n\
PREFIX ...<NL>\n\
PREFIX <LINE>00006:</LINE> <CC>end4</CC><NL>\n\
PREFIX <LINE>00007:</LINE> end5<NL>\n\
PREFIX <LINE>00008:</LINE> end6<NL>\n\
PREFIX <LINE>00009:</LINE> <CC>end7</CC><NL>\n\
PREFIX ...<NL>\n",
os.str());
// now test the regex ranges
RegexRanges regexRanges;
highlighter.setLineRanges(0);
highlighter.setRangeSeparator("");
highlighter.setRegexRanges(®exRanges);
regexRanges.addRegexRange("/// [[:alpha:]]+");
regexRanges.addRegexRange("/// [[:digit:]]+");
os.str("");
highlighter.highlight(
"this is /* a comment\nthat spans more lines\n*/ /// range end\nin range\nin range2\nend4 /// range\nend5\n/// 25\nend7\nend8\nend9\n/// 28\nend10");
cout << "HIGHLIGHTED: " << os.str() << endl;
assertEquals(
" PREFIX <NL>\n\
PREFIX <LINE>00004:</LINE> in range<NL>\n\
PREFIX <LINE>00005:</LINE> in range2<NL>\n\
PREFIX <LINE>00009:</LINE> end7<NL>\n\
PREFIX <LINE>00010:</LINE> end8<NL>\n\
PREFIX <LINE>00011:</LINE> end9<NL>\n",
os.str());
cout << "test_sourcefilehighlighter: SUCCESS" << endl;
return 0;
}
|