UNPKG

sku-pg

Version:

Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!

344 lines (313 loc) 15.6 kB
<!DOCTYPE html> <html> <!-- Copyright 2009 The Closure Library Authors. All Rights Reserved. Use of this source code is governed by the Apache License, Version 2.0. See the COPYING file for details. --> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Closure Unit Tests - goog.i18n.BidiFormatter</title> <script src="../base.js"></script> <script> goog.require('goog.i18n.BidiFormatter'); goog.require('goog.testing.jsunit'); </script> </head> <body> <script> var LRM = goog.i18n.bidi.Format.LRM; var RLM = goog.i18n.bidi.Format.RLM; var LRE = goog.i18n.bidi.Format.LRE; var RLE = goog.i18n.bidi.Format.RLE; var PDF = goog.i18n.bidi.Format.PDF; var LTR = goog.i18n.bidi.Dir.LTR; var RTL = goog.i18n.bidi.Dir.RTL; var UNKNOWN = goog.i18n.bidi.Dir.UNKNOWN; var he = '\u05e0\u05e1'; var en = 'abba'; var html = '&lt;'; var longEn = 'abba sabba gabba '; var longHe = '\u05e0 \u05e1 \u05e0 '; var ltrFmt = new goog.i18n.BidiFormatter(LTR, false); // LTR context var rtlFmt = new goog.i18n.BidiFormatter(RTL, false); // RTL context var unkFmt = new goog.i18n.BidiFormatter(UNKNOWN, false); // unknown context function testEstimateDirection() { assertEquals(UNKNOWN, ltrFmt.estimateDirection('')); assertEquals(UNKNOWN, rtlFmt.estimateDirection('')); assertEquals(UNKNOWN, unkFmt.estimateDirection('')); assertEquals(LTR, ltrFmt.estimateDirection(en)); assertEquals(LTR, rtlFmt.estimateDirection(en)); assertEquals(LTR, unkFmt.estimateDirection(en)); assertEquals(RTL, ltrFmt.estimateDirection(he)); assertEquals(RTL, rtlFmt.estimateDirection(he)); assertEquals(RTL, unkFmt.estimateDirection(he)); // Text contains HTML or HTML-escaping. assertEquals(LTR, ltrFmt.estimateDirection( '<some sort of tag/>' + he + ' &amp;', false)); assertEquals(RTL, ltrFmt.estimateDirection( '<some sort of tag/>' + he + ' &amp;', true)); } function testDirAttrValue() { assertEquals('overall dir is RTL, context dir is LTR', 'rtl', ltrFmt.dirAttrValue(he, true)); assertEquals('overall dir and context dir are RTL', 'rtl', rtlFmt.dirAttrValue(he, true)); assertEquals('overall dir is LTR, context dir is RTL', 'ltr', rtlFmt.dirAttrValue(en, true)); assertEquals('overall dir and context dir are LTR', 'ltr', ltrFmt.dirAttrValue(en, true)); // Input's directionality is unknown. assertEquals('ltr', ltrFmt.dirAttrValue('', true)); assertEquals('rtl', rtlFmt.dirAttrValue('', true)); assertEquals('ltr', unkFmt.dirAttrValue('', true)); // Text contains HTML or HTML-escaping: assertEquals('rtl', ltrFmt.dirAttrValue(he + "<some sort of an HTML tag>", true)); assertEquals('ltr', ltrFmt.dirAttrValue(he + "<some sort of an HTML tag>", false)); } function testKnownDirAttrValue() { assertEquals('rtl', ltrFmt.knownDirAttrValue(RTL)); assertEquals('rtl', rtlFmt.knownDirAttrValue(RTL)); assertEquals('ltr', rtlFmt.knownDirAttrValue(LTR)); assertEquals('ltr', ltrFmt.knownDirAttrValue(LTR)); // Input directionality is unknown. assertEquals('ltr', ltrFmt.knownDirAttrValue(UNKNOWN)); assertEquals('rtl', rtlFmt.knownDirAttrValue(UNKNOWN)); assertEquals('ltr', unkFmt.knownDirAttrValue(UNKNOWN)); } function testDirAttr() { assertEquals('overall dir (RTL) doesnt match context dir (LTR)', 'dir="rtl"', ltrFmt.dirAttr(he, true)); assertEquals('overall dir matches context dir (RTL)', '', rtlFmt.dirAttr(he, true)); assertEquals('overall dir (LTR) doesnt match context dir (RTL)', 'dir="ltr"', rtlFmt.dirAttr(en, true)); assertEquals('overall dir matches context dir (LTR)', '', ltrFmt.dirAttr(en, true)); // Text contains HTML or HTML-escaping: assertEquals('dir="rtl"', ltrFmt.dirAttr(he + "<some sort of an HTML tag>", true)); assertEquals('', ltrFmt.dirAttr(he + "<some sort of an HTML tag>", false)); } function testKnownDirAttr() { assertEquals('overall dir (RTL) doesnt match context dir (LTR)', 'dir="rtl"', ltrFmt.knownDirAttr(goog.i18n.bidi.Dir.RTL)); assertEquals('overall dir matches context dir (RTL)', '', rtlFmt.knownDirAttr(goog.i18n.bidi.Dir.RTL)); assertEquals('overall dir (LTR) doesnt match context dir (RTL)', 'dir="ltr"', rtlFmt.knownDirAttr(goog.i18n.bidi.Dir.LTR)); assertEquals('overall dir matches context dir (LTR)', '', ltrFmt.knownDirAttr(goog.i18n.bidi.Dir.LTR)); } function testSpanWrap() { // alwaysSpan is false and opt_isHtml is true, unless specified otherwise. assertEquals('overall dir matches context dir (LTR), no dirReset', en, ltrFmt.spanWrap(en, true, false)); assertEquals('overall dir matches context dir (LTR), dirReset', en, ltrFmt.spanWrap(en, true, true)); assertEquals('overall dir matches context dir (RTL), no dirReset', he, rtlFmt.spanWrap(he, true, false)); assertEquals('overall dir matches context dir (RTL), dirReset', he, rtlFmt.spanWrap(he, true, true)); assertEquals('overall dir (RTL) doesnt match context dir (LTR), ' + 'no dirReset', '<span dir="rtl">' + he + '<\/span>', ltrFmt.spanWrap(he, true, false)); assertEquals('overall dir (RTL) doesnt match context dir (LTR), dirReset', '<span dir="rtl">' + he + '<\/span>' + LRM, ltrFmt.spanWrap(he, true, true)); assertEquals('overall dir (LTR) doesnt match context dir (RTL), ' + 'no dirReset', '<span dir="ltr">' + en + '<\/span>', rtlFmt.spanWrap(en, true, false)); assertEquals('overall dir (LTR) doesnt match context dir (RTL), dirReset', '<span dir="ltr">' + en + '<\/span>' + RLM, rtlFmt.spanWrap(en, true, true)); assertEquals('overall dir (LTR) doesnt match context dir (unknown), ' + 'no dirReset', '<span dir="ltr">' + en + '<\/span>', unkFmt.spanWrap(en, true, false)); assertEquals('overall dir (RTL) doesnt match context dir (unknown), ' + 'dirReset', '<span dir="rtl">' + he + '<\/span>', unkFmt.spanWrap(he, true, true)); assertEquals('overall dir (neutral) doesnt match context dir (LTR), ' + 'dirReset', '', ltrFmt.spanWrap('', true, true)); assertEquals('exit dir (but not overall dir) is opposite to context dir, ' + 'dirReset', longEn + he + html + LRM, ltrFmt.spanWrap(longEn + he + html, true, true)); assertEquals('overall dir (but not exit dir) is opposite to context dir, ' + 'dirReset', '<span dir="ltr">' + longEn + he + '<\/span>' + RLM, rtlFmt.spanWrap(longEn + he, true, true)); assertEquals('input is plain text (not escaped)', '&lt;br&gt;' + en, ltrFmt.spanWrap('<br>' + en, false, false)); var ltrAlwaysSpanFmt = new goog.i18n.BidiFormatter(LTR, true); var rtlAlwaysSpanFmt = new goog.i18n.BidiFormatter(RTL, true); var unkAlwaysSpanFmt = new goog.i18n.BidiFormatter(UNKNOWN, true); assertEquals('alwaysSpan, overall dir matches context dir (LTR), ' + 'no dirReset', '<span>' + en + '<\/span>', ltrAlwaysSpanFmt.spanWrap(en, true, false)); assertEquals('alwaysSpan, overall dir matches context dir (LTR), dirReset', '<span>' + en + '<\/span>', ltrAlwaysSpanFmt.spanWrap(en, true, true)); assertEquals('alwaysSpan, overall dir matches context dir (RTL), ' + 'no dirReset', '<span>' + he + '<\/span>', rtlAlwaysSpanFmt.spanWrap(he, true, false)); assertEquals('alwaysSpan, overall dir matches context dir (RTL), dirReset', '<span>' + he + '<\/span>', rtlAlwaysSpanFmt.spanWrap(he, true, true)); assertEquals('alwaysSpan, overall dir (RTL) doesnt match ' + 'context dir (LTR), no dirReset', '<span dir="rtl">' + he + '<\/span>', ltrAlwaysSpanFmt.spanWrap(he, true, false)); assertEquals('alwaysSpan, overall dir (RTL) doesnt match ' + 'context dir (LTR), dirReset', '<span dir="rtl">' + he + '<\/span>' + LRM, ltrAlwaysSpanFmt.spanWrap(he, true, true)); assertEquals('alwaysSpan, overall dir (neutral) doesnt match ' + 'context dir (LTR), dirReset', '<span></span>', ltrAlwaysSpanFmt.spanWrap('', true, true)); } function testSpanWrapWithKnownDir() { assertEquals('overall dir matches context dir (LTR)', en, ltrFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, en)); assertEquals( 'overall dir (but not exit dir) supposedly matches context dir (LTR)', he + LRM, ltrFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he)); assertEquals('overall dir matches context dir (RTL)', he, rtlFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, he)); assertEquals( 'overall dir (but not exit dir) supposedly matches context dir (RTL)', en + RLM, rtlFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, en)); assertEquals('overall dir (RTL) doesnt match context dir (LTR)', '<span dir="rtl">' + he + '<\/span>' + LRM, ltrFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, he)); assertEquals('supposed overall dir (RTL) doesnt match context dir (LTR)', '<span dir="rtl">' + en + '<\/span>' + LRM, ltrFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, en)); assertEquals('overall dir (LTR) doesnt match context dir (RTL)', '<span dir="ltr">' + en + '<\/span>' + RLM, rtlFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, en)); assertEquals('supposed overall dir (LTR) doesnt match context dir (RTL)', '<span dir="ltr">' + he + '<\/span>' + RLM, rtlFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he)); assertEquals( 'supposed overall dir (LTR) doesnt match context dir (unknown)', '<span dir="ltr">' + he + '<\/span>', unkFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he)); assertEquals( 'supposed overall dir (neutral) doesnt match context dir (LTR)', he + LRM, ltrFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.UNKNOWN, he)); } function testUnicodeWrap() { // opt_isHtml is true, unless specified otherwise. assertEquals('overall dir matches context dir (LTR), no dirReset', en, ltrFmt.unicodeWrap(en, true, false)); assertEquals('overall dir matches context dir (LTR), dirReset', en, ltrFmt.unicodeWrap(en, true, true)); assertEquals('overall dir matches context dir (RTL), no dirReset', he, rtlFmt.unicodeWrap(he, true, false)); assertEquals('overall dir matches context dir (RTL), dirReset', he, rtlFmt.unicodeWrap(he, true, true)); assertEquals('overall dir (RTL) doesnt match context dir (LTR), ' + 'no dirReset', RLE + he + PDF, ltrFmt.unicodeWrap(he, true, false)); assertEquals('overall dir (RTL) doesnt match context dir (LTR), dirReset', RLE + he + PDF + LRM, ltrFmt.unicodeWrap(he, true, true)); assertEquals('overall dir (LTR) doesnt match context dir (RTL), ' + 'no dirReset', LRE + en + PDF, rtlFmt.unicodeWrap(en, true, false)); assertEquals('overall dir (LTR) doesnt match context dir (RTL), dirReset', LRE + en + PDF + RLM, rtlFmt.unicodeWrap(en, true, true)); assertEquals('overall dir (LTR) doesnt match context dir (unknown), ' + 'no dirReset', LRE + en + PDF, unkFmt.unicodeWrap(en, true, false)); assertEquals('overall dir (RTL) doesnt match context dir (unknown), ' + 'dirReset', RLE + he + PDF, unkFmt.unicodeWrap(he, true, true)); assertEquals('overall dir (neutral) doesnt match context dir (LTR), ' + 'dirReset', '', ltrFmt.unicodeWrap('', true, true)); assertEquals('exit dir (but not overall dir) is opposite to context dir, ' + 'dirReset', longEn + he + html + LRM, ltrFmt.unicodeWrap(longEn + he + html, true, true)); assertEquals('overall dir (but not exit dir) is opposite to context dir, ' + 'dirReset', LRE + longEn + he + PDF + RLM, rtlFmt.unicodeWrap(longEn + he, true, true)); } function testUnicodeWrapWithKnownDir() { assertEquals('overall dir matches context dir (LTR)', en, ltrFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, en)); assertEquals( 'overall dir (but not exit dir) supposedly matches context dir (LTR)', he + LRM, ltrFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he)); assertEquals('overall dir matches context dir (RTL)', he, rtlFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, he)); assertEquals( 'overall dir (but not exit dir) supposedly matches context dir (RTL)', en + RLM, rtlFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, en)); assertEquals('overall dir (RTL) doesnt match context dir (LTR)', RLE + he + PDF + LRM, ltrFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, he)); assertEquals('supposed overall dir (RTL) doesnt match context dir (LTR)', RLE + en + PDF + LRM, ltrFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, en)); assertEquals('overall dir (LTR) doesnt match context dir (RTL)', LRE + en + PDF + RLM, rtlFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, en)); assertEquals('supposed overall dir (LTR) doesnt match context dir (RTL)', LRE + he + PDF + RLM, rtlFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he)); assertEquals( 'supposed overall dir (LTR) doesnt match context dir (unknown)', LRE + he + PDF, unkFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he)); assertEquals( 'supposed overall dir (neutral) doesnt match context dir (LTR)', he + LRM, ltrFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.UNKNOWN, he)); } function testMarkAfter() { assertEquals('exit dir (RTL) is opposite to context dir (LTR)', LRM, ltrFmt.markAfter(longEn + he + html, true)); assertEquals('exit dir (LTR) is opposite to context dir (RTL)', RLM, rtlFmt.markAfter(longHe + en, true)); assertEquals('exit dir (LTR) doesnt match context dir (unknown)', '', unkFmt.markAfter(longEn + en, true)); assertEquals('overall dir (RTL) is opposite to context dir (LTR)', LRM, ltrFmt.markAfter(longHe + en, true)); assertEquals('overall dir (LTR) is opposite to context dir (RTL)', RLM, rtlFmt.markAfter(longEn + he, true)); assertEquals('exit dir and overall dir match context dir (LTR)', '', ltrFmt.markAfter(longEn + he + html, false)); assertEquals('exit dir and overall dir matches context dir (RTL)', '', rtlFmt.markAfter(longHe + he, true)); } function testMark() { // Implicitly, also tests the constructor. assertEquals(LRM, (new goog.i18n.BidiFormatter(LTR)).mark()); assertEquals('', (new goog.i18n.BidiFormatter(UNKNOWN)).mark()); assertEquals(RLM, (new goog.i18n.BidiFormatter(RTL)).mark()); assertEquals(RLM, (new goog.i18n.BidiFormatter(true)).mark()); assertEquals(LRM, (new goog.i18n.BidiFormatter(false)).mark()); } function testStartEdge() { assertEquals('left', ltrFmt.startEdge()); assertEquals('left', unkFmt.startEdge()); assertEquals('right', rtlFmt.startEdge()); } function testEndEdge() { assertEquals('right', ltrFmt.endEdge()); assertEquals('right', unkFmt.endEdge()); assertEquals('left', rtlFmt.endEdge()); } </script> </body> </html>