Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: tests/lib/i18n/bidi_format_test.dart

Issue 10592011: Add BiDirectional Text formatting utilites to the i18n library. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 &amp;', false), equals(LTR));
47 expect(ltrFmt.estimateDirection(
48 '<some sort of tag/>$he &amp;', true), equals(RTL));
49 });
50
51 test('spanWrap', () {
52 // Test overall dir matches context dir (LTR), no dirReset.
53 expect(ltrFmt.spanWrap(en, true, false), equals(en));
54 // Test overall dir matches context dir (LTR), dirReset.
55 expect(ltrFmt.spanWrap(en, true, true), equals(en));
56 // Test overall dir matches context dir (RTL), no dirReset'.
57 expect(rtlFmt.spanWrap(he, true, false), equals(he));
58 // Test overall dir matches context dir (RTL), dirReset.
59 expect(rtlFmt.spanWrap(he, true, true), equals(he));
60 // Test overall dir (RTL) doesn't match context dir (LTR), no dirReset.
61 expect(ltrFmt.spanWrap(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.spanWrap(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.spanWrap(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.spanWrap(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.spanWrap(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.spanWrap(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.spanWrap('', true, true), equals(''));
80
81 // Test exit dir (but not overall dir) is opposite to context dir, dirReset.
82 expect(ltrFmt.spanWrap('$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.spanWrap('$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.spanWrap('<br>$en', false, false),
90 equals('&lt;br&gt;$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.spanWrap(en, true, false),
98 equals('<span>$en</span>'));
99 // Test alwaysSpan, overall dir matches context dir (LTR), dirReset.
100 expect(ltrAlwaysSpanFmt.spanWrap(en, true, true),
101 equals('<span>$en</span>'));
102 // Test alwaysSpan, overall dir matches context dir (RTL), no dirReset.
103 expect(rtlAlwaysSpanFmt.spanWrap(he, true, false),
104 equals('<span>$he</span>'));
105 // Test alwaysSpan, overall dir matches context dir (RTL), dirReset.
106 expect(rtlAlwaysSpanFmt.spanWrap(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.spanWrap(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.spanWrap(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.spanWrap('', true, true),
120 equals('<span></span>'));
121
122 // Test overall dir matches context dir (LTR.
123 expect(ltrFmt.spanWrap(en, direction : TextDirection.LTR), equals(en));
124 // Test overall dir (but not exit dir) supposedly matches context dir (LTR).
125 expect(ltrFmt.spanWrap(he, direction : TextDirection.LTR),
126 equals('$he$LRM'));
127 // Test overall dir matches context dir (RTL.
128 expect(rtlFmt.spanWrap(he, direction : TextDirection.RTL), equals(he));
129 // Test overall dir (but not exit dir) supposedly matches context dir (RTL).
130 expect(rtlFmt.spanWrap(en, direction : TextDirection.RTL),
131 equals('$en$RLM'));
132
133 // Test overall dir (RTL) doesn't match context dir (LTR).
134 expect(ltrFmt.spanWrap(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.spanWrap(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.spanWrap(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.spanWrap(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.spanWrap(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.spanWrap(he, direction : TextDirection.UNKNOWN),
150 equals('$he$LRM'));
151 });
152
153 test('unicodeWrap', () {
154 // Test overall dir matches context dir (LTR), no dirReset.
155 expect(ltrFmt.unicodeWrap(en, true, false), equals(en));
156 // Test overall dir matches context dir (LTR), dirReset.
157 expect(ltrFmt.unicodeWrap(en, true, true), equals(en));
158 // Test overall dir matches context dir (RTL), no dirReset.
159 expect(rtlFmt.unicodeWrap(he, true, false), equals(he));
160 // Test overall dir matches context dir (RTL), dirReset.
161 expect(rtlFmt.unicodeWrap(he, true, true), equals(he));
162
163 // Test overall dir (RTL) doesn't match context dir (LTR), no dirReset.
164 expect(ltrFmt.unicodeWrap(he, true, false), equals('$RLE$he$PDF'));
165 // Test overall dir (RTL) doesn't match context dir (LTR), dirReset.
166 expect(ltrFmt.unicodeWrap(he, true, true), equals('$RLE$he$PDF$LRM'));
167 // Test overall dir (LTR) doesn't match context dir (RTL), no dirReset.
168 expect(rtlFmt.unicodeWrap(en, true, false), equals('$LRE$en$PDF'));
169 // Test overall dir (LTR) doesn't match context dir (RTL), dirReset.
170 expect(rtlFmt.unicodeWrap(en, true, true), equals('$LRE$en$PDF$RLM'));
171 // Test overall dir (LTR) doesn't match context dir (unknown), no dirReset.
172 expect(unkFmt.unicodeWrap(en, true, false), equals('$LRE$en$PDF'));
173 // Test overall dir (RTL) doesn't match context dir (unknown), dirReset.
174 expect(unkFmt.unicodeWrap(he, true, true), equals('$RLE$he$PDF'));
175 // Test overall dir (neutral) doesn't match context dir (LTR), dirReset.
176 expect(ltrFmt.unicodeWrap('', true, true), equals(''));
177
178 // Test exit dir (but not overall dir) is opposite to context dir, dirReset.
179 expect(ltrFmt.unicodeWrap('$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.unicodeWrap('$longEn$he', true, true),
183 equals('$LRE$longEn$he$PDF$RLM'));
184
185 // Test overall dir matches context dir (LTR).
186 expect(ltrFmt.unicodeWrap(en, direction : TextDirection.LTR), equals(en));
187 // Test overall dir (but not exit dir) supposedly matches context dir (LTR).
188 expect(ltrFmt.unicodeWrap(he, direction : TextDirection.LTR),
189 equals('$he$LRM'));
190 // Test overall dir matches context dir (RTL).
191 expect(rtlFmt.unicodeWrap(he, direction : TextDirection.RTL), equals(he));
192 // Test overall dir (but not exit dir) supposedly matches context dir (RTL).
193 expect(rtlFmt.unicodeWrap(en, direction : TextDirection.RTL),
194 equals('$en$RLM'));
195
196 // Test overall dir (RTL) doesn't match context dir (LTR).
197 expect(ltrFmt.unicodeWrap(he, direction : TextDirection.RTL),
198 equals('$RLE$he$PDF$LRM'));
199 // Test supposed overall dir (RTL) doesn't match context dir (LTR).
200 expect(ltrFmt.unicodeWrap(en, direction : TextDirection.RTL),
201 equals('$RLE$en$PDF$LRM'));
202 // Test overall dir (LTR) doesn't match context dir (RTL).
203 expect(rtlFmt.unicodeWrap(en, direction : TextDirection.LTR),
204 equals('$LRE$en$PDF$RLM'));
205 // Test supposed overall dir (LTR) doesn't match context dir (RTL).
206 expect(rtlFmt.unicodeWrap(he, direction : TextDirection.LTR),
207 equals('$LRE$he$PDF$RLM'));
208 // Test supposed overall dir (LTR) doesn't match context dir (unknown).
209 expect(unkFmt.unicodeWrap(he, direction : TextDirection.LTR),
210 equals('$LRE$he$PDF'));
211 // Test supposed overall dir (neutral) doesn't match context dir (LTR).
212 expect(ltrFmt.unicodeWrap(he, direction : TextDirection.UNKNOWN),
213 equals('$he$LRM'));
214 });
215
216 test('markAfter', () {
217 // Test exit dir (RTL) is opposite to context dir (LTR.
218 expect(ltrFmt.markAfter('$longEn$he$html', true), equals(LRM));
219 // Test exit dir (LTR) is opposite to context dir (RTL.
220 expect(rtlFmt.markAfter('$longHe$en', true), equals(RLM));
221 // Test exit dir (LTR) doesn't match context dir (unknown.
222 expect(unkFmt.markAfter('$longEn$en', true), equals(''));
223 // Test overall dir (RTL) is opposite to context dir (LTR.
224 expect(ltrFmt.markAfter('$longHe$en', true), equals(LRM));
225 // Test overall dir (LTR) is opposite to context dir (RTL.
226 expect(rtlFmt.markAfter('longEn$he', true), equals(RLM));
227 // Test exit dir and overall dir match context dir (LTR.
228 expect(ltrFmt.markAfter('$longEn$he$html', false), equals(''));
229 // Test exit dir and overall dir matches context dir (RTL.
230 expect(rtlFmt.markAfter('$longHe$he', true), equals(''));
231 });
232
233 test('mark', () {
234 // Implicitly also tests the constructor.
235 expect(new BidiFormatter.LTR().mark(), equals(BidiUtils.LRM));
236 expect(new BidiFormatter.UNKNOWN().mark(), equals(''));
237 expect(new BidiFormatter.RTL().mark(), equals(BidiUtils.RLM));
238 });
239 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698