1 /******************************************************************************* 2 * Copyright (c) 2010, 2011 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 ******************************************************************************/ 14 15 package org.eclipse.equinox.bidi.internal.tests; 16 17 import static org.junit.Assert.assertEquals; 18 import static org.junit.Assert.assertFalse; 19 import static org.junit.Assert.assertTrue; 20 21 import org.eclipse.equinox.bidi.StructuredTextTypeHandlerFactory; 22 import org.eclipse.equinox.bidi.advanced.*; 23 import org.eclipse.equinox.bidi.custom.StructuredTextCharTypes; 24 import org.eclipse.equinox.bidi.custom.StructuredTextTypeHandler; 25 import org.junit.Test; 26 27 /** 28 * Tests most public methods of BidiComplexEngine 29 */ 30 public class StructuredTextMethodsTest extends StructuredTextTestBase { 31 32 private final static int LTR = IStructuredTextExpert.DIR_LTR; 33 private final static int RTL = IStructuredTextExpert.DIR_RTL; 34 private final static StructuredTextEnvironment envLTR = new StructuredTextEnvironment(null, false, StructuredTextEnvironment.ORIENT_LTR); 35 private final static StructuredTextEnvironment envRTL = new StructuredTextEnvironment(null, false, StructuredTextEnvironment.ORIENT_RTL); 36 private final static StructuredTextEnvironment envRTLMIR = new StructuredTextEnvironment(null, true, StructuredTextEnvironment.ORIENT_RTL); 37 private final static StructuredTextEnvironment envIGN = new StructuredTextEnvironment(null, false, StructuredTextEnvironment.ORIENT_IGNORE); 38 private final static StructuredTextEnvironment envCLR = new StructuredTextEnvironment(null, false, StructuredTextEnvironment.ORIENT_CONTEXTUAL_LTR); 39 private final static StructuredTextEnvironment envCRL = new StructuredTextEnvironment(null, false, StructuredTextEnvironment.ORIENT_CONTEXTUAL_RTL); 40 private final static StructuredTextEnvironment envERR = new StructuredTextEnvironment(null, false, 9999); 41 private final static TestHandlerMyComma testMyCommaLL = new TestHandlerMyComma(LTR, LTR); 42 private final static TestHandlerMyComma testMyCommaRR = new TestHandlerMyComma(RTL, RTL); 43 private final static TestHandlerMyComma testMyCommaRL = new TestHandlerMyComma(RTL, LTR); 44 45 private static class TestHandlerMyComma extends StructuredTextTypeHandler { 46 47 private final static byte AL = Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC; 48 49 final int dirArabic; 50 final int dirHebrew; 51 TestHandlerMyComma(int dirArabic, int dirHebrew)52 public TestHandlerMyComma(int dirArabic, int dirHebrew) { 53 this.dirArabic = dirArabic; 54 this.dirHebrew = dirHebrew; 55 } 56 getSeparators(IStructuredTextExpert expert)57 public String getSeparators(IStructuredTextExpert expert) { 58 return ","; //$NON-NLS-1$ 59 } 60 skipProcessing(IStructuredTextExpert expert, String text, StructuredTextCharTypes charTypes)61 public boolean skipProcessing(IStructuredTextExpert expert, String text, StructuredTextCharTypes charTypes) { 62 byte charType = charTypes.getBidiTypeAt(0); 63 if (charType == AL) 64 return true; 65 return false; 66 } 67 getDirection(IStructuredTextExpert expert, String text)68 public int getDirection(IStructuredTextExpert expert, String text) { 69 return getDirection(expert, text, new StructuredTextCharTypes(expert, text)); 70 } 71 getDirection(IStructuredTextExpert expert, String text, StructuredTextCharTypes charTypes)72 public int getDirection(IStructuredTextExpert expert, String text, StructuredTextCharTypes charTypes) { 73 for (int i = 0; i < text.length(); i++) { 74 byte charType = charTypes.getBidiTypeAt(i); 75 if (charType == AL) 76 return dirArabic; 77 } 78 return dirHebrew; 79 } 80 } 81 doTestTools()82 private void doTestTools() { 83 84 // This method tests utility methods used by the JUnits 85 String data = "56789ABCDEFGHIJKLMNOPQRSTUVWXYZ~#@&><^|`"; 86 String text = toUT16(data); 87 String dat2 = toPseudo(text); 88 assertEquals(data, dat2); 89 90 text = toPseudo(data); 91 assertEquals("56789abcdefghijklmnopqrstuvwxyz~#@&><^|`", text); 92 93 text = array_display(null); 94 assertEquals("null", text); 95 } 96 doTestState()97 private void doTestState() { 98 String data, lean, full, model; 99 IStructuredTextExpert expert = StructuredTextExpertFactory.getStatefulExpert(StructuredTextTypeHandlerFactory.JAVA); 100 101 data = "A=B+C;/* D=E+F;"; 102 lean = toUT16(data); 103 full = expert.leanToFullText(lean); 104 model = "A@=B@+C@;/* D=E+F;"; 105 assertEquals("full1", model, toPseudo(full)); 106 107 data = "A=B+C; D=E+F;"; 108 lean = toUT16(data); 109 full = expert.leanToFullText(lean); 110 model = data; 111 assertEquals("full2", model, toPseudo(full)); 112 113 data = "SOME MORE COMMENTS"; 114 lean = toUT16(data); 115 full = expert.leanToFullText(lean); 116 model = data; 117 assertEquals("full3", model, toPseudo(full)); 118 119 data = "A=B+C;*/ D=E+F;"; 120 lean = toUT16(data); 121 full = expert.leanToFullText(lean); 122 model = "A=B+C;@*/ D@=E@+F;"; 123 assertEquals("full4", model, toPseudo(full)); 124 } 125 doTestOrientation()126 private void doTestOrientation() { 127 int orient = StructuredTextEnvironment.DEFAULT.getOrientation(); 128 assertEquals("orient #1", StructuredTextEnvironment.ORIENT_LTR, orient); 129 130 orient = envIGN.getOrientation(); 131 assertEquals("orient #2", StructuredTextEnvironment.ORIENT_IGNORE, orient); 132 133 orient = envCRL.getOrientation(); 134 assertEquals("orient #3", StructuredTextEnvironment.ORIENT_CONTEXTUAL_RTL, orient); 135 136 orient = envERR.getOrientation(); 137 assertEquals("orient #4", StructuredTextEnvironment.ORIENT_UNKNOWN, orient); 138 } 139 doTestOrient(StructuredTextTypeHandler handler, String label, String data, String resLTR, String resRTL, String resCon)140 private void doTestOrient(StructuredTextTypeHandler handler, String label, String data, String resLTR, String resRTL, String resCon) { 141 String full, lean; 142 143 IStructuredTextExpert expertLTR = StructuredTextExpertFactory.getStatefulExpert(handler, envLTR); 144 IStructuredTextExpert expertRTL = StructuredTextExpertFactory.getStatefulExpert(handler, envRTL); 145 IStructuredTextExpert expertCRL = StructuredTextExpertFactory.getStatefulExpert(handler, envCRL); 146 147 lean = toUT16(data); 148 full = expertLTR.leanToFullText(lean); 149 assertEquals(label + "LTR full", resLTR, toPseudo(full)); 150 full = expertRTL.leanToFullText(lean); 151 assertEquals("label + RTL full", resRTL, toPseudo(full)); 152 full = expertCRL.leanToFullText(lean); 153 assertEquals(label + "CON full", resCon, toPseudo(full)); 154 } 155 doTestSkipProcessing()156 private void doTestSkipProcessing() { 157 doTestOrient(testMyCommaLL, "Skip #1 ", "BCD,EF", "BCD@,EF", ">@BCD@,EF@^", "@BCD@,EF"); 158 doTestOrient(testMyCommaLL, "Skip #2 ", "#CD,EF", "#CD,EF", ">@#CD,EF@^", "@#CD,EF"); 159 } 160 doTestLeanOffsets()161 private void doTestLeanOffsets() { 162 String lean, data, label; 163 IStructuredTextExpert expert = StructuredTextExpertFactory.getStatefulExpert(StructuredTextTypeHandlerFactory.JAVA); 164 165 int[] offsets; 166 int[] model; 167 168 data = "A=B+C;/* D=E+F;"; 169 lean = toUT16(data); 170 offsets = expert.leanBidiCharOffsets(lean); 171 model = new int[] {1, 3, 5}; 172 label = "leanBidiCharOffsets() #1 "; 173 assertEquals(label, array_display(model), array_display(offsets)); 174 data = "A=B+C;*/ D=E+F;"; 175 lean = toUT16(data); 176 offsets = expert.leanBidiCharOffsets(lean); 177 model = new int[] {6, 10, 12}; 178 label = "leanBidiCharOffsets() #2 "; 179 assertEquals(label, array_display(model), array_display(offsets)); 180 } 181 doTestFullOffsets(String label, String data, int[] resLTR, int[] resRTL, int[] resCon)182 private void doTestFullOffsets(String label, String data, int[] resLTR, int[] resRTL, int[] resCon) { 183 String full, lean, msg; 184 int[] offsets; 185 IStructuredTextExpert expertLTR = StructuredTextExpertFactory.getExpert(StructuredTextTypeHandlerFactory.COMMA_DELIMITED, envLTR); 186 IStructuredTextExpert expertRTL = StructuredTextExpertFactory.getExpert(StructuredTextTypeHandlerFactory.COMMA_DELIMITED, envRTL); 187 IStructuredTextExpert expertCLR = StructuredTextExpertFactory.getExpert(StructuredTextTypeHandlerFactory.COMMA_DELIMITED, envCLR); 188 189 lean = toUT16(data); 190 full = expertLTR.leanToFullText(lean); 191 offsets = expertLTR.fullBidiCharOffsets(full); 192 msg = label + "LTR "; 193 assertEquals(msg, array_display(resLTR), array_display(offsets)); 194 full = expertRTL.leanToFullText(lean); 195 offsets = expertRTL.fullBidiCharOffsets(full); 196 msg = label + "RTL "; 197 assertEquals(msg, array_display(resRTL), array_display(offsets)); 198 full = expertCLR.leanToFullText(lean); 199 offsets = expertCLR.fullBidiCharOffsets(full); 200 msg = label + "CON "; 201 assertEquals(msg, array_display(resCon), array_display(offsets)); 202 } 203 doTestMirrored()204 private void doTestMirrored() { 205 boolean mirrored; 206 mirrored = StructuredTextEnvironment.DEFAULT.getMirrored(); 207 assertFalse("mirrored #1", mirrored); 208 StructuredTextEnvironment env = new StructuredTextEnvironment(null, true, StructuredTextEnvironment.ORIENT_LTR); 209 mirrored = env.getMirrored(); 210 assertTrue("mirrored #2", mirrored); 211 } 212 doTestDirection()213 private void doTestDirection() { 214 String data, lean, full, model; 215 int dirA, dirH; 216 IStructuredTextExpert expertRL = StructuredTextExpertFactory.getStatefulExpert(testMyCommaRL, envLTR); 217 dirA = expertRL.getTextDirection(toUT16("###")); 218 dirH = expertRL.getTextDirection(toUT16("ABC")); 219 assertTrue("TestDirection #1", dirA == RTL && dirH == LTR); 220 221 IStructuredTextExpert expertRR = StructuredTextExpertFactory.getStatefulExpert(testMyCommaRR, envLTR); 222 dirA = expertRR.getTextDirection(toUT16("###")); 223 dirH = expertRR.getTextDirection(toUT16("ABC")); 224 assertTrue("TestDirection #2", dirA == RTL && dirH == RTL); 225 226 IStructuredTextExpert expertLL = StructuredTextExpertFactory.getStatefulExpert(testMyCommaLL, envLTR); 227 lean = toUT16("ABC,#DEF,HOST,com"); 228 full = expertLL.leanToFullText(lean); 229 assertEquals("TestDirection #9 full", "ABC@,#DEF@,HOST,com", toPseudo(full)); 230 231 lean = toUT16("ABC,DEF,HOST,com"); 232 full = expertLL.leanToFullText(lean); 233 234 assertEquals("TestDirection #10 full", "ABC@,DEF@,HOST,com", toPseudo(full)); 235 236 StructuredTextEnvironment environment = new StructuredTextEnvironment(null, true, StructuredTextEnvironment.ORIENT_LTR); 237 IStructuredTextExpert expert = StructuredTextExpertFactory.getStatefulExpert(testMyCommaRL, environment); 238 dirA = expert.getTextDirection(toUT16("###")); 239 dirH = expert.getTextDirection(toUT16("ABC")); 240 assertTrue("TestDirection #10.5", dirA == RTL && dirH == LTR); 241 242 lean = toUT16("ABC,#DEF,HOST,com"); 243 full = expert.leanToFullText(lean); 244 assertEquals("TestDirection #11 full", "<&ABC,#DEF,HOST,com&^", toPseudo(full)); 245 246 data = "ABc,#DEF,HOSt,COM"; 247 lean = toUT16(data); 248 full = expert.leanToFullText(lean); 249 model = "<&ABc,#DEF,HOSt,COM&^"; 250 assertEquals("TestDirection #12 full", model, toPseudo(full)); 251 252 data = "ABc,#DEF,HOSt,"; 253 lean = toUT16(data); 254 full = expert.leanToFullText(lean); 255 model = "<&ABc,#DEF,HOSt,&^"; 256 assertEquals("TestDirection #13 full", model, toPseudo(full)); 257 258 data = "ABC,DEF,HOST,com"; 259 lean = toUT16(data); 260 full = expert.leanToFullText(lean); 261 model = "ABC@,DEF@,HOST,com"; 262 assertEquals("TestDirection #14 full", model, toPseudo(full)); 263 264 data = "--,---,----"; 265 lean = toUT16(data); 266 full = expert.leanToFullText(lean); 267 model = "--,---,----"; 268 assertEquals("TestDirection #15 full", model, toPseudo(full)); 269 270 data = "ABC,|DEF,HOST,com"; 271 lean = toUT16(data); 272 full = expert.leanToFullText(lean); 273 274 model = "ABC,|DEF@,HOST,com"; 275 assertEquals("TestDirection #16 full", model, toPseudo(full)); 276 277 data = "ABc,|#DEF,HOST,com"; 278 lean = toUT16(data); 279 expert = StructuredTextExpertFactory.getStatefulExpert(testMyCommaRL, envRTLMIR); 280 full = expert.leanToFullText(lean); 281 model = "ABc,|#DEF,HOST,com"; 282 assertEquals("TestDirection #17 full", model, toPseudo(full)); 283 int dir = expert.getTextDirection(lean); 284 assertEquals("Test curDirection", RTL, dir); 285 } 286 287 @Test testMethods()288 public void testMethods() { 289 290 doTestTools(); 291 292 doTestState(); 293 294 doTestOrientation(); 295 296 StructuredTextTypeHandler commaHandler = StructuredTextTypeHandlerFactory.getHandler(StructuredTextTypeHandlerFactory.COMMA_DELIMITED); 297 doTestOrient(commaHandler, "Methods #1 ", "", "", "", ""); 298 doTestOrient(commaHandler, "Methods #2 ", "abc", "abc", ">@abc@^", "abc"); 299 doTestOrient(commaHandler, "Methods #3 ", "ABC", "ABC", ">@ABC@^", "@ABC"); 300 doTestOrient(commaHandler, "Methods #4 ", "bcd,ef", "bcd,ef", ">@bcd,ef@^", "bcd,ef"); 301 doTestOrient(commaHandler, "Methods #5 ", "BCD,EF", "BCD@,EF", ">@BCD@,EF@^", "@BCD@,EF"); 302 doTestOrient(commaHandler, "Methods #6 ", "cde,FG", "cde,FG", ">@cde,FG@^", "cde,FG"); 303 doTestOrient(commaHandler, "Methods #7 ", "CDE,fg", "CDE,fg", ">@CDE,fg@^", "@CDE,fg"); 304 doTestOrient(commaHandler, "Methods #8 ", "12..def,GH", "12..def,GH", ">@12..def,GH@^", "12..def,GH"); 305 doTestOrient(commaHandler, "Methods #9 ", "34..DEF,gh", "34..DEF,gh", ">@34..DEF,gh@^", "@34..DEF,gh"); 306 307 doTestSkipProcessing(); 308 309 doTestLeanOffsets(); 310 311 doTestFullOffsets("TestFullOffsets ", "BCD,EF,G", new int[] {3, 7}, new int[] {0, 1, 5, 9, 12, 13}, new int[] {0, 4, 8}); 312 313 doTestMirrored(); 314 315 doTestDirection(); 316 317 IStructuredTextExpert expert = StructuredTextExpertFactory.getExpert(StructuredTextTypeHandlerFactory.COMMA_DELIMITED); 318 String data = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; 319 String lean = toUT16(data); 320 String full = expert.leanToFullText(lean); 321 String model = "A@,B@,C@,D@,E@,F@,G@,H@,I@,J@,K@,L@,M@,N@,O@,P@,Q@,R@,S@,T@,U@,V@,W@,X@,Y@,Z"; 322 assertEquals("many inserts", model, toPseudo(full)); 323 } 324 } 325