sprintf.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. if (!dojo._hasResource["dojox.string.tests.sprintf"]) { // _hasResource checks
  2. // added by build. Do
  3. // not use _hasResource
  4. // directly in your
  5. // code.
  6. dojo._hasResource["dojox.string.tests.sprintf"] = true;
  7. dojo.provide("dojox.string.tests.sprintf");
  8. dojo.require("dojox.string.sprintf");
  9. dojo.require("dojo.string");
  10. // Mapping using the %(var) format
  11. // Flags:
  12. // (space): Preceeds a positive number with a blank space
  13. // +: Preceeds a positive number with a + sign
  14. // 0: Pads numbers using zeroes
  15. // -: Left justify a number (they're right justified by default)
  16. // #: Alternate view for the specifier
  17. tests.register("dojox.string.tests.sprintf", [{
  18. name : "Flag: (space)",
  19. runTest : function(t) {
  20. var sprintf = dojox.string.sprintf;
  21. t.is(" 42", sprintf("% d", 42));
  22. t.is("-42", sprintf("% d", -42));
  23. t.is(" 42", sprintf("% 5d", 42));
  24. t.is(" -42", sprintf("% 5d", -42));
  25. t.is(" 42", sprintf("% 15d", 42));
  26. t.is(" -42", sprintf("% 15d", -42));
  27. }
  28. }, {
  29. name : "Flag: +",
  30. runTest : function(t) {
  31. var sprintf = dojox.string.sprintf;
  32. t.is("+42", sprintf("%+d", 42));
  33. t.is("-42", sprintf("%+d", -42));
  34. t.is(" +42", sprintf("%+5d", 42));
  35. t.is(" -42", sprintf("%+5d", -42));
  36. t.is(" +42", sprintf("%+15d", 42));
  37. t.is(" -42", sprintf("%+15d", -42));
  38. }
  39. }, {
  40. name : "Flag: 0",
  41. runTest : function(t) {
  42. var sprintf = dojox.string.sprintf;
  43. t.is("42", sprintf("%0d", 42));
  44. t.is("-42", sprintf("%0d", -42));
  45. t.is("00042", sprintf("%05d", 42));
  46. t.is("00-42", sprintf("%05d", -42));
  47. t.is("000000000000042", sprintf("%015d", 42));
  48. t.is("000000000000-42", sprintf("%015d", -42));
  49. }
  50. }, {
  51. name : "Flag: -",
  52. runTest : function(t) {
  53. var sprintf = dojox.string.sprintf;
  54. t.is("42", sprintf("%-d", 42));
  55. t.is("-42", sprintf("%-d", -42));
  56. t.is("42 ", sprintf("%-5d", 42));
  57. t.is("-42 ", sprintf("%-5d", -42));
  58. t.is("42 ", sprintf("%-15d", 42));
  59. t.is("-42 ", sprintf("%-15d", -42));
  60. t.is("42", sprintf("%-0d", 42));
  61. t.is("-42", sprintf("%-0d", -42));
  62. t.is("42 ", sprintf("%-05d", 42));
  63. t.is("-42 ", sprintf("%-05d", -42));
  64. t.is("42 ", sprintf("%-015d", 42));
  65. t.is("-42 ", sprintf("%-015d", -42));
  66. t.is("42", sprintf("%0-d", 42));
  67. t.is("-42", sprintf("%0-d", -42));
  68. t.is("42 ", sprintf("%0-5d", 42));
  69. t.is("-42 ", sprintf("%0-5d", -42));
  70. t.is("42 ", sprintf("%0-15d", 42));
  71. t.is("-42 ", sprintf("%0-15d", -42));
  72. }
  73. }, {
  74. name : "Precision",
  75. runTest : function(t) {
  76. var sprintf = dojox.string.sprintf;
  77. t.is("42", sprintf("%d", 42.8952));
  78. t.is("42", sprintf("%.2d", 42.8952)); // Note: the %d
  79. // format is an int
  80. t.is("42", sprintf("%.2i", 42.8952));
  81. t.is("42.90", sprintf("%.2f", 42.8952));
  82. t.is("42.90", sprintf("%.2F", 42.8952));
  83. t.is("42.8952000000", sprintf("%.10f", 42.8952));
  84. t.is("42.90", sprintf("%1.2f", 42.8952));
  85. t.is(" 42.90", sprintf("%6.2f", 42.8952));
  86. t.is("042.90", sprintf("%06.2f", 42.8952));
  87. t.is("+42.90", sprintf("%+6.2f", 42.8952));
  88. t.is("42.8952000000", sprintf("%5.10f", 42.8952));
  89. }
  90. }, {
  91. name : "Bases",
  92. runTest : function(t) {
  93. var sprintf = dojox.string.sprintf;
  94. t.is("\x7f", sprintf("%c", 0x7f));
  95. var error = false;
  96. try {
  97. sprintf("%c", -100);
  98. } catch (e) {
  99. t.is("invalid character code passed to %c in sprintf",
  100. e.message);
  101. error = true;
  102. }
  103. t.t(error);
  104. error = false;
  105. try {
  106. sprintf("%c", 0x200000);
  107. } catch (e) {
  108. t.is("invalid character code passed to %c in sprintf",
  109. e.message);
  110. error = true;
  111. }
  112. t.t(error);
  113. }
  114. }, {
  115. name : "Mapping",
  116. runTest : function(t) {
  117. var sprintf = dojox.string.sprintf;
  118. // %1$s format
  119. t.is("%1$", sprintf("%1$"));
  120. t.is("%0$s", sprintf("%0$s"));
  121. t.is("Hot Pocket", sprintf("%1$s %2$s", "Hot", "Pocket"));
  122. t.is("12.0 Hot Pockets", sprintf("%1$.1f %2$s %3$ss", 12,
  123. "Hot", "Pocket"));
  124. t.is(" 42", sprintf("%1$*.f", "42", 3));
  125. error = false;
  126. try {
  127. sprintf("%2$*s", "Hot Pocket");
  128. } catch (e) {
  129. t
  130. .is(
  131. "got 1 printf arguments, insufficient for '%2$*s'",
  132. e.message);
  133. error = true;
  134. }
  135. t.t(error);
  136. // %(map)s format
  137. t.is("%(foo", sprintf("%(foo", {}));
  138. t.is("Hot Pocket", sprintf("%(temperature)s %(crevace)s", {
  139. temperature : "Hot",
  140. crevace : "Pocket"
  141. }));
  142. t
  143. .is(
  144. "12.0 Hot Pockets",
  145. sprintf(
  146. "%(quantity).1f %(temperature)s %(crevace)ss",
  147. {
  148. quantity : 12,
  149. temperature : "Hot",
  150. crevace : "Pocket"
  151. }));
  152. var error = false;
  153. try {
  154. sprintf("%(foo)s", 42);
  155. } catch (e) {
  156. t.is("format requires a mapping", e.message);
  157. error = true;
  158. }
  159. t.t(error);
  160. error = false;
  161. try {
  162. sprintf("%(foo)s %(bar)s", "foo", 42);
  163. } catch (e) {
  164. t.is("format requires a mapping", e.message);
  165. error = true;
  166. }
  167. t.t(error);
  168. error = false;
  169. try {
  170. sprintf("%(foo)*s", {
  171. foo : "Hot Pocket"
  172. });
  173. } catch (e) {
  174. t.is("* width not supported in mapped formats",
  175. e.message);
  176. error = true;
  177. }
  178. t.t(error);
  179. }
  180. }, {
  181. name : "Positionals",
  182. runTest : function(t) {
  183. var sprintf = dojox.string.sprintf;
  184. t.is(" foo", sprintf("%*s", "foo", 4));
  185. t.is(" 3.14", sprintf("%*.*f", 3.14159265, 10, 2));
  186. t.is("0000003.14", sprintf("%0*.*f", 3.14159265, 10, 2));
  187. t.is("3.14 ", sprintf("%-*.*f", 3.14159265, 10, 2));
  188. var error = false;
  189. try {
  190. sprintf("%*s", "foo", "bar");
  191. } catch (e) {
  192. t
  193. .is(
  194. "the argument for * width at position 2 is not a number in %*s",
  195. e.message);
  196. error = true;
  197. }
  198. t.t(error);
  199. error = false;
  200. try {
  201. sprintf("%10.*f", "foo", 42);
  202. } catch (e) {
  203. t
  204. .is(
  205. "format argument 'foo' not a float; parseFloat returned NaN",
  206. e.message);
  207. error = true;
  208. }
  209. t.t(error);
  210. }
  211. }, {
  212. name : "vs. Formatter",
  213. runTest : function(t) {
  214. var sprintf = dojox.string.sprintf;
  215. for (var i = 0; i < 1000; i++) {
  216. sprintf("%d %s Pockets", i, "Hot");
  217. }
  218. }
  219. }, {
  220. name : "Formatter",
  221. runTest : function(t) {
  222. var Formatter = dojox.string.sprintf.Formatter;
  223. var str = new Formatter("%d %s Pockets");
  224. for (var i = 0; i < 1000; i++) {
  225. str.format(i, "Hot");
  226. }
  227. }
  228. }, {
  229. name : "Miscellaneous",
  230. runTest : function(t) {
  231. var sprintf = dojox.string.sprintf;
  232. t.is("+hello+", sprintf("+%s+", "hello"));
  233. t.is("+10+", sprintf("+%d+", 10));
  234. t.is("a", sprintf("%c", "a"));
  235. t.is('"', sprintf("%c", 34));
  236. t.is('$', sprintf("%c", 36));
  237. t.is("10", sprintf("%d", 10));
  238. var error = false;
  239. try {
  240. sprintf("%s%s", 42);
  241. } catch (e) {
  242. t.is("got 1 printf arguments, insufficient for '%s%s'",
  243. e.message);
  244. error = true;
  245. }
  246. t.t(error);
  247. error = false;
  248. try {
  249. sprintf("%c");
  250. } catch (e) {
  251. t.is("got 0 printf arguments, insufficient for '%c'",
  252. e.message);
  253. error = true;
  254. }
  255. t.t(error);
  256. t.is("%10", sprintf("%10", 42));
  257. }
  258. }]);
  259. }