repolho 0.1.5 → 0.1.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 60d2951254acb9e8431cbda5c6710c74959759ddbeadff1b2d2f1e60fe4d9da6
4
- data.tar.gz: 8b98f398503e47af413bfc9d4d5dc5a32c287946920b9be75cfd8db7f8f1cef2
3
+ metadata.gz: e241b0d81fce8a3b55de4dbf8eccfe3e34a72a95020fd6098bb501a2434257fe
4
+ data.tar.gz: ebb7296a6025cd82acb5461863f7599f52cfb8494184adc29fd6888004584e23
5
5
  SHA512:
6
- metadata.gz: f5b1f71c5f938aac694b6788d025e6965acea778c06f83c99f0159b093c562a2c10977f3987236b39f8c93918d780fe5500f04e34d5b88da484eccda7f931fe5
7
- data.tar.gz: 76d0fabef7c98d14940ae9e9c0cbd1e9a5d5cbe396cc12412ed12d5748a2944c81b2c52db26df8801f653ab3ae7e761bb48f0596a85a0befa721dced04b0f17e
6
+ metadata.gz: 773100dbaed0b7d43c0b8ca8139fcbd7c925ef18515ef39d89e8131f3738bfc4edb8070188a64c40cea5ea03532d4b07f1dcfe57ac66890b88954735b1b22a51
7
+ data.tar.gz: 97150a6758cd926f08efbe920d5be2013cbf5e92d77f4768947086ceb0e333bb0fabe187bf624127a2417f3ed2b99f4cf233dc84029361e5e87dc857e12525ee
checksums.yaml.gz.sig CHANGED
@@ -1,4 +1,3 @@
1
- ��o�.�n��-w-��{�a��2�#귗��W�\�)�G��y�U"�cy��C�ׁ���#������D�c�� ޳�1�
2
- �IY�qsͼS���= ��K���tT��L����S� 5tA�ک�nl��� ���=VI�լu�g��� �*[��b™�
3
- w����-&�>�
4
-
1
+ m��cN��N]ķ���Y\N�N/NE�]�d�%�qz�I�}�2�*��ֻ�f����� 6{�� F��H��6-F�
2
+ ��{��#7]�̩i��ѭ�][�dp�Tz�0��,{����ғk�y����C赅���U��9.���7�`�Q����DZ���p��e��y��r_�U$K�s�F7 Y�A�6GL?��u}e�1��"�4м)�k;BQ�
3
+ v>Q��).ꨟ\U3��^75)og��KC'��Q�|=��s�o�ߜV|�nُ�MC����$�x}ZQ�2P�̀M}��|椂�c�X[w�L�q��|�Z=}'N�|�&yn�V�,
data/README.md CHANGED
@@ -300,6 +300,27 @@ Repolho::Main.new(path, data, current_template: @current_template, user: current
300
300
 
301
301
  They become `@current_template` and `@user` inside the templates.
302
302
 
303
+ ## Testing
304
+
305
+ The project uses [Minitest](https://github.com/minitest/minitest).
306
+
307
+ ```bash
308
+ # From the gem root
309
+ bundle install
310
+ bundle exec rake test
311
+ # or
312
+ ruby -Ilib:test test/stats_test.rb
313
+ ```
314
+
315
+ Tests cover:
316
+
317
+ - `Stats` (sum, avg, min, max, count, reset, numeric coercion)
318
+ - `Container` (ERB rendering of headers/footers/rows, attribute access, stats delegation, status)
319
+ - `Page` (row counting via `<tr>`, page open/close, page breaks, raw append)
320
+ - `Report` (inheritance and composition)
321
+ - `Main` (data normalization for hashes / ActiveRecord-like objects / models, section extraction, full HTML generation, groupings, page breaks, pagination, print CSS for orientation/paper, previous/after ERB)
322
+
323
+ Integration tests assert on the generated HTML structure (`tbody.repolho-page`, data attributes, totals, labels).
303
324
 
304
325
  ## Signing
305
326
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Repolho
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.6"
5
5
  end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "test_helper"
4
+
5
+ module Repolho
6
+ class ContainerTest < Minitest::Test
7
+ def setup
8
+ @stats = { "value" => Stats.new }
9
+ @locals = { company: "Acme" }
10
+ @container = Container.new("group1", @locals, @stats)
11
+ @page = Page.new("page", {}, { "value" => Stats.new })
12
+ @container.page = @page
13
+ end
14
+
15
+ def test_locals_become_instance_variables
16
+ assert_equal "Acme", @container.instance_variable_get(:@company)
17
+ end
18
+
19
+ def test_sum_avg_count_min_max_delegate_to_stats
20
+ @stats["value"].add(10)
21
+ @stats["value"].add(20)
22
+
23
+ assert_equal 30.0, @container.sum(:value)
24
+ assert_in_delta 15.0, @container.avg(:value), 0.001
25
+ assert_equal 2, @container.count(:value)
26
+ assert_equal 10, @container.min(:value)
27
+ assert_equal 20, @container.max(:value)
28
+ end
29
+
30
+ def test_sum_missing_attr_returns_zero
31
+ assert_equal 0.0, @container.sum(:missing)
32
+ assert_equal 0, @container.count(:missing)
33
+ end
34
+
35
+ def test_attr_returns_current_row_value
36
+ @container.instance_variable_set(:@cur_row, { "id" => 42, "name" => "Test" })
37
+
38
+ assert_equal 42, @container.attr(:id)
39
+ assert_equal "Test", @container.attr(:name)
40
+ assert_nil @container.attr(:missing)
41
+ end
42
+
43
+ def test_attr_without_current_row
44
+ assert_equal "", @container.attr(:anything)
45
+ end
46
+
47
+ def test_start_renders_header_erb
48
+ @container.header = { content: "<tr><td><%= @company %></td></tr>" }
49
+
50
+ result = @container.start
51
+
52
+ assert_includes result, "Acme"
53
+ assert_includes result, "<tr>"
54
+ end
55
+
56
+ def test_finish_renders_footer_erb_and_increments_number
57
+ @container.footer = { content: "<tr><td>Total: <%= sum(:value) %></td></tr>" }
58
+ @stats["value"].add(100)
59
+
60
+ result = @container.finish
61
+
62
+ assert_includes result, "100"
63
+ assert_equal 2, @container.number
64
+ end
65
+
66
+ def test_row_renders_template
67
+ @container.row_template = "<tr><td><%= attr(:id) %></td></tr>"
68
+ @container.instance_variable_set(:@cur_row, { "id" => 7 })
69
+
70
+ result = @container.row
71
+
72
+ assert_includes result, "7"
73
+ end
74
+
75
+ def test_row_without_template_returns_nil
76
+ assert_nil @container.row
77
+ end
78
+
79
+ def test_force_header_reprint
80
+ @container.status = Container::STARTED
81
+ @container.force_header_reprint
82
+
83
+ assert_equal Container::FINISHED, @container.status
84
+ end
85
+
86
+ def test_reset_stats
87
+ @stats["value"].add(50)
88
+ @container.reset_stats
89
+
90
+ assert_equal 0.0, @stats["value"].sum
91
+ end
92
+
93
+ def test_status_constants
94
+ assert_equal 0, Container::UNSTARTED
95
+ assert_equal 1, Container::STARTED
96
+ assert_equal 2, Container::FINISHED
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head><title>Fixture</title></head>
4
+ <body>
5
+ <table>
6
+ <report>
7
+ <report-page rows="10">
8
+ <report-row>
9
+ <tr><td><%= attr(:id) %></td><td><%= attr(:value) %></td></tr>
10
+ </report-row>
11
+ </report-page>
12
+ </report>
13
+ </table>
14
+ </body>
15
+ </html>
data/test/main_test.rb ADDED
@@ -0,0 +1,397 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "test_helper"
4
+
5
+ module Repolho
6
+ class MainTest < Minitest::Test
7
+ include TestHelpers
8
+
9
+ SIMPLE_TEMPLATE = <<~HTML
10
+ <!DOCTYPE html>
11
+ <html>
12
+ <head><title>Test</title></head>
13
+ <body>
14
+ <table>
15
+ <report>
16
+ <report-header>
17
+ <tr><th>Report Header <%= @company %></th></tr>
18
+ </report-header>
19
+ <report-page rows="5">
20
+ <report-header>
21
+ <tr><th>Id</th><th>Value</th></tr>
22
+ </report-header>
23
+ <report-footer>
24
+ <tr><td>Page total</td><td><%= sum(:value) %></td></tr>
25
+ </report-footer>
26
+ <report-row>
27
+ <tr>
28
+ <td><%= attr(:id) %></td>
29
+ <td><%= attr(:value) %></td>
30
+ </tr>
31
+ </report-row>
32
+ </report-page>
33
+ <report-footer>
34
+ <tr><td>Grand total</td><td><%= sum(:value) %></td></tr>
35
+ </report-footer>
36
+ </report>
37
+ </table>
38
+ </body>
39
+ </html>
40
+ HTML
41
+
42
+ GROUP_TEMPLATE = <<~HTML
43
+ <!DOCTYPE html>
44
+ <html>
45
+ <body>
46
+ <table>
47
+ <report>
48
+ <report-page rows="10">
49
+ <report-header>
50
+ <tr><th>Name</th><th>Value</th></tr>
51
+ </report-header>
52
+ <report-group id="state" attr="state">
53
+ <report-header>
54
+ <tr><td colspan="2">State: <%= attr(:state) %></td></tr>
55
+ </report-header>
56
+ <report-footer>
57
+ <tr><td>State total</td><td><%= sum(:value) %></td></tr>
58
+ </report-footer>
59
+ <report-row>
60
+ <tr>
61
+ <td><%= attr(:name) %></td>
62
+ <td><%= attr(:value) %></td>
63
+ </tr>
64
+ </report-row>
65
+ </report-group>
66
+ </report-page>
67
+ </report>
68
+ </table>
69
+ </body>
70
+ </html>
71
+ HTML
72
+
73
+ def setup
74
+ @data = [
75
+ { "id" => 1, "name" => "Alice", "value" => 10.0, "state" => "SP" },
76
+ { "id" => 2, "name" => "Bob", "value" => 20.0, "state" => "SP" },
77
+ { "id" => 3, "name" => "Carol", "value" => 15.0, "state" => "RJ" }
78
+ ]
79
+ end
80
+
81
+ def test_normalize_data_array_of_hashes
82
+ main = Main.new("/dev/null", @data)
83
+ normalized = main.send(:normalize_data, @data)
84
+
85
+ assert_equal 3, normalized.size
86
+ assert_equal "Alice", normalized[0]["name"]
87
+ assert_kind_of String, normalized[0].keys.first
88
+ end
89
+
90
+ def test_normalize_data_empty
91
+ main = Main.new("/dev/null", [])
92
+ assert_empty main.send(:normalize_data, [])
93
+ assert_empty main.send(:normalize_data, nil)
94
+ end
95
+
96
+ def test_normalize_data_active_record_result_like
97
+ result = Object.new
98
+ rows = [{ "id" => 1, "value" => 100 }]
99
+ result.define_singleton_method(:columns) { %w[id value] }
100
+ result.define_singleton_method(:each) { |&b| rows.each(&b) }
101
+ result.define_singleton_method(:map) { |&b| rows.map(&b) }
102
+
103
+ main = Main.new("/dev/null", result)
104
+ normalized = main.send(:normalize_data, result)
105
+
106
+ assert_equal 1, normalized.size
107
+ assert_equal 100, normalized[0]["value"]
108
+ end
109
+
110
+ def test_normalize_data_models_with_attributes
111
+ model = Object.new
112
+ model.define_singleton_method(:attributes) { { "id" => 5, "name" => "X" } }
113
+
114
+ main = Main.new("/dev/null", [model])
115
+ normalized = main.send(:normalize_data, [model])
116
+
117
+ assert_equal "X", normalized[0]["name"]
118
+ end
119
+
120
+ def test_extract_sections
121
+ template = write_temp_template(SIMPLE_TEMPLATE)
122
+ main = Main.new(template.path, @data, { company: "Acme" })
123
+ main.send(:parse)
124
+
125
+ assert_includes main.instance_variable_get(:@previous), "<!DOCTYPE html>"
126
+ assert_includes main.instance_variable_get(:@body), "<body>"
127
+ assert_includes main.instance_variable_get(:@report_xml), "<report>"
128
+ assert_includes main.instance_variable_get(:@after), "</html>"
129
+ ensure
130
+ template&.unlink
131
+ end
132
+
133
+ def test_simple_report_html_structure
134
+ # Use higher row limit so headers/footers + 3 data rows fit in one page
135
+ template_content = SIMPLE_TEMPLATE.gsub('rows="5"', 'rows="20"')
136
+ template = write_temp_template(template_content)
137
+ main = Main.new(template.path, @data, { company: "Acme" })
138
+ main.run
139
+ html = main.content.join
140
+ doc = html_doc(html)
141
+
142
+ assert_includes html, "Acme"
143
+ assert_equal 1, page_tbodies(doc).size
144
+ assert_includes html, "1"
145
+ assert_includes html, "10"
146
+ assert_includes html, "Grand total"
147
+ # 10+20+15 = 45
148
+ assert_includes html, "45"
149
+ ensure
150
+ template&.unlink
151
+ end
152
+
153
+ def test_group_breaks_on_attribute_change
154
+ template = write_temp_template(GROUP_TEMPLATE)
155
+ main = Main.new(template.path, @data)
156
+ main.run
157
+ html = main.content.join
158
+
159
+ assert_includes html, "State: SP"
160
+ assert_includes html, "State: RJ"
161
+ # SP total 30, RJ total 15
162
+ assert_includes html, "30"
163
+ assert_includes html, "15"
164
+ ensure
165
+ template&.unlink
166
+ end
167
+
168
+ def test_page_break_with_small_row_limit
169
+ small_page = SIMPLE_TEMPLATE.gsub('rows="5"', 'rows="2"')
170
+ template = write_temp_template(small_page)
171
+ main = Main.new(template.path, @data, { company: "Test" })
172
+ main.run
173
+ html = main.content.join
174
+ doc = html_doc(html)
175
+
176
+ pages = page_tbodies(doc)
177
+ assert pages.size >= 2, "Expected multiple pages, got #{pages.size}"
178
+ assert_includes html, 'data-page="1"'
179
+ assert_includes html, 'data-page="2"'
180
+ ensure
181
+ template&.unlink
182
+ end
183
+
184
+ def test_pagination_enabled_when_multiple_pages
185
+ paginated = SIMPLE_TEMPLATE.gsub("<report>", '<report pagination="true">').gsub('rows="5"', 'rows="2"')
186
+ template = write_temp_template(paginated)
187
+ main = Main.new(template.path, @data)
188
+ main.run
189
+ html = main.content.join
190
+
191
+ assert_includes html, 'id="report-pagination"'
192
+ assert_includes html, "change_report_page"
193
+ assert_includes html, "First"
194
+ ensure
195
+ template&.unlink
196
+ end
197
+
198
+ def test_pagination_omitted_for_single_page
199
+ # High row limit guarantees a single page even with headers/footers
200
+ paginated = SIMPLE_TEMPLATE.gsub("<report>", '<report pagination="true">').gsub('rows="5"', 'rows="50"')
201
+ template = write_temp_template(paginated)
202
+ main = Main.new(template.path, @data)
203
+ main.run
204
+ html = main.content.join
205
+
206
+ refute_includes html, 'id="report-pagination"'
207
+ ensure
208
+ template&.unlink
209
+ end
210
+
211
+ def test_custom_pagination_labels
212
+ paginated = <<~HTML
213
+ <!DOCTYPE html>
214
+ <html><body>
215
+ <table>
216
+ <report pagination="true" pagination-first="Primeira" pagination-last="Última" pagination-prev="«" pagination-next="»">
217
+ <report-page rows="2">
218
+ <report-row><tr><td><%= attr(:id) %></td></tr></report-row>
219
+ </report-page>
220
+ </report>
221
+ </table>
222
+ </body></html>
223
+ HTML
224
+ template = write_temp_template(paginated)
225
+ main = Main.new(template.path, @data)
226
+ main.run
227
+ html = main.content.join
228
+
229
+ assert_includes html, "Primeira"
230
+ assert_includes html, "Última"
231
+ assert_includes html, "«"
232
+ ensure
233
+ template&.unlink
234
+ end
235
+
236
+ def test_orientation_and_paper_inject_print_css
237
+ oriented = SIMPLE_TEMPLATE.gsub("<report>", '<report orientation="landscape" paper="A4">')
238
+ template = write_temp_template(oriented)
239
+ main = Main.new(template.path, @data)
240
+ main.run
241
+ html = main.content.join
242
+
243
+ assert_includes html, "@media print"
244
+ assert_includes html, "size: A4 landscape"
245
+ ensure
246
+ template&.unlink
247
+ end
248
+
249
+ def test_previous_and_after_are_rendered
250
+ # previous (before <body>) and after (after </report>) are ERB-processed.
251
+ # Content between <body> and <report> is stored in @body and inserted raw.
252
+ template_content = <<~HTML
253
+ <!DOCTYPE html>
254
+ <html>
255
+ <head><title><%= @title %></title></head>
256
+ <body>
257
+ <p>Before raw</p>
258
+ <table>
259
+ <report>
260
+ <report-page rows="10">
261
+ <report-row><tr><td><%= attr(:id) %></td></tr></report-row>
262
+ </report-page>
263
+ </report>
264
+ </table>
265
+ <p>After <%= @company %></p>
266
+ </body>
267
+ </html>
268
+ HTML
269
+ template = write_temp_template(template_content)
270
+ main = Main.new(template.path, @data, { title: "My Report", company: "Acme" })
271
+ main.run
272
+ html = main.content.join
273
+
274
+ assert_includes html, "My Report"
275
+ assert_includes html, "Before raw"
276
+ assert_includes html, "After Acme"
277
+ ensure
278
+ template&.unlink
279
+ end
280
+
281
+ def test_reprint_header_on_page_break_attribute
282
+ reprint = GROUP_TEMPLATE.gsub(
283
+ '<report-group id="state" attr="state">',
284
+ '<report-group id="state" attr="state" reprint-header-on-page-break="true">'
285
+ )
286
+ template = write_temp_template(reprint)
287
+ main = Main.new(template.path, @data)
288
+ main.send(:parse)
289
+
290
+ groups = main.instance_variable_get(:@report).containers
291
+ # The page has the group as container
292
+ group = groups.first.containers.first
293
+ assert group.reprint_header_on_page_break
294
+ ensure
295
+ template&.unlink
296
+ end
297
+
298
+ def test_group_header_reprinted_on_page_break
299
+ # Force a page break in the middle of the same group
300
+ data = [
301
+ { "id" => 1, "name" => "Alice", "value" => 10.0, "state" => "SP" },
302
+ { "id" => 2, "name" => "Bob", "value" => 20.0, "state" => "SP" },
303
+ { "id" => 3, "name" => "Carol", "value" => 15.0, "state" => "SP" },
304
+ { "id" => 4, "name" => "Dave", "value" => 25.0, "state" => "SP" },
305
+ { "id" => 5, "name" => "Eve", "value" => 30.0, "state" => "SP" }
306
+ ]
307
+
308
+ template_content = GROUP_TEMPLATE.gsub(
309
+ '<report-page rows="10">',
310
+ '<report-page rows="3">'
311
+ ).gsub(
312
+ '<report-group id="state" attr="state">',
313
+ '<report-group id="state" attr="state" reprint-header-on-page-break="true">'
314
+ )
315
+
316
+ template = write_temp_template(template_content)
317
+ main = Main.new(template.path, data)
318
+ main.run
319
+ html = main.content.join
320
+ doc = html_doc(html)
321
+
322
+ pages = page_tbodies(doc)
323
+ assert pages.size >= 2, "Expected page break, got #{pages.size} page(s)"
324
+
325
+ # Group header should appear more than once because of reprint
326
+ header_count = html.scan(/State: SP/).size
327
+ assert header_count >= 2, "Expected group header to be reprinted, found #{header_count} time(s)"
328
+ ensure
329
+ template&.unlink
330
+ end
331
+
332
+ def test_page_break_across_groups
333
+ # Page break happens while groups are changing
334
+ data = [
335
+ { "id" => 1, "name" => "Alice", "value" => 10.0, "state" => "SP" },
336
+ { "id" => 2, "name" => "Bob", "value" => 20.0, "state" => "SP" },
337
+ { "id" => 3, "name" => "Carol", "value" => 15.0, "state" => "RJ" },
338
+ { "id" => 4, "name" => "Dave", "value" => 25.0, "state" => "RJ" },
339
+ { "id" => 5, "name" => "Eve", "value" => 30.0, "state" => "MG" }
340
+ ]
341
+
342
+ template_content = GROUP_TEMPLATE.gsub(
343
+ '<report-page rows="10">',
344
+ '<report-page rows="2">'
345
+ )
346
+
347
+ template = write_temp_template(template_content)
348
+ main = Main.new(template.path, data)
349
+ main.run
350
+ html = main.content.join
351
+ doc = html_doc(html)
352
+
353
+ pages = page_tbodies(doc)
354
+ assert pages.size >= 2
355
+
356
+ assert_includes html, "State: SP"
357
+ assert_includes html, "State: RJ"
358
+ assert_includes html, "State: MG"
359
+ ensure
360
+ template&.unlink
361
+ end
362
+
363
+ def test_make_columns_detects_numeric
364
+ main = Main.new("/dev/null", @data)
365
+ columns = main.send(:make_columns)
366
+
367
+ assert_includes columns, "id"
368
+ assert_includes columns, "value"
369
+ refute_includes columns, "name"
370
+ refute_includes columns, "state"
371
+ end
372
+
373
+ def test_page_number_available_in_after_erb
374
+ after_erb = <<~HTML
375
+ <!DOCTYPE html>
376
+ <html><body>
377
+ <table>
378
+ <report>
379
+ <report-page rows="2">
380
+ <report-row><tr><td><%= attr(:id) %></td></tr></report-row>
381
+ </report-page>
382
+ </report>
383
+ </table>
384
+ <p>Total pages: <%= page_number %></p>
385
+ </body></html>
386
+ HTML
387
+ template = write_temp_template(after_erb)
388
+ main = Main.new(template.path, @data)
389
+ main.run
390
+ html = main.content.join
391
+
392
+ assert_match(/Total pages: \d+/, html)
393
+ ensure
394
+ template&.unlink
395
+ end
396
+ end
397
+ end
data/test/page_test.rb ADDED
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "test_helper"
4
+
5
+ module Repolho
6
+ class PageTest < Minitest::Test
7
+ def setup
8
+ @stats = { "value" => Stats.new }
9
+ @page = Page.new("page", { title: "Report" }, @stats)
10
+ end
11
+
12
+ def test_default_rows
13
+ assert_equal 60, @page.rows
14
+ assert_equal 1, @page.row_number
15
+ assert_equal 1, @page.page_number
16
+ end
17
+
18
+ def test_rows_setter_updates_valid_rows
19
+ @page.rows = 20
20
+
21
+ assert_equal 20, @page.rows
22
+ end
23
+
24
+ def test_append_raw_adds_content_without_counting
25
+ @page.append_raw("<html><body>")
26
+
27
+ assert_includes @page.content, "<html><body>"
28
+ assert_equal 1, @page.row_number # unchanged
29
+ end
30
+
31
+ def test_shovel_counts_tr_and_opens_page
32
+ @page << "<tr><td>one</td></tr><tr><td>two</td></tr>"
33
+
34
+ assert_equal 3, @page.row_number # started at 1 + 2
35
+ assert_equal 2, @page.content.size # open_page tag + content
36
+ assert_includes @page.content.join, 'class="repolho-page"'
37
+ assert_includes @page.content.join, 'data-page="1"'
38
+ end
39
+
40
+ def test_shovel_nil_is_ignored
41
+ @page << nil
42
+
43
+ assert_empty @page.content
44
+ end
45
+
46
+ def test_open_and_close_page
47
+ @page.send(:open_page)
48
+ assert_includes @page.content.join, 'data-page="1"'
49
+
50
+ @page.send(:close_page)
51
+ assert_includes @page.content.join, "</tbody>"
52
+ assert_equal 2, @page.page_number
53
+ end
54
+
55
+ def test_finalize_closes_open_page
56
+ @page << "<tr><td>x</td></tr>"
57
+ @page.finalize
58
+
59
+ assert_includes @page.content.join, "</tbody>"
60
+ end
61
+
62
+ def test_page_break_when_row_limit_reached
63
+ @page.rows = 3
64
+ @page.page = @page # required so << works inside check_page_break
65
+ # Force row_number high enough
66
+ @page.instance_variable_set(:@row_number, 3)
67
+ @page.footer = { content: "<tr><td>footer</td></tr>" }
68
+
69
+ @page.send(:check_page_break)
70
+
71
+ assert_equal Container::FINISHED, @page.status
72
+ assert_equal 0, @page.row_number
73
+ end
74
+
75
+ def test_content_is_array_of_strings
76
+ @page.append_raw("before")
77
+ @page << "<tr><td>row</td></tr>"
78
+ @page.finalize
79
+
80
+ assert_kind_of Array, @page.content
81
+ assert @page.content.all? { |c| c.is_a?(String) }
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "test_helper"
4
+
5
+ module Repolho
6
+ class ReportTest < Minitest::Test
7
+ def test_report_is_a_container
8
+ stats = { "value" => Stats.new }
9
+ report = Report.new("report", {}, stats)
10
+
11
+ assert_kind_of Container, report
12
+ assert_equal "report", report.id
13
+ end
14
+
15
+ def test_report_can_hold_page
16
+ stats = { "value" => Stats.new }
17
+ report = Report.new("report", {}, stats)
18
+ page = Page.new("page", {}, stats)
19
+ report.containers << page
20
+ report.page = page
21
+
22
+ assert_equal page, report.containers.first
23
+ assert_equal page, report.page
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "test_helper"
4
+
5
+ module Repolho
6
+ class StatsTest < Minitest::Test
7
+ def setup
8
+ @stats = Stats.new
9
+ end
10
+
11
+ def test_initial_state
12
+ assert_equal 0.0, @stats.sum
13
+ assert_equal 0, @stats.count
14
+ assert_nil @stats.min
15
+ assert_nil @stats.max
16
+ assert_equal 0.0, @stats.avg
17
+ end
18
+
19
+ def test_add_numeric
20
+ @stats.add(10)
21
+ @stats.add(20.5)
22
+
23
+ assert_equal 30.5, @stats.sum
24
+ assert_equal 2, @stats.count
25
+ assert_equal 10, @stats.min
26
+ assert_equal 20.5, @stats.max
27
+ assert_in_delta 15.25, @stats.avg, 0.001
28
+ end
29
+
30
+ def test_add_string_numeric
31
+ @stats.add("15")
32
+ @stats.add("-5.5")
33
+
34
+ assert_equal 9.5, @stats.sum
35
+ assert_equal 2, @stats.count
36
+ assert_equal(-5.5, @stats.min)
37
+ assert_equal 15.0, @stats.max
38
+ end
39
+
40
+ def test_add_nil_is_ignored
41
+ @stats.add(nil)
42
+ @stats.add(10)
43
+
44
+ assert_equal 10.0, @stats.sum
45
+ assert_equal 1, @stats.count
46
+ end
47
+
48
+ def test_avg_zero_count
49
+ assert_equal 0.0, @stats.avg
50
+ end
51
+
52
+ def test_reset
53
+ @stats.add(100)
54
+ @stats.reset
55
+
56
+ assert_equal 0.0, @stats.sum
57
+ assert_equal 0, @stats.count
58
+ assert_nil @stats.min
59
+ assert_nil @stats.max
60
+ end
61
+
62
+ def test_min_max_single_value
63
+ @stats.add(42)
64
+
65
+ assert_equal 42, @stats.min
66
+ assert_equal 42, @stats.max
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+
5
+ require "minitest/autorun"
6
+ require "repolho"
7
+ require "nokogiri"
8
+ require "tempfile"
9
+ require "stringio"
10
+
11
+ module TestHelpers
12
+ def fixture_path(name)
13
+ File.join(__dir__, "fixtures", name)
14
+ end
15
+
16
+ def write_temp_template(content)
17
+ file = Tempfile.new(["report", ".rep.erb"])
18
+ file.write(content)
19
+ file.close
20
+ file
21
+ end
22
+
23
+ def html_doc(html)
24
+ Nokogiri::HTML(html)
25
+ end
26
+
27
+ def page_tbodies(doc)
28
+ doc.css("tbody.repolho-page")
29
+ end
30
+ end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repolho
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eustáquio Rangel
@@ -51,6 +51,34 @@ dependencies:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: '1.16'
54
+ - !ruby/object:Gem::Dependency
55
+ name: minitest
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '5.20'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '5.20'
68
+ - !ruby/object:Gem::Dependency
69
+ name: rake
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '13.0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '13.0'
54
82
  description: Repolho generates HTML reports from .rep.erb templates with support for
55
83
  nested groupings, page breaks, headers/footers, and statistics (sum, avg, min, max).
56
84
  Works with ActiveRecord::Result, Relation, or arrays of hashes.
@@ -70,6 +98,13 @@ files:
70
98
  - lib/repolho/report.rb
71
99
  - lib/repolho/stats.rb
72
100
  - lib/repolho/version.rb
101
+ - test/container_test.rb
102
+ - test/fixtures/simple.rep.erb
103
+ - test/main_test.rb
104
+ - test/page_test.rb
105
+ - test/report_test.rb
106
+ - test/stats_test.rb
107
+ - test/test_helper.rb
73
108
  homepage: https://github.com/taq/repolho
74
109
  licenses:
75
110
  - MIT
metadata.gz.sig CHANGED
@@ -1 +1,4 @@
1
- �i��tٗ.U�ג���Έ+{��Ws�����N���pX��U�s\̴ڈa�@s|.����%���LT$|�\��J�[+3�\A����MV��(�����y��۫�uv����-�2��(�:��KdKRZf��hʹ���8[�ό���'����q6��� �Wt#'!5���E��,I.Zo_7����vO�A��B�h��e���u�.ȟ��*%�zɳ��O I�#^��Ӥ(~Fӗ�.�:[F�L���:�`��Ld�!θ���y��\hø�(�[}G�?���p��eST�2��?���E�t�T�^�2v? ����������G`M ������/�}Ce�a��fz���<>]��_�����{���je�����/AH�7*
1
+ �e�G%�XY��6�H�2@ )����~1+ş]����,,x�؅J�O�Rh/�{#G�+K�I�Cem#���`�c
2
+ �Gf_���rPտ�e�t�E�>F�e����R9]�^�of&�%�l��N�x�ɔ�
3
+ ���2����� �?��|T�����,���׋�bɪ�A*��<�s�E]!f�����T��d�(��[�򟛹� �b���h�P a"��En�㦴a�\W�<J�B<�̙u��1�==�����-�����oB8�.r ����|@�'�����(q-O��S��Wۄ]&������kI�Sz��E������ۏ/ w�P&���E����sn��
4
+ �V�O/�w�)Zt��eY]�W�wG`�K�