public
Description: Fast, Nimble PDF Writer for Ruby
Homepage: http://prawn.majesticseacreature.com
Clone URL: git://github.com/sandal/prawn.git
sandal (author)
Thu Jun 26 16:31:06 -0700 2008
commit  eec1dc3021fac4d875e11bbafb9834c401c8851c
tree    1df6aae8e70f7e2b509d3ae72f7f9f7791d20ca8
parent  451da0ab5ef7501b52e0d32bb80235fdefb43449
prawn / spec / table_spec.rb
dc2841af » sandal 2008-06-24 Specs for table width calcu... 1 require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
2
3 describe "A table's width" do
4 it "should equal sum(col_widths)" do
5 pdf = Prawn::Document.new
6 table = Prawn::Document::Table.new( [%w[ a b c ], %w[d e f]], pdf,
7 :widths => { 0 => 50, 1 => 100, 2 => 150 })
8
9 table.width.should == 300
10 end
11
12 it "should calculate unspecified column widths as "+
13 "(max(string_width) + 2*horizontal_padding)" do
14 pdf = Prawn::Document.new
15 hpad, fs = 3, 12
16 columns = 2
17 table = Prawn::Document::Table.new( [%w[ foo b ], %w[d foobar]], pdf,
18 :horizontal_padding => hpad, :font_size => fs)
19
20 col0_width = pdf.font_metrics.string_width("foo",fs)
21 col1_width = pdf.font_metrics.string_width("foobar",fs)
22
23 table.width.should == col0_width + col1_width + 2*columns*hpad
24 end
25
26 it "should allow mixing autocalculated and preset"+
27 "column widths within a single table" do
28
29 pdf = Prawn::Document.new
30 hpad, fs = 10, 6
31 stretchy_columns = 2
32
33 col0_width = 50
34 col1_width = pdf.font_metrics.string_width("foo",fs)
35 col2_width = pdf.font_metrics.string_width("foobar",fs)
36 col3_width = 150
37
38 table = Prawn::Document::Table.new( [%w[snake foo b apple],
39 %w[kitten d foobar banana]], pdf,
40 :horizontal_padding => hpad, :font_size => fs,
41 :widths => { 0 => col0_width, 3 => col3_width } )
42
43 table.width.should == col1_width + col2_width + 2*stretchy_columns*hpad +
44 col0_width + col3_width
45
46 end
47
290d444e » sandal 2008-06-24 Pagination and height specs... 48 it "should paginate large tables" do
49 # 30 rows fit on the table with default setting, 31 exceed.
50 data = [["foo"]] * 31
51 pdf = Prawn::Document.new
52
53 pdf.table data
54 pdf.page_count.should == 2
55
56 pdf.table data
57 pdf.page_count.should == 3
58 end
59
60 it "should have a height of n rows + vertical padding" do
61 data = [["foo"],["bar"],["baaaz"]]
62 pdf = Prawn::Document.new
63 num_rows = data.length
64 vpad = 4
65
66 origin = pdf.y
67 pdf.table data, :vertical_padding => vpad
68
69 table_height = origin - pdf.y
70
71 font_height = pdf.font_metrics.font_height(12)
72
73 table_height.should be_close(num_rows*font_height + 2*vpad*num_rows + vpad, 0.001)
74 end
75
dc2841af » sandal 2008-06-24 Specs for table width calcu... 76 end
dce12d86 » sandal 2008-06-25 Added cursory tests for con... 77
78 class TableTextObserver
79 attr_accessor :font_settings, :size, :strings
80
81 def initialize
82 @font_settings = []
83 @fonts = {}
84 @strings = []
85 end
86
87 def resource_font(*params)
88 @fonts[params[0]] = params[1].basefont
89 end
90
91 def set_text_font_and_size(*params)
92 @font_settings << { :name => @fonts[params[0]], :size => params[1] }
93 end
94
95 def show_text(*params)
96 @strings << params[0]
97 end
98 end
99
100
101 describe "A table's content" do
102
103 it "should output content cell by cell, row by row" do
104 data = [["foo","bar"],["baz","bang"]]
105 @pdf = Prawn::Document.new
106 @pdf.table(data)
107 output = observer(TableTextObserver)
108 output.strings.should == data.flatten
109 end
110
111 it "should add headers to output when specified" do
112 data = [["foo","bar"],["baz","bang"]]
113 headers = %w[a b]
114 @pdf = Prawn::Document.new
115 @pdf.table(data, :headers => headers)
116 output = observer(TableTextObserver)
117 output.strings.should == headers + data.flatten
118 end
119
120 it "should repeat headers across pages" do
121 data = [["foo","bar"]]*30
122 headers = ["baz","foobar"]
123 @pdf = Prawn::Document.new
124 @pdf.table(data, :headers => headers)
125 output = observer(TableTextObserver)
126 output.strings.should == headers + data.flatten[0..-3] + headers +
127 data.flatten[-2..-1]
128 end
129
130 end