| Index: tests/lib/i18n/bidi_format_test.dart
|
| ===================================================================
|
| --- tests/lib/i18n/bidi_format_test.dart (revision 0)
|
| +++ tests/lib/i18n/bidi_format_test.dart (revision 0)
|
| @@ -0,0 +1,239 @@
|
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
| +// for details. All rights reserved. Use of this source code is governed by a
|
| +// BSD-style license that can be found in the LICENSE file.
|
| +
|
| +
|
| +#library('bidi_format_test');
|
| +
|
| +#import('../../../lib/i18n/bidi_formatter.dart');
|
| +#import('../../../lib/i18n/bidi_utils.dart');
|
| +#import('../../../lib/unittest/unittest.dart');
|
| +
|
| +/**
|
| + * Tests the bidirectional text formatting library.
|
| + */
|
| +main() {
|
| + var LRM = BidiUtils.LRM;
|
| + var RLM = BidiUtils.RLM;
|
| + var LRE = BidiUtils.LRE;
|
| + var RLE = BidiUtils.RLE;
|
| + var PDF = BidiUtils.PDF;
|
| + var LTR = TextDirection.LTR;
|
| + var RTL = TextDirection.RTL;
|
| + var UNKNOWN = TextDirection.UNKNOWN;
|
| + var he = '\u05e0\u05e1';
|
| + var en = 'abba';
|
| + var html = '<';
|
| + var longEn = 'abba sabba gabba ';
|
| + var longHe = '\u05e0 \u05e1 \u05e0 ';
|
| + var ltrFmt = new BidiFormatter.LTR(); // LTR context
|
| + var rtlFmt = new BidiFormatter.RTL(); // RTL context
|
| + var unkFmt = new BidiFormatter.UNKNOWN(); // unknown context
|
| +
|
| + test('estimateDirection', () {
|
| + expect(ltrFmt.estimateDirection(''), equals(UNKNOWN));
|
| + expect(rtlFmt.estimateDirection(''), equals(UNKNOWN));
|
| + expect(unkFmt.estimateDirection(''), equals(UNKNOWN));
|
| + expect(ltrFmt.estimateDirection(en), equals(LTR));
|
| + expect(rtlFmt.estimateDirection(en), equals(LTR));
|
| + expect(unkFmt.estimateDirection(en), equals(LTR));
|
| + expect(ltrFmt.estimateDirection(he), equals(RTL));
|
| + expect(rtlFmt.estimateDirection(he), equals(RTL));
|
| + expect(unkFmt.estimateDirection(he), equals(RTL));
|
| +
|
| + // Text contains HTML or HTML-escaping.
|
| + expect(ltrFmt.estimateDirection(
|
| + '<some sort of tag/>$he &', false), equals(LTR));
|
| + expect(ltrFmt.estimateDirection(
|
| + '<some sort of tag/>$he &', true), equals(RTL));
|
| + });
|
| +
|
| + test('spanWrap', () {
|
| + // Test overall dir matches context dir (LTR), no dirReset.
|
| + expect(ltrFmt.spanWrap(en, true, false), equals(en));
|
| + // Test overall dir matches context dir (LTR), dirReset.
|
| + expect(ltrFmt.spanWrap(en, true, true), equals(en));
|
| + // Test overall dir matches context dir (RTL), no dirReset'.
|
| + expect(rtlFmt.spanWrap(he, true, false), equals(he));
|
| + // Test overall dir matches context dir (RTL), dirReset.
|
| + expect(rtlFmt.spanWrap(he, true, true), equals(he));
|
| + // Test overall dir (RTL) doesn't match context dir (LTR), no dirReset.
|
| + expect(ltrFmt.spanWrap(he, true, false),
|
| + equals('<span dir=rtl>$he</span>'));
|
| + // Test overall dir (RTL) doesn't match context dir (LTR), dirReset.
|
| + expect(ltrFmt.spanWrap(he, true, true),
|
| + equals('<span dir=rtl>$he</span>$LRM'));
|
| + // Test overall dir (LTR) doesn't match context dir (RTL), no dirReset.
|
| + expect(rtlFmt.spanWrap(en, true, false),
|
| + equals('<span dir=ltr>$en</span>'));
|
| + // Test overall dir (LTR) doesn't match context dir (RTL), dirReset.
|
| + expect(rtlFmt.spanWrap(en, true, true),
|
| + equals('<span dir=ltr>$en</span>$RLM'));
|
| + // Test overall dir (LTR) doesn't match context dir (unknown), no dirReset.
|
| + expect(unkFmt.spanWrap(en, true, false),
|
| + equals('<span dir=ltr>$en</span>'));
|
| + // Test overall dir (RTL) doesn't match context dir (unknown), dirReset.
|
| + expect(unkFmt.spanWrap(he, true, true),
|
| + equals('<span dir=rtl>$he</span>'));
|
| + // Test overall dir (neutral) doesn't match context dir (LTR), dirReset.
|
| + expect(ltrFmt.spanWrap('', true, true), equals(''));
|
| +
|
| + // Test exit dir (but not overall dir) is opposite to context dir, dirReset.
|
| + expect(ltrFmt.spanWrap('$longEn$he$html', true, true),
|
| + equals('$longEn$he$html$LRM'));
|
| + // Test overall dir (but not exit dir) is opposite to context dir, dirReset.
|
| + expect(rtlFmt.spanWrap('$longEn$he', true, true),
|
| + equals('<span dir=ltr>$longEn$he</span>$RLM'));
|
| +
|
| + // Test input is plain text (not escaped).
|
| + expect(ltrFmt.spanWrap('<br>$en', false, false),
|
| + equals('<br>$en'));
|
| +
|
| + var ltrAlwaysSpanFmt = new BidiFormatter.LTR(true);
|
| + var rtlAlwaysSpanFmt = new BidiFormatter.RTL(true);
|
| + var unkAlwaysSpanFmt = new BidiFormatter.UNKNOWN(true);
|
| +
|
| + // Test alwaysSpan, overall dir matches context dir (LTR), no dirReset.
|
| + expect(ltrAlwaysSpanFmt.spanWrap(en, true, false),
|
| + equals('<span>$en</span>'));
|
| + // Test alwaysSpan, overall dir matches context dir (LTR), dirReset.
|
| + expect(ltrAlwaysSpanFmt.spanWrap(en, true, true),
|
| + equals('<span>$en</span>'));
|
| + // Test alwaysSpan, overall dir matches context dir (RTL), no dirReset.
|
| + expect(rtlAlwaysSpanFmt.spanWrap(he, true, false),
|
| + equals('<span>$he</span>'));
|
| + // Test alwaysSpan, overall dir matches context dir (RTL), dirReset.
|
| + expect(rtlAlwaysSpanFmt.spanWrap(he, true, true),
|
| + equals('<span>$he</span>'));
|
| +
|
| + // Test alwaysSpan, overall dir (RTL) doesn't match context dir (LTR),
|
| + // no dirReset.
|
| + expect(ltrAlwaysSpanFmt.spanWrap(he, true, false),
|
| + equals('<span dir=rtl>$he</span>'));
|
| + // Test alwaysSpan, overall dir (RTL) doesn't match context dir (LTR),
|
| + // dirReset.
|
| + expect(ltrAlwaysSpanFmt.spanWrap(he, true, true),
|
| + equals('<span dir=rtl>$he</span>$LRM'));
|
| + // Test alwaysSpan, overall dir (neutral) doesn't match context dir (LTR),
|
| + // dirReset
|
| + expect(ltrAlwaysSpanFmt.spanWrap('', true, true),
|
| + equals('<span></span>'));
|
| +
|
| + // Test overall dir matches context dir (LTR.
|
| + expect(ltrFmt.spanWrap(en, direction : TextDirection.LTR), equals(en));
|
| + // Test overall dir (but not exit dir) supposedly matches context dir (LTR).
|
| + expect(ltrFmt.spanWrap(he, direction : TextDirection.LTR),
|
| + equals('$he$LRM'));
|
| + // Test overall dir matches context dir (RTL.
|
| + expect(rtlFmt.spanWrap(he, direction : TextDirection.RTL), equals(he));
|
| + // Test overall dir (but not exit dir) supposedly matches context dir (RTL).
|
| + expect(rtlFmt.spanWrap(en, direction : TextDirection.RTL),
|
| + equals('$en$RLM'));
|
| +
|
| + // Test overall dir (RTL) doesn't match context dir (LTR).
|
| + expect(ltrFmt.spanWrap(he, direction : TextDirection.RTL),
|
| + equals('<span dir=rtl>$he</span>$LRM'));
|
| + // Test supposed overall dir (RTL) doesn't match context dir (LTR).
|
| + expect(ltrFmt.spanWrap(en, direction : TextDirection.RTL),
|
| + equals('<span dir=rtl>$en</span>$LRM'));
|
| + // Test overall dir (LTR) doesn't match context dir (RTL).
|
| + expect(rtlFmt.spanWrap(en, direction : TextDirection.LTR),
|
| + equals('<span dir=ltr>$en</span>$RLM'));
|
| + // Test supposed overall dir (LTR) doesn't match context dir (RTL).
|
| + expect(rtlFmt.spanWrap(he, direction : TextDirection.LTR),
|
| + equals('<span dir=ltr>$he</span>$RLM'));
|
| + // Test supposed overall dir (LTR) doesn't match context dir (unknown).
|
| + expect(unkFmt.spanWrap(he, direction : TextDirection.LTR),
|
| + equals('<span dir=ltr>$he</span>'));
|
| + // Test supposed overall dir (neutral) doesn't match context dir (LTR).
|
| + expect(ltrFmt.spanWrap(he, direction : TextDirection.UNKNOWN),
|
| + equals('$he$LRM'));
|
| + });
|
| +
|
| + test('unicodeWrap', () {
|
| + // Test overall dir matches context dir (LTR), no dirReset.
|
| + expect(ltrFmt.unicodeWrap(en, true, false), equals(en));
|
| + // Test overall dir matches context dir (LTR), dirReset.
|
| + expect(ltrFmt.unicodeWrap(en, true, true), equals(en));
|
| + // Test overall dir matches context dir (RTL), no dirReset.
|
| + expect(rtlFmt.unicodeWrap(he, true, false), equals(he));
|
| + // Test overall dir matches context dir (RTL), dirReset.
|
| + expect(rtlFmt.unicodeWrap(he, true, true), equals(he));
|
| +
|
| + // Test overall dir (RTL) doesn't match context dir (LTR), no dirReset.
|
| + expect(ltrFmt.unicodeWrap(he, true, false), equals('$RLE$he$PDF'));
|
| + // Test overall dir (RTL) doesn't match context dir (LTR), dirReset.
|
| + expect(ltrFmt.unicodeWrap(he, true, true), equals('$RLE$he$PDF$LRM'));
|
| + // Test overall dir (LTR) doesn't match context dir (RTL), no dirReset.
|
| + expect(rtlFmt.unicodeWrap(en, true, false), equals('$LRE$en$PDF'));
|
| + // Test overall dir (LTR) doesn't match context dir (RTL), dirReset.
|
| + expect(rtlFmt.unicodeWrap(en, true, true), equals('$LRE$en$PDF$RLM'));
|
| + // Test overall dir (LTR) doesn't match context dir (unknown), no dirReset.
|
| + expect(unkFmt.unicodeWrap(en, true, false), equals('$LRE$en$PDF'));
|
| + // Test overall dir (RTL) doesn't match context dir (unknown), dirReset.
|
| + expect(unkFmt.unicodeWrap(he, true, true), equals('$RLE$he$PDF'));
|
| + // Test overall dir (neutral) doesn't match context dir (LTR), dirReset.
|
| + expect(ltrFmt.unicodeWrap('', true, true), equals(''));
|
| +
|
| + // Test exit dir (but not overall dir) is opposite to context dir, dirReset.
|
| + expect(ltrFmt.unicodeWrap('$longEn$he$html', true, true),
|
| + equals('$longEn$he$html$LRM'));
|
| + // Test overall dir (but not exit dir) is opposite to context dir, dirReset.
|
| + expect(rtlFmt.unicodeWrap('$longEn$he', true, true),
|
| + equals('$LRE$longEn$he$PDF$RLM'));
|
| +
|
| + // Test overall dir matches context dir (LTR).
|
| + expect(ltrFmt.unicodeWrap(en, direction : TextDirection.LTR), equals(en));
|
| + // Test overall dir (but not exit dir) supposedly matches context dir (LTR).
|
| + expect(ltrFmt.unicodeWrap(he, direction : TextDirection.LTR),
|
| + equals('$he$LRM'));
|
| + // Test overall dir matches context dir (RTL).
|
| + expect(rtlFmt.unicodeWrap(he, direction : TextDirection.RTL), equals(he));
|
| + // Test overall dir (but not exit dir) supposedly matches context dir (RTL).
|
| + expect(rtlFmt.unicodeWrap(en, direction : TextDirection.RTL),
|
| + equals('$en$RLM'));
|
| +
|
| + // Test overall dir (RTL) doesn't match context dir (LTR).
|
| + expect(ltrFmt.unicodeWrap(he, direction : TextDirection.RTL),
|
| + equals('$RLE$he$PDF$LRM'));
|
| + // Test supposed overall dir (RTL) doesn't match context dir (LTR).
|
| + expect(ltrFmt.unicodeWrap(en, direction : TextDirection.RTL),
|
| + equals('$RLE$en$PDF$LRM'));
|
| + // Test overall dir (LTR) doesn't match context dir (RTL).
|
| + expect(rtlFmt.unicodeWrap(en, direction : TextDirection.LTR),
|
| + equals('$LRE$en$PDF$RLM'));
|
| + // Test supposed overall dir (LTR) doesn't match context dir (RTL).
|
| + expect(rtlFmt.unicodeWrap(he, direction : TextDirection.LTR),
|
| + equals('$LRE$he$PDF$RLM'));
|
| + // Test supposed overall dir (LTR) doesn't match context dir (unknown).
|
| + expect(unkFmt.unicodeWrap(he, direction : TextDirection.LTR),
|
| + equals('$LRE$he$PDF'));
|
| + // Test supposed overall dir (neutral) doesn't match context dir (LTR).
|
| + expect(ltrFmt.unicodeWrap(he, direction : TextDirection.UNKNOWN),
|
| + equals('$he$LRM'));
|
| + });
|
| +
|
| + test('markAfter', () {
|
| + // Test exit dir (RTL) is opposite to context dir (LTR.
|
| + expect(ltrFmt.markAfter('$longEn$he$html', true), equals(LRM));
|
| + // Test exit dir (LTR) is opposite to context dir (RTL.
|
| + expect(rtlFmt.markAfter('$longHe$en', true), equals(RLM));
|
| + // Test exit dir (LTR) doesn't match context dir (unknown.
|
| + expect(unkFmt.markAfter('$longEn$en', true), equals(''));
|
| + // Test overall dir (RTL) is opposite to context dir (LTR.
|
| + expect(ltrFmt.markAfter('$longHe$en', true), equals(LRM));
|
| + // Test overall dir (LTR) is opposite to context dir (RTL.
|
| + expect(rtlFmt.markAfter('longEn$he', true), equals(RLM));
|
| + // Test exit dir and overall dir match context dir (LTR.
|
| + expect(ltrFmt.markAfter('$longEn$he$html', false), equals(''));
|
| + // Test exit dir and overall dir matches context dir (RTL.
|
| + expect(rtlFmt.markAfter('$longHe$he', true), equals(''));
|
| + });
|
| +
|
| + test('mark', () {
|
| + // Implicitly also tests the constructor.
|
| + expect(new BidiFormatter.LTR().mark(), equals(BidiUtils.LRM));
|
| + expect(new BidiFormatter.UNKNOWN().mark(), equals(''));
|
| + expect(new BidiFormatter.RTL().mark(), equals(BidiUtils.RLM));
|
| + });
|
| +}
|
|
|