1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <comphelper/syntaxhighlight.hxx>
11 #include "cppunit/TestAssert.h"
12 #include "cppunit/TestFixture.h"
13 #include "cppunit/extensions/HelperMacros.h"
14 #include "cppunit/plugin/TestPlugIn.h"
15 #include <rtl/ustring.hxx>
20 class SyntaxHighlightTest
: public CppUnit::TestFixture
23 void testBasicString();
24 void testBasicComment();
25 void testBasicCommentNewline();
26 void testBasicEmptyComment();
27 void testBasicEmptyCommentNewline();
30 CPPUNIT_TEST_SUITE(SyntaxHighlightTest
);
31 CPPUNIT_TEST(testBasicString
);
32 CPPUNIT_TEST(testBasicComment
);
33 CPPUNIT_TEST(testBasicCommentNewline
);
34 CPPUNIT_TEST(testBasicEmptyComment
);
35 CPPUNIT_TEST(testBasicEmptyCommentNewline
);
36 CPPUNIT_TEST(testBasic
);
37 CPPUNIT_TEST_SUITE_END();
40 void SyntaxHighlightTest::testBasicString() {
41 OUString
s("\"foo\"");
42 std::vector
<HighlightPortion
> ps
;
43 SyntaxHighlighter(HIGHLIGHT_BASIC
).getHighlightPortions(s
, ps
);
45 static_cast<std::vector
<HighlightPortion
>::size_type
>(1), ps
.size());
46 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps
[0].nBegin
);
47 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps
[0].nEnd
);
48 CPPUNIT_ASSERT_EQUAL(TT_STRING
, ps
[0].tokenType
);
51 void SyntaxHighlightTest::testBasicComment() {
53 std::vector
<HighlightPortion
> ps
;
54 SyntaxHighlighter(HIGHLIGHT_BASIC
).getHighlightPortions(s
, ps
);
56 static_cast<std::vector
<HighlightPortion
>::size_type
>(1), ps
.size());
57 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps
[0].nBegin
);
58 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps
[0].nEnd
);
59 CPPUNIT_ASSERT_EQUAL(TT_COMMENT
, ps
[0].tokenType
);
62 void SyntaxHighlightTest::testBasicCommentNewline() {
63 OUString
s("' foo\n");
64 std::vector
<HighlightPortion
> ps
;
65 SyntaxHighlighter(HIGHLIGHT_BASIC
).getHighlightPortions(s
, ps
);
67 static_cast<std::vector
<HighlightPortion
>::size_type
>(2), ps
.size());
68 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps
[0].nBegin
);
69 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps
[0].nEnd
);
70 CPPUNIT_ASSERT_EQUAL(TT_COMMENT
, ps
[0].tokenType
);
71 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps
[1].nBegin
);
72 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), ps
[1].nEnd
);
73 CPPUNIT_ASSERT_EQUAL(TT_EOL
, ps
[1].tokenType
);
76 void SyntaxHighlightTest::testBasicEmptyComment() {
78 std::vector
<HighlightPortion
> ps
;
79 SyntaxHighlighter(HIGHLIGHT_BASIC
).getHighlightPortions(s
, ps
);
81 static_cast<std::vector
<HighlightPortion
>::size_type
>(1), ps
.size());
82 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps
[0].nBegin
);
83 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps
[0].nEnd
);
84 CPPUNIT_ASSERT_EQUAL(TT_COMMENT
, ps
[0].tokenType
);
87 void SyntaxHighlightTest::testBasicEmptyCommentNewline() {
89 std::vector
<HighlightPortion
> ps
;
90 SyntaxHighlighter(HIGHLIGHT_BASIC
).getHighlightPortions(s
, ps
);
92 static_cast<std::vector
<HighlightPortion
>::size_type
>(2), ps
.size());
93 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps
[0].nBegin
);
94 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps
[0].nEnd
);
95 CPPUNIT_ASSERT_EQUAL(TT_COMMENT
, ps
[0].tokenType
);
96 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps
[1].nBegin
);
97 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), ps
[1].nEnd
);
98 CPPUNIT_ASSERT_EQUAL(TT_EOL
, ps
[1].tokenType
);
101 void SyntaxHighlightTest::testBasic()
103 OUString
aBasicString(" if Mid(sText,iRun,1 )<> \" \" then Mid( sText ,iRun, 1, Chr( 1 + Asc( Mid(sText,iRun,1 )) ) '");
105 std::vector
<HighlightPortion
> aPortions
;
106 SyntaxHighlighter(HIGHLIGHT_BASIC
).getHighlightPortions(
107 aBasicString
, aPortions
);
109 sal_Int32 prevEnd
= 0;
110 for(std::vector
<HighlightPortion
>::const_iterator itr
=
111 aPortions
.begin(), itrEnd
= aPortions
.end(); itr
!= itrEnd
; ++itr
)
113 CPPUNIT_ASSERT_EQUAL(prevEnd
, itr
->nBegin
);
114 CPPUNIT_ASSERT(itr
->nBegin
< itr
->nEnd
);
117 CPPUNIT_ASSERT_EQUAL(aBasicString
.getLength(), prevEnd
);
120 CPPUNIT_TEST_SUITE_REGISTRATION(SyntaxHighlightTest
);
122 CPPUNIT_PLUGIN_IMPLEMENT();
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */