format_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.formatTest');
  15. goog.setTestOnly('goog.formatTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.format');
  19. goog.require('goog.string');
  20. goog.require('goog.testing.PropertyReplacer');
  21. goog.require('goog.testing.jsunit');
  22. var propertyReplacer = new goog.testing.PropertyReplacer();
  23. function tearDown() {
  24. // set wordBreakHtml back to the original value (some tests edit this member).
  25. propertyReplacer.reset();
  26. }
  27. function testFormatFileSize() {
  28. var fileSize = goog.format.fileSize;
  29. assertEquals('45', fileSize(45));
  30. assertEquals('45', fileSize(45, 0));
  31. assertEquals('45', fileSize(45, 1));
  32. assertEquals('45', fileSize(45, 3));
  33. assertEquals('454', fileSize(454));
  34. assertEquals('600', fileSize(600));
  35. assertEquals('1K', fileSize(1024));
  36. assertEquals('2K', fileSize(2 * 1024));
  37. assertEquals('5K', fileSize(5 * 1024));
  38. assertEquals('5.123K', fileSize(5.12345 * 1024, 3));
  39. assertEquals('5.68K', fileSize(5.678 * 1024, 2));
  40. assertEquals('1M', fileSize(1024 * 1024));
  41. assertEquals('1.5M', fileSize(1.5 * 1024 * 1024));
  42. assertEquals('2M', fileSize(1.5 * 1024 * 1024, 0));
  43. assertEquals('1.5M', fileSize(1.51 * 1024 * 1024, 1));
  44. assertEquals('1.56M', fileSize(1.56 * 1024 * 1024, 2));
  45. assertEquals('1G', fileSize(1024 * 1024 * 1024));
  46. assertEquals('6G', fileSize(6 * 1024 * 1024 * 1024));
  47. assertEquals('12.06T', fileSize(12345.6789 * 1024 * 1024 * 1024));
  48. }
  49. function testIsConvertableScaledNumber() {
  50. var isConvertableScaledNumber = goog.format.isConvertableScaledNumber;
  51. assertTrue(isConvertableScaledNumber('0'));
  52. assertTrue(isConvertableScaledNumber('45'));
  53. assertTrue(isConvertableScaledNumber('45K'));
  54. assertTrue(isConvertableScaledNumber('45MB'));
  55. assertTrue(isConvertableScaledNumber('45GB'));
  56. assertTrue(isConvertableScaledNumber('45T'));
  57. assertTrue(isConvertableScaledNumber('2.33P'));
  58. assertTrue(isConvertableScaledNumber('45m'));
  59. assertTrue(isConvertableScaledNumber('45u'));
  60. assertTrue(isConvertableScaledNumber('-5.0n'));
  61. assertFalse(isConvertableScaledNumber('45x'));
  62. assertFalse(isConvertableScaledNumber('ux'));
  63. assertFalse(isConvertableScaledNumber('K'));
  64. }
  65. function testNumericValueToString() {
  66. var numericValueToString = goog.format.numericValueToString;
  67. assertEquals('0', numericValueToString(0.0));
  68. assertEquals('45', numericValueToString(45));
  69. assertEquals('454', numericValueToString(454));
  70. assertEquals('600', numericValueToString(600));
  71. assertEquals('1.02K', numericValueToString(1024));
  72. assertEquals('2.05K', numericValueToString(2 * 1024));
  73. assertEquals('5.12K', numericValueToString(5 * 1024));
  74. assertEquals('5.246K', numericValueToString(5.12345 * 1024, 3));
  75. assertEquals('5.81K', numericValueToString(5.678 * 1024, 2));
  76. assertEquals('1.05M', numericValueToString(1024 * 1024));
  77. assertEquals('1.57M', numericValueToString(1.5 * 1024 * 1024));
  78. assertEquals('2M', numericValueToString(1.5 * 1024 * 1024, 0));
  79. assertEquals('1.6M', numericValueToString(1.51 * 1024 * 1024, 1));
  80. assertEquals('1.64M', numericValueToString(1.56 * 1024 * 1024, 2));
  81. assertEquals('1.07G', numericValueToString(1024 * 1024 * 1024));
  82. assertEquals('6.44G', numericValueToString(6 * 1024 * 1024 * 1024));
  83. assertEquals('13.26T', numericValueToString(12345.6789 * 1024 * 1024 * 1024));
  84. assertEquals('23.4m', numericValueToString(0.0234));
  85. assertEquals('1.23u', numericValueToString(0.00000123));
  86. assertEquals('15.78n', numericValueToString(0.000000015784));
  87. assertEquals('0.58u', numericValueToString(0.0000005784));
  88. assertEquals('0.5', numericValueToString(0.5));
  89. assertEquals('-45', numericValueToString(-45.3, 0));
  90. assertEquals('-45', numericValueToString(-45.5, 0));
  91. assertEquals('-46', numericValueToString(-45.51, 0));
  92. }
  93. function testFormatNumBytes() {
  94. var numBytesToString = goog.format.numBytesToString;
  95. assertEquals('45', numBytesToString(45));
  96. assertEquals('454', numBytesToString(454));
  97. assertEquals('5KB', numBytesToString(5 * 1024));
  98. assertEquals('1MB', numBytesToString(1024 * 1024));
  99. assertEquals('6GB', numBytesToString(6 * 1024 * 1024 * 1024));
  100. assertEquals('12.06TB', numBytesToString(12345.6789 * 1024 * 1024 * 1024));
  101. assertEquals('454', numBytesToString(454, 2, true, true));
  102. assertEquals('5 KB', numBytesToString(5 * 1024, 2, true, true));
  103. }
  104. function testStringToNumeric() {
  105. var stringToNumericValue = goog.format.stringToNumericValue;
  106. var epsilon = Math.pow(10, -10);
  107. assertNaN(stringToNumericValue('foo'));
  108. assertEquals(45, stringToNumericValue('45'));
  109. assertEquals(-45, stringToNumericValue('-45'));
  110. assertEquals(-45, stringToNumericValue('-45'));
  111. assertEquals(454, stringToNumericValue('454'));
  112. assertEquals(5 * 1024, stringToNumericValue('5KB'));
  113. assertEquals(1024 * 1024, stringToNumericValue('1MB'));
  114. assertEquals(6 * 1024 * 1024 * 1024, stringToNumericValue('6GB'));
  115. assertEquals(13260110230978.56, stringToNumericValue('12.06TB'));
  116. assertEquals(5010, stringToNumericValue('5.01K'));
  117. assertEquals(5100000, stringToNumericValue('5.1M'));
  118. assertTrue(Math.abs(0.051 - stringToNumericValue('51.0m')) < epsilon);
  119. assertTrue(Math.abs(0.000051 - stringToNumericValue('51.0u')) < epsilon);
  120. }
  121. function testStringToNumBytes() {
  122. var stringToNumBytes = goog.format.stringToNumBytes;
  123. assertEquals(45, stringToNumBytes('45'));
  124. assertEquals(454, stringToNumBytes('454'));
  125. assertEquals(5 * 1024, stringToNumBytes('5K'));
  126. assertEquals(1024 * 1024, stringToNumBytes('1M'));
  127. assertEquals(6 * 1024 * 1024 * 1024, stringToNumBytes('6G'));
  128. assertEquals(13260110230978.56, stringToNumBytes('12.06T'));
  129. }
  130. function testInsertWordBreaks() {
  131. // HTML that gets inserted is browser dependent, ensure for the test it is
  132. // a constant - browser dependent HTML is for display purposes only.
  133. propertyReplacer.set(goog.format, 'WORD_BREAK_HTML', '<wbr>');
  134. var insertWordBreaks = goog.format.insertWordBreaks;
  135. assertEquals('abcdef', insertWordBreaks('abcdef', 10));
  136. assertEquals('ab<wbr>cd<wbr>ef', insertWordBreaks('abcdef', 2));
  137. assertEquals(
  138. 'a<wbr>b<wbr>c<wbr>d<wbr>e<wbr>f', insertWordBreaks('abcdef', 1));
  139. assertEquals(
  140. 'a&amp;b=<wbr>=fal<wbr>se', insertWordBreaks('a&amp;b==false', 4));
  141. assertEquals(
  142. '&lt;&amp;&gt;&raquo;<wbr>&laquo;',
  143. insertWordBreaks('&lt;&amp;&gt;&raquo;&laquo;', 4));
  144. assertEquals('a<wbr>b<wbr>c d<wbr>e<wbr>f', insertWordBreaks('abc def', 1));
  145. assertEquals('ab<wbr>c de<wbr>f', insertWordBreaks('abc def', 2));
  146. assertEquals('abc def', insertWordBreaks('abc def', 3));
  147. assertEquals('abc def', insertWordBreaks('abc def', 4));
  148. assertEquals('a<b>cd</b>e<wbr>f', insertWordBreaks('a<b>cd</b>ef', 4));
  149. assertEquals(
  150. 'Thi<wbr>s is a <a href="">lin<wbr>k</a>.',
  151. insertWordBreaks('This is a <a href="">link</a>.', 3));
  152. assertEquals(
  153. '<abc a="&amp;&amp;&amp;&amp;&amp;">a<wbr>b',
  154. insertWordBreaks('<abc a="&amp;&amp;&amp;&amp;&amp;">ab', 1));
  155. assertEquals('ab\u0300<wbr>cd', insertWordBreaks('ab\u0300cd', 2));
  156. assertEquals('ab\u036F<wbr>cd', insertWordBreaks('ab\u036Fcd', 2));
  157. assertEquals('ab<wbr>\u0370c<wbr>d', insertWordBreaks('ab\u0370cd', 2));
  158. assertEquals('ab<wbr>\uFE1Fc<wbr>d', insertWordBreaks('ab\uFE1Fcd', 2));
  159. assertEquals(
  160. 'ab\u0300<wbr>c\u0301<wbr>de<wbr>f',
  161. insertWordBreaks('ab\u0300c\u0301def', 2));
  162. }
  163. function testInsertWordBreaksWithFormattingCharacters() {
  164. // HTML that gets inserted is browser dependent, ensure for the test it is
  165. // a constant - browser dependent HTML is for display purposes only.
  166. propertyReplacer.set(goog.format, 'WORD_BREAK_HTML', '<wbr>');
  167. var insertWordBreaks = goog.format.insertWordBreaks;
  168. // A date in Arabic-Indic digits with Right-to-Left Marks (U+200F).
  169. // The date is "11<RLM>/01<RLM>/2012".
  170. var textWithRLMs = 'This is a date - ' +
  171. '\u0661\u0661\u200f/\u0660\u0661\u200f/\u0662\u0660\u0661\u0662';
  172. // A string of 10 Xs with invisible formatting characters in between.
  173. // These characters are in the ranges U+200C to U+200F and U+202A to
  174. // U+202E, inclusive. See: http://unicode.org/charts/PDF/U2000.pdf
  175. var stringWithInvisibleFormatting = 'X\u200cX\u200dX\u200eX\u200fX\u202a' +
  176. 'X\u202bX\u202cX\u202dX\u202eX';
  177. // A string formed by concatenating copies of the previous string alternating
  178. // with characters which behave like breaking spaces. Besides the space
  179. // character itself, the other characters are in the range U+2000 to U+200B
  180. // inclusive, except for the exclusion of U+2007 and inclusion of U+2029.
  181. // See: http://unicode.org/charts/PDF/U2000.pdf
  182. var stringWithInvisibleFormattingAndSpacelikeCharacters =
  183. stringWithInvisibleFormatting + ' ' + stringWithInvisibleFormatting +
  184. '\u2000' + stringWithInvisibleFormatting + '\u2001' +
  185. stringWithInvisibleFormatting + '\u2002' + stringWithInvisibleFormatting +
  186. '\u2003' + stringWithInvisibleFormatting + '\u2005' +
  187. stringWithInvisibleFormatting + '\u2006' + stringWithInvisibleFormatting +
  188. '\u2008' + stringWithInvisibleFormatting + '\u2009' +
  189. stringWithInvisibleFormatting + '\u200A' + stringWithInvisibleFormatting +
  190. '\u200B' + stringWithInvisibleFormatting + '\u2029' +
  191. stringWithInvisibleFormatting;
  192. // Test that the word break algorithm does not count RLMs towards word
  193. // length, and therefore does not insert word breaks into a typical date
  194. // written in Arabic-Indic digits with RTMs (b/5853915).
  195. assertEquals(textWithRLMs, insertWordBreaks(textWithRLMs, 10));
  196. // Test that invisible formatting characters are not counted towards word
  197. // length, and that characters which are treated as breaking spaces behave as
  198. // breaking spaces.
  199. assertEquals(
  200. stringWithInvisibleFormattingAndSpacelikeCharacters,
  201. insertWordBreaks(
  202. stringWithInvisibleFormattingAndSpacelikeCharacters, 10));
  203. }
  204. function testInsertWordBreaksBasic() {
  205. // HTML that gets inserted is browser dependent, ensure for the test it is
  206. // a constant - browser dependent HTML is for display purposes only.
  207. propertyReplacer.set(goog.format, 'WORD_BREAK_HTML', '<wbr>');
  208. var insertWordBreaksBasic = goog.format.insertWordBreaksBasic;
  209. assertEquals('abcdef', insertWordBreaksBasic('abcdef', 10));
  210. assertEquals('ab<wbr>cd<wbr>ef', insertWordBreaksBasic('abcdef', 2));
  211. assertEquals(
  212. 'a<wbr>b<wbr>c<wbr>d<wbr>e<wbr>f', insertWordBreaksBasic('abcdef', 1));
  213. assertEquals(
  214. 'ab\u0300<wbr>c\u0301<wbr>de<wbr>f',
  215. insertWordBreaksBasic('ab\u0300c\u0301def', 2));
  216. assertEquals(
  217. 'Inserting word breaks into the word "Russia" should work fine.',
  218. '\u0420\u043E<wbr>\u0441\u0441<wbr>\u0438\u044F',
  219. insertWordBreaksBasic('\u0420\u043E\u0441\u0441\u0438\u044F', 2));
  220. // The word 'Internet' in Hindi.
  221. var hindiInternet = '\u0907\u0902\u091F\u0930\u0928\u0947\u091F';
  222. assertEquals(
  223. 'The basic algorithm is not good enough to insert word ' +
  224. 'breaks into Hindi.',
  225. hindiInternet, insertWordBreaksBasic(hindiInternet, 2));
  226. // The word 'Internet' in Hindi broken into slashes.
  227. assertEquals(
  228. 'Hindi can have word breaks inserted between slashes',
  229. hindiInternet + '<wbr>/' + hindiInternet + '<wbr>.' + hindiInternet,
  230. insertWordBreaksBasic(
  231. hindiInternet + '/' + hindiInternet + '.' + hindiInternet, 2));
  232. }
  233. function testWordBreaksWorking() {
  234. var text = goog.string.repeat('test', 20);
  235. var textWbr = goog.string.repeat('test' + goog.format.WORD_BREAK_HTML, 20);
  236. var overflowEl = goog.dom.createDom(
  237. goog.dom.TagName.DIV,
  238. {'style': 'width: 100px; overflow: hidden; margin 5px'});
  239. var wbrEl = goog.dom.createDom(
  240. goog.dom.TagName.DIV,
  241. {'style': 'width: 100px; overflow: hidden; margin-top: 15px'});
  242. goog.dom.appendChild(goog.global.document.body, overflowEl);
  243. goog.dom.appendChild(goog.global.document.body, wbrEl);
  244. overflowEl.innerHTML = text;
  245. wbrEl.innerHTML = textWbr;
  246. assertTrue('Text should overflow', overflowEl.scrollWidth > 100);
  247. assertTrue('Text should not overflow', wbrEl.scrollWidth <= 100);
  248. }
  249. function testWordBreaksRemovedFromTextContent() {
  250. var expectedText = goog.string.repeat('test', 20);
  251. var textWbr = goog.string.repeat('test' + goog.format.WORD_BREAK_HTML, 20);
  252. var wbrEl = goog.dom.createDom(goog.dom.TagName.DIV, null);
  253. wbrEl.innerHTML = textWbr;
  254. assertEquals(
  255. 'text content should have wbr character removed', expectedText,
  256. goog.dom.getTextContent(wbrEl));
  257. }