Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : */
9 :
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>
16 :
17 : #include <cassert>
18 : #include <vector>
19 :
20 36 : class SyntaxHighlightTest : public CppUnit::TestFixture
21 : {
22 : public:
23 : void testBasicString();
24 : void testBasicComment();
25 : void testBasicCommentNewline();
26 : void testBasicEmptyComment();
27 : void testBasicEmptyCommentNewline();
28 : void testBasic();
29 :
30 4 : CPPUNIT_TEST_SUITE(SyntaxHighlightTest);
31 2 : CPPUNIT_TEST(testBasicString);
32 2 : CPPUNIT_TEST(testBasicComment);
33 2 : CPPUNIT_TEST(testBasicCommentNewline);
34 2 : CPPUNIT_TEST(testBasicEmptyComment);
35 2 : CPPUNIT_TEST(testBasicEmptyCommentNewline);
36 2 : CPPUNIT_TEST(testBasic);
37 4 : CPPUNIT_TEST_SUITE_END();
38 : };
39 :
40 2 : void SyntaxHighlightTest::testBasicString() {
41 2 : OUString s("\"foo\"");
42 4 : std::vector<HighlightPortion> ps;
43 2 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(s, ps);
44 4 : CPPUNIT_ASSERT_EQUAL(
45 2 : static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size());
46 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin);
47 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[0].nEnd);
48 4 : CPPUNIT_ASSERT_EQUAL(TT_STRING, ps[0].tokenType);
49 2 : }
50 :
51 2 : void SyntaxHighlightTest::testBasicComment() {
52 2 : OUString s("' foo");
53 4 : std::vector<HighlightPortion> ps;
54 2 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(s, ps);
55 4 : CPPUNIT_ASSERT_EQUAL(
56 2 : static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size());
57 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin);
58 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[0].nEnd);
59 4 : CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
60 2 : }
61 :
62 2 : void SyntaxHighlightTest::testBasicCommentNewline() {
63 2 : OUString s("' foo\n");
64 4 : std::vector<HighlightPortion> ps;
65 2 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(s, ps);
66 4 : CPPUNIT_ASSERT_EQUAL(
67 2 : static_cast<std::vector<HighlightPortion>::size_type>(2), ps.size());
68 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin);
69 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[0].nEnd);
70 2 : CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
71 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[1].nBegin);
72 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), ps[1].nEnd);
73 4 : CPPUNIT_ASSERT_EQUAL(TT_EOL, ps[1].tokenType);
74 2 : }
75 :
76 2 : void SyntaxHighlightTest::testBasicEmptyComment() {
77 2 : OUString s("'");
78 4 : std::vector<HighlightPortion> ps;
79 2 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(s, ps);
80 4 : CPPUNIT_ASSERT_EQUAL(
81 2 : static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size());
82 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin);
83 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps[0].nEnd);
84 4 : CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
85 2 : }
86 :
87 2 : void SyntaxHighlightTest::testBasicEmptyCommentNewline() {
88 2 : OUString s("'\n");
89 4 : std::vector<HighlightPortion> ps;
90 2 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(s, ps);
91 4 : CPPUNIT_ASSERT_EQUAL(
92 2 : static_cast<std::vector<HighlightPortion>::size_type>(2), ps.size());
93 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin);
94 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps[0].nEnd);
95 2 : CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
96 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps[1].nBegin);
97 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(2), ps[1].nEnd);
98 4 : CPPUNIT_ASSERT_EQUAL(TT_EOL, ps[1].tokenType);
99 2 : }
100 :
101 2 : void SyntaxHighlightTest::testBasic()
102 : {
103 2 : OUString aBasicString(" if Mid(sText,iRun,1 )<> \" \" then Mid( sText ,iRun, 1, Chr( 1 + Asc( Mid(sText,iRun,1 )) ) '");
104 :
105 4 : std::vector<HighlightPortion> aPortions;
106 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(
107 2 : aBasicString, aPortions );
108 :
109 2 : sal_Int32 prevEnd = 0;
110 112 : for(std::vector<HighlightPortion>::const_iterator itr =
111 2 : aPortions.begin(), itrEnd = aPortions.end(); itr != itrEnd; ++itr)
112 : {
113 110 : CPPUNIT_ASSERT_EQUAL(prevEnd, itr->nBegin);
114 110 : CPPUNIT_ASSERT(itr->nBegin < itr->nEnd);
115 110 : prevEnd = itr->nEnd;
116 : }
117 4 : CPPUNIT_ASSERT_EQUAL(aBasicString.getLength(), prevEnd);
118 2 : }
119 :
120 2 : CPPUNIT_TEST_SUITE_REGISTRATION(SyntaxHighlightTest);
121 :
122 8 : CPPUNIT_PLUGIN_IMPLEMENT();
123 :
124 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|