| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 |
| 6 #library('bidi_format_test'); |
| 7 |
| 8 #import('../../../lib/i18n/bidi_formatter.dart'); |
| 9 #import('../../../lib/i18n/bidi_utils.dart'); |
| 10 #import('../../../lib/unittest/unittest.dart'); |
| 11 |
| 12 /** |
| 13 * Tests the bidirectional text formatting library. |
| 14 */ |
| 15 main() { |
| 16 var LRM = BidiUtils.LRM; |
| 17 var RLM = BidiUtils.RLM; |
| 18 var LRE = BidiUtils.LRE; |
| 19 var RLE = BidiUtils.RLE; |
| 20 var PDF = BidiUtils.PDF; |
| 21 var LTR = TextDirection.LTR; |
| 22 var RTL = TextDirection.RTL; |
| 23 var UNKNOWN = TextDirection.UNKNOWN; |
| 24 var he = '\u05e0\u05e1'; |
| 25 var en = 'abba'; |
| 26 var html = '<'; |
| 27 var longEn = 'abba sabba gabba '; |
| 28 var longHe = '\u05e0 \u05e1 \u05e0 '; |
| 29 var ltrFmt = new BidiFormatter.LTR(); // LTR context |
| 30 var rtlFmt = new BidiFormatter.RTL(); // RTL context |
| 31 var unkFmt = new BidiFormatter.UNKNOWN(); // unknown context |
| 32 |
| 33 test('estimateDirection', () { |
| 34 expect(ltrFmt.estimateDirection(''), equals(UNKNOWN)); |
| 35 expect(rtlFmt.estimateDirection(''), equals(UNKNOWN)); |
| 36 expect(unkFmt.estimateDirection(''), equals(UNKNOWN)); |
| 37 expect(ltrFmt.estimateDirection(en), equals(LTR)); |
| 38 expect(rtlFmt.estimateDirection(en), equals(LTR)); |
| 39 expect(unkFmt.estimateDirection(en), equals(LTR)); |
| 40 expect(ltrFmt.estimateDirection(he), equals(RTL)); |
| 41 expect(rtlFmt.estimateDirection(he), equals(RTL)); |
| 42 expect(unkFmt.estimateDirection(he), equals(RTL)); |
| 43 |
| 44 // Text contains HTML or HTML-escaping. |
| 45 expect(ltrFmt.estimateDirection( |
| 46 '<some sort of tag/>$he &', false), equals(LTR)); |
| 47 expect(ltrFmt.estimateDirection( |
| 48 '<some sort of tag/>$he &', true), equals(RTL)); |
| 49 }); |
| 50 |
| 51 test('wrapWithSpan', () { |
| 52 // Test overall dir matches context dir (LTR), no dirReset. |
| 53 expect(ltrFmt.wrapWithSpan(en, true, false), equals(en)); |
| 54 // Test overall dir matches context dir (LTR), dirReset. |
| 55 expect(ltrFmt.wrapWithSpan(en, true, true), equals(en)); |
| 56 // Test overall dir matches context dir (RTL), no dirReset'. |
| 57 expect(rtlFmt.wrapWithSpan(he, true, false), equals(he)); |
| 58 // Test overall dir matches context dir (RTL), dirReset. |
| 59 expect(rtlFmt.wrapWithSpan(he, true, true), equals(he)); |
| 60 // Test overall dir (RTL) doesn't match context dir (LTR), no dirReset. |
| 61 expect(ltrFmt.wrapWithSpan(he, true, false), |
| 62 equals('<span dir=rtl>$he</span>')); |
| 63 // Test overall dir (RTL) doesn't match context dir (LTR), dirReset. |
| 64 expect(ltrFmt.wrapWithSpan(he, true, true), |
| 65 equals('<span dir=rtl>$he</span>$LRM')); |
| 66 // Test overall dir (LTR) doesn't match context dir (RTL), no dirReset. |
| 67 expect(rtlFmt.wrapWithSpan(en, true, false), |
| 68 equals('<span dir=ltr>$en</span>')); |
| 69 // Test overall dir (LTR) doesn't match context dir (RTL), dirReset. |
| 70 expect(rtlFmt.wrapWithSpan(en, true, true), |
| 71 equals('<span dir=ltr>$en</span>$RLM')); |
| 72 // Test overall dir (LTR) doesn't match context dir (unknown), no dirReset. |
| 73 expect(unkFmt.wrapWithSpan(en, true, false), |
| 74 equals('<span dir=ltr>$en</span>')); |
| 75 // Test overall dir (RTL) doesn't match context dir (unknown), dirReset. |
| 76 expect(unkFmt.wrapWithSpan(he, true, true), |
| 77 equals('<span dir=rtl>$he</span>')); |
| 78 // Test overall dir (neutral) doesn't match context dir (LTR), dirReset. |
| 79 expect(ltrFmt.wrapWithSpan('', true, true), equals('')); |
| 80 |
| 81 // Test exit dir (but not overall dir) is opposite to context dir, dirReset. |
| 82 expect(ltrFmt.wrapWithSpan('$longEn$he$html', true, true), |
| 83 equals('$longEn$he$html$LRM')); |
| 84 // Test overall dir (but not exit dir) is opposite to context dir, dirReset. |
| 85 expect(rtlFmt.wrapWithSpan('$longEn$he', true, true), |
| 86 equals('<span dir=ltr>$longEn$he</span>$RLM')); |
| 87 |
| 88 // Test input is plain text (not escaped). |
| 89 expect(ltrFmt.wrapWithSpan('<br>$en', false, false), |
| 90 equals('<br>$en')); |
| 91 |
| 92 var ltrAlwaysSpanFmt = new BidiFormatter.LTR(true); |
| 93 var rtlAlwaysSpanFmt = new BidiFormatter.RTL(true); |
| 94 var unkAlwaysSpanFmt = new BidiFormatter.UNKNOWN(true); |
| 95 |
| 96 // Test alwaysSpan, overall dir matches context dir (LTR), no dirReset. |
| 97 expect(ltrAlwaysSpanFmt.wrapWithSpan(en, true, false), |
| 98 equals('<span>$en</span>')); |
| 99 // Test alwaysSpan, overall dir matches context dir (LTR), dirReset. |
| 100 expect(ltrAlwaysSpanFmt.wrapWithSpan(en, true, true), |
| 101 equals('<span>$en</span>')); |
| 102 // Test alwaysSpan, overall dir matches context dir (RTL), no dirReset. |
| 103 expect(rtlAlwaysSpanFmt.wrapWithSpan(he, true, false), |
| 104 equals('<span>$he</span>')); |
| 105 // Test alwaysSpan, overall dir matches context dir (RTL), dirReset. |
| 106 expect(rtlAlwaysSpanFmt.wrapWithSpan(he, true, true), |
| 107 equals('<span>$he</span>')); |
| 108 |
| 109 // Test alwaysSpan, overall dir (RTL) doesn't match context dir (LTR), |
| 110 // no dirReset. |
| 111 expect(ltrAlwaysSpanFmt.wrapWithSpan(he, true, false), |
| 112 equals('<span dir=rtl>$he</span>')); |
| 113 // Test alwaysSpan, overall dir (RTL) doesn't match context dir (LTR), |
| 114 // dirReset. |
| 115 expect(ltrAlwaysSpanFmt.wrapWithSpan(he, true, true), |
| 116 equals('<span dir=rtl>$he</span>$LRM')); |
| 117 // Test alwaysSpan, overall dir (neutral) doesn't match context dir (LTR), |
| 118 // dirReset |
| 119 expect(ltrAlwaysSpanFmt.wrapWithSpan('', true, true), |
| 120 equals('<span></span>')); |
| 121 |
| 122 // Test overall dir matches context dir (LTR. |
| 123 expect(ltrFmt.wrapWithSpan(en, direction : TextDirection.LTR), equals(en)); |
| 124 // Test overall dir (but not exit dir) supposedly matches context dir (LTR). |
| 125 expect(ltrFmt.wrapWithSpan(he, direction : TextDirection.LTR), |
| 126 equals('$he$LRM')); |
| 127 // Test overall dir matches context dir (RTL. |
| 128 expect(rtlFmt.wrapWithSpan(he, direction : TextDirection.RTL), equals(he)); |
| 129 // Test overall dir (but not exit dir) supposedly matches context dir (RTL). |
| 130 expect(rtlFmt.wrapWithSpan(en, direction : TextDirection.RTL), |
| 131 equals('$en$RLM')); |
| 132 |
| 133 // Test overall dir (RTL) doesn't match context dir (LTR). |
| 134 expect(ltrFmt.wrapWithSpan(he, direction : TextDirection.RTL), |
| 135 equals('<span dir=rtl>$he</span>$LRM')); |
| 136 // Test supposed overall dir (RTL) doesn't match context dir (LTR). |
| 137 expect(ltrFmt.wrapWithSpan(en, direction : TextDirection.RTL), |
| 138 equals('<span dir=rtl>$en</span>$LRM')); |
| 139 // Test overall dir (LTR) doesn't match context dir (RTL). |
| 140 expect(rtlFmt.wrapWithSpan(en, direction : TextDirection.LTR), |
| 141 equals('<span dir=ltr>$en</span>$RLM')); |
| 142 // Test supposed overall dir (LTR) doesn't match context dir (RTL). |
| 143 expect(rtlFmt.wrapWithSpan(he, direction : TextDirection.LTR), |
| 144 equals('<span dir=ltr>$he</span>$RLM')); |
| 145 // Test supposed overall dir (LTR) doesn't match context dir (unknown). |
| 146 expect(unkFmt.wrapWithSpan(he, direction : TextDirection.LTR), |
| 147 equals('<span dir=ltr>$he</span>')); |
| 148 // Test supposed overall dir (neutral) doesn't match context dir (LTR). |
| 149 expect(ltrFmt.wrapWithSpan(he, direction : TextDirection.UNKNOWN), |
| 150 equals('$he$LRM')); |
| 151 }); |
| 152 |
| 153 test('wrapWithUnicode', () { |
| 154 // Test overall dir matches context dir (LTR), no dirReset. |
| 155 expect(ltrFmt.wrapWithUnicode(en, true, false), equals(en)); |
| 156 // Test overall dir matches context dir (LTR), dirReset. |
| 157 expect(ltrFmt.wrapWithUnicode(en, true, true), equals(en)); |
| 158 // Test overall dir matches context dir (RTL), no dirReset. |
| 159 expect(rtlFmt.wrapWithUnicode(he, true, false), equals(he)); |
| 160 // Test overall dir matches context dir (RTL), dirReset. |
| 161 expect(rtlFmt.wrapWithUnicode(he, true, true), equals(he)); |
| 162 |
| 163 // Test overall dir (RTL) doesn't match context dir (LTR), no dirReset. |
| 164 expect(ltrFmt.wrapWithUnicode(he, true, false), equals('$RLE$he$PDF')); |
| 165 // Test overall dir (RTL) doesn't match context dir (LTR), dirReset. |
| 166 expect(ltrFmt.wrapWithUnicode(he, true, true), equals('$RLE$he$PDF$LRM')); |
| 167 // Test overall dir (LTR) doesn't match context dir (RTL), no dirReset. |
| 168 expect(rtlFmt.wrapWithUnicode(en, true, false), equals('$LRE$en$PDF')); |
| 169 // Test overall dir (LTR) doesn't match context dir (RTL), dirReset. |
| 170 expect(rtlFmt.wrapWithUnicode(en, true, true), equals('$LRE$en$PDF$RLM')); |
| 171 // Test overall dir (LTR) doesn't match context dir (unknown), no dirReset. |
| 172 expect(unkFmt.wrapWithUnicode(en, true, false), equals('$LRE$en$PDF')); |
| 173 // Test overall dir (RTL) doesn't match context dir (unknown), dirReset. |
| 174 expect(unkFmt.wrapWithUnicode(he, true, true), equals('$RLE$he$PDF')); |
| 175 // Test overall dir (neutral) doesn't match context dir (LTR), dirReset. |
| 176 expect(ltrFmt.wrapWithUnicode('', true, true), equals('')); |
| 177 |
| 178 // Test exit dir (but not overall dir) is opposite to context dir, dirReset. |
| 179 expect(ltrFmt.wrapWithUnicode('$longEn$he$html', true, true), |
| 180 equals('$longEn$he$html$LRM')); |
| 181 // Test overall dir (but not exit dir) is opposite to context dir, dirReset. |
| 182 expect(rtlFmt.wrapWithUnicode('$longEn$he', true, true), |
| 183 equals('$LRE$longEn$he$PDF$RLM')); |
| 184 |
| 185 // Test overall dir matches context dir (LTR). |
| 186 expect(ltrFmt.wrapWithUnicode(en, |
| 187 direction : TextDirection.LTR), equals(en)); |
| 188 // Test overall dir (but not exit dir) supposedly matches context dir (LTR). |
| 189 expect(ltrFmt.wrapWithUnicode(he, direction : TextDirection.LTR), |
| 190 equals('$he$LRM')); |
| 191 // Test overall dir matches context dir (RTL). |
| 192 expect(rtlFmt.wrapWithUnicode(he, |
| 193 direction : TextDirection.RTL), equals(he)); |
| 194 // Test overall dir (but not exit dir) supposedly matches context dir (RTL). |
| 195 expect(rtlFmt.wrapWithUnicode(en, direction : TextDirection.RTL), |
| 196 equals('$en$RLM')); |
| 197 |
| 198 // Test overall dir (RTL) doesn't match context dir (LTR). |
| 199 expect(ltrFmt.wrapWithUnicode(he, direction : TextDirection.RTL), |
| 200 equals('$RLE$he$PDF$LRM')); |
| 201 // Test supposed overall dir (RTL) doesn't match context dir (LTR). |
| 202 expect(ltrFmt.wrapWithUnicode(en, direction : TextDirection.RTL), |
| 203 equals('$RLE$en$PDF$LRM')); |
| 204 // Test overall dir (LTR) doesn't match context dir (RTL). |
| 205 expect(rtlFmt.wrapWithUnicode(en, direction : TextDirection.LTR), |
| 206 equals('$LRE$en$PDF$RLM')); |
| 207 // Test supposed overall dir (LTR) doesn't match context dir (RTL). |
| 208 expect(rtlFmt.wrapWithUnicode(he, direction : TextDirection.LTR), |
| 209 equals('$LRE$he$PDF$RLM')); |
| 210 // Test supposed overall dir (LTR) doesn't match context dir (unknown). |
| 211 expect(unkFmt.wrapWithUnicode(he, direction : TextDirection.LTR), |
| 212 equals('$LRE$he$PDF')); |
| 213 // Test supposed overall dir (neutral) doesn't match context dir (LTR). |
| 214 expect(ltrFmt.wrapWithUnicode(he, direction : TextDirection.UNKNOWN), |
| 215 equals('$he$LRM')); |
| 216 }); |
| 217 } |
| OLD | NEW |