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 18 : 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 2 : CPPUNIT_TEST_SUITE(SyntaxHighlightTest);
31 1 : CPPUNIT_TEST(testBasicString);
32 1 : CPPUNIT_TEST(testBasicComment);
33 1 : CPPUNIT_TEST(testBasicCommentNewline);
34 1 : CPPUNIT_TEST(testBasicEmptyComment);
35 1 : CPPUNIT_TEST(testBasicEmptyCommentNewline);
36 1 : CPPUNIT_TEST(testBasic);
37 5 : CPPUNIT_TEST_SUITE_END();
38 : };
39 :
40 1 : void SyntaxHighlightTest::testBasicString() {
41 1 : OUString s("\"foo\"");
42 2 : std::vector<HighlightPortion> ps;
43 1 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(s, ps);
44 2 : CPPUNIT_ASSERT_EQUAL(
45 1 : static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size());
46 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin);
47 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[0].nEnd);
48 2 : CPPUNIT_ASSERT_EQUAL(TT_STRING, ps[0].tokenType);
49 1 : }
50 :
51 1 : void SyntaxHighlightTest::testBasicComment() {
52 1 : OUString s("' foo");
53 2 : std::vector<HighlightPortion> ps;
54 1 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(s, ps);
55 2 : CPPUNIT_ASSERT_EQUAL(
56 1 : static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size());
57 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin);
58 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[0].nEnd);
59 2 : CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
60 1 : }
61 :
62 1 : void SyntaxHighlightTest::testBasicCommentNewline() {
63 1 : OUString s("' foo\n");
64 2 : std::vector<HighlightPortion> ps;
65 1 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(s, ps);
66 2 : CPPUNIT_ASSERT_EQUAL(
67 1 : static_cast<std::vector<HighlightPortion>::size_type>(2), ps.size());
68 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin);
69 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[0].nEnd);
70 1 : CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
71 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(5), ps[1].nBegin);
72 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), ps[1].nEnd);
73 2 : CPPUNIT_ASSERT_EQUAL(TT_EOL, ps[1].tokenType);
74 1 : }
75 :
76 1 : void SyntaxHighlightTest::testBasicEmptyComment() {
77 1 : OUString s("'");
78 2 : std::vector<HighlightPortion> ps;
79 1 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(s, ps);
80 2 : CPPUNIT_ASSERT_EQUAL(
81 1 : static_cast<std::vector<HighlightPortion>::size_type>(1), ps.size());
82 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin);
83 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps[0].nEnd);
84 2 : CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
85 1 : }
86 :
87 1 : void SyntaxHighlightTest::testBasicEmptyCommentNewline() {
88 1 : OUString s("'\n");
89 2 : std::vector<HighlightPortion> ps;
90 1 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(s, ps);
91 2 : CPPUNIT_ASSERT_EQUAL(
92 1 : static_cast<std::vector<HighlightPortion>::size_type>(2), ps.size());
93 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ps[0].nBegin);
94 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps[0].nEnd);
95 1 : CPPUNIT_ASSERT_EQUAL(TT_COMMENT, ps[0].tokenType);
96 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(1), ps[1].nBegin);
97 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(2), ps[1].nEnd);
98 2 : CPPUNIT_ASSERT_EQUAL(TT_EOL, ps[1].tokenType);
99 1 : }
100 :
101 1 : void SyntaxHighlightTest::testBasic()
102 : {
103 1 : OUString aBasicString(" if Mid(sText,iRun,1 )<> \" \" then Mid( sText ,iRun, 1, Chr( 1 + Asc( Mid(sText,iRun,1 )) ) '");
104 :
105 2 : std::vector<HighlightPortion> aPortions;
106 : SyntaxHighlighter(HIGHLIGHT_BASIC).getHighlightPortions(
107 1 : aBasicString, aPortions );
108 :
109 1 : sal_Int32 prevEnd = 0;
110 56 : for(std::vector<HighlightPortion>::const_iterator itr =
111 1 : aPortions.begin(), itrEnd = aPortions.end(); itr != itrEnd; ++itr)
112 : {
113 55 : CPPUNIT_ASSERT_EQUAL(prevEnd, itr->nBegin);
114 55 : CPPUNIT_ASSERT(itr->nBegin < itr->nEnd);
115 55 : prevEnd = itr->nEnd;
116 : }
117 2 : CPPUNIT_ASSERT_EQUAL(aBasicString.getLength(), prevEnd);
118 1 : }
119 :
120 1 : CPPUNIT_TEST_SUITE_REGISTRATION(SyntaxHighlightTest);
121 :
122 4 : CPPUNIT_PLUGIN_IMPLEMENT();
123 :
124 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|