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