1 #assert('Kernel.sprintf') do
5 assert_equal "one=1", "one=%d" % 1
6 assert_equal "1 one", "%d %s" % [ 1, "one" ]
7 assert_equal "123 < 456", "%{num} < %<str>s" % { num: 123, str: "456" }
8 assert_equal 15, ("%b" % (1<<14)).size
9 skip unless Object.const_defined?(:Float)
10 assert_equal "1.0", "%3.1f" % 1.01
13 assert('String#% with inf') do
14 skip unless Object.const_defined?(:Float)
17 assert_equal "Inf", "%f" % inf
18 assert_equal "Inf", "%2f" % inf
19 assert_equal "Inf", "%3f" % inf
20 assert_equal " Inf", "%4f" % inf
21 assert_equal " Inf", "%5f" % inf
23 assert_equal "+Inf", "%+f" % inf
24 assert_equal "+Inf", "%+2f" % inf
25 assert_equal "+Inf", "%+3f" % inf
26 assert_equal "+Inf", "%+4f" % inf
27 assert_equal " +Inf", "%+5f" % inf
29 assert_equal "Inf", "%-f" % inf
30 assert_equal "Inf", "%-2f" % inf
31 assert_equal "Inf", "%-3f" % inf
32 assert_equal "Inf ", "%-4f" % inf
33 assert_equal "Inf ", "%-5f" % inf
35 assert_equal " Inf", "% f" % inf
36 assert_equal " Inf", "% 2f" % inf
37 assert_equal " Inf", "% 3f" % inf
38 assert_equal " Inf", "% 4f" % inf
39 assert_equal " Inf", "% 5f" % inf
42 assert('String#% with nan') do
43 skip unless Object.const_defined?(:Float)
46 assert_equal "NaN", "%f" % nan
47 assert_equal "NaN", "%2f" % nan
48 assert_equal "NaN", "%3f" % nan
49 assert_equal " NaN", "%4f" % nan
50 assert_equal " NaN", "%5f" % nan
52 assert_equal "+NaN", "%+f" % nan
53 assert_equal "+NaN", "%+2f" % nan
54 assert_equal "+NaN", "%+3f" % nan
55 assert_equal "+NaN", "%+4f" % nan
56 assert_equal " +NaN", "%+5f" % nan
58 assert_equal "NaN", "%-f" % nan
59 assert_equal "NaN", "%-2f" % nan
60 assert_equal "NaN", "%-3f" % nan
61 assert_equal "NaN ", "%-4f" % nan
62 assert_equal "NaN ", "%-5f" % nan
64 assert_equal " NaN", "% f" % nan
65 assert_equal " NaN", "% 2f" % nan
66 assert_equal " NaN", "% 3f" % nan
67 assert_equal " NaN", "% 4f" % nan
68 assert_equal " NaN", "% 5f" % nan
71 assert("String#% with invalid chr") do
74 alias_method :chr_, :chr if method_defined?(:chr)
81 assert_raise TypeError do
86 if method_defined?(:chr_)
87 alias_method :chr, :chr_
94 assert("String#% %b") do
95 assert_equal("..10115", "%0b5" % -5)
98 assert("String#% %d") do
99 assert_equal(" 10", "%4d" % 10)
100 assert_equal("1000", "%4d" % 1000)
101 assert_equal("10000", "%4d" % 10000)
104 assert("String#% invalid format") do
105 assert_raise ArgumentError do
110 assert("String#% invalid format shared substring") do
111 fmt = ("x"*30+"%!")[0...-1]
112 assert_equal fmt, sprintf(fmt, "")