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 6 : 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 2 : CPPUNIT_TEST_SUITE(TestTextSearch);
48 1 : CPPUNIT_TEST(testICU);
49 1 : CPPUNIT_TEST(testSearches);
50 5 : CPPUNIT_TEST_SUITE_END();
51 : private:
52 : uno::Reference<util::XTextSearch> m_xSearch;
53 : };
54 :
55 : // Sanity check our ICU first ...
56 1 : void TestTextSearch::testICU()
57 : {
58 1 : UErrorCode nErr = U_ZERO_ERROR;
59 : RegexMatcher* pRegexMatcher;
60 1 : sal_uInt32 nSearchFlags = UREGEX_UWORD | UREGEX_CASE_INSENSITIVE;
61 :
62 1 : OUString aString( "abcdefgh" );
63 2 : OUString aPattern( "e" );
64 2 : IcuUniString aSearchPat( reinterpret_cast<const UChar*>(aPattern.getStr()), aPattern.getLength() );
65 :
66 1 : pRegexMatcher = new RegexMatcher( aSearchPat, nSearchFlags, nErr );
67 :
68 2 : IcuUniString aSource( reinterpret_cast<const UChar*>(aString.getStr()), aString.getLength() );
69 1 : pRegexMatcher->reset( aSource );
70 :
71 1 : CPPUNIT_ASSERT( pRegexMatcher->find( 0, nErr ) );
72 1 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
73 1 : CPPUNIT_ASSERT( pRegexMatcher->start( nErr ) == 4 );
74 1 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
75 1 : CPPUNIT_ASSERT( pRegexMatcher->end( nErr ) == 5 );
76 1 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
77 :
78 1 : delete pRegexMatcher;
79 :
80 2 : OUString aString2( "acababaabcababadcdaa" );
81 2 : OUString aPattern2( "a" );
82 :
83 2 : IcuUniString aSearchPat2( reinterpret_cast<const UChar*>(aPattern2.getStr()), aPattern2.getLength() );
84 1 : pRegexMatcher = new RegexMatcher( aSearchPat2, nSearchFlags, nErr );
85 :
86 2 : IcuUniString aSource2( reinterpret_cast<const UChar*>(aString2.getStr()), aString2.getLength() );
87 1 : pRegexMatcher->reset( aSource2 );
88 :
89 1 : CPPUNIT_ASSERT( pRegexMatcher->find( 0, nErr ) );
90 1 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
91 1 : CPPUNIT_ASSERT( pRegexMatcher->start( nErr ) == 0 );
92 1 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
93 1 : CPPUNIT_ASSERT( pRegexMatcher->end( nErr ) == 1 );
94 1 : CPPUNIT_ASSERT( nErr == U_ZERO_ERROR );
95 2 : delete pRegexMatcher;
96 1 : }
97 :
98 1 : void TestTextSearch::testSearches()
99 : {
100 1 : OUString str( "acababaabcababadcdaa" );
101 1 : sal_Int32 startPos = 2, endPos = 20 ;
102 2 : OUString searchStr( "(ab)*a(c|d)+" );
103 1 : sal_Int32 fStartRes = 10, fEndRes = 18 ;
104 1 : sal_Int32 bStartRes = 18, bEndRes = 10 ;
105 :
106 : // set options
107 2 : util::SearchOptions aOptions;
108 1 : aOptions.algorithmType = util::SearchAlgorithms_REGEXP ;
109 1 : aOptions.searchFlag = util::SearchFlags::ALL_IGNORE_CASE;
110 1 : aOptions.searchString = searchStr;
111 1 : m_xSearch->setOptions( aOptions );
112 :
113 2 : util::SearchResult aRes;
114 :
115 : // search forward
116 1 : aRes = m_xSearch->searchForward( str, startPos, endPos );
117 1 : CPPUNIT_ASSERT( aRes.subRegExpressions > 0 );
118 1 : CPPUNIT_ASSERT( aRes.startOffset[0] == fStartRes );
119 1 : CPPUNIT_ASSERT( aRes.endOffset[0] == fEndRes );
120 :
121 : // search backwards
122 1 : aRes = m_xSearch->searchBackward( str, endPos, startPos );
123 1 : CPPUNIT_ASSERT( aRes.subRegExpressions > 0 );
124 1 : CPPUNIT_ASSERT( aRes.startOffset[0] == bStartRes );
125 2 : CPPUNIT_ASSERT( aRes.endOffset[0] == bEndRes );
126 1 : }
127 :
128 2 : void TestTextSearch::setUp()
129 : {
130 2 : BootstrapFixtureBase::setUp();
131 6 : m_xSearch = uno::Reference< util::XTextSearch >(m_xSFactory->createInstance(
132 4 : "com.sun.star.util.TextSearch"), uno::UNO_QUERY_THROW);
133 2 : }
134 :
135 2 : void TestTextSearch::tearDown()
136 : {
137 2 : m_xSearch.clear();
138 2 : BootstrapFixtureBase::tearDown();
139 2 : }
140 :
141 1 : CPPUNIT_TEST_SUITE_REGISTRATION(TestTextSearch);
142 :
143 4 : CPPUNIT_PLUGIN_IMPLEMENT();
144 :
145 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|