1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Kernel#sprintf" do
it "is a private method" do
Kernel.should have_private_instance_method(:sprintf)
end
it "treats nil arguments as zero-width strings in %s slots" do
sprintf("%s%d%s%s", nil, 4, 'a', 'b').should == '4ab'
end
it "passes some tests for positive %x" do
sprintf("%x", 123).should == "7b"
sprintf("%0x", 123).should == "7b"
sprintf("% x", 123).should == " 7b"
sprintf("%+x", 123).should == "+7b"
sprintf("%+0x", 123).should == "+7b"
sprintf("%+ x", 123).should == "+7b"
sprintf("% 0x", 123).should == " 7b"
sprintf("%#x", 123).should == "0x7b"
sprintf("%#0x", 123).should == "0x7b"
sprintf("%# x", 123).should == " 0x7b"
sprintf("%#+x", 123).should == "+0x7b"
sprintf("%#+0x", 123).should == "+0x7b"
sprintf("%#+ x", 123).should == "+0x7b"
sprintf("%# 0x", 123).should == " 0x7b"
sprintf("%8x", 123).should == " 7b"
sprintf("%08x", 123).should == "0000007b"
sprintf("% 8x", 123).should == " 7b"
sprintf("%+8x", 123).should == " +7b"
sprintf("%+08x", 123).should == "+000007b"
sprintf("%+ 8x", 123).should == " +7b"
sprintf("% 08x", 123).should == " 000007b"
sprintf("%#8x", 123).should == " 0x7b"
sprintf("%#08x", 123).should == "0x00007b"
sprintf("%# 8x", 123).should == " 0x7b"
sprintf("%#+8x", 123).should == " +0x7b"
sprintf("%#+08x", 123).should == "+0x0007b"
sprintf("%#+ 8x", 123).should == " +0x7b"
sprintf("%# 08x", 123).should == " 0x0007b"
sprintf("%8.10x", 123).should == "000000007b"
sprintf("%08.10x", 123).should == "000000007b"
sprintf("% 8.10x", 123).should == " 000000007b"
sprintf("%+8.10x", 123).should == "+000000007b"
sprintf("%+08.10x", 123).should == "+000000007b"
sprintf("%+ 8.10x", 123).should == "+000000007b"
sprintf("% 08.10x", 123).should == " 000000007b"
sprintf("%10.8x", 123).should == " 0000007b"
sprintf("%010.8x", 123).should == " 0000007b"
sprintf("% 10.8x", 123).should == " 0000007b"
sprintf("%+10.8x", 123).should == " +0000007b"
sprintf("%+010.8x", 123).should == " +0000007b"
sprintf("%+ 10.8x", 123).should == " +0000007b"
sprintf("% 010.8x", 123).should == " 0000007b"
end
describe "with format string that contains %{} sections" do
it "substitutes values for named references" do
sprintf("%{foo}f", {foo: 1}).should == "1f"
end
it "raises KeyError when no matching key is in second argument" do
lambda { sprintf("%{foo}f", {}) }.should raise_error(KeyError)
end
end
describe "with format string that contains %<> sections" do
it "formats values for named references" do
sprintf("%<foo>f", {foo: 1}).should == "1.000000"
end
it "raises KeyError when no matching key is in second argument" do
lambda { sprintf("%<foo>f", {}) }.should raise_error(KeyError)
end
it "raises ArgumentError if missing second named argument" do
lambda { sprintf("%<key><foo>d", {key: 1}) }.should raise_error(ArgumentError)
end
end
describe "with negative values" do
describe "with format %x" do
it "precedes the number with '..'" do
sprintf("%0x", -123).should == "..f85"
sprintf("%#0x", -123).should == "0x..f85"
sprintf("%08x", -123).should == "..ffff85"
sprintf("%#08x", -123).should == "0x..ff85"
sprintf("%8.10x", -123).should == "..ffffff85"
sprintf("%08.10x", -123).should == "..ffffff85"
sprintf("%10.8x", -123).should == " ..ffff85"
sprintf("%010.8x", -123).should == " ..ffff85"
end
end
describe "with format %b or %B" do
it "precedes the number with '..'" do
sprintf("%.7b", -5).should == "..11011"
sprintf("%.7B", -5).should == "..11011"
sprintf("%0b", -5).should == "..1011"
end
end
end
it "passes some tests for negative %x" do
sprintf("%x", -123).should == "..f85"
sprintf("% x", -123).should == "-7b"
sprintf("%+x", -123).should == "-7b"
sprintf("%+0x", -123).should == "-7b"
sprintf("%+ x", -123).should == "-7b"
sprintf("% 0x", -123).should == "-7b"
sprintf("%#x", -123).should == "0x..f85"
sprintf("%# x", -123).should == "-0x7b"
sprintf("%#+x", -123).should == "-0x7b"
sprintf("%#+0x", -123).should == "-0x7b"
sprintf("%#+ x", -123).should == "-0x7b"
sprintf("%# 0x", -123).should == "-0x7b"
sprintf("%8x", -123).should == " ..f85"
sprintf("% 8x", -123).should == " -7b"
sprintf("%+8x", -123).should == " -7b"
sprintf("%+08x", -123).should == "-000007b"
sprintf("%+ 8x", -123).should == " -7b"
sprintf("% 08x", -123).should == "-000007b"
sprintf("%#8x", -123).should == " 0x..f85"
sprintf("%# 8x", -123).should == " -0x7b"
sprintf("%#+8x", -123).should == " -0x7b"
sprintf("%#+08x", -123).should == "-0x0007b"
sprintf("%#+ 8x", -123).should == " -0x7b"
sprintf("%# 08x", -123).should == "-0x0007b"
sprintf("% 8.10x", -123).should == "-000000007b"
sprintf("%+8.10x", -123).should == "-000000007b"
sprintf("%+08.10x", -123).should == "-000000007b"
sprintf("%+ 8.10x", -123).should == "-000000007b"
sprintf("% 08.10x", -123).should == "-000000007b"
sprintf("% 10.8x", -123).should == " -0000007b"
sprintf("%+10.8x", -123).should == " -0000007b"
sprintf("%+010.8x", -123).should == " -0000007b"
sprintf("%+ 10.8x", -123).should == " -0000007b"
sprintf("% 010.8x", -123).should == " -0000007b"
end
it "passes some tests for negative %u" do
sprintf("%u", -123).should == "-123"
sprintf("%0u", -123).should == "-123"
sprintf("%#u", -123).should == "-123"
sprintf("%#0u", -123).should == "-123"
sprintf("%8u", -123).should == " -123"
sprintf("%08u", -123).should == "-0000123"
sprintf("%#8u", -123).should == " -123"
sprintf("%#08u", -123).should == "-0000123"
sprintf("%30u", -123).should == " -123"
sprintf("%030u", -123).should == "-00000000000000000000000000123"
sprintf("%#30u", -123).should == " -123"
sprintf("%#030u", -123).should == "-00000000000000000000000000123"
sprintf("%24.30u", -123).should == "-000000000000000000000000000123"
sprintf("%024.30u", -123).should == "-000000000000000000000000000123"
sprintf("%#24.30u", -123).should == "-000000000000000000000000000123"
sprintf("%#024.30u", -123).should == "-000000000000000000000000000123"
sprintf("%30.24u", -123).should == " -000000000000000000000123"
sprintf("%030.24u", -123).should == " -000000000000000000000123"
sprintf("%#30.24u", -123).should == " -000000000000000000000123"
sprintf("%#030.24u", -123).should == " -000000000000000000000123"
end
it "passes some tests for positive %u" do
sprintf("%30u", 123).should == " 123"
sprintf("%030u", 123).should == "000000000000000000000000000123"
sprintf("%#30u", 123).should == " 123"
sprintf("%#030u", 123).should == "000000000000000000000000000123"
sprintf("%24.30u", 123).should == "000000000000000000000000000123"
sprintf("%024.30u", 123).should == "000000000000000000000000000123"
sprintf("%#24.30u", 123).should == "000000000000000000000000000123"
sprintf("%#024.30u", 123).should == "000000000000000000000000000123"
sprintf("%30.24u", 123).should == " 000000000000000000000123"
sprintf("%030.24u", 123).should == " 000000000000000000000123"
sprintf("%#30.24u", 123).should == " 000000000000000000000123"
sprintf("%#030.24u", 123).should == " 000000000000000000000123"
end
it "passes some tests for positive %d" do
sprintf("%30d", 123).should == " 123"
sprintf("%030d", 123).should == "000000000000000000000000000123"
sprintf("%#30d", 123).should == " 123"
sprintf("%#030d", 123).should == "000000000000000000000000000123"
sprintf("%24.30d", 123).should == "000000000000000000000000000123"
sprintf("%024.30d", 123).should == "000000000000000000000000000123"
sprintf("%#24.30d", 123).should == "000000000000000000000000000123"
sprintf("%#024.30d", 123).should == "000000000000000000000000000123"
sprintf("%30.24d", 123).should == " 000000000000000000000123"
sprintf("%030.24d", 123).should == " 000000000000000000000123"
sprintf("%#30.24d", 123).should == " 000000000000000000000123"
sprintf("%#030.24d", 123).should == " 000000000000000000000123"
end
it "passes some tests for positive %f" do
sprintf("%30f", 123.1).should == " 123.100000"
sprintf("%030f", 123.1).should == "00000000000000000000123.100000"
sprintf("%#30f", 123.1).should == " 123.100000"
sprintf("%#030f", 123.1).should == "00000000000000000000123.100000"
sprintf("%10.4f", 123.1).should == " 123.1000"
sprintf("%010.4f", 123.1).should == "00123.1000"
sprintf("%10.0f", 123.1).should == " 123"
sprintf("%010.0f", 123.1).should == "0000000123"
end
it "passes some tests for negative %f" do
sprintf("%30f", -123.5).should == " -123.500000"
sprintf("%030f", -123.5).should == "-0000000000000000000123.500000"
sprintf("%#30f", -123.5).should == " -123.500000"
sprintf("%#030f", -123.5).should == "-0000000000000000000123.500000"
sprintf("%10.4f", -123.5).should == " -123.5000"
sprintf("%010.4f", -123.5).should == "-0123.5000"
sprintf("%10.0f", -123.5).should == " -124"
sprintf("%010.0f", -123.5).should == "-000000124"
end
it "passes some tests for infinite and nan" do
sprintf("%f", Float::INFINITY).should == "Inf"
sprintf("%f", -Float::INFINITY).should == "-Inf"
sprintf("%f", Float::NAN).should == "NaN"
sprintf("%10f", Float::INFINITY).should == " Inf"
sprintf("%10f", -Float::INFINITY).should == " -Inf"
sprintf("%10f", Float::NAN).should == " NaN"
end
it "passes kstephens's tests" do
sprintf("%*1$.*2$3$d", 10, 5, 1).should == " 00001"
sprintf("%b", 0).should == "0"
sprintf("%B", 0).should == "0"
sprintf("%b", -5).should == "..1011"
sprintf("%B", -5).should == "..1011"
sprintf("%+b", -5).should == "-101"
sprintf("%+b", 10).should == "+1010"
sprintf("%+b", 0).should == "+0"
sprintf("%+o", -5).should == "-5"
sprintf("%+o", 10).should == "+12"
sprintf("%+o", 0).should == "+0"
sprintf("%+d", -5).should == "-5"
sprintf("%+d", 10).should == "+10"
sprintf("%+d", 0).should == "+0"
sprintf("%+x", -15).should == "-f"
sprintf("%+x", 100).should == "+64"
sprintf("%+x", 0).should == "+0"
sprintf("%+X", -15).should == "-F"
sprintf("%+X", 100).should == "+64"
sprintf("%+X", 0).should == "+0"
sprintf("=%02X", 1).should == "=01"
sprintf("%+03d", 0).should == "+00"
sprintf("%+03d", 5).should == "+05"
sprintf("%+03d", -5).should == "-05"
sprintf("%+03d", 12).should == "+12"
sprintf("%+03d", -12).should == "-12"
sprintf("%+03d", 123).should == "+123"
sprintf("%+03d", -123).should == "-123"
end
with_feature :encoding do
it "returns a String in the same encoding as the format String if compatible" do
format = "%.2f %4s".force_encoding(Encoding::KOI8_U)
result = sprintf(format, 1.2, "dogs")
result.encoding.should equal(Encoding::KOI8_U)
end
it "returns a String in the argument encoding if format encoding is more restrictive" do
format = "foo %s".force_encoding(Encoding::US_ASCII)
arg = "b\303\274r".force_encoding(Encoding::UTF_8)
result = sprintf(format, arg)
result.encoding.should equal(Encoding::UTF_8)
end
end
end
describe "Kernel.sprintf" do
it "needs to be reviewed for spec completeness"
end
|