summaryrefslogtreecommitdiffstats
path: root/comphelper/qa/unit/syntaxhighlighttest.cxx
blob: 294db3e76fc09905e0a03a3d6dfaad9048fc2f86 (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <comphelper/syntaxhighlight.hxx>
#include "cppunit/TestAssert.h"
#include "cppunit/TestFixture.h"
#include "cppunit/extensions/HelperMacros.h"
#include "cppunit/plugin/TestPlugIn.h"
#include "rtl/ustring.hxx"

#include <cassert>
#include <vector>

class SyntaxHighlightTest : public CppUnit::TestFixture
{
public:
    void testBasicString();
    void testBasicComment();
    void testBasicCommentNewline();
    void testBasicEmptyComment();
    void testBasicEmptyCommentNewline();
    void testBasic();

    CPPUNIT_TEST_SUITE(SyntaxHighlightTest);
    CPPUNIT_TEST(testBasicString);
    CPPUNIT_TEST(testBasicComment);
    CPPUNIT_TEST(testBasicCommentNewline);
    CPPUNIT_TEST(testBasicEmptyComment);
    CPPUNIT_TEST(testBasicEmptyCommentNewline);
    CPPUNIT_TEST(testBasic);
    CPPUNIT_TEST_SUITE_END();
};

void SyntaxHighlightTest::testBasicString() {
    SyntaxHighlighter h;
    h.initialize(HIGHLIGHT_BASIC);
    OUString s("\"foo\"");
    std::vector<HighlightPortion> ps;
    h.getHighlightPortions(0, s, ps);
    CPPUNIT_ASSERT_EQUAL(
        static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size());
    CPPUNIT_ASSERT_EQUAL(0, ps[0].nBegin);
    CPPUNIT_ASSERT_EQUAL(5, ps[0].nEnd);
    CPPUNIT_ASSERT_EQUAL(TT_STRING, ps[0].tokenType);
}

void SyntaxHighlightTest::testBasicComment() {
    SyntaxHighlighter h;
    h.initialize(HIGHLIGHT_BASIC);
    OUString s("' foo");
    std::vector<HighlightPortion> ps;
    h.getHighlightPortions(0, s, ps);
    CPPUNIT_ASSERT_EQUAL(
        static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size());
    CPPUNIT_ASSERT_EQUAL(0, ps[0].nBegin);
    CPPUNIT_ASSERT_EQUAL(5, ps[0].nEnd);
    CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
}

void SyntaxHighlightTest::testBasicCommentNewline() {
    SyntaxHighlighter h;
    h.initialize(HIGHLIGHT_BASIC);
    OUString s("' foo\n");
    std::vector<HighlightPortion> ps;
    h.getHighlightPortions(0, s, ps);
    CPPUNIT_ASSERT_EQUAL(
        static_cast<std::vector<HighlightPortion>::size_type>(2), ps.size());
    CPPUNIT_ASSERT_EQUAL(0, ps[0].nBegin);
    CPPUNIT_ASSERT_EQUAL(5, ps[0].nEnd);
    CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
    CPPUNIT_ASSERT_EQUAL(5, ps[1].nBegin);
    CPPUNIT_ASSERT_EQUAL(6, ps[1].nEnd);
    CPPUNIT_ASSERT_EQUAL(TT_EOL, ps[1].tokenType);
}

void SyntaxHighlightTest::testBasicEmptyComment() {
    SyntaxHighlighter h;
    h.initialize(HIGHLIGHT_BASIC);
    OUString s("'");
    std::vector<HighlightPortion> ps;
    h.getHighlightPortions(0, s, ps);
    CPPUNIT_ASSERT_EQUAL(
        static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size());
    CPPUNIT_ASSERT_EQUAL(0, ps[0].nBegin);
    CPPUNIT_ASSERT_EQUAL(1, ps[0].nEnd);
    CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
}

void SyntaxHighlightTest::testBasicEmptyCommentNewline() {
    SyntaxHighlighter h;
    h.initialize(HIGHLIGHT_BASIC);
    OUString s("'\n");
    std::vector<HighlightPortion> ps;
    h.getHighlightPortions(0, s, ps);
    CPPUNIT_ASSERT_EQUAL(
        static_cast<std::vector<HighlightPortion>::size_type>(2), ps.size());
    CPPUNIT_ASSERT_EQUAL(0, ps[0].nBegin);
    CPPUNIT_ASSERT_EQUAL(1, ps[0].nEnd);
    CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
    CPPUNIT_ASSERT_EQUAL(1, ps[1].nBegin);
    CPPUNIT_ASSERT_EQUAL(2, ps[1].nEnd);
    CPPUNIT_ASSERT_EQUAL(TT_EOL, ps[1].tokenType);
}

void SyntaxHighlightTest::testBasic()
{
    OUString aBasicString("        if Mid(sText,iRun,1 )<> \" \" then Mid( sText ,iRun, 1, Chr( 1 + Asc( Mid(sText,iRun,1 )) ) '");

    SyntaxHighlighter aHighlighter;
    aHighlighter.initialize( HIGHLIGHT_BASIC );

    std::vector<HighlightPortion> aPortions;
    aHighlighter.getHighlightPortions( 0, aBasicString, aPortions );

    sal_Int32 prevEnd = 0;
    for(std::vector<HighlightPortion>::const_iterator itr =
            aPortions.begin(), itrEnd = aPortions.end(); itr != itrEnd; ++itr)
    {
        CPPUNIT_ASSERT_EQUAL(prevEnd, itr->nBegin);
        CPPUNIT_ASSERT(itr->nBegin < itr->nEnd);
        prevEnd = itr->nEnd;
    }
    CPPUNIT_ASSERT_EQUAL(aBasicString.getLength(), prevEnd);
}

CPPUNIT_TEST_SUITE_REGISTRATION(SyntaxHighlightTest);

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */