ruport 1.4.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +3 -3
- data/examples/anon.rb +43 -0
- data/examples/btree/commaleon/commaleon.rb +9 -9
- data/examples/centered_pdf_text_box.rb +7 -7
- data/examples/line_plotter.rb +1 -1
- data/examples/pdf_report_with_common_base.rb +4 -4
- data/examples/png_embed.rb +4 -4
- data/examples/row_renderer.rb +4 -4
- data/examples/simple_pdf_lines.rb +1 -1
- data/examples/trac_ticket_status.rb +1 -1
- data/lib/ruport.rb +15 -3
- data/lib/ruport/{renderer.rb → controller.rb} +183 -87
- data/lib/ruport/{renderer → controller}/grouping.rb +5 -5
- data/lib/ruport/{renderer → controller}/table.rb +5 -5
- data/lib/ruport/data/grouping.rb +2 -2
- data/lib/ruport/data/record.rb +4 -2
- data/lib/ruport/data/table.rb +128 -4
- data/lib/ruport/formatter.rb +36 -37
- data/lib/ruport/formatter/csv.rb +37 -20
- data/lib/ruport/formatter/html.rb +11 -12
- data/lib/ruport/formatter/pdf.rb +10 -6
- data/lib/ruport/formatter/template.rb +1 -1
- data/lib/ruport/formatter/text.rb +8 -15
- data/test/{renderer_test.rb → controller_test.rb} +179 -74
- data/test/csv_formatter_test.rb +6 -6
- data/test/grouping_test.rb +4 -4
- data/test/helpers.rb +1 -0
- data/test/html_formatter_test.rb +18 -18
- data/test/pdf_formatter_test.rb +28 -3
- data/test/record_test.rb +2 -2
- data/test/table_pivot_test.rb +134 -0
- data/test/table_test.rb +10 -3
- data/test/text_formatter_test.rb +6 -6
- data/util/bench/data/record/bench_as_vs_to.rb +1 -1
- metadata +19 -16
@@ -12,7 +12,7 @@
|
|
12
12
|
#
|
13
13
|
module Ruport
|
14
14
|
# This class produces HTML output for Ruport's Row, Table, Group, and
|
15
|
-
# Grouping
|
15
|
+
# Grouping controllers. It can be subclassed, as it has some helper methods
|
16
16
|
# that might be useful for custom output.
|
17
17
|
#
|
18
18
|
# === Rendering Options
|
@@ -25,8 +25,8 @@ module Ruport
|
|
25
25
|
#
|
26
26
|
class Formatter::HTML < Formatter
|
27
27
|
|
28
|
-
renders :html, :for => [
|
29
|
-
|
28
|
+
renders :html, :for => [ Controller::Row, Controller::Table,
|
29
|
+
Controller::Group, Controller::Grouping ]
|
30
30
|
|
31
31
|
# Hook for setting available options using a template. See the template
|
32
32
|
# documentation for the available options and their format.
|
@@ -48,22 +48,21 @@ module Ruport
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
# Uses the Row
|
51
|
+
# Uses the Row controller to build up the table body.
|
52
52
|
# Replaces nil and empty strings with " "
|
53
53
|
def build_table_body
|
54
|
-
|
55
|
-
|
56
|
-
rend.data = r.map { |e| e.to_s.empty? ? " " : e }
|
54
|
+
data.each do |row|
|
55
|
+
build_row(row.map { |e| e.to_s.empty? ? " " : e })
|
57
56
|
end
|
58
57
|
end
|
59
58
|
|
60
59
|
# Simply closes the table tag.
|
61
60
|
def build_table_footer
|
62
|
-
output << "\t</table
|
61
|
+
output << "\t</table>\n"
|
63
62
|
end
|
64
63
|
|
65
64
|
# Renders individual rows for the table.
|
66
|
-
def build_row
|
65
|
+
def build_row(data = self.data)
|
67
66
|
output <<
|
68
67
|
"\t\t<tr>\n\t\t\t<td>" +
|
69
68
|
data.to_a.join("</td>\n\t\t\t<td>") +
|
@@ -77,14 +76,14 @@ module Ruport
|
|
77
76
|
end
|
78
77
|
|
79
78
|
# Creates the group body. Since group data is a table, just uses the
|
80
|
-
# Table
|
79
|
+
# Table controller.
|
81
80
|
#
|
82
81
|
def build_group_body
|
83
82
|
render_table data, options.to_hash
|
84
83
|
end
|
85
84
|
|
86
85
|
# Generates the body for a grouping. Iterates through the groups and
|
87
|
-
# renders them using the group
|
86
|
+
# renders them using the group controller.
|
88
87
|
#
|
89
88
|
def build_grouping_body
|
90
89
|
case options.style
|
@@ -138,7 +137,7 @@ module Ruport
|
|
138
137
|
"</td>\n\t\t</tr>\n"
|
139
138
|
end
|
140
139
|
end
|
141
|
-
output << "\t</table
|
140
|
+
output << "\t</table>\n"
|
142
141
|
end
|
143
142
|
|
144
143
|
def grouping_columns
|
data/lib/ruport/formatter/pdf.rb
CHANGED
@@ -15,7 +15,7 @@
|
|
15
15
|
module Ruport
|
16
16
|
|
17
17
|
# This class provides PDF output for Ruport's Table, Group, and Grouping
|
18
|
-
#
|
18
|
+
# controllers. It wraps Austin Ziegler's PDF::Writer to provide a higher
|
19
19
|
# level interface and provides a number of helpers designed to make
|
20
20
|
# generating PDF reports much easier. You will typically want to build
|
21
21
|
# subclasses of this formatter to customize it as needed.
|
@@ -49,8 +49,8 @@ module Ruport
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
renders :pdf, :for => [
|
53
|
-
|
52
|
+
renders :pdf, :for => [ Controller::Row, Controller::Table,
|
53
|
+
Controller::Group, Controller::Grouping ]
|
54
54
|
|
55
55
|
attr_writer :pdf_writer
|
56
56
|
|
@@ -104,18 +104,18 @@ module Ruport
|
|
104
104
|
render_pdf unless options.skip_finalize_table
|
105
105
|
end
|
106
106
|
|
107
|
-
# Generates a header with the group name for
|
107
|
+
# Generates a header with the group name for Controller::Group.
|
108
108
|
def build_group_header
|
109
109
|
pad(10) { add_text data.name.to_s, :justification => :center }
|
110
110
|
end
|
111
111
|
|
112
|
-
# Renders the group as a table for
|
112
|
+
# Renders the group as a table for Controller::Group.
|
113
113
|
def build_group_body
|
114
114
|
render_table data, options.to_hash.merge(:formatter => pdf_writer)
|
115
115
|
end
|
116
116
|
|
117
117
|
# Determines which style to use and renders the main body for
|
118
|
-
#
|
118
|
+
# Controller::Grouping.
|
119
119
|
def build_grouping_body
|
120
120
|
case options.style
|
121
121
|
when :inline
|
@@ -382,6 +382,10 @@ module Ruport
|
|
382
382
|
text_opts[:font_size],
|
383
383
|
text_opts[:angle] || 0)
|
384
384
|
move_cursor_to(ypos)
|
385
|
+
end
|
386
|
+
|
387
|
+
def finalize
|
388
|
+
render_pdf
|
385
389
|
end
|
386
390
|
end
|
387
391
|
|
@@ -142,7 +142,7 @@ class Ruport::Formatter::TemplateNotDefined < StandardError; end
|
|
142
142
|
# available to
|
143
143
|
# FasterCSV.new
|
144
144
|
#
|
145
|
-
class Ruport::Formatter::Template < Ruport::
|
145
|
+
class Ruport::Formatter::Template < Ruport::Controller::Options
|
146
146
|
|
147
147
|
# Returns all existing templates in a hash keyed by the template names.
|
148
148
|
def self.templates
|
@@ -13,7 +13,7 @@
|
|
13
13
|
module Ruport
|
14
14
|
|
15
15
|
# This class provides text output for Ruport's Row, Table, Group, and
|
16
|
-
# Grouping
|
16
|
+
# Grouping controllers
|
17
17
|
#
|
18
18
|
# It handles things like automatically truncating tables that go off the
|
19
19
|
# edge of the screen in the console, proper column alignment, and pretty
|
@@ -45,8 +45,8 @@ module Ruport
|
|
45
45
|
# truncating it. Useful for file output.
|
46
46
|
class Formatter::Text < Formatter
|
47
47
|
|
48
|
-
renders [:txt, :text], :for => [
|
49
|
-
|
48
|
+
renders [:txt, :text], :for => [ Controller::Row, Controller::Table,
|
49
|
+
Controller::Group, Controller::Grouping ]
|
50
50
|
|
51
51
|
# Hook for setting available options using a template. See the template
|
52
52
|
# documentation for the available options and their format.
|
@@ -94,14 +94,7 @@ module Ruport
|
|
94
94
|
|
95
95
|
calculate_max_col_widths unless options.max_col_width
|
96
96
|
|
97
|
-
|
98
|
-
rend.options do |o|
|
99
|
-
o.max_col_width = options.max_col_width
|
100
|
-
o.alignment = options.alignment
|
101
|
-
o.table_width = options.table_width
|
102
|
-
o.ignore_table_width = options.ignore_table_width
|
103
|
-
end
|
104
|
-
end
|
97
|
+
data.each { |row| build_row(row) }
|
105
98
|
|
106
99
|
output << fit_to_width(hr)
|
107
100
|
end
|
@@ -114,7 +107,7 @@ module Ruport
|
|
114
107
|
#
|
115
108
|
# Uses fit_to_width to truncate the row if necessary.
|
116
109
|
#
|
117
|
-
def build_row
|
110
|
+
def build_row(data = self.data)
|
118
111
|
max_col_widths_for_row(data) unless options.max_col_width
|
119
112
|
|
120
113
|
data.enum_for(:each_with_index).inject(line=[]) { |s,e|
|
@@ -136,14 +129,14 @@ module Ruport
|
|
136
129
|
end
|
137
130
|
|
138
131
|
# Creates the group body. Since group data is a table, just uses the
|
139
|
-
# Table
|
132
|
+
# Table controller.
|
140
133
|
#
|
141
134
|
def build_group_body
|
142
135
|
render_table data, options
|
143
136
|
end
|
144
137
|
|
145
138
|
# Generates the body for a grouping. Iterates through the groups and
|
146
|
-
# renders them using the group
|
139
|
+
# renders them using the group controller.
|
147
140
|
#
|
148
141
|
def build_grouping_body
|
149
142
|
render_inline_grouping(options)
|
@@ -170,7 +163,7 @@ module Ruport
|
|
170
163
|
#
|
171
164
|
# Otherwise, uses SystemExtensions to determine terminal width.
|
172
165
|
def width
|
173
|
-
options.table_width
|
166
|
+
options.table_width ||= SystemExtensions.terminal_width
|
174
167
|
end
|
175
168
|
|
176
169
|
# Truncates a string so that it does not exceed Text#width
|
@@ -14,11 +14,11 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "helpers")
|
|
14
14
|
|
15
15
|
#============================================================================
|
16
16
|
# These two renderers represent the two styles that can be used when defining
|
17
|
-
# renderers in Ruport. The
|
17
|
+
# renderers in Ruport. The OldSchoolController approach has largely been
|
18
18
|
# deprecated, but still has uses in edge cases that we need to support.
|
19
19
|
#============================================================================
|
20
20
|
|
21
|
-
class
|
21
|
+
class OldSchoolController < Ruport::Controller
|
22
22
|
|
23
23
|
def run
|
24
24
|
formatter do
|
@@ -30,7 +30,7 @@ class OldSchoolRenderer < Ruport::Renderer
|
|
30
30
|
|
31
31
|
end
|
32
32
|
|
33
|
-
class
|
33
|
+
class VanillaController < Ruport::Controller
|
34
34
|
stage :header,:body,:footer
|
35
35
|
end
|
36
36
|
|
@@ -40,7 +40,7 @@ end
|
|
40
40
|
# be replaced by mock objects in the future.
|
41
41
|
class DummyText < Ruport::Formatter
|
42
42
|
|
43
|
-
renders :text, :for =>
|
43
|
+
renders :text, :for => OldSchoolController
|
44
44
|
|
45
45
|
def prepare_document
|
46
46
|
output << "p"
|
@@ -63,22 +63,54 @@ class DummyText < Ruport::Formatter
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
+
# This formatter modifies the (String) data object passed to it
|
67
|
+
class Destructive < Ruport::Formatter
|
68
|
+
|
69
|
+
def prepare_document; end
|
70
|
+
|
71
|
+
def build_header; end
|
72
|
+
|
73
|
+
def build_body
|
74
|
+
output << "You sent #{data}"
|
75
|
+
data.replace("RUBBISH")
|
76
|
+
end
|
77
|
+
|
78
|
+
def build_footer; end
|
79
|
+
|
80
|
+
def finalize_document; end
|
81
|
+
end
|
82
|
+
|
83
|
+
|
66
84
|
class VanillaBinary < Ruport::Formatter
|
67
|
-
renders :bin, :for =>
|
85
|
+
renders :bin, :for => VanillaController
|
68
86
|
save_as_binary_file
|
69
|
-
end
|
87
|
+
end
|
70
88
|
|
89
|
+
class SpecialFinalize < Ruport::Formatter
|
90
|
+
renders :with_finalize, :for => VanillaController
|
91
|
+
|
92
|
+
def finalize
|
93
|
+
output << "I has been finalized"
|
94
|
+
end
|
95
|
+
end
|
71
96
|
|
72
|
-
class
|
97
|
+
class TestController < Test::Unit::TestCase
|
73
98
|
|
74
99
|
def teardown
|
75
100
|
Ruport::Formatter::Template.instance_variable_set(:@templates, nil)
|
76
101
|
end
|
77
102
|
|
78
103
|
def test_trivial
|
79
|
-
actual =
|
104
|
+
actual = OldSchoolController.render(:text)
|
80
105
|
assert_equal "header\nbody\nfooter\n", actual
|
81
|
-
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "when running a formatter with custom a finalize method" do
|
109
|
+
def specify_finalize_method_should_be_called
|
110
|
+
assert_equal "I has been finalized",
|
111
|
+
VanillaController.render_with_finalize
|
112
|
+
end
|
113
|
+
end
|
82
114
|
|
83
115
|
context "when using templates" do
|
84
116
|
def specify_apply_template_should_be_called
|
@@ -124,7 +156,7 @@ class TestRenderer < Test::Unit::TestCase
|
|
124
156
|
def test_using_io
|
125
157
|
require "stringio"
|
126
158
|
out = StringIO.new
|
127
|
-
a =
|
159
|
+
a = OldSchoolController.render(:text) { |r| r.io = out }
|
128
160
|
out.rewind
|
129
161
|
assert_equal "header\nbody\nfooter\n", out.read
|
130
162
|
assert_equal "", out.read
|
@@ -133,12 +165,12 @@ class TestRenderer < Test::Unit::TestCase
|
|
133
165
|
def test_using_file
|
134
166
|
f = []
|
135
167
|
File.expects(:open).yields(f)
|
136
|
-
a =
|
168
|
+
a = OldSchoolController.render(:text, :file => "foo.text")
|
137
169
|
assert_equal "header\nbody\nfooter\n", f[0]
|
138
170
|
|
139
171
|
f = []
|
140
172
|
File.expects(:open).with("blah","wb").yields(f)
|
141
|
-
|
173
|
+
VanillaController.render(:bin, :file => "blah")
|
142
174
|
end
|
143
175
|
|
144
176
|
def test_using_file_via_rendering_tools
|
@@ -150,25 +182,25 @@ class TestRenderer < Test::Unit::TestCase
|
|
150
182
|
|
151
183
|
|
152
184
|
def test_formats
|
153
|
-
assert_equal( {}, Ruport::
|
154
|
-
assert_equal( { :text => DummyText },
|
185
|
+
assert_equal( {}, Ruport::Controller.formats )
|
186
|
+
assert_equal( { :text => DummyText },OldSchoolController.formats )
|
155
187
|
end
|
156
188
|
|
157
189
|
def test_method_missing
|
158
|
-
actual =
|
190
|
+
actual = OldSchoolController.render_text
|
159
191
|
assert_equal "header\nbody\nfooter\n", actual
|
160
192
|
end
|
161
193
|
|
162
194
|
def test_formatter
|
163
195
|
# normal instance mode
|
164
|
-
rend =
|
196
|
+
rend = OldSchoolController.new
|
165
197
|
rend.send(:use_formatter,:text)
|
166
198
|
|
167
199
|
assert_kind_of Ruport::Formatter, rend.formatter
|
168
200
|
assert_kind_of DummyText, rend.formatter
|
169
201
|
|
170
202
|
# render mode
|
171
|
-
|
203
|
+
OldSchoolController.render_text do |r|
|
172
204
|
assert_kind_of Ruport::Formatter, r.formatter
|
173
205
|
assert_kind_of DummyText, r.formatter
|
174
206
|
end
|
@@ -180,7 +212,7 @@ class TestRenderer < Test::Unit::TestCase
|
|
180
212
|
end
|
181
213
|
|
182
214
|
def test_options_act_like_indifferent_hash
|
183
|
-
opts = Ruport::
|
215
|
+
opts = Ruport::Controller::Options.new
|
184
216
|
opts.foo = "bar"
|
185
217
|
assert_equal "bar", opts[:foo]
|
186
218
|
assert_equal "bar", opts["foo"]
|
@@ -202,7 +234,7 @@ end
|
|
202
234
|
class TestFormatterUsingBuild < Test::Unit::TestCase
|
203
235
|
# This formatter uses the build syntax
|
204
236
|
class UsesBuild < Ruport::Formatter
|
205
|
-
renders :text_using_build, :for =>
|
237
|
+
renders :text_using_build, :for => VanillaController
|
206
238
|
|
207
239
|
build :header do
|
208
240
|
output << "header\n"
|
@@ -219,8 +251,8 @@ class TestFormatterUsingBuild < Test::Unit::TestCase
|
|
219
251
|
|
220
252
|
def test_should_render_using_build_syntax
|
221
253
|
assert_equal "header\nbody\nfooter\n",
|
222
|
-
|
223
|
-
|
254
|
+
VanillaController.render_text_using_build
|
255
|
+
VanillaController.render_text_using_build do |rend|
|
224
256
|
assert rend.formatter.respond_to?(:build_header)
|
225
257
|
assert rend.formatter.respond_to?(:build_body)
|
226
258
|
assert rend.formatter.respond_to?(:build_footer)
|
@@ -233,7 +265,7 @@ class TestFormatterWithLayout < Test::Unit::TestCase
|
|
233
265
|
# This formatter is meant to check out a special case in Ruport's renderer,
|
234
266
|
# in which a layout method is called and yielded to when defined
|
235
267
|
class WithLayout < DummyText
|
236
|
-
renders :text_with_layout, :for =>
|
268
|
+
renders :text_with_layout, :for => VanillaController
|
237
269
|
|
238
270
|
def layout
|
239
271
|
output << "---\n"
|
@@ -245,21 +277,22 @@ class TestFormatterWithLayout < Test::Unit::TestCase
|
|
245
277
|
|
246
278
|
def test_layout
|
247
279
|
assert_equal "---\nheader\nbody\nfooter\n---\n",
|
248
|
-
|
280
|
+
VanillaController.render_text_with_layout
|
249
281
|
end
|
250
282
|
|
251
283
|
def test_layout_disabled
|
252
284
|
assert_equal "header\nbody\nfooter\n",
|
253
|
-
|
285
|
+
VanillaController.render_text_with_layout(:layout => false)
|
254
286
|
end
|
255
287
|
|
256
288
|
end
|
257
289
|
|
258
290
|
|
259
|
-
class
|
260
|
-
# This provides a way to check several hooks that
|
261
|
-
class
|
291
|
+
class TestControllerWithManyHooks < Test::Unit::TestCase
|
292
|
+
# This provides a way to check several hooks that controllers supports
|
293
|
+
class ControllerWithManyHooks < Ruport::Controller
|
262
294
|
add_format DummyText, :text
|
295
|
+
add_format Destructive, :destructive
|
263
296
|
|
264
297
|
prepare :document
|
265
298
|
|
@@ -276,7 +309,7 @@ class TestRendererWithManyHooks < Test::Unit::TestCase
|
|
276
309
|
end
|
277
310
|
|
278
311
|
def test_hash_options_setters
|
279
|
-
a =
|
312
|
+
a = ControllerWithManyHooks.render(:text, :subtitle => "foo",
|
280
313
|
:subsubtitle => "bar") { |r|
|
281
314
|
assert_equal "foo", r.options.subtitle
|
282
315
|
assert_equal "bar", r.options.subsubtitle
|
@@ -284,54 +317,61 @@ class TestRendererWithManyHooks < Test::Unit::TestCase
|
|
284
317
|
end
|
285
318
|
|
286
319
|
def test_data_accessors
|
287
|
-
a =
|
320
|
+
a = ControllerWithManyHooks.render(:text, :data => [1,2,4]) { |r|
|
288
321
|
assert_equal [1,2,4], r.data
|
289
322
|
}
|
290
323
|
|
291
|
-
b =
|
324
|
+
b = ControllerWithManyHooks.render_text(%w[a b c]) { |r|
|
292
325
|
assert_equal %w[a b c], r.data
|
293
326
|
}
|
294
327
|
|
295
|
-
c =
|
328
|
+
c = ControllerWithManyHooks.render_text(%w[a b f],:snapper => :red) { |r|
|
296
329
|
assert_equal %w[a b f], r.data
|
297
330
|
assert_equal :red, r.options.snapper
|
298
331
|
}
|
299
332
|
end
|
300
333
|
|
334
|
+
def test_formatter_data_dup
|
335
|
+
source = "some text"
|
336
|
+
result = ControllerWithManyHooks.render(:destructive, :data => source)
|
337
|
+
assert_equal("You sent some text", result)
|
338
|
+
assert_equal("some text", source)
|
339
|
+
end
|
340
|
+
|
301
341
|
def test_stage_helper
|
302
|
-
assert
|
342
|
+
assert ControllerWithManyHooks.stages.include?('body')
|
303
343
|
end
|
304
344
|
|
305
345
|
def test_finalize_helper
|
306
|
-
assert_equal :document,
|
346
|
+
assert_equal :document, ControllerWithManyHooks.final_stage
|
307
347
|
end
|
308
348
|
|
309
349
|
def test_prepare_helper
|
310
|
-
assert_equal :document,
|
350
|
+
assert_equal :document, ControllerWithManyHooks.first_stage
|
311
351
|
end
|
312
352
|
|
313
353
|
def test_finalize_again
|
314
|
-
assert_raise(Ruport::
|
315
|
-
|
354
|
+
assert_raise(Ruport::Controller::StageAlreadyDefinedError) {
|
355
|
+
ControllerWithManyHooks.finalize :report
|
316
356
|
}
|
317
357
|
end
|
318
358
|
|
319
359
|
def test_prepare_again
|
320
|
-
assert_raise(Ruport::
|
321
|
-
|
360
|
+
assert_raise(Ruport::Controller::StageAlreadyDefinedError) {
|
361
|
+
ControllerWithManyHooks.prepare :foo
|
322
362
|
}
|
323
363
|
end
|
324
364
|
|
325
365
|
def test_renderer_using_helpers
|
326
|
-
actual =
|
366
|
+
actual = ControllerWithManyHooks.render(:text)
|
327
367
|
assert_equal "pheader\nbody\nfooter\nf", actual
|
328
368
|
|
329
|
-
actual =
|
369
|
+
actual = ControllerWithManyHooks.render_text
|
330
370
|
assert_equal "pheader\nbody\nfooter\nf", actual
|
331
371
|
end
|
332
372
|
|
333
373
|
def test_required_option_helper
|
334
|
-
a =
|
374
|
+
a = ControllerWithManyHooks.dup
|
335
375
|
a.required_option :title
|
336
376
|
|
337
377
|
a.render_text do |r|
|
@@ -342,18 +382,18 @@ class TestRendererWithManyHooks < Test::Unit::TestCase
|
|
342
382
|
end
|
343
383
|
|
344
384
|
def test_without_required_option
|
345
|
-
a =
|
385
|
+
a = ControllerWithManyHooks.dup
|
346
386
|
a.required_option :title
|
347
387
|
|
348
|
-
assert_raise(Ruport::
|
388
|
+
assert_raise(Ruport::Controller::RequiredOptionNotSet) { a.render(:text) }
|
349
389
|
end
|
350
390
|
|
351
391
|
end
|
352
392
|
|
353
393
|
|
354
|
-
class
|
394
|
+
class TestControllerWithRunHook < Test::Unit::TestCase
|
355
395
|
|
356
|
-
class
|
396
|
+
class ControllerWithRunHook < Ruport::Controller
|
357
397
|
add_format DummyText, :text
|
358
398
|
|
359
399
|
required_option :foo,:bar
|
@@ -370,15 +410,15 @@ class TestRendererWithRunHook < Test::Unit::TestCase
|
|
370
410
|
|
371
411
|
def test_renderer_with_run_hooks
|
372
412
|
assert_equal "|header\nbody\nfooter\n",
|
373
|
-
|
413
|
+
ControllerWithRunHook.render_text(:foo => "bar",:bar => "baz")
|
374
414
|
end
|
375
415
|
|
376
416
|
end
|
377
417
|
|
378
418
|
|
379
|
-
class
|
419
|
+
class TestControllerWithHelperModule < Test::Unit::TestCase
|
380
420
|
|
381
|
-
class
|
421
|
+
class ControllerWithHelperModule < VanillaController
|
382
422
|
|
383
423
|
add_format DummyText, :stub
|
384
424
|
|
@@ -390,7 +430,7 @@ class TestRendererWithHelperModule < Test::Unit::TestCase
|
|
390
430
|
end
|
391
431
|
|
392
432
|
def test_renderer_helper_module
|
393
|
-
|
433
|
+
ControllerWithHelperModule.render_stub do |r|
|
394
434
|
assert_equal "Hello Dolly", r.formatter.say_hello
|
395
435
|
end
|
396
436
|
end
|
@@ -398,10 +438,10 @@ end
|
|
398
438
|
|
399
439
|
|
400
440
|
class TestMultiPurposeFormatter < Test::Unit::TestCase
|
401
|
-
# This provides a way to check the multi-format hooks for the
|
441
|
+
# This provides a way to check the multi-format hooks for the Controller
|
402
442
|
class MultiPurposeFormatter < Ruport::Formatter
|
403
443
|
|
404
|
-
renders [:html,:text], :for =>
|
444
|
+
renders [:html,:text], :for => VanillaController
|
405
445
|
|
406
446
|
def build_header
|
407
447
|
a = 10
|
@@ -419,9 +459,9 @@ class TestMultiPurposeFormatter < Test::Unit::TestCase
|
|
419
459
|
end
|
420
460
|
|
421
461
|
def test_multi_purpose
|
422
|
-
text =
|
462
|
+
text = VanillaController.render_text(:body_text => "foo")
|
423
463
|
assert_equal "Foo: 10\nfoo", text
|
424
|
-
html =
|
464
|
+
html = VanillaController.render_html(:body_text => "bar")
|
425
465
|
assert_equal "<b>Foo: 10</b>\n<pre>\nbar\n</pre>\n",html
|
426
466
|
end
|
427
467
|
|
@@ -452,7 +492,7 @@ end
|
|
452
492
|
class TestFormatterErbHelper < Test::Unit::TestCase
|
453
493
|
class ErbFormatter < Ruport::Formatter
|
454
494
|
|
455
|
-
renders :terb, :for =>
|
495
|
+
renders :terb, :for => VanillaController
|
456
496
|
|
457
497
|
def build_body
|
458
498
|
# demonstrate local binding
|
@@ -470,25 +510,25 @@ class TestFormatterErbHelper < Test::Unit::TestCase
|
|
470
510
|
#FIXME: need to test file
|
471
511
|
|
472
512
|
def test_self_bound
|
473
|
-
assert_equal "Default Binding: bar",
|
513
|
+
assert_equal "Default Binding: bar", VanillaController.render_terb
|
474
514
|
end
|
475
515
|
|
476
516
|
def test_custom_bound
|
477
517
|
a = [1,2,3]
|
478
518
|
arr_binding = a.instance_eval { binding }
|
479
519
|
assert_equal "Binding Override: 321",
|
480
|
-
|
520
|
+
VanillaController.render_terb(:binding => arr_binding)
|
481
521
|
end
|
482
522
|
end
|
483
523
|
|
484
524
|
|
485
525
|
class TestOptionReaders < Test::Unit::TestCase
|
486
526
|
|
487
|
-
class
|
527
|
+
class ControllerForCheckingOptionReaders < Ruport::Controller
|
488
528
|
required_option :foo
|
489
529
|
end
|
490
530
|
|
491
|
-
class
|
531
|
+
class ControllerForCheckingPassivity < Ruport::Controller
|
492
532
|
def foo
|
493
533
|
"apples"
|
494
534
|
end
|
@@ -496,10 +536,10 @@ class TestOptionReaders < Test::Unit::TestCase
|
|
496
536
|
end
|
497
537
|
|
498
538
|
def setup
|
499
|
-
@renderer =
|
539
|
+
@renderer = ControllerForCheckingOptionReaders.new
|
500
540
|
@renderer.formatter = Ruport::Formatter.new
|
501
541
|
|
502
|
-
@passive =
|
542
|
+
@passive = ControllerForCheckingPassivity.new
|
503
543
|
@passive.formatter = Ruport::Formatter.new
|
504
544
|
end
|
505
545
|
|
@@ -519,7 +559,7 @@ end
|
|
519
559
|
|
520
560
|
class TestSetupOrdering < Test::Unit::TestCase
|
521
561
|
|
522
|
-
class
|
562
|
+
class ControllerWithSetup < Ruport::Controller
|
523
563
|
stage :bar
|
524
564
|
def setup
|
525
565
|
options.foo.capitalize!
|
@@ -527,7 +567,7 @@ class TestSetupOrdering < Test::Unit::TestCase
|
|
527
567
|
end
|
528
568
|
|
529
569
|
class BasicFormatter < Ruport::Formatter
|
530
|
-
renders :text, :for =>
|
570
|
+
renders :text, :for => ControllerWithSetup
|
531
571
|
|
532
572
|
def build_bar
|
533
573
|
output << options.foo
|
@@ -535,24 +575,89 @@ class TestSetupOrdering < Test::Unit::TestCase
|
|
535
575
|
end
|
536
576
|
|
537
577
|
def test_render_hash_options_should_be_called_before_setup
|
538
|
-
assert_equal "Hello",
|
578
|
+
assert_equal "Hello", ControllerWithSetup.render_text(:foo => "hello")
|
539
579
|
end
|
540
580
|
|
541
581
|
def test_render_block_should_be_called_before_setup
|
542
582
|
assert_equal "Hello",
|
543
|
-
|
583
|
+
ControllerWithSetup.render_text { |r| r.options.foo = "hello" }
|
544
584
|
end
|
545
585
|
|
546
586
|
end
|
547
587
|
|
548
|
-
class
|
588
|
+
class CustomFormatter < Ruport::Formatter
|
589
|
+
def custom_helper
|
590
|
+
output << "Custom!"
|
591
|
+
end
|
592
|
+
end
|
593
|
+
|
594
|
+
class ControllerWithAnonymousFormatters < Ruport::Controller
|
595
|
+
|
596
|
+
stage :report
|
597
|
+
|
598
|
+
formatter :html do
|
599
|
+
build :report do
|
600
|
+
output << textile("h1. Hi there")
|
601
|
+
end
|
602
|
+
end
|
603
|
+
|
604
|
+
formatter :csv do
|
605
|
+
build :report do
|
606
|
+
build_row([1,2,3])
|
607
|
+
end
|
608
|
+
end
|
609
|
+
|
610
|
+
formatter :pdf do
|
611
|
+
build :report do
|
612
|
+
add_text "hello world"
|
613
|
+
end
|
614
|
+
end
|
615
|
+
|
616
|
+
formatter :text do
|
617
|
+
build :report do
|
618
|
+
output << "Hello world"
|
619
|
+
end
|
620
|
+
end
|
621
|
+
|
622
|
+
formatter :custom => CustomFormatter do
|
623
|
+
|
624
|
+
build :report do
|
625
|
+
output << "This is "
|
626
|
+
custom_helper
|
627
|
+
end
|
628
|
+
|
629
|
+
end
|
630
|
+
|
631
|
+
end
|
632
|
+
|
633
|
+
class TestAnonymousFormatter < Test::Unit::TestCase
|
634
|
+
context "When using built in Ruport formatters" do
|
635
|
+
|
636
|
+
def specify_text_formatter_shortcut_is_accessible
|
637
|
+
assert_equal "Hello world", ControllerWithAnonymousFormatters.render_text
|
638
|
+
assert_equal "1,2,3\n", ControllerWithAnonymousFormatters.render_csv
|
639
|
+
assert_equal "<h1>Hi there</h1>", ControllerWithAnonymousFormatters.render_html
|
640
|
+
assert_not_nil ControllerWithAnonymousFormatters.render_pdf
|
641
|
+
end
|
642
|
+
|
643
|
+
end
|
644
|
+
|
645
|
+
context "When using custom formatters" do
|
646
|
+
def specify_custom_formatter_shortcut_is_accessible
|
647
|
+
assert_equal "This is Custom!", ControllerWithAnonymousFormatters.render_custom
|
648
|
+
end
|
649
|
+
end
|
650
|
+
|
651
|
+
end
|
652
|
+
|
653
|
+
class TestControllerHooks < Test::Unit::TestCase
|
549
654
|
|
550
655
|
context "when renderable_data omitted" do
|
551
656
|
|
552
657
|
require "mocha"
|
553
658
|
|
554
659
|
class DummyObject
|
555
|
-
include Ruport::
|
660
|
+
include Ruport::Controller::Hooks
|
556
661
|
renders_as_table
|
557
662
|
end
|
558
663
|
|
@@ -560,7 +665,7 @@ class TestRendererHooks < Test::Unit::TestCase
|
|
560
665
|
a = DummyObject.new
|
561
666
|
rend = mock("renderer")
|
562
667
|
rend.expects(:data=).with(a)
|
563
|
-
Ruport::
|
668
|
+
Ruport::Controller::Table.expects(:render).with(:csv,{}).yields(rend)
|
564
669
|
a.as(:csv)
|
565
670
|
end
|
566
671
|
|
@@ -569,7 +674,7 @@ class TestRendererHooks < Test::Unit::TestCase
|
|
569
674
|
context "when using renderable_data" do
|
570
675
|
|
571
676
|
class DummyObject2
|
572
|
-
include Ruport::
|
677
|
+
include Ruport::Controller::Hooks
|
573
678
|
renders_as_table
|
574
679
|
|
575
680
|
def renderable_data(format)
|
@@ -581,12 +686,12 @@ class TestRendererHooks < Test::Unit::TestCase
|
|
581
686
|
a = DummyObject2.new
|
582
687
|
rend = mock("renderer")
|
583
688
|
rend.expects(:data=).with(1)
|
584
|
-
Ruport::
|
689
|
+
Ruport::Controller::Table.expects(:render).with(:csv,{}).yields(rend)
|
585
690
|
a.as(:csv)
|
586
691
|
end
|
587
692
|
|
588
693
|
class DummyObject3
|
589
|
-
include Ruport::
|
694
|
+
include Ruport::Controller::Hooks
|
590
695
|
renders_as_table
|
591
696
|
|
592
697
|
def renderable_data
|
@@ -599,7 +704,7 @@ class TestRendererHooks < Test::Unit::TestCase
|
|
599
704
|
end
|
600
705
|
|
601
706
|
class DummyObject4
|
602
|
-
include Ruport::
|
707
|
+
include Ruport::Controller::Hooks
|
603
708
|
renders_as_table
|
604
709
|
|
605
710
|
def renderable_data(format)
|
@@ -616,7 +721,7 @@ class TestRendererHooks < Test::Unit::TestCase
|
|
616
721
|
a = DummyObject4.new
|
617
722
|
rend = mock("renderer")
|
618
723
|
rend.expects(:data=).with(2)
|
619
|
-
Ruport::
|
724
|
+
Ruport::Controller::Table.expects(:render).with(:csv,{}).yields(rend)
|
620
725
|
a.as(:csv)
|
621
726
|
end
|
622
727
|
|
@@ -626,8 +731,8 @@ class TestRendererHooks < Test::Unit::TestCase
|
|
626
731
|
|
627
732
|
def specify_an_unknown_format_error_should_be_raised
|
628
733
|
|
629
|
-
assert_raises(Ruport::
|
630
|
-
Ruport::
|
734
|
+
assert_raises(Ruport::Controller::UnknownFormatError) do
|
735
|
+
Ruport::Controller.render_foo
|
631
736
|
end
|
632
737
|
|
633
738
|
end
|