File: test_sourcehighlight_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 (144 lines) | stat: -rw-r--r-- 4,857 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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <iostream>

#include "srchilite/sourcehighlight.h"
#include "srchilite/textstyleformatter.h"
#include <sstream>
#include "asserttestexit.h"
#include "srchilite/ioexception.h"
#include "srchilite/docgenerator.h"
#include "srchilite/parserexception.h"

using namespace std;
using namespace srchilite;

/*
 static void showFormatters(const TextStyleFormatterCollection &collection);

 void showFormatters(const TextStyleFormatterCollection &collection) {
 for (TextStyleFormatterCollection::const_iterator it = collection.begin(); it
 != collection.end(); ++it) {
 cout << (*it)->toString() << endl;
 }
 }
 */

int main() {
    istringstream input;
    ostringstream output;

    SourceHighlight sourcehighlight("html.outlang");

    sourcehighlight.setDataDir(BASEDIR);

    string commentString = "this /* is a comment\nspanning more line */ end";
    input.str(commentString);

    sourcehighlight.highlight(input, output, "c_comment.lang");

    // save start and end of the generated documents for testing
    ostringstream docGeneratorBuffer;
    sourcehighlight.getNoDocGenerator()->generate_start_doc(&docGeneratorBuffer);
    const string startDoc = docGeneratorBuffer.str();
    docGeneratorBuffer.str("");
    sourcehighlight.getNoDocGenerator()->generate_end_doc(&docGeneratorBuffer);
    const string endDoc = docGeneratorBuffer.str();

    // showFormatters(sourcehighlight.getFormatterCollection());

    cout << "formatted: " << output.str() << endl;
    assertEquals(
            startDoc
                    + "this <i><font color=\"#9A1900\">/* is a comment</font></i>\n\
<i><font color=\"#9A1900\">spanning more line */</font></i> end"
                    + endDoc, output.str());

    istringstream input2("this /* is a comment\nspanning more line */ end");
    output.str("");

    // tests for line numbers
    sourcehighlight.setGenerateLineNumbers();

    sourcehighlight.highlight(input2, output, "c_comment.lang");

    cout << "formatted: " << output.str() << endl;
    assertEquals(
            startDoc
                    + "<font color=\"#000000\">00001:</font> this <i><font color=\"#9A1900\">/* is a comment</font></i>\n\
<font color=\"#000000\">00002:</font> <i><font color=\"#9A1900\">spanning more line */</font></i> end"
                    + endDoc, output.str());

    // tests for line numbers with explicit number of digits
    sourcehighlight.setLineNumberDigits(2);

    output.str("");

    istringstream input_ndigits2("this /* is a comment\nspanning more line */ end");

    sourcehighlight.highlight(input_ndigits2, output, "c_comment.lang");

    cout << "formatted: " << output.str() << endl;
    assertEquals(
            startDoc
                    + "<font color=\"#000000\">01:</font> this <i><font color=\"#9A1900\">/* is a comment</font></i>\n\
<font color=\"#000000\">02:</font> <i><font color=\"#9A1900\">spanning more line */</font></i> end"
                    + endDoc, output.str());

    sourcehighlight.setGenerateLineNumbers(false);

    // test HTML char translation
    istringstream input3("this < > & html chars");
    output.str("");

    sourcehighlight.highlight(input3, output, "c_comment.lang");

    cout << "formatted: " << output.str() << endl;

    assertEquals(startDoc + "this &lt; &gt; &amp; html chars" + endDoc,
            output.str());

    // test tab translation translation
    istringstream input4("these\t\tare\ttabs\tchars");
    output.str("");

    SourceHighlight sourcehighlightTabs("html.outlang");
    sourcehighlightTabs.setDataDir(BASEDIR);
    sourcehighlightTabs.setTabSpaces(3);
    sourcehighlightTabs.highlight(input4, output, "c_comment.lang");

    cout << "formatted: " << output.str() << endl;

    assertEquals(startDoc + "these    are   tabs  chars" + endDoc, output.str());

    sourcehighlight.setGenerateLineNumbers();

    // try with files
    sourcehighlight.highlight(BASEDIR "../doc/Hello.java", "Hello.java.html", "java.lang");

    // try with non existent input file
    try {
        sourcehighlight.highlight("FOO.java", "Hello.java.html", "java.lang");
        assertTrue(false); // must not get here
    } catch (IOException &e) {
        cout << "expected exception: " << e << endl;
        assertEquals("FOO.java: cannot open input file FOO.java.", e.filename + ": " + e.what() + ".");
    }

    // try with entire doc
    sourcehighlight.setGenerateEntireDoc();
    sourcehighlight.highlight(BASEDIR "../doc/Hello.java", "HelloDoc.java.html", "java.lang");

    // test with html tables
    SourceHighlight sourcehighlightTable("htmltable.outlang");

    sourcehighlightTable.setDataDir(BASEDIR);

    sourcehighlightTable.highlight(BASEDIR "../doc/Hello.java", "HelloTable.java.html", "java.lang");

    cout << "test_sourcehighlight: SUCCESS" << endl;

    return 0;
}