@@ -22,7 +22,7 @@ def cell(options={})
2222 include CellHelpers
2323
2424 it "should draw the cell" do
25- Prawn ::Table ::Cell ::Text . any_instance . expects ( :draw ) . once
25+ expect_any_instance_of ( Prawn ::Table ::Cell ::Text ) . to receive ( :draw ) . once
2626 @pdf . cell ( :content => "text" )
2727 end
2828
@@ -54,9 +54,9 @@ def cell(options={})
5454
5555 it "should draw text at the given point plus padding, with the given " +
5656 "size and style" do
57- @pdf . expects ( :bounding_box ) . yields
58- @pdf . expects ( :move_down )
59- @pdf . expects ( :draw_text! ) . with { | text , options | text == "hello world" }
57+ expect ( @pdf ) . to receive ( :bounding_box ) . and_yield
58+ expect ( @pdf ) . to receive ( :move_down )
59+ expect ( @pdf ) . to receive ( :draw_text! ) . with ( "hello world" , anything )
6060
6161 @pdf . cell ( :content => "hello world" ,
6262 :at => [ 10 , 20 ] ,
@@ -68,7 +68,7 @@ def cell(options={})
6868
6969 describe "Prawn::Document#make_cell" do
7070 it "should not draw the cell" do
71- Prawn ::Table ::Cell ::Text . any_instance . expects ( :draw ) . never
71+ expect_any_instance_of ( Prawn ::Table ::Cell ::Text ) . to_not receive ( :draw )
7272 @pdf . make_cell ( "text" )
7373 end
7474
@@ -83,8 +83,8 @@ def cell(options={})
8383 it "should set each property in turn" do
8484 c = cell ( :content => "text" )
8585
86- c . expects ( :padding= ) . with ( 50 )
87- c . expects ( :size= ) . with ( 7 )
86+ expect ( c ) . to receive ( :padding= ) . with ( 50 )
87+ expect ( c ) . to receive ( :size= ) . with ( 7 )
8888
8989 c . style ( :padding => 50 , :size => 7 )
9090 end
@@ -262,33 +262,29 @@ def cell(options={})
262262 include CellHelpers
263263
264264 it "should fill a rectangle with the given background color" do
265- @pdf . stubs ( :mask ) . yields
266- @pdf . expects ( :mask ) . with ( :fill_color ) . yields
267-
268- @pdf . stubs ( :fill_color )
269- @pdf . expects ( :fill_color ) . with ( '123456' )
270- @pdf . expects ( :fill_rectangle ) . checking do |( x , y ) , w , h |
271- expect ( x ) . to be_within ( 0.01 ) . of ( 0 )
272- expect ( y ) . to be_within ( 0.01 ) . of ( @pdf . cursor )
273- expect ( w ) . to be_within ( 0.01 ) . of ( 29.344 )
274- expect ( h ) . to be_within ( 0.01 ) . of ( 23.872 )
275- end
265+ allow ( @pdf ) . to receive ( :mask ) . and_yield
266+ expect ( @pdf ) . to receive ( :mask ) . with ( :fill_color ) . and_yield
267+
268+ allow ( @pdf ) . to receive ( :fill_color )
269+ expect ( @pdf ) . to receive ( :fill_color ) . with ( '123456' )
270+ expect ( @pdf ) . to receive ( :fill_rectangle ) . with ( [ 0 , @pdf . cursor ] , 29.344 , 23.872 )
276271 @pdf . cell ( :content => "text" , :background_color => '123456' )
277272 end
278273
279274 it "should draw the background in the right place if cell is drawn at a " +
280275 "different location" do
281- @pdf . stubs ( :mask ) . yields
282- @pdf . expects ( :mask ) . with ( :fill_color ) . yields
283-
284- @pdf . stubs ( :fill_color )
285- @pdf . expects ( :fill_color ) . with ( '123456' )
286- @pdf . expects ( :fill_rectangle ) . checking do |( x , y ) , w , h |
287- expect ( x ) . to be_within ( 0.01 ) . of ( 12.0 )
288- expect ( y ) . to be_within ( 0.01 ) . of ( 34.0 )
289- expect ( w ) . to be_within ( 0.01 ) . of ( 29.344 )
290- expect ( h ) . to be_within ( 0.01 ) . of ( 23.872 )
291- end
276+ allow ( @pdf ) . to receive ( :mask ) . and_yield
277+ expect ( @pdf ) . to receive ( :mask ) . with ( :fill_color ) . and_yield
278+
279+ allow ( @pdf ) . to receive ( :fill_color )
280+ expect ( @pdf ) . to receive ( :fill_color ) . with ( '123456' )
281+ expect ( @pdf ) . to receive ( :fill_rectangle ) . with ( [ 12.0 , 34.0 ] , 29.344 , 23.872 )
282+ # .checking do |(x, y), w, h|
283+ # expect(x).to be_within(0.01).of(12.0)
284+ # expect(y).to be_within(0.01).of(34.0)
285+ # expect(w).to be_within(0.01).of(29.344)
286+ # expect(h).to be_within(0.01).of(23.872)
287+ #end
292288 c = @pdf . make_cell ( :content => "text" , :background_color => '123456' )
293289 c . draw ( [ 12.0 , 34.0 ] )
294290 end
@@ -297,8 +293,8 @@ def cell(options={})
297293 describe "color" do
298294 it "should set fill color when :text_color is provided" do
299295 pdf = Prawn ::Document . new
300- pdf . stubs ( :fill_color )
301- pdf . expects ( :fill_color ) . with ( '555555' )
296+ allow ( pdf ) . to receive ( :fill_color )
297+ expect ( pdf ) . to receive ( :fill_color ) . with ( '555555' )
302298 pdf . cell :content => 'foo' , :text_color => '555555'
303299 end
304300
@@ -312,75 +308,95 @@ def cell(options={})
312308
313309 describe "Borders" do
314310 it "should draw all borders by default" do
315- @pdf . expects ( :stroke_line ) . times ( 4 )
311+ expect ( @pdf ) . to receive ( :stroke_line ) . exactly ( 4 ) . times
316312 @pdf . cell ( :content => "text" )
317313 end
318314
319315 it "should draw all borders when requested" do
320- @pdf . expects ( :stroke_line ) . times ( 4 )
316+ expect ( @pdf ) . to receive ( :stroke_line ) . exactly ( 4 ) . times
321317 @pdf . cell ( :content => "text" , :borders => [ :top , :right , :bottom , :left ] )
322318 end
323319
324320 # Only roughly verifying the integer coordinates so that we don't have to
325321 # do any FP closeness arithmetic. Can plug in that math later if this goes
326322 # wrong.
327323 it "should draw top border when requested" do
328- @pdf . expects ( :stroke_line ) . checking do |from , to |
329- expect ( @pdf . map_to_absolute ( from ) . map { |x | x . round } ) . to eq [ 36 , 756 ]
330- expect ( @pdf . map_to_absolute ( to ) . map { |x | x . round } ) . to eq [ 65 , 756 ]
331- end
324+ expect ( @pdf ) . to receive ( :stroke_line )
325+ . and_wrap_original do |original_method , *args , &block |
326+ from , to , = args
327+ expect ( @pdf . map_to_absolute ( from ) . map { |x | x . round } ) . to eq [ 36 , 756 ]
328+ expect ( @pdf . map_to_absolute ( to ) . map { |x | x . round } ) . to eq [ 65 , 756 ]
329+
330+ original_method . call ( *args , &block )
331+ end
332332 @pdf . cell ( :content => "text" , :borders => [ :top ] )
333333 end
334334
335335 it "should draw bottom border when requested" do
336- @pdf . expects ( :stroke_line ) . checking do |from , to |
337- expect ( @pdf . map_to_absolute ( from ) . map { |x | x . round } ) . to eq [ 36 , 732 ]
338- expect ( @pdf . map_to_absolute ( to ) . map { |x | x . round } ) . to eq [ 65 , 732 ]
339- end
336+ expect ( @pdf ) . to receive ( :stroke_line )
337+ . and_wrap_original do |original_method , *args , &block |
338+ from , to , = args
339+ expect ( @pdf . map_to_absolute ( from ) . map { |x | x . round } ) . to eq [ 36 , 732 ]
340+ expect ( @pdf . map_to_absolute ( to ) . map { |x | x . round } ) . to eq [ 65 , 732 ]
341+
342+ original_method . call ( *args , &block )
343+ end
340344 @pdf . cell ( :content => "text" , :borders => [ :bottom ] )
341345 end
342346
343347 it "should draw left border when requested" do
344- @pdf . expects ( :stroke_line ) . checking do |from , to |
345- expect ( @pdf . map_to_absolute ( from ) . map { |x | x . round } ) . to eq [ 36 , 756 ]
346- expect ( @pdf . map_to_absolute ( to ) . map { |x | x . round } ) . to eq [ 36 , 732 ]
347- end
348+ expect ( @pdf ) . to receive ( :stroke_line )
349+ . and_wrap_original do |original_method , *args , &block |
350+ from , to , = args
351+ expect ( @pdf . map_to_absolute ( from ) . map { |x | x . round } ) . to eq [ 36 , 756 ]
352+ expect ( @pdf . map_to_absolute ( to ) . map { |x | x . round } ) . to eq [ 36 , 732 ]
353+
354+ original_method . call ( *args , &block )
355+ end
348356 @pdf . cell ( :content => "text" , :borders => [ :left ] )
349357 end
350358
351359 it "should draw right border when requested" do
352- @pdf . expects ( :stroke_line ) . checking do |from , to |
353- expect ( @pdf . map_to_absolute ( from ) . map { |x | x . round } ) . to eq [ 65 , 756 ]
354- expect ( @pdf . map_to_absolute ( to ) . map { |x | x . round } ) . to eq [ 65 , 732 ]
355- end
360+ expect ( @pdf ) . to receive ( :stroke_line )
361+ . and_wrap_original do |original_method , *args , &block |
362+ from , to , = args
363+ expect ( @pdf . map_to_absolute ( from ) . map { |x | x . round } ) . to eq [ 65 , 756 ]
364+ expect ( @pdf . map_to_absolute ( to ) . map { |x | x . round } ) . to eq [ 65 , 732 ]
365+
366+ original_method . call ( *args , &block )
367+ end
356368 @pdf . cell ( :content => "text" , :borders => [ :right ] )
357369 end
358370
359371 it "should draw borders at the same location when in or out of bbox" do
360- @pdf . expects ( :stroke_line ) . checking do |from , to |
361- expect ( @pdf . map_to_absolute ( from ) . map { |x | x . round } ) . to eq [ 36 , 756 ]
362- expect ( @pdf . map_to_absolute ( to ) . map { |x | x . round } ) . to eq [ 65 , 756 ]
363- end
372+ expect ( @pdf ) . to receive ( :stroke_line )
373+ . and_wrap_original do |original_method , *args , &block |
374+ from , to , = args
375+ expect ( @pdf . map_to_absolute ( from ) . map { |x | x . round } ) . to eq [ 36 , 756 ]
376+ expect ( @pdf . map_to_absolute ( to ) . map { |x | x . round } ) . to eq [ 65 , 756 ]
377+
378+ original_method . call ( *args , &block )
379+ end
364380 @pdf . bounding_box ( [ 0 , @pdf . cursor ] , :width => @pdf . bounds . width ) do
365381 @pdf . cell ( :content => "text" , :borders => [ :top ] )
366382 end
367383 end
368384
369385 it "should set border color with :border_..._color" do
370- @pdf . ignores ( :stroke_color= ) . with ( "000000" )
371- @pdf . expects ( :stroke_color= ) . with ( "ff0000" )
386+ allow ( @pdf ) . to receive ( :stroke_color= ) . with ( "000000" )
387+ expect ( @pdf ) . to receive ( :stroke_color= ) . with ( "ff0000" )
372388
373389 c = @pdf . cell ( :content => "text" , :border_top_color => "ff0000" )
374390 expect ( c . border_top_color ) . to eq "ff0000"
375391 expect ( c . border_colors [ 0 ] ) . to eq "ff0000"
376392 end
377393
378394 it "should set border colors with :border_color" do
379- @pdf . ignores ( :stroke_color= ) . with ( "000000" )
380- @pdf . expects ( :stroke_color= ) . with ( "ff0000" )
381- @pdf . expects ( :stroke_color= ) . with ( "00ff00" )
382- @pdf . expects ( :stroke_color= ) . with ( "0000ff" )
383- @pdf . expects ( :stroke_color= ) . with ( "ff00ff" )
395+ allow ( @pdf ) . to receive ( :stroke_color= ) . with ( "000000" )
396+ expect ( @pdf ) . to receive ( :stroke_color= ) . with ( "ff0000" )
397+ expect ( @pdf ) . to receive ( :stroke_color= ) . with ( "00ff00" )
398+ expect ( @pdf ) . to receive ( :stroke_color= ) . with ( "0000ff" )
399+ expect ( @pdf ) . to receive ( :stroke_color= ) . with ( "ff00ff" )
384400
385401 c = @pdf . cell ( :content => "text" ,
386402 :border_color => %w[ ff0000 00ff00 0000ff ff00ff ] )
@@ -394,20 +410,20 @@ def cell(options={})
394410 end
395411
396412 it "should set border width with :border_..._width" do
397- @pdf . ignores ( :line_width= ) . with ( 1 )
398- @pdf . expects ( :line_width= ) . with ( 2 )
413+ allow ( @pdf ) . to receive ( :line_width= ) . with ( 1 )
414+ expect ( @pdf ) . to receive ( :line_width= ) . with ( 2 )
399415
400416 c = @pdf . cell ( :content => "text" , :border_bottom_width => 2 )
401417 expect ( c . border_bottom_width ) . to eq 2
402418 expect ( c . border_widths [ 2 ] ) . to eq 2
403419 end
404420
405421 it "should set border widths with :border_width" do
406- @pdf . ignores ( :line_width= ) . with ( 1 )
407- @pdf . expects ( :line_width= ) . with ( 2 )
408- @pdf . expects ( :line_width= ) . with ( 3 )
409- @pdf . expects ( :line_width= ) . with ( 4 )
410- @pdf . expects ( :line_width= ) . with ( 5 )
422+ allow ( @pdf ) . to receive ( :line_width= ) . with ( 1 )
423+ expect ( @pdf ) . to receive ( :line_width= ) . with ( 2 )
424+ expect ( @pdf ) . to receive ( :line_width= ) . with ( 3 )
425+ expect ( @pdf ) . to receive ( :line_width= ) . with ( 4 )
426+ expect ( @pdf ) . to receive ( :line_width= ) . with ( 5 )
411427
412428 c = @pdf . cell ( :content => "text" ,
413429 :border_width => [ 2 , 3 , 4 , 5 ] )
@@ -449,10 +465,8 @@ def cell(options={})
449465
450466 box = Prawn ::Text ::Box . new ( "text" , :document => @pdf )
451467
452- Prawn ::Text ::Box . expects ( :new ) . checking do |text , options |
453- expect ( text ) . to eq "text"
454- expect ( options [ :align ] ) . to eq :right
455- end . at_least_once . returns ( box )
468+ expect ( Prawn ::Text ::Box ) . to receive ( :new ) . with ( "text" , hash_including ( align : :right ) )
469+ . at_least ( :once ) . and_return ( box )
456470
457471 c . draw
458472 end
@@ -462,10 +476,8 @@ def cell(options={})
462476
463477 box = Prawn ::Text ::Box . new ( "text" , :document => @pdf )
464478
465- Prawn ::Text ::Box . expects ( :new ) . checking do |text , options |
466- expect ( text ) . to eq "text"
467- expect ( options [ :style ] ) . to eq :bold
468- end . at_least_once . returns ( box )
479+ expect ( Prawn ::Text ::Box ) . to receive ( :new ) . with ( "text" , hash_including ( style : :bold ) )
480+ . at_least ( :once ) . and_return ( box )
469481
470482 c . draw
471483 end
@@ -476,11 +488,14 @@ def cell(options={})
476488 c = cell ( :content => "text" , :font_style => :bold )
477489
478490 box = Prawn ::Text ::Box . new ( "text" , :document => @pdf )
479- Prawn ::Text ::Box . expects ( :new ) . checking do |text , options |
480- expect ( text ) . to eq "text"
481- expect ( options [ :style ] ) . to eq :bold
482- expect ( @pdf . font . family ) . to eq 'Courier'
483- end . at_least_once . returns ( box )
491+ expect ( Prawn ::Text ::Box ) . to receive ( :new )
492+ . and_wrap_original do |original_method , *args , &block |
493+ text , options , = args
494+ expect ( text ) . to eq "text"
495+ expect ( options [ :style ] ) . to eq :bold
496+ expect ( @pdf . font . family ) . to eq 'Courier'
497+ box
498+ end . at_least ( :once )
484499
485500 c . draw
486501 end
@@ -492,11 +507,14 @@ def cell(options={})
492507 c = cell ( :content => "text" )
493508
494509 box = Prawn ::Text ::Box . new ( "text" , :document => @pdf )
495- Prawn ::Text ::Box . expects ( :new ) . checking do |text , options |
496- expect ( text ) . to eq "text"
497- expect ( @pdf . font . family ) . to eq 'Courier'
498- expect ( @pdf . font . options [ :style ] ) . to eq :bold
499- end . at_least_once . returns ( box )
510+ expect ( Prawn ::Text ::Box ) . to receive ( :new )
511+ . and_wrap_original do |original_method , *args , &block |
512+ text = args . first
513+ expect ( text ) . to eq "text"
514+ expect ( @pdf . font . family ) . to eq 'Courier'
515+ expect ( @pdf . font . options [ :style ] ) . to eq :bold
516+ box
517+ end . at_least ( :once )
500518
501519 c . draw
502520 end
@@ -506,16 +524,14 @@ def cell(options={})
506524
507525 box = Prawn ::Text ::Formatted ::Box . new ( [ ] , :document => @pdf )
508526
509- Prawn ::Text ::Formatted ::Box . expects ( :new ) . checking do |array , options |
510- expect ( array [ 0 ] [ :text ] ) . to eq "foo "
511- expect ( array [ 0 ] [ :styles ] ) . to eq [ ]
512-
513- expect ( array [ 1 ] [ :text ] ) . to eq "bar"
514- expect ( array [ 1 ] [ :styles ] ) . to eq [ :bold ]
515-
516- expect ( array [ 2 ] [ :text ] ) . to eq " baz"
517- expect ( array [ 2 ] [ :styles ] ) . to eq [ ]
518- end . at_least_once . returns ( box )
527+ expect ( Prawn ::Text ::Formatted ::Box ) . to receive ( :new ) . with (
528+ [
529+ hash_including ( text : "foo " , styles : [ ] ) ,
530+ hash_including ( text : "bar" , styles : [ :bold ] ) ,
531+ hash_including ( text : " baz" , styles : [ ] )
532+ ] ,
533+ kind_of ( Hash )
534+ ) . at_least ( :once ) . and_return ( box )
519535
520536 c . draw
521537 end
@@ -609,14 +625,17 @@ def cell(options={})
609625 end
610626
611627 it "should pass through image options" do
612- @pdf . expects ( :embed_image ) . checking do |_ , _ , options |
613- expect ( options [ :scale ] ) . to eq 2
614- expect ( options [ :fit ] ) . to eq [ 100 , 200 ]
615- expect ( options [ :width ] ) . to eq 123
616- expect ( options [ :height ] ) . to eq 456
617- expect ( options [ :position ] ) . to eq :center
618- expect ( options [ :vposition ] ) . to eq :center
619- end
628+ expect ( @pdf ) . to receive ( :embed_image ) . with (
629+ anything , anything ,
630+ hash_including (
631+ scale : 2 ,
632+ fit : [ 100 , 200 ] ,
633+ width : 123 ,
634+ height : 456 ,
635+ position : :center ,
636+ vposition : :center
637+ )
638+ )
620639
621640 @table . draw
622641 end
0 commit comments