ruport 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +4 -4
- data/Rakefile +1 -1
- data/lib/ruport.rb +1 -1
- data/lib/ruport/acts_as_reportable.rb +34 -2
- data/lib/ruport/data/grouping.rb +6 -4
- data/lib/ruport/formatter/pdf.rb +54 -17
- data/lib/ruport/formatter/text.rb +6 -3
- data/test/acts_as_reportable_test.rb +95 -72
- data/test/csv_formatter_test.rb +40 -36
- data/test/grouping_test.rb +139 -134
- data/test/html_formatter_test.rb +24 -10
- data/test/pdf_formatter_test.rb +102 -8
- data/test/query_test.rb +0 -1
- data/test/record_test.rb +45 -33
- data/test/renderer_test.rb +236 -211
- data/test/sql_split_test.rb +8 -7
- data/test/table_test.rb +437 -425
- data/test/text_formatter_test.rb +62 -45
- metadata +2 -2
data/test/html_formatter_test.rb
CHANGED
@@ -1,13 +1,7 @@
|
|
1
1
|
require "test/unit"
|
2
2
|
require "ruport"
|
3
3
|
|
4
|
-
|
5
|
-
require "rubygems"
|
6
|
-
rescue LoadError
|
7
|
-
nil
|
8
|
-
end
|
9
|
-
|
10
|
-
class TestFormatHTML < Test::Unit::TestCase
|
4
|
+
class TestRenderHTMLTable < Test::Unit::TestCase
|
11
5
|
|
12
6
|
def test_html_table
|
13
7
|
a = Ruport::Formatter::HTML.new
|
@@ -37,13 +31,21 @@ class TestFormatHTML < Test::Unit::TestCase
|
|
37
31
|
"\n\t\t\t<td>4</td>\n\t\t\t<td>5</td>\n\t\t\t<td>6</td>\n\t"+
|
38
32
|
"\t</tr>\n\t</table>",actual)
|
39
33
|
|
40
|
-
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
41
37
|
|
38
|
+
class TestRenderHTMLRow < Test::Unit::TestCase
|
39
|
+
|
42
40
|
def test_render_html_row
|
43
41
|
actual = Ruport::Renderer::Row.render_html { |r| r.data = [1,2,3] }
|
44
42
|
assert_equal("\t\t<tr>\n\t\t\t<td>1</td>\n\t\t\t<td>2"+
|
45
43
|
"</td>\n\t\t\t<td>3</td>\n\t\t</tr>\n",actual)
|
46
44
|
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
class TestRenderHTMLGroup < Test::Unit::TestCase
|
47
49
|
|
48
50
|
def test_render_html_group
|
49
51
|
group = Ruport::Data::Group.new(:name => 'test',
|
@@ -68,7 +70,11 @@ class TestFormatHTML < Test::Unit::TestCase
|
|
68
70
|
"\n\t\t\t<td>2</td>\n\t\t\t<td>3</td>\n\t\t</tr>\n\t\t<tr>"+
|
69
71
|
"\n\t\t\t<td>4</td>\n\t\t\t<td>5</td>\n\t\t\t<td>6</td>\n\t"+
|
70
72
|
"\t</tr>\n\t</table>", actual
|
71
|
-
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
class TestRenderHTMLGrouping < Test::Unit::TestCase
|
72
78
|
|
73
79
|
def test_render_html_grouping
|
74
80
|
table = Table(%w[a b c]) << [1,2,3] << [1,1,3] << [2,7,9]
|
@@ -95,6 +101,15 @@ class TestFormatHTML < Test::Unit::TestCase
|
|
95
101
|
"\t<th>c</th>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td>7</td>\n"+
|
96
102
|
"\t\t\t<td>9</td>\n\t\t</tr>\n\t</table>\n", actual
|
97
103
|
end
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
class TestHTMLFormatterHelpers < Test::Unit::TestCase
|
108
|
+
begin
|
109
|
+
require "rubygems"
|
110
|
+
rescue LoadError
|
111
|
+
nil
|
112
|
+
end
|
98
113
|
|
99
114
|
def test_textile
|
100
115
|
require "redcloth"
|
@@ -103,5 +118,4 @@ class TestFormatHTML < Test::Unit::TestCase
|
|
103
118
|
rescue LoadError
|
104
119
|
STDERR.puts "Skipping textile test... needs redcloth"
|
105
120
|
end
|
106
|
-
|
107
121
|
end
|
data/test/pdf_formatter_test.rb
CHANGED
@@ -9,17 +9,37 @@ end
|
|
9
9
|
|
10
10
|
#require "pdf/writer"
|
11
11
|
|
12
|
-
class
|
12
|
+
class TestRenderPDFTable < Test::Unit::TestCase
|
13
13
|
|
14
|
-
def test_render_pdf_basic
|
14
|
+
def test_render_pdf_basic
|
15
|
+
# can't render without column names
|
15
16
|
data = [[1,2],[3,4]].to_table
|
16
|
-
assert_raise(Ruport::FormatterError)
|
17
|
+
assert_raise(Ruport::FormatterError) do
|
17
18
|
data.to_pdf
|
18
|
-
|
19
|
+
end
|
19
20
|
|
20
21
|
data.column_names = %w[a b]
|
21
22
|
assert_nothing_raised { data.to_pdf }
|
23
|
+
|
24
|
+
assert_nothing_raised { Table(%w[a b c]).to_pdf }
|
25
|
+
end
|
26
|
+
|
27
|
+
# this is mostly to check that the transaction hack gets called
|
28
|
+
def test_relatively_large_pdf
|
29
|
+
table = Table("test/samples/dates.csv")
|
30
|
+
table.reduce(0..99)
|
31
|
+
assert_nothing_raised { table.to_pdf }
|
22
32
|
end
|
33
|
+
|
34
|
+
# this is just to make sure that the column_opts code is being called.
|
35
|
+
# FIXME: add mocks to be sure
|
36
|
+
def test_table_with_options
|
37
|
+
data = [[1,2],[3,4]].to_table(%w[a b])
|
38
|
+
assert_nothing_raised do
|
39
|
+
data.to_pdf(:table_format => {
|
40
|
+
:column_options => { :justification => :center } } )
|
41
|
+
end
|
42
|
+
end
|
23
43
|
|
24
44
|
#--------BUG TRAPS--------#
|
25
45
|
|
@@ -28,7 +48,13 @@ class TestFormatPDF < Test::Unit::TestCase
|
|
28
48
|
def test_tables_should_render_with_symbol_column_name
|
29
49
|
data = [[1,2,3],[4,5,6]].to_table([:a,:b,:c])
|
30
50
|
assert_nothing_raised { data.to_pdf }
|
31
|
-
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
class TestRenderPDFGrouping < Test::Unit::TestCase
|
56
|
+
|
57
|
+
#--------BUG TRAPS----------#
|
32
58
|
|
33
59
|
# As of Ruport 0.10.0, PDF's justified group output was throwing
|
34
60
|
# UnknownFormatError (#288)
|
@@ -39,10 +65,78 @@ class TestFormatPDF < Test::Unit::TestCase
|
|
39
65
|
assert_nothing_raised { grouping.to_pdf(:style => :inline) }
|
40
66
|
assert_nothing_raised { grouping.to_pdf(:style => :offset) }
|
41
67
|
assert_nothing_raised { grouping.to_pdf(:style => :justified) }
|
42
|
-
assert_nothing_raised { grouping.to_pdf(:style => :separated) }
|
68
|
+
assert_nothing_raised { grouping.to_pdf(:style => :separated) }
|
69
|
+
assert_raises(NotImplementedError) do
|
70
|
+
grouping.to_pdf(:style => :red_snapper)
|
71
|
+
end
|
43
72
|
end
|
44
73
|
|
74
|
+
end
|
75
|
+
|
76
|
+
class TestPDFFormatterHelpers < Test::Unit::TestCase
|
45
77
|
|
78
|
+
def test_boundaries
|
79
|
+
a = Ruport::Formatter::PDF.new
|
80
|
+
|
81
|
+
assert_equal 36, a.left_boundary
|
82
|
+
a.pdf_writer.left_margin = 50
|
83
|
+
assert_equal 50, a.left_boundary
|
84
|
+
|
85
|
+
assert_equal 576, a.right_boundary
|
86
|
+
a.pdf_writer.right_margin -= 10
|
87
|
+
assert_equal 586, a.right_boundary
|
88
|
+
|
89
|
+
assert_equal 756, a.top_boundary
|
90
|
+
a.pdf_writer.top_margin -= 10
|
91
|
+
assert_equal 766, a.top_boundary
|
92
|
+
|
93
|
+
assert_equal 36, a.bottom_boundary
|
94
|
+
a.pdf_writer.bottom_margin -= 10
|
95
|
+
assert_equal 26, a.bottom_boundary
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_move_cursor
|
99
|
+
a = Ruport::Formatter::PDF.new
|
100
|
+
a.move_cursor_to(500)
|
101
|
+
assert_equal(500,a.cursor)
|
102
|
+
a.move_cursor(-25)
|
103
|
+
assert_equal(475,a.cursor)
|
104
|
+
a.move_cursor(50)
|
105
|
+
assert_equal(525,a.cursor)
|
106
|
+
end
|
46
107
|
|
47
|
-
|
48
|
-
|
108
|
+
def test_padding
|
109
|
+
a = Ruport::Formatter::PDF.new
|
110
|
+
a.move_cursor_to(100)
|
111
|
+
|
112
|
+
# padding on top and bottom
|
113
|
+
a.pad(10) do
|
114
|
+
assert_equal 90, a.cursor
|
115
|
+
a.move_cursor(-10)
|
116
|
+
assert_equal 80, a.cursor
|
117
|
+
end
|
118
|
+
assert_equal(70,a.cursor)
|
119
|
+
|
120
|
+
a.move_cursor_to(100)
|
121
|
+
|
122
|
+
# padding just on top
|
123
|
+
a.pad_top(10) do
|
124
|
+
assert_equal 90, a.cursor
|
125
|
+
a.move_cursor(-10)
|
126
|
+
assert_equal 80, a.cursor
|
127
|
+
end
|
128
|
+
|
129
|
+
assert_equal 80, a.cursor
|
130
|
+
|
131
|
+
a.move_cursor_to(100)
|
132
|
+
|
133
|
+
# padding just on bottom
|
134
|
+
a.pad_bottom(10) do
|
135
|
+
assert_equal 100, a.cursor
|
136
|
+
a.move_cursor(-10)
|
137
|
+
assert_equal 90, a.cursor
|
138
|
+
end
|
139
|
+
|
140
|
+
assert_equal 80, a.cursor
|
141
|
+
end
|
142
|
+
end
|
data/test/query_test.rb
CHANGED
data/test/record_test.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
require "test/unit"
|
2
2
|
require "ruport"
|
3
|
-
|
4
|
-
|
5
|
-
class TestRecord < Test::Unit::TestCase
|
6
|
-
|
7
|
-
include Ruport::Data
|
8
|
-
|
3
|
+
|
4
|
+
module RecordTestSetup
|
9
5
|
def setup
|
10
6
|
@attributes = %w[a b c d]
|
11
7
|
@record = Ruport::Data::Record.new [1,2,3,4], :attributes => @attributes
|
12
8
|
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class TestRecord < Test::Unit::TestCase
|
12
|
+
|
13
|
+
include Ruport::Data
|
14
|
+
include RecordTestSetup
|
13
15
|
|
14
16
|
def test_init
|
15
17
|
record = Ruport::Data::Record.new [1,2,3,4]
|
@@ -208,27 +210,6 @@ class TestRecord < Test::Unit::TestCase
|
|
208
210
|
new_object_id = @record.instance_variable_get(:@attributes).object_id
|
209
211
|
assert_equal a.object_id, new_object_id
|
210
212
|
end
|
211
|
-
|
212
|
-
def test_record_as
|
213
|
-
rendered_row = @record.as(:text)
|
214
|
-
assert_equal("| 1 | 2 | 3 | 4 |\n", rendered_row)
|
215
|
-
end
|
216
|
-
|
217
|
-
def test_to_hack
|
218
|
-
rendered_row = @record.to_text
|
219
|
-
assert_equal("| 1 | 2 | 3 | 4 |\n", rendered_row)
|
220
|
-
|
221
|
-
rendered_row = @record.to_csv(:format_options => { :col_sep => "\t"})
|
222
|
-
assert_equal("1\t2\t3\t4\n",rendered_row)
|
223
|
-
end
|
224
|
-
|
225
|
-
def test_as_throws_proper_errors
|
226
|
-
a = Record.new({ "a" => 1, "b" => 2 })
|
227
|
-
assert_nothing_raised { a.as(:csv) }
|
228
|
-
assert_nothing_raised { a.to_csv }
|
229
|
-
assert_raises(Ruport::Renderer::UnknownFormatError) { a.as(:nothing) }
|
230
|
-
assert_raises(Ruport::Renderer::UnknownFormatError) { a.to_nothing }
|
231
|
-
end
|
232
213
|
|
233
214
|
#----------------------------------------------------------------------
|
234
215
|
# BUG Traps
|
@@ -257,12 +238,7 @@ class TestRecord < Test::Unit::TestCase
|
|
257
238
|
assert_equal(2,a.get("b"))
|
258
239
|
assert_equal(2,a.get(:b))
|
259
240
|
end
|
260
|
-
|
261
|
-
def test_ensure_attributes_not_broken_by_to_hack
|
262
|
-
record = Ruport::Data::Record.new [1,2], :attributes => %w[a to_something]
|
263
|
-
assert_equal 2, record.to_something
|
264
|
-
end
|
265
|
-
|
241
|
+
|
266
242
|
def test_ensure_get_throws_argument_error
|
267
243
|
a = Record.new({"a" => 1, "b" => 2})
|
268
244
|
assert_raises(ArgumentError) { a.get([]) }
|
@@ -298,5 +274,41 @@ class TestRecord < Test::Unit::TestCase
|
|
298
274
|
a = MyRecordSub.new [1,2,3]
|
299
275
|
assert_equal "1,2,3\n", a.to_csv
|
300
276
|
end
|
277
|
+
|
278
|
+
end
|
279
|
+
|
280
|
+
|
281
|
+
class TestRecordRenderering < Test::Unit::TestCase
|
282
|
+
|
283
|
+
include RecordTestSetup
|
284
|
+
include Ruport::Data
|
285
|
+
|
286
|
+
def test_record_as
|
287
|
+
rendered_row = @record.as(:text)
|
288
|
+
assert_equal("| 1 | 2 | 3 | 4 |\n", rendered_row)
|
289
|
+
end
|
290
|
+
|
291
|
+
def test_to_hack
|
292
|
+
rendered_row = @record.to_text
|
293
|
+
assert_equal("| 1 | 2 | 3 | 4 |\n", rendered_row)
|
294
|
+
|
295
|
+
rendered_row = @record.to_csv(:format_options => { :col_sep => "\t"})
|
296
|
+
assert_equal("1\t2\t3\t4\n",rendered_row)
|
297
|
+
end
|
298
|
+
|
299
|
+
def test_as_throws_proper_errors
|
300
|
+
a = Record.new({ "a" => 1, "b" => 2 })
|
301
|
+
assert_nothing_raised { a.as(:csv) }
|
302
|
+
assert_nothing_raised { a.to_csv }
|
303
|
+
assert_raises(Ruport::Renderer::UnknownFormatError) { a.as(:nothing) }
|
304
|
+
assert_raises(Ruport::Renderer::UnknownFormatError) { a.to_nothing }
|
305
|
+
end
|
306
|
+
|
307
|
+
## -- BUG TRAPS --------------------
|
308
|
+
|
309
|
+
def test_ensure_attributes_not_broken_by_to_hack
|
310
|
+
record = Ruport::Data::Record.new [1,2], :attributes => %w[a to_something]
|
311
|
+
assert_equal 2, record.to_something
|
312
|
+
end
|
301
313
|
|
302
314
|
end
|
data/test/renderer_test.rb
CHANGED
@@ -1,8 +1,24 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'ruport'
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
###########################################################################
|
5
|
+
#
|
6
|
+
# NOTE:
|
7
|
+
#
|
8
|
+
# As it stands, we haven't found a more clever way to test the formatting
|
9
|
+
# system than to just create a bunch of renderers and basic formatters for
|
10
|
+
# different concepts we're trying to test. Patches and ideas welcome:
|
11
|
+
#
|
12
|
+
# list.rubyreports.org
|
13
|
+
############################################################################
|
14
|
+
|
15
|
+
#============================================================================
|
16
|
+
# These two renderers represent the two styles that can be used when defining
|
17
|
+
# renderers in Ruport. The OldSchoolRenderer approach has largely been
|
18
|
+
# deprecated, but still has uses in edge cases that we need to support.
|
19
|
+
#============================================================================
|
20
|
+
|
21
|
+
class OldSchoolRenderer < Ruport::Renderer
|
6
22
|
|
7
23
|
def run
|
8
24
|
formatter do
|
@@ -14,13 +30,17 @@ class TrivialRenderer < Ruport::Renderer
|
|
14
30
|
|
15
31
|
end
|
16
32
|
|
17
|
-
class
|
33
|
+
class VanillaRenderer < Ruport::Renderer
|
18
34
|
stage :header,:body,:footer
|
19
35
|
end
|
20
36
|
|
37
|
+
|
38
|
+
# This formatter implements some junk output so we can be sure
|
39
|
+
# that the hooks are being set up right. Perhaps these could
|
40
|
+
# be replaced by mock objects in the future.
|
21
41
|
class DummyText < Ruport::Formatter
|
22
42
|
|
23
|
-
renders :text, :for =>
|
43
|
+
renders :text, :for => OldSchoolRenderer
|
24
44
|
|
25
45
|
def prepare_document
|
26
46
|
output << "p"
|
@@ -43,157 +63,119 @@ class DummyText < Ruport::Formatter
|
|
43
63
|
end
|
44
64
|
end
|
45
65
|
|
46
|
-
class WithLayout < DummyText
|
47
|
-
renders :text_with_layout, :for => YetAnotherRenderer
|
48
|
-
|
49
|
-
def layout
|
50
|
-
output << "---\n"
|
51
|
-
yield
|
52
|
-
output << "---\n"
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
class TrivialRenderer2 < TrivialRenderer; end
|
58
|
-
|
59
|
-
class MultiPurposeFormatter < Ruport::Formatter
|
60
|
-
|
61
|
-
renders [:html,:text], :for => TrivialRenderer2
|
62
|
-
|
63
|
-
def build_header
|
64
|
-
a = 10
|
65
|
-
|
66
|
-
text { output << "Foo: #{a}\n" }
|
67
|
-
html { output << "<b>Foo: #{a}</b>\n" }
|
68
|
-
end
|
69
|
-
|
70
|
-
def build_body
|
71
|
-
html { output << "<pre>\n" }
|
72
|
-
output << options.body_text
|
73
|
-
html { output << "\n</pre>\n" }
|
74
|
-
end
|
75
|
-
|
76
|
-
def build_footer; end
|
77
|
-
end
|
78
|
-
|
79
|
-
# FIXME: come up with a better name
|
80
|
-
class RendererWithHelpers < Ruport::Renderer
|
81
|
-
|
82
|
-
add_format DummyText, :text
|
83
|
-
|
84
|
-
prepare :document
|
85
66
|
|
86
|
-
|
67
|
+
class TestRenderer < Test::Unit::TestCase
|
87
68
|
|
88
|
-
|
89
|
-
|
90
|
-
|
69
|
+
def test_trivial
|
70
|
+
actual = OldSchoolRenderer.render(:text)
|
71
|
+
assert_equal "header\nbody\nfooter\n", actual
|
72
|
+
end
|
91
73
|
|
92
|
-
|
74
|
+
def test_using_io
|
75
|
+
require "stringio"
|
76
|
+
out = StringIO.new
|
77
|
+
a = OldSchoolRenderer.render(:text) { |r| r.io = out }
|
78
|
+
out.rewind
|
79
|
+
assert_equal "header\nbody\nfooter\n", out.read
|
80
|
+
assert_equal "", out.read
|
81
|
+
end
|
93
82
|
|
94
|
-
def
|
95
|
-
|
83
|
+
def test_formats
|
84
|
+
assert_equal( {}, Ruport::Renderer.formats )
|
85
|
+
assert_equal( { :text => DummyText },OldSchoolRenderer.formats )
|
96
86
|
end
|
97
87
|
|
98
|
-
|
88
|
+
def test_method_missing
|
89
|
+
actual = OldSchoolRenderer.render_text
|
90
|
+
assert_equal "header\nbody\nfooter\n", actual
|
91
|
+
end
|
99
92
|
|
100
|
-
|
101
|
-
|
102
|
-
|
93
|
+
def test_formatter
|
94
|
+
# normal instance mode
|
95
|
+
rend = OldSchoolRenderer.new
|
96
|
+
rend.send(:use_formatter,:text)
|
103
97
|
|
104
|
-
|
98
|
+
assert_kind_of Ruport::Formatter, rend.formatter
|
99
|
+
assert_kind_of DummyText, rend.formatter
|
105
100
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
101
|
+
# render mode
|
102
|
+
OldSchoolRenderer.render_text do |r|
|
103
|
+
assert_kind_of Ruport::Formatter, r.formatter
|
104
|
+
assert_kind_of DummyText, r.formatter
|
105
|
+
end
|
110
106
|
|
111
|
-
|
112
|
-
formatter.output << "|"
|
113
|
-
end
|
107
|
+
assert_equal "body\n", rend.formatter { build_body }.output
|
114
108
|
|
109
|
+
rend.formatter.clear_output
|
110
|
+
assert_equal "", rend.formatter.output
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_options_act_like_indifferent_hash
|
114
|
+
opts = Ruport::Renderer::Options.new
|
115
|
+
opts.foo = "bar"
|
116
|
+
assert_equal "bar", opts[:foo]
|
117
|
+
assert_equal "bar", opts["foo"]
|
118
|
+
|
119
|
+
opts["f"] = "bar"
|
120
|
+
assert_equal "bar", opts[:f]
|
121
|
+
assert_equal "bar", opts.f
|
122
|
+
assert_equal "bar", opts["f"]
|
123
|
+
|
124
|
+
opts[:apple] = "banana"
|
125
|
+
assert_equal "banana", opts.apple
|
126
|
+
assert_equal "banana", opts["apple"]
|
127
|
+
assert_equal "banana", opts[:apple]
|
128
|
+
end
|
129
|
+
|
115
130
|
end
|
116
131
|
|
117
|
-
class RendererWithHelperModule < TrivialRenderer2
|
118
132
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
133
|
+
class TestFormatterWithLayout < Test::Unit::TestCase
|
134
|
+
# This formatter is meant to check out a special case in Ruport's renderer,
|
135
|
+
# in which a layout method is called and yielded to when defined
|
136
|
+
class WithLayout < DummyText
|
137
|
+
renders :text_with_layout, :for => VanillaRenderer
|
138
|
+
|
139
|
+
def layout
|
140
|
+
output << "---\n"
|
141
|
+
yield
|
142
|
+
output << "---\n"
|
143
|
+
end
|
144
|
+
|
125
145
|
end
|
126
|
-
end
|
127
146
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
def build_header; end #gross, refactor
|
133
|
-
def build_footer; end #gross, refactor
|
134
|
-
def build_body
|
135
|
-
# demonstrate local binding
|
136
|
-
@foo = "bar"
|
137
|
-
if options.binding
|
138
|
-
output << erb("Binding Override: <%= reverse %>",
|
139
|
-
:binding => options.binding)
|
140
|
-
else
|
141
|
-
output << erb("Default Binding: <%= @foo %>")
|
142
|
-
end
|
147
|
+
def test_layout
|
148
|
+
assert_equal "---\nheader\nbody\nfooter\n---\n",
|
149
|
+
VanillaRenderer.render_text_with_layout
|
143
150
|
end
|
144
|
-
|
145
|
-
end
|
146
151
|
|
147
|
-
class TestFormatterErbHelper < Test::Unit::TestCase
|
148
|
-
def test_self_bound
|
149
|
-
assert_equal "Default Binding: bar", TrivialRenderer2.render_terb
|
150
|
-
end
|
151
|
-
|
152
|
-
def test_custom_bound
|
153
|
-
a = [1,2,3]
|
154
|
-
arr_binding = a.instance_eval { binding }
|
155
|
-
assert_equal "Binding Override: 321",
|
156
|
-
TrivialRenderer2.render_terb(:binding => arr_binding)
|
157
|
-
end
|
158
152
|
end
|
159
153
|
|
160
|
-
class TestRenderer < Test::Unit::TestCase
|
161
154
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
html = TrivialRenderer2.render_html(:body_text => "bar")
|
166
|
-
assert_equal "<b>Foo: 10</b>\n<pre>\nbar\n</pre>\n",html
|
167
|
-
end
|
155
|
+
class TestRendererWithManyHooks < Test::Unit::TestCase
|
156
|
+
# This provides a way to check several hooks that Renderer supports
|
157
|
+
class RendererWithManyHooks < Ruport::Renderer
|
168
158
|
|
159
|
+
add_format DummyText, :text
|
169
160
|
|
170
|
-
|
171
|
-
assert_equal "|header\nbody\nfooter\n",
|
172
|
-
RendererWithRunHook.render_text(:foo => "bar",:bar => "baz")
|
173
|
-
end
|
161
|
+
prepare :document
|
174
162
|
|
175
|
-
|
176
|
-
assert_equal [:html,:text], MultiPurposeFormatter.formats
|
163
|
+
option :subtitle, :subsubtitle
|
177
164
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
visited = false
|
182
|
-
a.html { visited = true }
|
165
|
+
stage :header
|
166
|
+
stage :body
|
167
|
+
stage :footer
|
183
168
|
|
184
|
-
|
185
|
-
|
186
|
-
visited = false
|
187
|
-
a.text { visited = true }
|
188
|
-
assert !visited
|
169
|
+
finalize :document
|
189
170
|
|
190
|
-
|
191
|
-
|
171
|
+
def setup
|
172
|
+
options.apple = true
|
192
173
|
end
|
174
|
+
|
193
175
|
end
|
194
176
|
|
195
177
|
def test_hash_options_setters
|
196
|
-
a =
|
178
|
+
a = RendererWithManyHooks.render(:text, :subtitle => "foo",
|
197
179
|
:subsubtitle => "bar") { |r|
|
198
180
|
assert_equal "foo", r.options.subtitle
|
199
181
|
assert_equal "bar", r.options.subsubtitle
|
@@ -201,88 +183,69 @@ class TestRenderer < Test::Unit::TestCase
|
|
201
183
|
end
|
202
184
|
|
203
185
|
def test_data_accessors
|
204
|
-
a =
|
186
|
+
a = RendererWithManyHooks.render(:text, :data => [1,2,4]) { |r|
|
205
187
|
assert_equal [1,2,4], r.data
|
206
188
|
}
|
207
189
|
|
208
|
-
b =
|
190
|
+
b = RendererWithManyHooks.render_text(%w[a b c]) { |r|
|
209
191
|
assert_equal %w[a b c], r.data
|
210
192
|
}
|
211
193
|
|
212
|
-
c =
|
194
|
+
c = RendererWithManyHooks.render_text(%w[a b f],:snapper => :red) { |r|
|
213
195
|
assert_equal %w[a b f], r.data
|
214
196
|
assert_equal :red, r.options.snapper
|
215
197
|
}
|
216
198
|
end
|
217
199
|
|
218
|
-
def test_using_io
|
219
|
-
require "stringio"
|
220
|
-
out = StringIO.new
|
221
|
-
a = TrivialRenderer.render(:text) { |r| r.io = out }
|
222
|
-
out.rewind
|
223
|
-
assert_equal "header\nbody\nfooter\n", out.read
|
224
|
-
assert_equal "", out.read
|
225
|
-
end
|
226
|
-
|
227
|
-
def test_trivial
|
228
|
-
actual = TrivialRenderer.render(:text)
|
229
|
-
assert_equal "header\nbody\nfooter\n", actual
|
230
|
-
end
|
231
|
-
|
232
|
-
def test_formats
|
233
|
-
assert_equal( {}, Ruport::Renderer.formats )
|
234
|
-
assert_equal( { :text => DummyText },TrivialRenderer.formats )
|
235
|
-
end
|
236
|
-
|
237
200
|
def test_stage_helper
|
238
|
-
assert
|
201
|
+
assert RendererWithManyHooks.stages.include?('body')
|
239
202
|
end
|
240
203
|
|
241
204
|
def test_finalize_helper
|
242
|
-
assert_equal :document,
|
205
|
+
assert_equal :document, RendererWithManyHooks.final_stage
|
243
206
|
end
|
244
207
|
|
245
208
|
def test_prepare_helper
|
246
|
-
assert_equal :document,
|
209
|
+
assert_equal :document, RendererWithManyHooks.first_stage
|
247
210
|
end
|
248
211
|
|
249
212
|
def test_finalize_again
|
250
213
|
assert_raise(Ruport::Renderer::StageAlreadyDefinedError) {
|
251
|
-
|
214
|
+
RendererWithManyHooks.finalize :report
|
252
215
|
}
|
253
216
|
end
|
254
217
|
|
255
218
|
def test_prepare_again
|
256
219
|
assert_raise(Ruport::Renderer::StageAlreadyDefinedError) {
|
257
|
-
|
220
|
+
RendererWithManyHooks.prepare :foo
|
258
221
|
}
|
259
222
|
end
|
260
223
|
|
261
224
|
def test_renderer_using_helpers
|
262
|
-
actual =
|
225
|
+
actual = RendererWithManyHooks.render(:text)
|
263
226
|
assert_equal "pheader\nbody\nfooter\nf", actual
|
264
227
|
|
265
|
-
actual =
|
228
|
+
actual = RendererWithManyHooks.render_text
|
266
229
|
assert_equal "pheader\nbody\nfooter\nf", actual
|
267
230
|
end
|
268
231
|
|
269
232
|
def test_setup
|
270
233
|
actual = false
|
271
|
-
|
234
|
+
RendererWithManyHooks.render_text { |r|
|
272
235
|
actual = r.options.apple
|
273
236
|
}
|
274
237
|
assert actual
|
275
238
|
end
|
276
239
|
|
277
240
|
def test_option_helper
|
278
|
-
|
241
|
+
RendererWithManyHooks.render_text do |r|
|
279
242
|
r.subtitle = "Test Report"
|
280
243
|
assert_equal "Test Report", r.options.subtitle
|
281
244
|
end
|
282
245
|
end
|
283
246
|
|
284
247
|
def test_required_option_helper
|
285
|
-
a =
|
248
|
+
a = RendererWithManyHooks.dup
|
286
249
|
a.required_option :title
|
287
250
|
|
288
251
|
a.render_text do |r|
|
@@ -293,83 +256,145 @@ class TestRenderer < Test::Unit::TestCase
|
|
293
256
|
end
|
294
257
|
|
295
258
|
def test_without_required_option
|
296
|
-
|
297
|
-
a = RendererWithHelpers.dup
|
259
|
+
a = RendererWithManyHooks.dup
|
298
260
|
a.required_option :title
|
299
261
|
|
300
262
|
assert_raise(Ruport::Renderer::RequiredOptionNotSet) { a.render(:text) }
|
301
263
|
end
|
302
|
-
|
303
|
-
def test_method_missing
|
304
|
-
actual = TrivialRenderer.render_text
|
305
|
-
assert_equal "header\nbody\nfooter\n", actual
|
306
|
-
end
|
307
|
-
|
308
|
-
def test_formatter
|
309
|
-
#RendererWithHelpers.required_option :title
|
310
264
|
|
311
|
-
|
312
|
-
end
|
265
|
+
end
|
313
266
|
|
314
267
|
|
315
|
-
|
316
|
-
actual = TrivialRenderer.render_text
|
317
|
-
assert_equal "header\nbody\nfooter\n", actual
|
318
|
-
end
|
268
|
+
class TestRendererWithRunHook < Test::Unit::TestCase
|
319
269
|
|
320
|
-
|
321
|
-
# normal instance mode
|
270
|
+
class RendererWithRunHook < Ruport::Renderer
|
322
271
|
|
323
|
-
|
324
|
-
rend.send(:use_formatter,:text)
|
272
|
+
include AutoRunner
|
325
273
|
|
326
|
-
|
327
|
-
assert_kind_of DummyText, rend.formatter
|
274
|
+
add_format DummyText, :text
|
328
275
|
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
276
|
+
required_option :foo,:bar
|
277
|
+
stage :header
|
278
|
+
stage :body
|
279
|
+
stage :footer
|
280
|
+
|
281
|
+
def run
|
282
|
+
formatter.output << "|"
|
333
283
|
end
|
334
284
|
|
335
|
-
|
285
|
+
end
|
336
286
|
|
337
|
-
|
338
|
-
assert_equal "",
|
339
|
-
|
340
|
-
|
341
|
-
def test_options_act_like_indifferent_hash
|
342
|
-
opts = Ruport::Renderer::Options.new
|
343
|
-
opts.foo = "bar"
|
344
|
-
assert_equal "bar", opts[:foo]
|
345
|
-
assert_equal "bar", opts["foo"]
|
346
|
-
|
347
|
-
opts["f"] = "bar"
|
348
|
-
assert_equal "bar", opts[:f]
|
349
|
-
assert_equal "bar", opts.f
|
350
|
-
assert_equal "bar", opts["f"]
|
351
|
-
|
352
|
-
opts[:apple] = "banana"
|
353
|
-
assert_equal "banana", opts.apple
|
354
|
-
assert_equal "banana", opts["apple"]
|
355
|
-
assert_equal "banana", opts[:apple]
|
356
|
-
end
|
357
|
-
|
358
|
-
def test_add_format_private
|
359
|
-
assert_raise(NoMethodError) { Ruport::Renderer.add_format }
|
360
|
-
end
|
361
|
-
|
362
|
-
def test_layout
|
363
|
-
assert_equal "---\nheader\nbody\nfooter\n---\n",
|
364
|
-
YetAnotherRenderer.render_text_with_layout
|
287
|
+
def test_renderer_with_run_hooks
|
288
|
+
assert_equal "|header\nbody\nfooter\n",
|
289
|
+
RendererWithRunHook.render_text(:foo => "bar",:bar => "baz")
|
365
290
|
end
|
366
291
|
|
292
|
+
end
|
293
|
+
|
294
|
+
|
295
|
+
class TestRendererWithHelperModule < Test::Unit::TestCase
|
296
|
+
|
297
|
+
class RendererWithHelperModule < VanillaRenderer
|
298
|
+
|
299
|
+
add_format DummyText, :stub
|
300
|
+
|
301
|
+
module Helpers
|
302
|
+
def say_hello
|
303
|
+
"Hello Dolly"
|
304
|
+
end
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
367
308
|
def test_renderer_helper_module
|
368
309
|
RendererWithHelperModule.render_stub do |r|
|
369
310
|
assert_equal "Hello Dolly", r.formatter.say_hello
|
370
311
|
end
|
371
312
|
end
|
313
|
+
end
|
314
|
+
|
315
|
+
|
316
|
+
class TestMultiPurposeFormatter < Test::Unit::TestCase
|
317
|
+
# This provides a way to check the multi-format hooks for the Renderer
|
318
|
+
class MultiPurposeFormatter < Ruport::Formatter
|
319
|
+
|
320
|
+
renders [:html,:text], :for => VanillaRenderer
|
321
|
+
|
322
|
+
def build_header
|
323
|
+
a = 10
|
324
|
+
|
325
|
+
text { output << "Foo: #{a}\n" }
|
326
|
+
html { output << "<b>Foo: #{a}</b>\n" }
|
327
|
+
end
|
328
|
+
|
329
|
+
def build_body
|
330
|
+
html { output << "<pre>\n" }
|
331
|
+
output << options.body_text
|
332
|
+
html { output << "\n</pre>\n" }
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
336
|
+
|
337
|
+
def test_multi_purpose
|
338
|
+
text = VanillaRenderer.render_text(:body_text => "foo")
|
339
|
+
assert_equal "Foo: 10\nfoo", text
|
340
|
+
html = VanillaRenderer.render_html(:body_text => "bar")
|
341
|
+
assert_equal "<b>Foo: 10</b>\n<pre>\nbar\n</pre>\n",html
|
342
|
+
end
|
343
|
+
|
344
|
+
|
345
|
+
def test_method_missing_hack_formatter
|
346
|
+
assert_equal [:html,:text], MultiPurposeFormatter.formats
|
347
|
+
|
348
|
+
a = MultiPurposeFormatter.new
|
349
|
+
a.format = :html
|
350
|
+
|
351
|
+
visited = false
|
352
|
+
a.html { visited = true }
|
353
|
+
|
354
|
+
assert visited
|
355
|
+
|
356
|
+
visited = false
|
357
|
+
a.text { visited = true }
|
358
|
+
assert !visited
|
359
|
+
|
360
|
+
assert_raises(NoMethodError) do
|
361
|
+
a.pdf { 'do nothing' }
|
362
|
+
end
|
363
|
+
end
|
372
364
|
|
365
|
+
end
|
366
|
+
|
367
|
+
|
368
|
+
class TestFormatterErbHelper < Test::Unit::TestCase
|
369
|
+
class ErbFormatter < Ruport::Formatter
|
370
|
+
|
371
|
+
renders :terb, :for => VanillaRenderer
|
372
|
+
|
373
|
+
def build_body
|
374
|
+
# demonstrate local binding
|
375
|
+
@foo = "bar"
|
376
|
+
if options.binding
|
377
|
+
output << erb("Binding Override: <%= reverse %>",
|
378
|
+
:binding => options.binding)
|
379
|
+
else
|
380
|
+
output << erb("Default Binding: <%= @foo %>")
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
end
|
373
385
|
|
386
|
+
#FIXME: need to test file
|
374
387
|
|
388
|
+
def test_self_bound
|
389
|
+
assert_equal "Default Binding: bar", VanillaRenderer.render_terb
|
390
|
+
end
|
391
|
+
|
392
|
+
def test_custom_bound
|
393
|
+
a = [1,2,3]
|
394
|
+
arr_binding = a.instance_eval { binding }
|
395
|
+
assert_equal "Binding Override: 321",
|
396
|
+
VanillaRenderer.render_terb(:binding => arr_binding)
|
397
|
+
end
|
375
398
|
end
|
399
|
+
|
400
|
+
|