summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/modulo_spec.rb
blob: 99c1694417de3a09237f45897dfd1b0e4deb1efe (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
require_relative '../../spec_helper'
require_relative '../kernel/shared/sprintf'
require_relative '../kernel/shared/sprintf_encoding'
require_relative 'fixtures/classes'
require_relative '../../shared/hash/key_error'

describe "String#%" do
  it_behaves_like :kernel_sprintf, -> format, *args {
    format % args
  }

  it_behaves_like :kernel_sprintf_encoding, -> format, *args {
    format % args
  }
end

# TODO: these specs are mostly redundant with kernel/shared/sprintf.rb specs.
# These specs should be moved there and deduplicated.
describe "String#%" do
  context "when key is missing from passed-in hash" do
    it_behaves_like :key_error, -> obj, key { "%{#{key}}" % obj }, { a: 5 }
  end

  it "formats multiple expressions" do
    ("%b %x %d %s" % [10, 10, 10, 10]).should == "1010 a 10 10"
  end

  it "formats expressions mid string" do
    ("hello %s!" % "world").should == "hello world!"
  end

  it "formats %% into %" do
    ("%d%% %s" % [10, "of chickens!"]).should == "10% of chickens!"
  end

  describe "output's encoding" do
    it "is the same as the format string if passed value is encoding-compatible" do
      [Encoding::BINARY, Encoding::US_ASCII, Encoding::UTF_8, Encoding::SHIFT_JIS].each do |encoding|
        ("hello %s!".encode(encoding) % "world").encoding.should == encoding
      end
    end

    it "negotiates a compatible encoding if necessary" do
      ("hello %s" % 195.chr).encoding.should == Encoding::BINARY
      ("hello %s".encode("shift_jis") % "wörld").encoding.should == Encoding::UTF_8
    end

    it "raises if a compatible encoding can't be found" do
      -> { "hello %s".encode("utf-8") % "world".encode("UTF-16LE") }.should raise_error(Encoding::CompatibilityError)
    end
  end

  it "raises an error if single % appears at the end" do
    -> { ("%" % []) }.should raise_error(ArgumentError)
    -> { ("foo%" % [])}.should raise_error(ArgumentError)
  end

  it "formats single % character before a newline as literal %" do
    ("%\n" % []).should == "%\n"
    ("foo%\n" % []).should == "foo%\n"
    ("%\n.3f" % 1.2).should == "%\n.3f"
  end

  it "formats single % character before a NUL as literal %" do
    ("%\0" % []).should == "%\0"
    ("foo%\0" % []).should == "foo%\0"
    ("%\0.3f" % 1.2).should == "%\0.3f"
  end

  it "raises an error if single % appears anywhere else" do
    -> { (" % " % []) }.should raise_error(ArgumentError)
    -> { ("foo%quux" % []) }.should raise_error(ArgumentError)
  end

  it "raises an error if NULL or \\n appear anywhere else in the format string" do
    begin
      old_debug, $DEBUG = $DEBUG, false

      -> { "%.\n3f" % 1.2 }.should raise_error(ArgumentError)
      -> { "%.3\nf" % 1.2 }.should raise_error(ArgumentError)
      -> { "%.\03f" % 1.2 }.should raise_error(ArgumentError)
      -> { "%.3\0f" % 1.2 }.should raise_error(ArgumentError)
    ensure
      $DEBUG = old_debug
    end
  end

  it "ignores unused arguments when $DEBUG is false" do
    begin
      old_debug = $DEBUG
      $DEBUG = false

      ("" % [1, 2, 3]).should == ""
      ("%s" % [1, 2, 3]).should == "1"
    ensure
      $DEBUG = old_debug
    end
  end

  it "raises an ArgumentError for unused arguments when $DEBUG is true" do
    begin
      old_debug = $DEBUG
      $DEBUG = true
      s = $stderr
      $stderr = IOStub.new

      -> { "" % [1, 2, 3]   }.should raise_error(ArgumentError)
      -> { "%s" % [1, 2, 3] }.should raise_error(ArgumentError)
    ensure
      $DEBUG = old_debug
      $stderr = s
    end
  end

  it "always allows unused arguments when positional argument style is used" do
    begin
      old_debug = $DEBUG
      $DEBUG = false

      ("%2$s" % [1, 2, 3]).should == "2"
      $DEBUG = true
      ("%2$s" % [1, 2, 3]).should == "2"
    ensure
      $DEBUG = old_debug
    end
  end

  it "replaces trailing absolute argument specifier without type with percent sign" do
    ("hello %1$" % "foo").should == "hello %"
  end

  it "raises an ArgumentError when given invalid argument specifiers" do
    -> { "%1" % [] }.should raise_error(ArgumentError)
    -> { "%+" % [] }.should raise_error(ArgumentError)
    -> { "%-" % [] }.should raise_error(ArgumentError)
    -> { "%#" % [] }.should raise_error(ArgumentError)
    -> { "%0" % [] }.should raise_error(ArgumentError)
    -> { "%*" % [] }.should raise_error(ArgumentError)
    -> { "%." % [] }.should raise_error(ArgumentError)
    -> { "%_" % [] }.should raise_error(ArgumentError)
    -> { "%0$s" % "x"              }.should raise_error(ArgumentError)
    -> { "%*0$s" % [5, "x"]        }.should raise_error(ArgumentError)
    -> { "%*1$.*0$1$s" % [1, 2, 3] }.should raise_error(ArgumentError)
  end

  it "raises an ArgumentError when multiple positional argument tokens are given for one format specifier" do
    -> { "%1$1$s" % "foo" }.should raise_error(ArgumentError)
  end

  it "respects positional arguments and precision tokens given for one format specifier" do
    ("%2$1d" % [1, 0]).should == "0"
    ("%2$1d" % [0, 1]).should == "1"

    ("%2$.2f" % [1, 0]).should == "0.00"
    ("%2$.2f" % [0, 1]).should == "1.00"
  end

  it "allows more than one digit of position" do
    ("%50$d" % (0..100).to_a).should == "49"
  end

  it "raises an ArgumentError when multiple width star tokens are given for one format specifier" do
    -> { "%**s" % [5, 5, 5] }.should raise_error(ArgumentError)
  end

  it "raises an ArgumentError when a width star token is seen after a width token" do
    -> { "%5*s" % [5, 5] }.should raise_error(ArgumentError)
  end

  it "raises an ArgumentError when multiple precision tokens are given" do
    -> { "%.5.5s" % 5      }.should raise_error(ArgumentError)
    -> { "%.5.*s" % [5, 5] }.should raise_error(ArgumentError)
    -> { "%.*.5s" % [5, 5] }.should raise_error(ArgumentError)
  end

  it "raises an ArgumentError when there are less arguments than format specifiers" do
    ("foo" % []).should == "foo"
    -> { "%s" % []     }.should raise_error(ArgumentError)
    -> { "%s %s" % [1] }.should raise_error(ArgumentError)
  end

  it "raises an ArgumentError when absolute and relative argument numbers are mixed" do
    -> { "%s %1$s" % "foo" }.should raise_error(ArgumentError)
    -> { "%1$s %s" % "foo" }.should raise_error(ArgumentError)

    -> { "%s %2$s" % ["foo", "bar"] }.should raise_error(ArgumentError)
    -> { "%2$s %s" % ["foo", "bar"] }.should raise_error(ArgumentError)

    -> { "%*2$s" % [5, 5, 5]     }.should raise_error(ArgumentError)
    -> { "%*.*2$s" % [5, 5, 5]   }.should raise_error(ArgumentError)
    -> { "%*2$.*2$s" % [5, 5, 5] }.should raise_error(ArgumentError)
    -> { "%*.*2$s" % [5, 5, 5]   }.should raise_error(ArgumentError)
  end

  it "allows reuse of the one argument multiple via absolute argument numbers" do
    ("%1$s %1$s" % "foo").should == "foo foo"
    ("%1$s %2$s %1$s %2$s" % ["foo", "bar"]).should == "foo bar foo bar"
  end

  it "always interprets an array argument as a list of argument parameters" do
    -> { "%p" % [] }.should raise_error(ArgumentError)
    ("%p" % [1]).should == "1"
    ("%p %p" % [1, 2]).should == "1 2"
  end

  it "always interprets an array subclass argument as a list of argument parameters" do
    -> { "%p" % StringSpecs::MyArray[] }.should raise_error(ArgumentError)
    ("%p" % StringSpecs::MyArray[1]).should == "1"
    ("%p %p" % StringSpecs::MyArray[1, 2]).should == "1 2"
  end

  it "allows positional arguments for width star and precision star arguments" do
    ("%*1$.*2$3$d" % [10, 5, 1]).should == "     00001"
  end

  it "allows negative width to imply '-' flag" do
    ("%*1$.*2$3$d" % [-10, 5, 1]).should == "00001     "
    ("%-*1$.*2$3$d" % [10, 5, 1]).should == "00001     "
    ("%-*1$.*2$3$d" % [-10, 5, 1]).should == "00001     "
  end

  it "ignores negative precision" do
    ("%*1$.*2$3$d" % [10, -5, 1]).should == "         1"
  end

  it "allows a star to take an argument number to use as the width" do
    ("%1$*2$s" % ["a", 8]).should == "       a"
    ("%1$*10$s" % ["a",0,0,0,0,0,0,0,0,8]).should == "       a"
  end

  it "calls to_int on width star and precision star tokens" do
    w = mock('10')
    w.should_receive(:to_int).and_return(10)

    p = mock('5')
    p.should_receive(:to_int).and_return(5)

    ("%*.*f" % [w, p, 1]).should == "   1.00000"


    w = mock('10')
    w.should_receive(:to_int).and_return(10)

    p = mock('5')
    p.should_receive(:to_int).and_return(5)

    ("%*.*d" % [w, p, 1]).should == "     00001"
  end

  it "does not call #to_a to convert the argument" do
    x = mock("string modulo to_a")
    x.should_not_receive(:to_a)
    x.should_receive(:to_s).and_return("x")

    ("%s" % x).should == "x"
  end

  it "calls #to_ary to convert the argument" do
    x = mock("string modulo to_ary")
    x.should_not_receive(:to_s)
    x.should_receive(:to_ary).and_return(["x"])

    ("%s" % x).should == "x"
  end

  it "wraps the object in an Array if #to_ary returns nil" do
    x = mock("string modulo to_ary")
    x.should_receive(:to_ary).and_return(nil)
    x.should_receive(:to_s).and_return("x")

    ("%s" % x).should == "x"
  end

  it "raises a TypeError if #to_ary does not return an Array" do
    x = mock("string modulo to_ary")
    x.should_receive(:to_ary).and_return("x")

    -> { "%s" % x }.should raise_error(TypeError)
  end

  it "tries to convert the argument to Array by calling #to_ary" do
    obj = mock('[1,2]')
    def obj.to_ary() [1, 2] end
    def obj.to_s() "obj" end
    ("%s %s" % obj).should == "1 2"
    ("%s" % obj).should == "1"
  end

  it "doesn't return subclass instances when called on a subclass" do
    universal = mock('0')
    def universal.to_int() 0 end
    def universal.to_str() "0" end
    def universal.to_f() 0.0 end

    [
      "", "foo",
      "%b", "%B", "%c", "%d", "%e", "%E",
      "%f", "%g", "%G", "%i", "%o", "%p",
      "%s", "%u", "%x", "%X"
    ].each do |format|
      (StringSpecs::MyString.new(format) % universal).should be_an_instance_of(String)
    end
  end

  it "supports binary formats using %b for positive numbers" do
    ("%b" % 10).should == "1010"
    ("% b" % 10).should == " 1010"
    ("%1$b" % [10, 20]).should == "1010"
    ("%#b" % 10).should == "0b1010"
    ("%+b" % 10).should == "+1010"
    ("%-9b" % 10).should == "1010     "
    ("%05b" % 10).should == "01010"
    ("%*b" % [10, 6]).should == "       110"
    ("%*b" % [-10, 6]).should == "110       "
    ("%.4b" % 2).should == "0010"
    ("%.32b" % 2147483648).should == "10000000000000000000000000000000"
  end

  it "supports binary formats using %b for negative numbers" do
    ("%b" % -5).should == "..1011"
    ("%0b" % -5).should == "..1011"
    ("%.1b" % -5).should == "..1011"
    ("%.7b" % -5).should == "..11011"
    ("%.10b" % -5).should == "..11111011"
    ("% b" % -5).should == "-101"
    ("%+b" % -5).should == "-101"
    not_supported_on :opal do
      ("%b" % -(2 ** 64 + 5)).should ==
        "..101111111111111111111111111111111111111111111111111111111111111011"
    end
  end

  it "supports binary formats using %B with same behaviour as %b except for using 0B instead of 0b for #" do
    ("%B" % 10).should == ("%b" % 10)
    ("% B" % 10).should == ("% b" % 10)
    ("%1$B" % [10, 20]).should == ("%1$b" % [10, 20])
    ("%+B" % 10).should == ("%+b" % 10)
    ("%-9B" % 10).should == ("%-9b" % 10)
    ("%05B" % 10).should == ("%05b" % 10)
    ("%*B" % [10, 6]).should == ("%*b" % [10, 6])
    ("%*B" % [-10, 6]).should == ("%*b" % [-10, 6])

    ("%B" % -5).should == ("%b" % -5)
    ("%0B" % -5).should == ("%0b" % -5)
    ("%.1B" % -5).should == ("%.1b" % -5)
    ("%.7B" % -5).should == ("%.7b" % -5)
    ("%.10B" % -5).should == ("%.10b" % -5)
    ("% B" % -5).should == ("% b" % -5)
    ("%+B" % -5).should == ("%+b" % -5)
    not_supported_on :opal do
      ("%B" % -(2 ** 64 + 5)).should == ("%b" % -(2 ** 64 + 5))
    end

    ("%#B" % 10).should == "0B1010"
  end

  it "supports character formats using %c" do
    ("%c" % 10).should == "\n"
    ("%2$c" % [10, 11, 14]).should == "\v"
    ("%-4c" % 10).should == "\n   "
    ("%*c" % [10, 3]).should == "         \003"
    ("%c" % 42).should == "*"

    -> { "%c" % Object }.should raise_error(TypeError)
  end

  it "supports single character strings as argument for %c" do
    ("%c" % 'A').should == "A"
  end

  it "raises an exception for multiple character strings as argument for %c" do
    -> { "%c" % 'AA' }.should raise_error(ArgumentError)
  end

  it "calls to_str on argument for %c formats" do
    obj = mock('A')
    obj.should_receive(:to_str).and_return('A')

    ("%c" % obj).should == "A"
  end

  it "calls #to_ary on argument for %c formats" do
    obj = mock('65')
    obj.should_receive(:to_ary).and_return([65])
    ("%c" % obj).should == ("%c" % [65])
  end

  it "calls #to_int on argument for %c formats, if the argument does not respond to #to_ary" do
    obj = mock('65')
    obj.should_receive(:to_int).and_return(65)

    ("%c" % obj).should == ("%c" % 65)
  end

  %w(d i).each do |f|
    format = "%" + f

    it "supports integer formats using #{format}" do
      ("%#{f}" % 10).should == "10"
      ("% #{f}" % 10).should == " 10"
      ("%1$#{f}" % [10, 20]).should == "10"
      ("%+#{f}" % 10).should == "+10"
      ("%-7#{f}" % 10).should == "10     "
      ("%04#{f}" % 10).should == "0010"
      ("%*#{f}" % [10, 4]).should == "         4"
      ("%6.4#{f}" % 123).should == "  0123"
    end

    it "supports negative integers using #{format}" do
      ("%#{f}" % -5).should == "-5"
      ("%3#{f}" % -5).should == " -5"
      ("%03#{f}" % -5).should == "-05"
      ("%+03#{f}" % -5).should == "-05"
      ("%+.2#{f}" % -5).should == "-05"
      ("%-3#{f}" % -5).should == "-5 "
      ("%6.4#{f}" % -123).should == " -0123"
    end

    it "supports negative integers using #{format}, giving priority to `-`" do
      ("%-03#{f}" % -5).should == "-5 "
      ("%+-03#{f}" % -5).should == "-5 "
    end
  end

  it "supports float formats using %e" do
    ("%e" % 10).should == "1.000000e+01"
    ("% e" % 10).should == " 1.000000e+01"
    ("%1$e" % 10).should == "1.000000e+01"
    ("%#e" % 10).should == "1.000000e+01"
    ("%+e" % 10).should == "+1.000000e+01"
    ("%-7e" % 10).should == "1.000000e+01"
    ("%05e" % 10).should == "1.000000e+01"
    ("%*e" % [10, 9]).should == "9.000000e+00"
  end

  it "supports float formats using %e, but Inf, -Inf, and NaN are not floats" do
    ("%e" % 1e1020).should == "Inf"
    ("%e" % -1e1020).should == "-Inf"
    ("%e" % -Float::NAN).should == "NaN"
    ("%e" % Float::NAN).should == "NaN"
  end

  it "supports float formats using %E, but Inf, -Inf, and NaN are not floats" do
    ("%E" % 1e1020).should == "Inf"
    ("%E" % -1e1020).should == "-Inf"
    ("%-10E" % 1e1020).should == "Inf       "
    ("%10E" % 1e1020).should == "       Inf"
    ("%+E" % 1e1020).should == "+Inf"
    ("% E" % 1e1020).should == " Inf"
    ("%E" % Float::NAN).should == "NaN"
    ("%E" % -Float::NAN).should == "NaN"
  end

  it "supports float formats using %E" do
    ("%E" % 10).should == "1.000000E+01"
    ("% E" % 10).should == " 1.000000E+01"
    ("%1$E" % 10).should == "1.000000E+01"
    ("%#E" % 10).should == "1.000000E+01"
    ("%+E" % 10).should == "+1.000000E+01"
    ("%-7E" % 10).should == "1.000000E+01"
    ("%05E" % 10).should == "1.000000E+01"
    ("%*E" % [10, 9]).should == "9.000000E+00"
  end

  it "pads with spaces for %E with Inf, -Inf, and NaN" do
    ("%010E" % -1e1020).should == "      -Inf"
    ("%010E" % 1e1020).should == "       Inf"
    ("%010E" % Float::NAN).should == "       NaN"
  end

  it "supports float formats using %f" do
    ("%f" % 10).should == "10.000000"
    ("% f" % 10).should == " 10.000000"
    ("%1$f" % 10).should == "10.000000"
    ("%#f" % 10).should == "10.000000"
    ("%#0.3f" % 10).should == "10.000"
    ("%+f" % 10).should == "+10.000000"
    ("%-7f" % 10).should == "10.000000"
    ("%05f" % 10).should == "10.000000"
    ("%0.5f" % 10).should == "10.00000"
    ("%*f" % [10, 9]).should == "  9.000000"
  end

  it "supports float formats using %g" do
    ("%g" % 10).should == "10"
    ("% g" % 10).should == " 10"
    ("%1$g" % 10).should == "10"
    ("%#g" % 10).should == "10.0000"
    ("%#.3g" % 10).should == "10.0"
    ("%+g" % 10).should == "+10"
    ("%-7g" % 10).should == "10     "
    ("%05g" % 10).should == "00010"
    ("%g" % 10**10).should == "1e+10"
    ("%*g" % [10, 9]).should == "         9"
  end

  it "supports float formats using %G" do
    ("%G" % 10).should == "10"
    ("% G" % 10).should == " 10"
    ("%1$G" % 10).should == "10"
    ("%#G" % 10).should == "10.0000"
    ("%#.3G" % 10).should == "10.0"
    ("%+G" % 10).should == "+10"
    ("%-7G" % 10).should == "10     "
    ("%05G" % 10).should == "00010"
    ("%G" % 10**10).should == "1E+10"
    ("%*G" % [10, 9]).should == "         9"
  end

  it "supports octal formats using %o for positive numbers" do
    ("%o" % 10).should == "12"
    ("% o" % 10).should == " 12"
    ("%1$o" % [10, 20]).should == "12"
    ("%#o" % 10).should == "012"
    ("%+o" % 10).should == "+12"
    ("%-9o" % 10).should == "12       "
    ("%05o" % 10).should == "00012"
    ("%*o" % [10, 6]).should == "         6"
  end

  it "supports octal formats using %o for negative numbers" do
    # These are incredibly wrong. -05 == -5, not 7177777...whatever
    ("%o" % -5).should == "..73"
    ("%0o" % -5).should == "..73"
    ("%.4o" % 20).should == "0024"
    ("%.1o" % -5).should == "..73"
    ("%.7o" % -5).should == "..77773"
    ("%.10o" % -5).should == "..77777773"

    ("% o" % -26).should == "-32"
    ("%+o" % -26).should == "-32"
    not_supported_on :opal do
      ("%o" % -(2 ** 64 + 5)).should == "..75777777777777777777773"
    end
  end

  it "supports inspect formats using %p" do
    ("%p" % 10).should == "10"
    ("%1$p" % [10, 5]).should == "10"
    ("%-22p" % 10).should == "10                    "
    ("%*p" % [10, 10]).should == "        10"
    ("%p" % {capture: 1}).should == "{:capture=>1}"
    ("%p" % "str").should == "\"str\""
  end

  it "calls inspect on arguments for %p format" do
    obj = mock('obj')
    def obj.inspect() "obj" end
    ("%p" % obj).should == "obj"

    # undef is not working
    # obj = mock('obj')
    # class << obj; undef :inspect; end
    # def obj.method_missing(*args) "obj" end
    # ("%p" % obj).should == "obj"
  end

  it "supports string formats using %s" do
    ("%s" % "hello").should == "hello"
    ("%s" % "").should == ""
    ("%s" % 10).should == "10"
    ("%1$s" % [10, 8]).should == "10"
    ("%-5s" % 10).should == "10   "
    ("%*s" % [10, 9]).should == "         9"
  end

  it "respects a space padding request not as part of the width" do
    x = "% -5s" % ["foo"]
    x.should == "foo  "
  end

  it "calls to_s on non-String arguments for %s format" do
    obj = mock('obj')
    def obj.to_s() "obj" end

    ("%s" % obj).should == "obj"

    # undef doesn't work
    # obj = mock('obj')
    # class << obj; undef :to_s; end
    # def obj.method_missing(*args) "obj" end
    #
    # ("%s" % obj).should == "obj"
  end

  # MRI crashes on this one.
  # See http://groups.google.com/group/ruby-core-google/t/c285c18cd94c216d
  it "raises an ArgumentError for huge precisions for %s" do
    block = -> { "%.25555555555555555555555555555555555555s" % "hello world" }
    block.should raise_error(ArgumentError)
  end

  # Note: %u has been changed to an alias for %d in 1.9.
  it "supports unsigned formats using %u" do
    ("%u" % 10).should == "10"
    ("% u" % 10).should == " 10"
    ("%1$u" % [10, 20]).should == "10"
    ("%+u" % 10).should == "+10"
    ("%-7u" % 10).should == "10     "
    ("%04u" % 10).should == "0010"
    ("%*u" % [10, 4]).should == "         4"
  end

  it "formats negative values with a leading sign using %u" do
    ("% u" % -26).should == "-26"
    ("%+u" % -26).should == "-26"
  end

  it "supports negative bignums with %u or %d" do
    ("%u" % -(2 ** 64 + 5)).should == "-18446744073709551621"
    ("%d" % -(2 ** 64 + 5)).should == "-18446744073709551621"
  end

  it "supports hex formats using %x for positive numbers" do
    ("%x" % 10).should == "a"
    ("% x" % 10).should == " a"
    ("%1$x" % [10, 20]).should == "a"
    ("%#x" % 10).should == "0xa"
    ("%+x" % 10).should == "+a"
    ("%-9x" % 10).should == "a        "
    ("%05x" % 10).should == "0000a"
    ("%*x" % [10, 6]).should == "         6"
    ("%.4x" % 20).should == "0014"
    ("%x" % 0xFFFFFFFF).should == "ffffffff"
  end

  it "supports hex formats using %x for negative numbers" do
    ("%x" % -5).should == "..fb"
    ("%0x" % -5).should == "..fb"
    ("%.1x" % -5).should == "..fb"
    ("%.7x" % -5).should == "..ffffb"
    ("%.10x" % -5).should == "..fffffffb"
    ("% x" % -26).should == "-1a"
    ("%+x" % -26).should == "-1a"
    not_supported_on :opal do
      ("%x" % -(2 ** 64 + 5)).should == "..fefffffffffffffffb"
    end
  end

  it "supports hex formats using %X for positive numbers" do
    ("%X" % 10).should == "A"
    ("% X" % 10).should == " A"
    ("%1$X" % [10, 20]).should == "A"
    ("%#X" % 10).should == "0XA"
    ("%+X" % 10).should == "+A"
    ("%-9X" % 10).should == "A        "
    ("%05X" % 10).should == "0000A"
    ("%*X" % [10, 6]).should == "         6"
    ("%X" % 0xFFFFFFFF).should == "FFFFFFFF"
  end

  it "supports hex formats using %X for negative numbers" do
    ("%X" % -5).should == "..FB"
    ("%0X" % -5).should == "..FB"
    ("%.1X" % -5).should == "..FB"
    ("%.7X" % -5).should == "..FFFFB"
    ("%.10X" % -5).should == "..FFFFFFFB"
    ("% X" % -26).should == "-1A"
    ("%+X" % -26).should == "-1A"
    not_supported_on :opal do
      ("%X" % -(2 ** 64 + 5)).should == "..FEFFFFFFFFFFFFFFFB"
    end
  end

  it "formats zero without prefix using %#x" do
    ("%#x" % 0).should == "0"
  end

  it "formats zero without prefix using %#X" do
    ("%#X" % 0).should == "0"
  end

  %w(b d i o u x X).each do |f|
    format = "%" + f

    it "behaves as if calling Kernel#Integer for #{format} argument, if it does not respond to #to_ary" do
      (format % "10").should == (format % Kernel.Integer("10"))
      (format % "0x42").should == (format % Kernel.Integer("0x42"))
      (format % "0b1101").should == (format % Kernel.Integer("0b1101"))
      (format % "0b1101_0000").should == (format % Kernel.Integer("0b1101_0000"))
      (format % "0777").should == (format % Kernel.Integer("0777"))
      -> {
        # see [ruby-core:14139] for more details
        (format % "0777").should == (format % Kernel.Integer("0777"))
      }.should_not raise_error(ArgumentError)

      -> { format % "0__7_7_7" }.should raise_error(ArgumentError)

      -> { format % "" }.should raise_error(ArgumentError)
      -> { format % "x" }.should raise_error(ArgumentError)
      -> { format % "5x" }.should raise_error(ArgumentError)
      -> { format % "08" }.should raise_error(ArgumentError)
      -> { format % "0b2" }.should raise_error(ArgumentError)
      -> { format % "123__456" }.should raise_error(ArgumentError)

      obj = mock('5')
      obj.should_receive(:to_i).and_return(5)
      (format % obj).should == (format % 5)

      obj = mock('6')
      obj.stub!(:to_i).and_return(5)
      obj.should_receive(:to_int).and_return(6)
      (format % obj).should == (format % 6)
    end
  end

  %w(e E f g G).each do |f|
    format = "%" + f

    it "tries to convert the passed argument to an Array using #to_ary" do
      obj = mock('3.14')
      obj.should_receive(:to_ary).and_return([3.14])
      (format % obj).should == (format % [3.14])
    end

    it "behaves as if calling Kernel#Float for #{format} arguments, when the passed argument does not respond to #to_ary" do
      (format % 10).should == (format % 10.0)
      (format % "-10.4e-20").should == (format % -10.4e-20)
      (format % ".5").should == (format % 0.5)
      (format % "-.5").should == (format % -0.5)
      # Something's strange with this spec:
      # it works just fine in individual mode, but not when run as part of a group
      (format % "10_1_0.5_5_5").should == (format % 1010.555)

      (format % "0777").should == (format % 777)

      -> { format % "" }.should raise_error(ArgumentError)
      -> { format % "x" }.should raise_error(ArgumentError)
      -> { format % "." }.should raise_error(ArgumentError)
      -> { format % "10." }.should raise_error(ArgumentError)
      -> { format % "5x" }.should raise_error(ArgumentError)
      -> { format % "0b1" }.should raise_error(ArgumentError)
      -> { format % "10e10.5" }.should raise_error(ArgumentError)
      -> { format % "10__10" }.should raise_error(ArgumentError)
      -> { format % "10.10__10" }.should raise_error(ArgumentError)

      obj = mock('5.0')
      obj.should_receive(:to_f).and_return(5.0)
      (format % obj).should == (format % 5.0)
    end

    it "behaves as if calling Kernel#Float for #{format} arguments, when the passed argument is hexadecimal string" do
      (format % "0xA").should == (format % 0xA)
    end
  end

  describe "when format string contains %{} sections" do
    it "replaces %{} sections with values from passed-in hash" do
      ("%{foo}bar" % {foo: 'oof'}).should == "oofbar"
    end

    it "should raise ArgumentError if no hash given" do
      -> {"%{foo}" % []}.should raise_error(ArgumentError)
    end
  end

  describe "when format string contains %<> formats" do
    it "uses the named argument for the format's value" do
      ("%<foo>d" % {foo: 1}).should == "1"
    end

    it "raises KeyError if key is missing from passed-in hash" do
      -> {"%<foo>d" % {}}.should raise_error(KeyError)
    end

    it "should raise ArgumentError if no hash given" do
      -> {"%<foo>" % []}.should raise_error(ArgumentError)
    end
  end
end