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