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