stringformat_test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2008 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.string.formatTest');
  15. goog.setTestOnly('goog.string.formatTest');
  16. goog.require('goog.string.format');
  17. goog.require('goog.testing.jsunit');
  18. // The discussion on naming this functionality is going on.
  19. var f = goog.string.format;
  20. function testImmediateFormatSpecifier() {
  21. assertEquals('Empty String', '', f(''));
  22. assertEquals('Immediate Value', 'Immediate Value', f('Immediate Value'));
  23. }
  24. function testPercentSign() {
  25. assertEquals('%', '%', f('%'));
  26. assertEquals('%%', '%', f('%%'));
  27. assertEquals('%%%', '%%', f('%%%'));
  28. assertEquals('%%%%', '%%', f('%%%%'));
  29. assertEquals('width of the percent sign ???', '%%', f('%345%%-67.987%'));
  30. }
  31. function testStringConversionSpecifier() {
  32. assertEquals('%s', 'abc', f('%s', 'abc'));
  33. assertEquals('%2s', 'abc', f('%2s', 'abc'));
  34. assertEquals('%6s', ' abc', f('%6s', 'abc'));
  35. assertEquals('%-6s', 'abc ', f('%-6s', 'abc'));
  36. }
  37. function testFloatConversionSpecifier() {
  38. assertEquals('%f', '123', f('%f', 123));
  39. assertEquals('%f', '0.1', f('%f', 0.1));
  40. assertEquals('%f', '123.456', f('%f', 123.456));
  41. // Precisions, paddings and other flags are handled on a flag to flag basis.
  42. }
  43. function testAliasedConversionSpecifiers() {
  44. assertEquals('%i vs. %d', f('%i', 123), f('%d', 123));
  45. assertEquals('%u vs. %d', f('%u', 123), f('%d', 123));
  46. }
  47. function testIntegerConversion() {
  48. assertEquals('%d', '0', f('%d', 0));
  49. assertEquals('%d', '123', f('%d', 123));
  50. assertEquals('%d', '0', f('%d', 0.1));
  51. assertEquals('%d', '0', f('%d', 0.9));
  52. assertEquals('%d', '123', f('%d', 123.456));
  53. assertEquals('%d', '-1', f('%d', -1));
  54. assertEquals('%d', '0', f('%d', -0.1));
  55. assertEquals('%d', '0', f('%d', -0.9));
  56. assertEquals('%d', '-123', f('%d', -123.456));
  57. // Precisions, paddings and other flags are handled on a flag to flag basis.
  58. }
  59. function testSpaceFlag() {
  60. assertEquals('zero %+d ', ' 0', f('% d', 0));
  61. assertEquals('positive % d ', ' 123', f('% d', 123));
  62. assertEquals('negative % d ', '-123', f('% d', -123));
  63. assertEquals('positive % 3d', ' 123', f('% 3d', 123));
  64. assertEquals('negative % 3d', '-123', f('% 3d', -123));
  65. assertEquals('positive % 4d', ' 123', f('% 4d', 123));
  66. assertEquals('negative % 4d', '-123', f('% 4d', -123));
  67. assertEquals('positive % 6d', ' 123', f('% 6d', 123));
  68. assertEquals('negative % 6d', '- 123', f('% 6d', -123));
  69. assertEquals('positive % f ', ' 123.456', f('% f', 123.456));
  70. assertEquals('negative % f ', '-123.456', f('% f', -123.456));
  71. assertEquals('positive % .2f ', ' 123.46', f('% .2f', 123.456));
  72. assertEquals('negative % .2f ', '-123.46', f('% .2f', -123.456));
  73. assertEquals('positive % 6.2f', ' 123.46', f('% 6.2f', 123.456));
  74. assertEquals('negative % 6.2f', '-123.46', f('% 6.2f', -123.456));
  75. assertEquals('positive % 7.2f', ' 123.46', f('% 7.2f', 123.456));
  76. assertEquals('negative % 7.2f', '-123.46', f('% 7.2f', -123.456));
  77. assertEquals('positive % 10.2f', ' 123.46', f('% 10.2f', 123.456));
  78. assertEquals('negative % 10.2f', '- 123.46', f('% 10.2f', -123.456));
  79. assertEquals('string % s ', 'abc', f('% s', 'abc'));
  80. assertEquals('string % 3s', 'abc', f('% 3s', 'abc'));
  81. assertEquals('string % 4s', ' abc', f('% 4s', 'abc'));
  82. assertEquals('string % 6s', ' abc', f('% 6s', 'abc'));
  83. }
  84. function testPlusFlag() {
  85. assertEquals('zero %+d ', '+0', f('%+d', 0));
  86. assertEquals('positive %+d ', '+123', f('%+d', 123));
  87. assertEquals('negative %+d ', '-123', f('%+d', -123));
  88. assertEquals('positive %+3d', '+123', f('%+3d', 123));
  89. assertEquals('negative %+3d', '-123', f('%+3d', -123));
  90. assertEquals('positive %+4d', '+123', f('%+4d', 123));
  91. assertEquals('negative %+4d', '-123', f('%+4d', -123));
  92. assertEquals('positive %+6d', '+ 123', f('%+6d', 123));
  93. assertEquals('negative %+6d', '- 123', f('%+6d', -123));
  94. assertEquals('positive %+f ', '+123.456', f('%+f', 123.456));
  95. assertEquals('negative %+f ', '-123.456', f('%+f', -123.456));
  96. assertEquals('positive %+.2f ', '+123.46', f('%+.2f', 123.456));
  97. assertEquals('negative %+.2f ', '-123.46', f('%+.2f', -123.456));
  98. assertEquals('positive %+6.2f', '+123.46', f('%+6.2f', 123.456));
  99. assertEquals('negative %+6.2f', '-123.46', f('%+6.2f', -123.456));
  100. assertEquals('positive %+7.2f', '+123.46', f('%+7.2f', 123.456));
  101. assertEquals('negative %+7.2f', '-123.46', f('%+7.2f', -123.456));
  102. assertEquals('positive %+10.2f', '+ 123.46', f('%+10.2f', 123.456));
  103. assertEquals('negative %+10.2f', '- 123.46', f('%+10.2f', -123.456));
  104. assertEquals('string %+s ', 'abc', f('%+s', 'abc'));
  105. assertEquals('string %+3s', 'abc', f('%+3s', 'abc'));
  106. assertEquals('string %+4s', ' abc', f('%+4s', 'abc'));
  107. assertEquals('string %+6s', ' abc', f('%+6s', 'abc'));
  108. }
  109. function testPrecision() {
  110. assertEquals('%.5d', '0', f('%.5d', 0));
  111. assertEquals('%d', '123', f('%d', 123.456));
  112. assertEquals('%.2d', '123', f('%.2d', 123.456));
  113. assertEquals('%f', '123.456', f('%f', 123.456));
  114. assertEquals('%.2f', '123.46', f('%.2f', 123.456));
  115. assertEquals('%.3f', '123.456', f('%.3f', 123.456));
  116. assertEquals('%.6f', '123.456000', f('%.6f', 123.456));
  117. assertEquals('%1.2f', '123.46', f('%1.2f', 123.456));
  118. assertEquals('%7.2f', ' 123.46', f('%7.2f', 123.456));
  119. assertEquals('%5.6f', '123.456000', f('%5.6f', 123.456));
  120. assertEquals('%11.6f', ' 123.456000', f('%11.6f', 123.456));
  121. assertEquals('%07.2f', '0123.46', f('%07.2f', 123.456));
  122. assertEquals('%+7.2f', '+123.46', f('%+7.2f', 123.456));
  123. }
  124. function testZeroFlag() {
  125. assertEquals('%0s', 'abc', f('%0s', 'abc'));
  126. assertEquals('%02s', 'abc', f('%02s', 'abc'));
  127. assertEquals('%06s', ' abc', f('%06s', 'abc'));
  128. assertEquals('%0d', '123', f('%0d', 123));
  129. assertEquals('%0d', '-123', f('%0d', -123));
  130. assertEquals('%06d', '000123', f('%06d', 123));
  131. assertEquals('%06d', '-00123', f('%06d', -123));
  132. assertEquals('%010d', '0000000123', f('%010d', 123));
  133. assertEquals('%010d', '-000000123', f('%010d', -123));
  134. }
  135. function testFlagMinus() {
  136. assertEquals('%-s', 'abc', f('%-s', 'abc'));
  137. assertEquals('%-2s', 'abc', f('%-2s', 'abc'));
  138. assertEquals('%-6s', 'abc ', f('%-6s', 'abc'));
  139. assertEquals('%-d', '123', f('%-d', 123));
  140. assertEquals('%-d', '-123', f('%-d', -123));
  141. assertEquals('%-2d', '123', f('%-2d', 123));
  142. assertEquals('%-2d', '-123', f('%-2d', -123));
  143. assertEquals('%-4d', '123 ', f('%-4d', 123));
  144. assertEquals('%-4d', '-123', f('%-4d', -123));
  145. assertEquals('%-d', '123', f('%-0d', 123));
  146. assertEquals('%-d', '-123', f('%-0d', -123));
  147. assertEquals('%-4d', '123 ', f('%-04d', 123));
  148. assertEquals('%-4d', '-123', f('%-04d', -123));
  149. }
  150. function testExceptions() {
  151. var e = assertThrows(goog.partial(f, '%f%f', 123.456));
  152. assertEquals('[goog.string.format] Not enough arguments', e.message);
  153. e = assertThrows(f);
  154. assertEquals('[goog.string.format] Template required', e.message);
  155. }
  156. function testNonParticipatingGroupHandling() {
  157. // Firefox supplies empty string instead of undefined for non-participating
  158. // capture groups. This can trigger bad behavior if a demuxer only checks
  159. // isNaN(val) and not also val == ''. Check for regressions.
  160. var format = '%s %d %i %u';
  161. var expected = '1 2 3 4';
  162. // Good types
  163. assertEquals(expected, goog.string.format(format, 1, '2', '3', '4'));
  164. // Bad types
  165. assertEquals(expected, goog.string.format(format, '1', 2, 3, 4));
  166. }
  167. function testMinusString() {
  168. var format = '%0.1f%%';
  169. var expected = '-0.7%';
  170. assertEquals(expected, goog.string.format(format, '-0.723'));
  171. }