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>
19 class SyntaxHighlightTest
: public CppUnit::TestFixture
22 void testBasicString();
23 void testBasicComment();
24 void testBasicCommentNewline();
25 void testBasicEmptyComment();
26 void testBasicEmptyCommentNewline();
29 CPPUNIT_TEST_SUITE(SyntaxHighlightTest
);
30 CPPUNIT_TEST(testBasicString
);
31 CPPUNIT_TEST(testBasicComment
);
32 CPPUNIT_TEST(testBasicCommentNewline
);
33 CPPUNIT_TEST(testBasicEmptyComment
);
34 CPPUNIT_TEST(testBasicEmptyCommentNewline
);
35 CPPUNIT_TEST(testBasic
);
36 CPPUNIT_TEST_SUITE_END();
39 void SyntaxHighlightTest::testBasicString() {
40 std::vector
<HighlightPortion
> ps
;
41 SyntaxHighlighter(HighlighterLanguage::Basic
).getHighlightPortions(u
"\"foo\"", ps
);
43 static_cast<std::vector
<HighlightPortion
>::size_type
>(1), ps
.size());
44 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps
[0].nBegin
);
45 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps
[0].nEnd
);
46 CPPUNIT_ASSERT_EQUAL(TokenType::String
, ps
[0].tokenType
);
49 void SyntaxHighlightTest::testBasicComment() {
50 std::vector
<HighlightPortion
> ps
;
51 SyntaxHighlighter(HighlighterLanguage::Basic
).getHighlightPortions(u
"' foo", ps
);
53 static_cast<std::vector
<HighlightPortion
>::size_type
>(1), ps
.size());
54 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps
[0].nBegin
);
55 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps
[0].nEnd
);
56 CPPUNIT_ASSERT_EQUAL(TokenType::Comment
, ps
[0].tokenType
);
59 void SyntaxHighlightTest::testBasicCommentNewline() {
60 std::vector
<HighlightPortion
> ps
;
61 SyntaxHighlighter(HighlighterLanguage::Basic
).getHighlightPortions(u
"' foo\n", ps
);
63 static_cast<std::vector
<HighlightPortion
>::size_type
>(2), ps
.size());
64 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps
[0].nBegin
);
65 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps
[0].nEnd
);
66 CPPUNIT_ASSERT_EQUAL(TokenType::Comment
, ps
[0].tokenType
);
67 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps
[1].nBegin
);
68 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), ps
[1].nEnd
);
69 CPPUNIT_ASSERT_EQUAL(TokenType::EOL
, ps
[1].tokenType
);
72 void SyntaxHighlightTest::testBasicEmptyComment() {
73 std::vector
<HighlightPortion
> ps
;
74 SyntaxHighlighter(HighlighterLanguage::Basic
).getHighlightPortions(u
"'", ps
);
76 static_cast<std::vector
<HighlightPortion
>::size_type
>(1), ps
.size());
77 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps
[0].nBegin
);
78 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps
[0].nEnd
);
79 CPPUNIT_ASSERT_EQUAL(TokenType::Comment
, ps
[0].tokenType
);
82 void SyntaxHighlightTest::testBasicEmptyCommentNewline() {
83 std::vector
<HighlightPortion
> ps
;
84 SyntaxHighlighter(HighlighterLanguage::Basic
).getHighlightPortions(u
"'\n", ps
);
86 static_cast<std::vector
<HighlightPortion
>::size_type
>(2), ps
.size());
87 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps
[0].nBegin
);
88 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps
[0].nEnd
);
89 CPPUNIT_ASSERT_EQUAL(TokenType::Comment
, ps
[0].tokenType
);
90 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps
[1].nBegin
);
91 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), ps
[1].nEnd
);
92 CPPUNIT_ASSERT_EQUAL(TokenType::EOL
, ps
[1].tokenType
);
95 void SyntaxHighlightTest::testBasic()
97 OUString
aBasicString(" if Mid(sText,iRun,1 )<> \" \" then Mid( sText ,iRun, 1, Chr( 1 + Asc( Mid(sText,iRun,1 )) ) '");
99 std::vector
<HighlightPortion
> aPortions
;
100 SyntaxHighlighter(HighlighterLanguage::Basic
).getHighlightPortions(
101 aBasicString
, aPortions
);
103 sal_Int32 prevEnd
= 0;
104 for (auto const& portion
: aPortions
)
106 CPPUNIT_ASSERT_EQUAL(prevEnd
, portion
.nBegin
);
107 CPPUNIT_ASSERT(portion
.nBegin
< portion
.nEnd
);
108 prevEnd
= portion
.nEnd
;
110 CPPUNIT_ASSERT_EQUAL(aBasicString
.getLength(), prevEnd
);
113 CPPUNIT_TEST_SUITE_REGISTRATION(SyntaxHighlightTest
);
115 CPPUNIT_PLUGIN_IMPLEMENT();
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */