ruport 1.6.3 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,354 +0,0 @@
1
- #!/usr/bin/env ruby -w
2
- require File.join(File.expand_path(File.dirname(__FILE__)), "helpers")
3
-
4
- class TestRenderPDFTable < Test::Unit::TestCase
5
-
6
- def setup
7
- Ruport::Formatter::Template.create(:simple) do |format|
8
- format.page = {
9
- :size => "LETTER",
10
- :layout => :landscape
11
- }
12
- format.text = {
13
- :font_size => 16
14
- }
15
- format.table = {
16
- :show_headings => false
17
- }
18
- format.column = {
19
- :alignment => :center,
20
- :width => 50
21
- }
22
- format.heading = {
23
- :alignment => :right,
24
- :bold => false,
25
- :title => "Test"
26
- }
27
- format.grouping = {
28
- :style => :separated
29
- }
30
- end
31
- end
32
-
33
- def test_render_pdf_basic
34
- # can't render without column names
35
- data = Table([], :data => [[1,2],[3,4]])
36
- assert_raise(Ruport::FormatterError) do
37
- data.to_pdf
38
- end
39
-
40
- data.column_names = %w[a b]
41
- assert_nothing_raised { data.to_pdf }
42
-
43
- assert_nothing_raised { Table(%w[a b c]).to_pdf }
44
- end
45
-
46
- # this is mostly to check that the transaction hack gets called
47
- def test_relatively_large_pdf
48
- table = Table(File.join(File.expand_path(File.dirname(__FILE__)),
49
- %w[samples dates.csv]))
50
- table.reduce(0..99)
51
- assert_nothing_raised { table.to_pdf }
52
- end
53
-
54
- # this is just to make sure that the column_opts code is being called.
55
- # FIXME: add mocks to be sure
56
- def test_table_with_options
57
- data = Table(%w[a b], :data => [[1,2],[3,4]])
58
- assert_nothing_raised do
59
- data.to_pdf(:table_format => {
60
- :column_options => { :justification => :center } } )
61
- end
62
- end
63
-
64
- def test_render_with_template
65
- formatter = Ruport::Formatter::PDF.new
66
- formatter.options = Ruport::Controller::Options.new
67
- formatter.options.template = :simple
68
- formatter.apply_template
69
-
70
- assert_equal "LETTER", formatter.options.paper_size
71
- assert_equal :landscape, formatter.options.paper_orientation
72
-
73
- assert_equal 16, formatter.options.text_format[:font_size]
74
-
75
- assert_equal false, formatter.options.table_format[:show_headings]
76
-
77
- assert_equal :center,
78
- formatter.options.table_format[:column_options][:justification]
79
- assert_equal 50,
80
- formatter.options.table_format[:column_options][:width]
81
-
82
- assert_equal :right,
83
- formatter.options.table_format[:column_options][:heading][:justification]
84
- assert_equal false,
85
- formatter.options.table_format[:column_options][:heading][:bold]
86
- assert_equal "Test",
87
- formatter.options.table_format[:column_options][:heading][:title]
88
-
89
- assert_equal :separated, formatter.options.style
90
- end
91
-
92
- def test_options_hashes_override_template
93
- opts = nil
94
- table = Table(%w[a b c])
95
- table.to_pdf(
96
- :template => :simple,
97
- :page_format => {
98
- :size => "LEGAL",
99
- :layout => :portrait
100
- },
101
- :text_format => {
102
- :font_size => 20
103
- },
104
- :table_format => {
105
- :show_headings => true
106
- },
107
- :column_format => {
108
- :alignment => :left,
109
- :width => 25
110
- },
111
- :heading_format => {
112
- :alignment => :left,
113
- :bold => true,
114
- :title => "Replace"
115
- },
116
- :grouping_format => {
117
- :style => :inline
118
- }
119
- ) do |r|
120
- opts = r.options
121
- end
122
-
123
- assert_equal "LEGAL", opts.paper_size
124
- assert_equal :portrait, opts.paper_orientation
125
-
126
- assert_equal 20, opts.text_format[:font_size]
127
-
128
- assert_equal true, opts.table_format[:show_headings]
129
-
130
- assert_equal :left, opts.table_format[:column_options][:justification]
131
- assert_equal 25, opts.table_format[:column_options][:width]
132
-
133
- assert_equal :left,
134
- opts.table_format[:column_options][:heading][:justification]
135
- assert_equal true, opts.table_format[:column_options][:heading][:bold]
136
- assert_equal "Replace", opts.table_format[:column_options][:heading][:title]
137
-
138
- assert_equal :inline, opts.style
139
- end
140
-
141
- def test_individual_options_override_template
142
- opts = nil
143
- table = Table(%w[a b c])
144
- table.to_pdf(
145
- :template => :simple,
146
- :paper_size => "LEGAL",
147
- :paper_orientation => :portrait,
148
- :text_format => { :font_size => 20 },
149
- :table_format => {
150
- :show_headings => true,
151
- :column_options => {
152
- :justification => :left,
153
- :width => 25,
154
- :heading => {
155
- :justification => :left,
156
- :bold => true,
157
- :title => "Replace"
158
- }
159
- }
160
- },
161
- :style => :inline
162
- ) do |r|
163
- opts = r.options
164
- end
165
-
166
- assert_equal "LEGAL", opts.paper_size
167
- assert_equal :portrait, opts.paper_orientation
168
-
169
- assert_equal 20, opts.text_format[:font_size]
170
-
171
- assert_equal true, opts.table_format[:show_headings]
172
-
173
- assert_equal :left, opts.table_format[:column_options][:justification]
174
- assert_equal 25, opts.table_format[:column_options][:width]
175
-
176
- assert_equal :left,
177
- opts.table_format[:column_options][:heading][:justification]
178
- assert_equal true, opts.table_format[:column_options][:heading][:bold]
179
- assert_equal "Replace", opts.table_format[:column_options][:heading][:title]
180
-
181
- assert_equal :inline, opts.style
182
- end
183
-
184
- #--------BUG TRAPS--------#
185
-
186
- # PDF::SimpleTable does not handle symbols as column names
187
- # Ruport should smartly fix this surprising behaviour (#283)
188
- def test_tables_should_render_with_symbol_column_name
189
- data = Table([:a,:b,:c], :data => [[1,2,3],[4,5,6]])
190
- assert_nothing_raised { data.to_pdf }
191
- end
192
-
193
- # draw_table has destructive behavior on nested rendering options (#359)
194
- def test_draw_table_should_not_destroy_nested_rendering_options
195
- f = Ruport::Formatter::PDF.new
196
- f.options = Ruport::Controller::Options.new
197
- f.options[:table_format] =
198
- { :column_options => { :heading => {:justification => :center }}}
199
- f.draw_table Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
200
- assert_equal({ :justification => :center },
201
- f.options[:table_format][:column_options][:heading])
202
- end
203
-
204
- end
205
-
206
- class TestRenderPDFGrouping < Test::Unit::TestCase
207
-
208
- #--------BUG TRAPS----------#
209
-
210
- # As of Ruport 0.10.0, PDF's justified group output was throwing
211
- # UnknownFormatError (#288)
212
- def test_group_styles_should_not_throw_error
213
- table = Table(%w[a b c], :data => [[1,2,3],[4,5,6],[1,7,9]])
214
- grouping = Grouping(table,:by => "a")
215
- assert_nothing_raised { grouping.to_pdf }
216
- assert_nothing_raised { grouping.to_pdf(:style => :inline) }
217
- assert_nothing_raised { grouping.to_pdf(:style => :offset) }
218
- assert_nothing_raised { grouping.to_pdf(:style => :justified) }
219
- assert_nothing_raised { grouping.to_pdf(:style => :separated) }
220
- assert_raises(NotImplementedError) do
221
- grouping.to_pdf(:style => :red_snapper)
222
- end
223
- end
224
-
225
- def test_grouping_should_have_consistent_font_size
226
- a = Table(%w[a b c]) << %w[eye like chicken] << %w[eye like liver] <<
227
- %w[meow mix meow ] << %w[mix please deliver ]
228
- b = Grouping(a, :by => "a")
229
- splat = b.to_pdf.split("\n")
230
- splat.grep(/meow/).each do |m|
231
- assert_equal '10.0', m.split[5]
232
- end
233
- splat.grep(/mix/).each do |m|
234
- assert_equal '10.0', m.split[5]
235
- end
236
- splat.grep(/eye/).each do |m|
237
- assert_equal '10.0', m.split[5]
238
- end
239
- end
240
-
241
- end
242
-
243
- class TestPDFFormatterHelpers < Test::Unit::TestCase
244
-
245
- def test_boundaries
246
- a = Ruport::Formatter::PDF.new
247
-
248
- assert_equal 36, a.left_boundary
249
- a.pdf_writer.left_margin = 50
250
- assert_equal 50, a.left_boundary
251
-
252
- assert_equal 576, a.right_boundary
253
- a.pdf_writer.right_margin -= 10
254
- assert_equal 586, a.right_boundary
255
-
256
- assert_equal 756, a.top_boundary
257
- a.pdf_writer.top_margin -= 10
258
- assert_equal 766, a.top_boundary
259
-
260
- assert_equal 36, a.bottom_boundary
261
- a.pdf_writer.bottom_margin -= 10
262
- assert_equal 26, a.bottom_boundary
263
- end
264
-
265
- def test_move_cursor
266
- a = Ruport::Formatter::PDF.new
267
- a.move_cursor_to(500)
268
- assert_equal(500,a.cursor)
269
- a.move_cursor(-25)
270
- assert_equal(475,a.cursor)
271
- a.move_cursor(50)
272
- assert_equal(525,a.cursor)
273
- end
274
-
275
- def test_move_up
276
- a = Ruport::Formatter::PDF.new
277
- a.move_cursor_to(500)
278
- a.move_up(50)
279
- assert_equal(550,a.cursor)
280
- a.move_down(100)
281
- assert_equal(450,a.cursor)
282
- end
283
-
284
- def test_padding
285
- a = Ruport::Formatter::PDF.new
286
- a.move_cursor_to(100)
287
-
288
- # padding on top and bottom
289
- a.pad(10) do
290
- assert_equal 90, a.cursor
291
- a.move_cursor(-10)
292
- assert_equal 80, a.cursor
293
- end
294
- assert_equal(70,a.cursor)
295
-
296
- a.move_cursor_to(100)
297
-
298
- # padding just on top
299
- a.pad_top(10) do
300
- assert_equal 90, a.cursor
301
- a.move_cursor(-10)
302
- assert_equal 80, a.cursor
303
- end
304
-
305
- assert_equal 80, a.cursor
306
-
307
- a.move_cursor_to(100)
308
-
309
- # padding just on bottom
310
- a.pad_bottom(10) do
311
- assert_equal 100, a.cursor
312
- a.move_cursor(-10)
313
- assert_equal 90, a.cursor
314
- end
315
-
316
- assert_equal 80, a.cursor
317
- end
318
-
319
- def test_draw_text_retains_cursor
320
- a = Ruport::Formatter::PDF.new
321
- a.move_cursor_to(100)
322
-
323
- a.draw_text "foo", :left => a.left_boundary
324
- assert_equal 100, a.cursor
325
-
326
- a.draw_text "foo", :left => a.left_boundary + 50, :y => 500
327
- assert_equal 100, a.cursor
328
- end
329
- end
330
-
331
- class SimpleController < Ruport::Controller
332
- stage :foo
333
-
334
- class PDF < Ruport::Formatter::PDF
335
- renders :pdf, :for => SimpleController
336
-
337
- build :foo do
338
- add_text "Blah"
339
- end
340
- end
341
- end
342
-
343
- class TestPDFFinalize < Test::Unit::TestCase
344
-
345
- context "When rendering a PDF" do
346
- def specify_finalize_should_be_called
347
- SimpleController.render_pdf do |r|
348
- r.formatter.expects(:render_pdf)
349
- end
350
- end
351
- end
352
-
353
- end
354
-