public
Description: Fast, Nimble PDF Writer for Ruby
Homepage: http://prawn.majesticseacreature.com
Clone URL: git://github.com/sandal/prawn.git
prawn / spec / table_spec.rb
100644 131 lines (99 sloc) 3.61 kb
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
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
 
describe "A table's width" do
  it "should equal sum(col_widths)" do
    pdf = Prawn::Document.new
    table = Prawn::Document::Table.new( [%w[ a b c ], %w[d e f]], pdf,
       :widths => { 0 => 50, 1 => 100, 2 => 150 })
 
    table.width.should == 300
  end
 
  it "should calculate unspecified column widths as "+
     "(max(string_width) + 2*horizontal_padding)" do
    pdf = Prawn::Document.new
    hpad, fs = 3, 12
    columns = 2
    table = Prawn::Document::Table.new( [%w[ foo b ], %w[d foobar]], pdf,
      :horizontal_padding => hpad, :font_size => fs)
 
    col0_width = pdf.font_metrics.string_width("foo",fs)
    col1_width = pdf.font_metrics.string_width("foobar",fs)
 
    table.width.should == col0_width + col1_width + 2*columns*hpad
  end
 
  it "should allow mixing autocalculated and preset"+
     "column widths within a single table" do
 
    pdf = Prawn::Document.new
    hpad, fs = 10, 6
    stretchy_columns = 2
    
    col0_width = 50
    col1_width = pdf.font_metrics.string_width("foo",fs)
    col2_width = pdf.font_metrics.string_width("foobar",fs)
    col3_width = 150
 
    table = Prawn::Document::Table.new( [%w[snake foo b apple],
                                         %w[kitten d foobar banana]], pdf,
      :horizontal_padding => hpad, :font_size => fs,
      :widths => { 0 => col0_width, 3 => col3_width } )
 
        table.width.should == col1_width + col2_width + 2*stretchy_columns*hpad +
                              col0_width + col3_width
 
  end
 
  it "should paginate large tables" do
    # 30 rows fit on the table with default setting, 31 exceed.
    data = [["foo"]] * 31
    pdf = Prawn::Document.new
 
    pdf.table data
    pdf.page_count.should == 2
 
    pdf.table data
    pdf.page_count.should == 3
  end
 
  it "should have a height of n rows + vertical padding" do
    data = [["foo"],["bar"],["baaaz"]]
    pdf = Prawn::Document.new
    num_rows = data.length
    vpad = 4
    
    origin = pdf.y
    pdf.table data, :vertical_padding => vpad
 
    table_height = origin - pdf.y
 
    font_height = pdf.font_metrics.font_height(12)
 
    table_height.should be_close(num_rows*font_height + 2*vpad*num_rows + vpad, 0.001)
  end
 
end
 
class TableTextObserver
  attr_accessor :font_settings, :size, :strings
            
  def initialize
    @font_settings = []
    @fonts = {}
    @strings = []
  end
  
  def resource_font(*params)
    @fonts[params[0]] = params[1].basefont
  end
 
  def set_text_font_and_size(*params)
    @font_settings << { :name => @fonts[params[0]], :size => params[1] }
  end
  
  def show_text(*params)
    @strings << params[0]
  end
end
 
 
describe "A table's content" do
 
  it "should output content cell by cell, row by row" do
    data = [["foo","bar"],["baz","bang"]]
    @pdf = Prawn::Document.new
    @pdf.table(data)
    output = observer(TableTextObserver)
    output.strings.should == data.flatten
  end
 
  it "should add headers to output when specified" do
    data = [["foo","bar"],["baz","bang"]]
    headers = %w[a b]
    @pdf = Prawn::Document.new
    @pdf.table(data, :headers => headers)
    output = observer(TableTextObserver)
    output.strings.should == headers + data.flatten
  end
 
  it "should repeat headers across pages" do
    data = [["foo","bar"]]*30
    headers = ["baz","foobar"]
    @pdf = Prawn::Document.new
    @pdf.table(data, :headers => headers)
    output = observer(TableTextObserver)
    output.strings.should == headers + data.flatten[0..-3] + headers +
      data.flatten[-2..-1]
  end
    
end