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 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <cppuhelper/compbase1.hxx>
21 : #include <cppuhelper/bootstrap.hxx>
22 : #include <cppuhelper/basemutex.hxx>
23 : #include <com/sun/star/util/SearchFlags.hpp>
24 : #include <com/sun/star/util/SearchOptions.hpp>
25 : #include <com/sun/star/util/SearchAlgorithms.hpp>
26 : #include <com/sun/star/util/XTextSearch.hpp>
27 : #include <unotest/bootstrapfixturebase.hxx>
28 :
29 : #include <unicode/regex.h>
30 :
31 : #include <rtl/strbuf.hxx>
32 : #include <rtl/ustrbuf.hxx>
33 :
34 : using namespace ::com::sun::star;
35 : using namespace U_ICU_NAMESPACE;
36 : typedef U_ICU_NAMESPACE::UnicodeString IcuUniString;
37 :
38 12 : class TestTextSearch : public test::BootstrapFixtureBase
39 : {
40 : public:
41 : virtual void setUp() SAL_OVERRIDE;
42 : virtual void tearDown() SAL_OVERRIDE;
43 :
44 : void testICU();
45 : void testSearches();
46 :
47 4 : CPPUNIT_TEST_SUITE(TestTextSearch);
48 2 : CPPUNIT_TEST(testICU);
49 2 : CPPUNIT_TEST(testSearches);
50 4 : CPPUNIT_TEST_SUITE_END();
51 : private:
52 : uno::Reference<util::XTextSearch> m_xSearch;
53 : };
54 :
55 : // Sanity check our ICU first ...
56 2 : void TestTextSearch::testICU()
57 : {
58 2 : UErrorCode nErr = U_ZERO_ERROR;
59 : RegexMatcher* pRegexMatcher;
60 2 : sal_uInt32 nSearchFlags = UREGEX_UWORD | UREGEX_CASE_INSENSITIVE;
61 :
62 2 : OUString aString( "abcdefgh" );
63 4 : OUString aPattern( "e" );
64 4 : IcuUniString aSearchPat( (const UChar*)aPattern.getStr(), aPattern.getLength() );
65 :
66 2 : pRegexMatcher = new RegexMatcher( aSearchPat, nSearchFlags, nErr );
67 :
68 4 : IcuUniString aSource( (const UChar*)aString.getStr(), aString.getLength() );
69 2 : pRegexMatcher->reset( aSource );
70 :
71 2 : CPPUNIT_ASSERT( pRegexMatcher->find( 0, nErr ) );
72 2 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
73 2 : CPPUNIT_ASSERT( pRegexMatcher->start( nErr ) == 4 );
74 2 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
75 2 : CPPUNIT_ASSERT( pRegexMatcher->end( nErr ) == 5 );
76 2 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
77 :
78 2 : delete pRegexMatcher;
79 :
80 4 : OUString aString2( "acababaabcababadcdaa" );
81 4 : OUString aPattern2( "a" );
82 :
83 4 : IcuUniString aSearchPat2( (const UChar*)aPattern2.getStr(), aPattern2.getLength() );
84 2 : pRegexMatcher = new RegexMatcher( aSearchPat2, nSearchFlags, nErr );
85 :
86 4 : IcuUniString aSource2( (const UChar*)aString2.getStr(), aString2.getLength() );
87 2 : pRegexMatcher->reset( aSource2 );
88 :
89 2 : CPPUNIT_ASSERT( pRegexMatcher->find( 0, nErr ) );
90 2 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
91 2 : CPPUNIT_ASSERT( pRegexMatcher->start( nErr ) == 0 );
92 2 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
93 2 : CPPUNIT_ASSERT( pRegexMatcher->end( nErr ) == 1 );
94 2 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
95 4 : delete pRegexMatcher;
96 2 : }
97 :
98 2 : void TestTextSearch::testSearches()
99 : {
100 2 : OUString str( "acababaabcababadcdaa" );
101 2 : sal_Int32 startPos = 2, endPos = 20 ;
102 4 : OUString searchStr( "(ab)*a(c|d)+" );
103 2 : sal_Int32 fStartRes = 10, fEndRes = 18 ;
104 2 : sal_Int32 bStartRes = 18, bEndRes = 10 ;
105 :
106 : // set options
107 4 : util::SearchOptions aOptions;
108 2 : aOptions.algorithmType = util::SearchAlgorithms_REGEXP ;
109 2 : aOptions.searchFlag = util::SearchFlags::ALL_IGNORE_CASE;
110 2 : aOptions.searchString = searchStr;
111 2 : m_xSearch->setOptions( aOptions );
112 :
113 4 : util::SearchResult aRes;
114 :
115 : // search forward
116 2 : aRes = m_xSearch->searchForward( str, startPos, endPos );
117 2 : CPPUNIT_ASSERT( aRes.subRegExpressions > 0 );
118 2 : CPPUNIT_ASSERT( aRes.startOffset[0] == fStartRes );
119 2 : CPPUNIT_ASSERT( aRes.endOffset[0] == fEndRes );
120 :
121 : // search backwards
122 2 : aRes = m_xSearch->searchBackward( str, endPos, startPos );
123 2 : CPPUNIT_ASSERT( aRes.subRegExpressions > 0 );
124 2 : CPPUNIT_ASSERT( aRes.startOffset[0] == bStartRes );
125 4 : CPPUNIT_ASSERT( aRes.endOffset[0] == bEndRes );
126 2 : }
127 :
128 4 : void TestTextSearch::setUp()
129 : {
130 4 : BootstrapFixtureBase::setUp();
131 12 : m_xSearch = uno::Reference< util::XTextSearch >(m_xSFactory->createInstance(
132 8 : "com.sun.star.util.TextSearch"), uno::UNO_QUERY_THROW);
133 4 : }
134 :
135 4 : void TestTextSearch::tearDown()
136 : {
137 4 : m_xSearch.clear();
138 4 : BootstrapFixtureBase::tearDown();
139 4 : }
140 :
141 2 : CPPUNIT_TEST_SUITE_REGISTRATION(TestTextSearch);
142 :
143 8 : CPPUNIT_PLUGIN_IMPLEMENT();
144 :
145 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|