| <?xml version="1.0" encoding="UTF-8"?> |
| <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> |
| <script:module xmlns:script="http://openoffice.org/2000/script" script:name="i18n_XBreakIterator" script:language="StarBasic"> |
| |
| |
| '************************************************************************* |
| ' |
| ' Licensed to the Apache Software Foundation (ASF) under one |
| ' or more contributor license agreements. See the NOTICE file |
| ' distributed with this work for additional information |
| ' regarding copyright ownership. The ASF licenses this file |
| ' to you under the Apache License, Version 2.0 (the |
| ' "License"); you may not use this file except in compliance |
| ' with the License. You may obtain a copy of the License at |
| ' |
| ' http://www.apache.org/licenses/LICENSE-2.0 |
| ' |
| ' Unless required by applicable law or agreed to in writing, |
| ' software distributed under the License is distributed on an |
| ' "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| ' KIND, either express or implied. See the License for the |
| ' specific language governing permissions and limitations |
| ' under the License. |
| ' |
| '************************************************************************* |
| |
| |
| |
| |
| |
| ' Be sure that all variables are dimensioned: |
| option explicit |
| |
| '************************************************************************* |
| ' This Interface/Service test depends on the following GLOBAL variables, |
| ' which must be specified in the object creation: |
| |
| ' - Global oLocale As Object |
| ' - Global UnicodeString As String |
| ' - Global ScriptType(2, 1) As Integer |
| |
| '************************************************************************* |
| |
| |
| |
| |
| |
| const arrSize = 300 |
| |
| |
| Sub RunTest() |
| |
| '************************************************************************* |
| ' INTERFACE: |
| ' com.sun.star.i18n.XBreakIterator |
| '************************************************************************* |
| On Error Goto ErrHndl |
| Dim bOK As Boolean |
| Dim i As Integer |
| Dim nCharacterIteratorMode As Integer |
| Dim lDone As Long |
| Dim lRes As Long |
| Dim wordType As Integer |
| Dim Boundary As Variant |
| Dim BoundsToTest(arrSize) As new com.sun.star.i18n.Boundary |
| Dim Types(arrSize) As Integer |
| Dim nBoundsToTest As Integer |
| Dim iPos As Integer |
| Dim iType As Integer |
| Dim bFound As Boolean |
| Dim j As Integer |
| Dim NextCharacters(2, 3) As Integer |
| Dim PreviousCharacters(2, 3) As Integer |
| |
| Test.StartMethod("nextCharacters()") |
| bOK = true |
| nCharacterIteratorMode = com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER |
| |
| 'Start from position : Travel ... chars : Actual position after : How many chars traveled |
| NextCharacters(0, 0) = 1 : NextCharacters(0, 1) = 5000 : NextCharacters(0, 2) = len(UnicodeString) : NextCharacters(0, 3) = len(UnicodeString) - 1 |
| NextCharacters(1, 0) = 5 : NextCharacters(1, 1) = -5000 : NextCharacters(1, 2) = 0 : NextCharacters(1, 3) = -5 |
| NextCharacters(2, 0) = 10 : NextCharacters(2, 1) = 6 : NextCharacters(2, 2) = 16 : NextCharacters(2, 3) = 6 |
| |
| for i = 0 to 2 |
| lRes = oObj.nextCharacters(UnicodeString, NextCharacters(i, 0), _ |
| oLocale, nCharacterIteratorMode, NextCharacters(i, 1), lDone) |
| Out.Log("Expected result is: lRes = " + NextCharacters(i, 2) + _ |
| "; lDone = " + NextCharacters(i, 3)) |
| Out.Log("Actual result is: lRes = " + lRes + "; lDone = " + lDone) |
| |
| bOK = bOK AND lRes = NextCharacters(i, 2) |
| bOK = bOK AND lDone = NextCharacters(i, 3) |
| next i |
| |
| Test.MethodTested("nextCharacters()", bOK) |
| |
| Test.StartMethod("previousCharacters()") |
| bOK = true |
| nCharacterIteratorMode = com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER |
| |
| 'Start from position : Travel ... chars : Actual position after : How many chars traveled |
| PreviousCharacters(0, 0) = 10 : PreviousCharacters(0, 1) = -5000 : PreviousCharacters(0, 2) = len(UnicodeString) : PreviousCharacters(0, 3) = len(UnicodeString) - 10 |
| PreviousCharacters(1, 0) = 5 : PreviousCharacters(1, 1) = 5000 : PreviousCharacters(1, 2) = 0 : PreviousCharacters(1, 3) = 5 |
| PreviousCharacters(2, 0) = 10 : PreviousCharacters(2, 1) = 6 : PreviousCharacters(2, 2) = 4 : PreviousCharacters(2, 3) = 6 |
| |
| for i = 0 to 2 |
| lRes = oObj.previousCharacters(UnicodeString, PreviousCharacters(i, 0), _ |
| oLocale, nCharacterIteratorMode, PreviousCharacters(i, 1), lDone) |
| Out.Log("Expected result is: lRes = " + PreviousCharacters(i, 2) + _ |
| "; lDone = " + PreviousCharacters(i, 3)) |
| Out.Log("Actual result is: lRes = " + lRes + "; lDone = " + lDone) |
| |
| bOK = bOK AND lRes = PreviousCharacters(i, 2) |
| bOK = bOK AND lDone = PreviousCharacters(i, 3) |
| next i |
| |
| Test.MethodTested("previousCharacters()", bOK) |
| |
| |
| Test.StartMethod("nextWord()") |
| Test.StartMethod("previousWord()") |
| bOK = true |
| wordType = com.sun.star.i18n.WordType.ANYWORD_IGNOREWHITESPACES |
| Dim wordCount1 As Integer |
| Dim wordCount2 As Integer |
| wordCount1 = 0 |
| wordCount2 = 0 |
| i = 0 |
| nBoundsToTest = 0 |
| |
| while i < len(UnicodeString) - 1 |
| Boundary = oObj.nextWord(UnicodeString, i, oLocale, wordType) |
| i = Boundary.endPos - 1 |
| wordCount1 = wordCount1 + 1 |
| if ((nBoundsToTest < arrSize) AND (Boundary.endPos - Boundary.startPos > 3)) then |
| BoundsToTest(nBoundsToTest).startPos = Boundary.startPos |
| BoundsToTest(nBoundsToTest).endPos = Boundary.endPos ' Here we find out some words and |
| nBoundsToTest = nBoundsToTest + 1 ' get their bounds to test in future |
| end if |
| Out.Log("Word " + wordCount1 + "(" + Boundary.startPos + "," + Boundary.endPos + "): '" + _ |
| mid(UnicodeString, Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'") |
| wend |
| Out.Log("In text there are " + wordCount1 + " words, if count from left to right") |
| |
| while i > 0 |
| Boundary = oObj.previousWord(UnicodeString, i, oLocale, wordType) |
| i = Boundary.startPos |
| wordCount2 = wordCount2 + 1 |
| Out.Log("Word " + wordCount2 + "(" + Boundary.startPos + "," + Boundary.endPos + "): '" + _ |
| mid(UnicodeString, Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'") |
| wend |
| Out.Log("In text there are " + wordCount2 + " words, if count from right to left") |
| |
| bOK = bOK AND wordCount1 = wordCount2 |
| Test.MethodTested("previousWord()", bOK) |
| Test.MethodTested("nextWord()", bOK) |
| |
| Test.StartMethod("getWordBoundary()") |
| bOK = true |
| if (nBoundsToTest = 0) then |
| Out.Log("Couldn't find words longer than 3 letters.") |
| bOK = false |
| end if |
| |
| for i = 0 to nBoundsToTest - 1 |
| iPos = (BoundsToTest(i).endPos - BoundsToTest(i).startPos) / 2 + BoundsToTest(i).startPos ' calculate middle of the word |
| Boundary = oObj.getWordBoundary(UnicodeString, iPos, oLocale, wordType, true) |
| Out.Log("Expected result is: startPos = " + BoundsToTest(i).startPos + _ |
| "; endPos = " + BoundsToTest(i).endPos) |
| Out.Log("Actual result is: startPos = " + Boundary.startPos + "; endPos = " + Boundary.endPos + _ |
| " Word is: '" + mid(UnicodeString, Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'") |
| |
| bOK = bOK AND BoundsToTest(i).startPos = Boundary.startPos |
| bOK = bOK AND BoundsToTest(i).endPos = Boundary.endPos |
| next i |
| |
| Test.MethodTested("getWordBoundary()", bOK) |
| |
| Test.StartMethod("getWordType()") |
| bOK = true |
| for i = 0 to nBoundsToTest - 1 |
| iPos = (BoundsToTest(i).endPos - BoundsToTest(i).startPos) / 2 + BoundsToTest(i).startPos ' calculate middle of the word |
| wordType = oObj.getWordType(UnicodeString, iPos, oLocale) |
| bOK = bOK AND wordType >= 0 AND wordType <= 2 |
| next i |
| Test.MethodTested("getWordType()", bOK) |
| |
| Test.StartMethod("isBeginWord()") |
| bOK = true |
| if (nBoundsToTest = 0) then |
| Out.Log("Couldn't find words longer than 3 letters.") |
| bOK = false |
| end if |
| |
| for i = 0 to nBoundsToTest - 1 |
| bOK = bOK AND oObj.isBeginWord(UnicodeString, BoundsToTest(i).startPos, oLocale, com.sun.star.i18n.WordType.ANY_WORD) |
| bOK = bOK AND NOT oObj.isBeginWord(UnicodeString, BoundsToTest(i).startPos + 1, oLocale, com.sun.star.i18n.WordType.ANY_WORD) |
| next i |
| Test.MethodTested("isBeginWord()", bOK) |
| |
| Test.StartMethod("isEndWord()") |
| bOK = true |
| if (nBoundsToTest = 0) then |
| Out.Log("Couldn't find words longer than 3 letters.") |
| bOK = false |
| end if |
| |
| for i = 0 to nBoundsToTest - 1 |
| bOK = bOK AND oObj.isEndWord(UnicodeString, BoundsToTest(i).endPos, oLocale, com.sun.star.i18n.WordType.ANY_WORD) |
| bOK = bOK AND NOT oObj.isEndWord(UnicodeString, BoundsToTest(i).endPos - 1, oLocale, com.sun.star.i18n.WordType.ANY_WORD) |
| next i |
| Test.MethodTested("isEndWord()", bOK) |
| |
| Test.StartMethod("beginOfSentence()") |
| Test.StartMethod("endOfSentence()") |
| Dim sStart As Integer |
| Dim sEnd As Integer |
| |
| bOK = true |
| iPos = 0 |
| while (iPos <= len(UnicodeString))' AND bOK) |
| sStart = oObj.beginOfSentence(UnicodeString, iPos, oLocale) |
| sEnd = oObj.endOfSentence(UnicodeString, iPos, oLocale) |
| if (iPos < sStart OR iPos > sEnd) then |
| bOK = false |
| Out.Log("From position " + iPos + " sentence range is [" + sStart + ", " + sEnd + "]") |
| Out.Log("'" + mid(UnicodeString, sStart + 1, sEnd - sStart) + "'") |
| end if |
| iPos = iPos + 1 |
| wend |
| |
| if (bOK) then |
| bOK = oObj.beginOfSentence(UnicodeString, -10, oLocale) = -1 |
| bOK = oObj.endOfSentence(UnicodeString, -10, oLocale) = -1 |
| bOK = oObj.beginOfSentence(UnicodeString, len(UnicodeString) + 1, oLocale) = -1 |
| bOK = oObj.endOfSentence(UnicodeString, len(UnicodeString) + 1, oLocale) = -1 |
| if (NOT bOK) then Out.Log("When invalid position, returned range is NOT [-1, -1]") |
| end if |
| |
| Test.MethodTested("beginOfSentence()", bOK) |
| Test.MethodTested("endOfSentence()", bOK) |
| |
| Test.StartMethod("getLineBreak()") |
| bOK = true |
| Dim LineBreakResults As Variant |
| Dim _LineBreakHyphenationOptions As new com.sun.star.i18n.LineBreakHyphenationOptions |
| Dim _LineBreakUserOptions As new com.sun.star.i18n.LineBreakUserOptions |
| Dim breakPos As Long, pos As Long |
| _LineBreakUserOptions.applyForbiddenRules = false |
| _LineBreakUserOptions.allowHyphenateEnglish = false |
| |
| breakPos = 0 : pos = 0 |
| |
| while breakPos = 0 AND pos < len(UnicodeString) |
| LineBreakResults = oObj.getLineBreak(UnicodeString, pos, oLocale, 0, _LineBreakHyphenationOptions, _LineBreakUserOptions) |
| breakPos = LineBreakResults.BreakIndex |
| pos = pos + 1 |
| wend |
| |
| ' finally the position of break must be found in the middle and |
| ' it must be before the break position specified |
| bOK = (breakPos <= pos AND breakPos > 0) |
| |
| if (NOT bOK) then |
| Out.Log("The last position was: " + pos + ", and the break position was: " + breakPos) |
| end if |
| |
| Test.MethodTested("getLineBreak()", bOK) |
| |
| |
| Test.StartMethod("beginOfScript()") |
| Test.StartMethod("endOfScript()") |
| bOK = true |
| Out.Log("Searching for scripts...") |
| iPos = 0 |
| nBoundsToTest = 0 |
| |
| while iPos < len(UnicodeString) AND iPos > -1 AND nBoundsToTest < arrSize |
| iType = oObj.getScriptType(UnicodeString, iPos) |
| BoundsToTest(nBoundsToTest).startPos = oObj.beginOfScript(UnicodeString, iPos, iType) |
| BoundsToTest(nBoundsToTest).endPos = oObj.endOfScript(UnicodeString, iPos, iType) |
| Types(nBoundsToTest) = iType |
| iPos = BoundsToTest(nBoundsToTest).endPos |
| Out.Log("Type: " + iType + "; Bounds: [" + BoundsToTest(nBoundsToTest).startPos + "," + BoundsToTest(nBoundsToTest).endPos + "]") |
| nBoundsToTest = nBoundsToTest + 1 |
| wend |
| bOK = bOK AND nBoundsToTest > 0 |
| |
| Test.MethodTested("beginOfScript()", bOK) |
| Test.MethodTested("endOfScript()", bOK) |
| |
| |
| Test.StartMethod("nextScript()") |
| bOK = true |
| i = 1 |
| iPos = -1 |
| while i < nBoundsToTest AND bOK |
| iPos = oObj.nextScript(UnicodeString, iPos, Types(i)) |
| Out.Log("Expected index for Type " + Types(i) + " for nextScript is " + BoundsToTest(i).startPos) |
| bOK = bOK AND iPos = BoundsToTest(i).startPos |
| Out.Log("NextScript returned " + iPos + ";") |
| i = i + 1 |
| wend |
| Test.MethodTested("nextScript()", bOK) |
| |
| Test.StartMethod("previousScript()") |
| bOK = true |
| i = nBoundsToTest - 2 |
| iPos = len(UnicodeString) + 1 |
| while i >= 0 AND bOK |
| iPos = oObj.previousScript(UnicodeString, iPos, Types(i)) |
| Out.Log("Expected index for Type " + Types(i) + " for previousScript is " + BoundsToTest(i).startPos) |
| bOK = bOK AND iPos = BoundsToTest(i).startPos |
| Out.Log("PreviousScript returned " + iPos + ";") |
| i = i - 1 |
| wend |
| Test.MethodTested("previousScript()", bOK) |
| |
| Test.StartMethod("getScriptType()") |
| bOK = true |
| for i = 0 to 2 |
| iPos = oObj.getScriptType(UnicodeString, ScriptType(i, 0)) |
| Out.Log("Expected result is: ScriptType = " + ScriptType(i, 1)) |
| Out.Log("Actual result is: ScriptType = " + iPos) |
| bOK = bOK AND iPos = ScriptType(i, 1) |
| next i |
| Test.MethodTested("getScriptType()", bOK) |
| |
| Test.StartMethod("beginOfCharBlock()") |
| Test.StartMethod("endOfCharBlock()") |
| bOK = true |
| Out.Log("getting CharBlocks bounds...") |
| iPos = 0 |
| nBoundsToTest = 0 |
| |
| while iPos < len(UnicodeString) AND iPos > -1 AND nBoundsToTest < arrSize |
| iType = getCharBlockType(iPos) |
| BoundsToTest(nBoundsToTest).startPos = oObj.beginOfCharBlock(UnicodeString, iPos, oLocale, iType) |
| BoundsToTest(nBoundsToTest).endPos = oObj.endOfCharBlock(UnicodeString, iPos, oLocale, iType) |
| iPos = BoundsToTest(nBoundsToTest).endPos |
| Out.Log("" + nBoundsToTest + "). Bounds: [" + BoundsToTest(nBoundsToTest).startPos + "," + BoundsToTest(nBoundsToTest).endPos + "]; Type = " + iType) |
| Types(nBoundsToTest) = iType |
| nBoundsToTest = nBoundsToTest + 1 |
| wend |
| |
| for i = 0 to nBoundsToTest - 2 |
| bOK = bOK AND BoundsToTest(i).endPos = BoundsToTest(i + 1).startPos |
| next |
| Out.Log("Testing for no intersections : " + bOK) |
| bOK = bOK AND BoundsToTest(0).startPos = 0 |
| bOK = bOK AND BoundsToTest(nBoundsToTest - 1).endPos = len(UnicodeString) |
| Out.Log("Regions should starts with 0 and ends with " + len(UnicodeString)) |
| |
| Test.MethodTested("beginOfCharBlock()", bOK) |
| Test.MethodTested("endOfCharBlock()", bOK) |
| |
| Test.StartMethod("nextCharBlock()") |
| bOK = true |
| for i = 0 to nBoundsToTest - 1 |
| iPos = oObj.nextCharBlock(UnicodeString, BoundsToTest(i).startPos - 1, oLocale, Types(i)) |
| if (iPos <> BoundsToTest(i).startPos) then |
| bOK = false |
| Out.Log("nextCharBlock(UnicodeString, " + BoundsToTest(i).startPos - 1 + ", oLocale, " + Types(i) + ") should return " + BoundsToTest(i).startPos) |
| Out.Log("... and actual value is " + iPos) |
| end if |
| next i |
| Test.MethodTested("nextCharBlock()", bOK) |
| |
| Test.StartMethod("previousCharBlock()") |
| bOK = true |
| for i = 0 to nBoundsToTest - 1 |
| iPos = oObj.previousCharBlock(UnicodeString, BoundsToTest(i).endPos + 1, oLocale, Types(i)) |
| if (iPos <> BoundsToTest(i).startPos) then |
| bOK = false |
| Out.Log("previousCharBlock(UnicodeString, " + BoundsToTest(i).startPos - 1 +", oLocale, " + Types(i) + ") should return " + BoundsToTest(i).startPos) |
| Out.Log("... and actual value is " + iPos) |
| end if |
| next i |
| Test.MethodTested("previousCharBlock()", bOK) |
| |
| Exit Sub |
| ErrHndl: |
| Test.Exception() |
| bOK = false |
| resume next |
| End Sub |
| Function getCharBlockType(pos As Integer) As Integer |
| Dim i As Integer |
| Dim cType As Integer |
| |
| cType = 0 |
| i = 1 |
| while (i < 31) |
| if (oObj.beginOfCharBlock(UnicodeString, pos, oLocale, i) <> -1) then |
| cType = i |
| i = 100 |
| end if |
| i = i + 1 |
| wend |
| |
| getCharBlockType = cType |
| End Function |
| </script:module> |