Skip to content

Commit dbe186c

Browse files
Fix/first page empty (#489)
* test: when row fits on current page, should be added to current page * fix: when row fits on current page, should be added to current page * test: Refactor tests Standardize use of names: (when... should...)
1 parent 4cc620d commit dbe186c

File tree

3 files changed

+97
-34
lines changed

3 files changed

+97
-34
lines changed

maroto.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func (m *Maroto) addRow(r core.Row) {
208208
sumHeight := rowHeight + m.currentHeight + m.footerHeight
209209

210210
// Row smaller than the remain space on page
211-
if sumHeight < maxHeight {
211+
if sumHeight <= maxHeight {
212212
m.currentHeight += rowHeight
213213
m.rows = append(m.rows, r)
214214
return

maroto_test.go

+39-33
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestNew(t *testing.T) {
3030
assert.NotNil(t, sut)
3131
assert.Equal(t, "*maroto.Maroto", fmt.Sprintf("%T", sut))
3232
})
33-
t.Run("new with config", func(t *testing.T) {
33+
t.Run("when config is sent, it should create Maroto object", func(t *testing.T) {
3434
// Arrange
3535
cfg := config.NewBuilder().
3636
Build()
@@ -42,7 +42,7 @@ func TestNew(t *testing.T) {
4242
assert.NotNil(t, sut)
4343
assert.Equal(t, "*maroto.Maroto", fmt.Sprintf("%T", sut))
4444
})
45-
t.Run("new with config an concurrent mode on", func(t *testing.T) {
45+
t.Run("when config with an concurrent mode is sent, should create Maroto object", func(t *testing.T) {
4646
// Arrange
4747
cfg := config.NewBuilder().
4848
WithConcurrentMode(7).
@@ -55,7 +55,7 @@ func TestNew(t *testing.T) {
5555
assert.NotNil(t, sut)
5656
assert.Equal(t, "*maroto.Maroto", fmt.Sprintf("%T", sut))
5757
})
58-
t.Run("new with config an low memory mode on", func(t *testing.T) {
58+
t.Run("when config with an low memory mode is sent, should create Maroto object", func(t *testing.T) {
5959
// Arrange
6060
cfg := config.NewBuilder().
6161
WithSequentialLowMemoryMode(10).
@@ -71,26 +71,32 @@ func TestNew(t *testing.T) {
7171
}
7272

7373
func TestMaroto_AddRow(t *testing.T) {
74-
t.Run("when col is not sent, should empty col is set", func(t *testing.T) {
75-
// Arrange
76-
sut := maroto.New()
74+
t.Run("When row height and available sapacing are equals, should add row in current page", func(t *testing.T) {
75+
cfg := config.NewBuilder().
76+
WithDimensions(20, 20).
77+
WithBottomMargin(0).
78+
WithTopMargin(0).
79+
WithLeftMargin(0).
80+
WithRightMargin(0).
81+
Build()
82+
sut := maroto.New(cfg)
7783
// Act
78-
sut.AddRow(10)
84+
sut.AddRow(19)
85+
sut.AddRow(1)
7986

8087
// Assert
81-
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_row_4.json")
88+
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_row.json")
8289
})
83-
t.Run("add one row", func(t *testing.T) {
90+
t.Run("when col is not sent, should empty col is set", func(t *testing.T) {
8491
// Arrange
8592
sut := maroto.New()
86-
8793
// Act
88-
sut.AddRow(10, col.New(12))
94+
sut.AddRow(10)
8995

9096
// Assert
91-
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_row_1.json")
97+
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_row_4.json")
9298
})
93-
t.Run("add one row", func(t *testing.T) {
99+
t.Run("when one row is sent, should create one row", func(t *testing.T) {
94100
// Arrange
95101
sut := maroto.New()
96102

@@ -100,7 +106,7 @@ func TestMaroto_AddRow(t *testing.T) {
100106
// Assert
101107
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_row_1.json")
102108
})
103-
t.Run("add two rows", func(t *testing.T) {
109+
t.Run("when two rows are sent, should create two rows", func(t *testing.T) {
104110
// Arrange
105111
sut := maroto.New()
106112

@@ -111,7 +117,7 @@ func TestMaroto_AddRow(t *testing.T) {
111117
// Assert
112118
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_row_2.json")
113119
})
114-
t.Run("add rows until add new page", func(t *testing.T) {
120+
t.Run("when rows do not fit on the current page, should create a new page", func(t *testing.T) {
115121
// Arrange
116122
sut := maroto.New()
117123

@@ -136,7 +142,7 @@ func TestMaroto_AddRows(t *testing.T) {
136142
// Assert
137143
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_rows_4.json")
138144
})
139-
t.Run("add one row", func(t *testing.T) {
145+
t.Run("when one row is sent, should create one row", func(t *testing.T) {
140146
// Arrange
141147
sut := maroto.New()
142148

@@ -146,7 +152,7 @@ func TestMaroto_AddRows(t *testing.T) {
146152
// Assert
147153
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_rows_1.json")
148154
})
149-
t.Run("add two rows", func(t *testing.T) {
155+
t.Run("when two rows are sent, should create two rows", func(t *testing.T) {
150156
// Arrange
151157
sut := maroto.New()
152158

@@ -157,7 +163,7 @@ func TestMaroto_AddRows(t *testing.T) {
157163
// Assert
158164
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_rows_2.json")
159165
})
160-
t.Run("add rows until add new page", func(t *testing.T) {
166+
t.Run("when rows do not fit on the current page, should create a new page", func(t *testing.T) {
161167
// Arrange
162168
sut := maroto.New()
163169

@@ -200,7 +206,7 @@ func TestMaroto_AddAutoRow(t *testing.T) {
200206
}
201207

202208
func TestMaroto_AddPages(t *testing.T) {
203-
t.Run("add one page", func(t *testing.T) {
209+
t.Run("when a new page is created, should add a page", func(t *testing.T) {
204210
// Arrange
205211
sut := maroto.New()
206212

@@ -214,7 +220,7 @@ func TestMaroto_AddPages(t *testing.T) {
214220
// Assert
215221
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_pages_1.json")
216222
})
217-
t.Run("add two pages", func(t *testing.T) {
223+
t.Run("when two pages are created, should add two pages", func(t *testing.T) {
218224
// Arrange
219225
sut := maroto.New()
220226

@@ -231,7 +237,7 @@ func TestMaroto_AddPages(t *testing.T) {
231237
// Assert
232238
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_pages_2.json")
233239
})
234-
t.Run("add page greater than one page", func(t *testing.T) {
240+
t.Run("when the sent page uses two pages, two pages are created", func(t *testing.T) {
235241
// Arrange
236242
sut := maroto.New()
237243
var rows []core.Row
@@ -248,7 +254,7 @@ func TestMaroto_AddPages(t *testing.T) {
248254
}
249255

250256
func TestMaroto_Generate(t *testing.T) {
251-
t.Run("add one row", func(t *testing.T) {
257+
t.Run("when one row is sent, should generate one row", func(t *testing.T) {
252258
// Arrange
253259
sut := maroto.New()
254260

@@ -260,7 +266,7 @@ func TestMaroto_Generate(t *testing.T) {
260266
assert.Nil(t, err)
261267
assert.NotNil(t, doc)
262268
})
263-
t.Run("add two rows", func(t *testing.T) {
269+
t.Run("when two row are sent, should generate two row", func(t *testing.T) {
264270
// Arrange
265271
sut := maroto.New()
266272

@@ -273,7 +279,7 @@ func TestMaroto_Generate(t *testing.T) {
273279
assert.Nil(t, err)
274280
assert.NotNil(t, doc)
275281
})
276-
t.Run("add rows until add new page", func(t *testing.T) {
282+
t.Run("when rows do not fit on the current page, should generate two pages", func(t *testing.T) {
277283
// Arrange
278284
sut := maroto.New()
279285

@@ -287,7 +293,7 @@ func TestMaroto_Generate(t *testing.T) {
287293
assert.Nil(t, err)
288294
assert.NotNil(t, doc)
289295
})
290-
t.Run("add rows until add new page, execute in parallel", func(t *testing.T) {
296+
t.Run("when rows do not fit on the current page and concurrent mode is active, should executed in parallel", func(t *testing.T) {
291297
// Arrange
292298
cfg := config.NewBuilder().
293299
WithConcurrentMode(7).
@@ -305,7 +311,7 @@ func TestMaroto_Generate(t *testing.T) {
305311
assert.Nil(t, err)
306312
assert.NotNil(t, doc)
307313
})
308-
t.Run("add rows until add new page, execute in low memory mode", func(t *testing.T) {
314+
t.Run("when two pages are sent and low memory mode is active, should executed in low memory mode", func(t *testing.T) {
309315
// Arrange
310316
cfg := config.NewBuilder().
311317
WithSequentialLowMemoryMode(10).
@@ -323,7 +329,7 @@ func TestMaroto_Generate(t *testing.T) {
323329
assert.Nil(t, err)
324330
assert.NotNil(t, doc)
325331
})
326-
t.Run("sequential generation", func(t *testing.T) {
332+
t.Run("when two pages are sent and sequential generation is active, should executed in sequential generation mode", func(t *testing.T) {
327333
// Arrange
328334
cfg := config.NewBuilder().
329335
WithSequentialMode().
@@ -339,7 +345,7 @@ func TestMaroto_Generate(t *testing.T) {
339345
// Assert
340346
test.New(t).Assert(sut.GetStructure()).Equals("maroto_sequential.json")
341347
})
342-
t.Run("sequential low memory generation", func(t *testing.T) {
348+
t.Run("when two pages are sent and sequential low memory is active, should executed in sequential low memory mode", func(t *testing.T) {
343349
// Arrange
344350
cfg := config.NewBuilder().
345351
WithSequentialLowMemoryMode(10).
@@ -355,7 +361,7 @@ func TestMaroto_Generate(t *testing.T) {
355361
// Assert
356362
test.New(t).Assert(sut.GetStructure()).Equals("maroto_sequential_low_memory.json")
357363
})
358-
t.Run("sequential low memory generation", func(t *testing.T) {
364+
t.Run("when two pages are sent and concurrent mode is active, should executed in parallel", func(t *testing.T) {
359365
// Arrange
360366
cfg := config.NewBuilder().
361367
WithConcurrentMode(10).
@@ -396,7 +402,7 @@ func TestMaroto_Generate(t *testing.T) {
396402
assert.Nil(t, err3)
397403
assert.Equal(t, initialGoroutines, finalGoroutines)
398404
})
399-
t.Run("page number", func(t *testing.T) {
405+
t.Run("when two pages are sent and page number is active, should add page number", func(t *testing.T) {
400406
// Arrange
401407
cfg := config.NewBuilder().
402408
WithPageNumber().
@@ -415,7 +421,7 @@ func TestMaroto_Generate(t *testing.T) {
415421
}
416422

417423
func TestMaroto_FitlnCurrentPage(t *testing.T) {
418-
t.Run("when component is smaller should available size, then false", func(t *testing.T) {
424+
t.Run("when component is smaller should available size, should return false", func(t *testing.T) {
419425
sut := maroto.New(config.NewBuilder().
420426
WithDimensions(210.0, 297.0).
421427
Build())
@@ -428,7 +434,7 @@ func TestMaroto_FitlnCurrentPage(t *testing.T) {
428434
sut.AddPages(page.New().Add(rows...))
429435
assert.False(t, sut.FitlnCurrentPage(40))
430436
})
431-
t.Run("when component is larger should the available size, then true", func(t *testing.T) {
437+
t.Run("when component is larger should the available size, should return true", func(t *testing.T) {
432438
sut := maroto.New(config.NewBuilder().
433439
WithDimensions(210.0, 297.0).
434440
Build())
@@ -458,7 +464,7 @@ func TestMaroto_FitlnCurrentPage(t *testing.T) {
458464
}
459465

460466
func TestMaroto_GetCurrentConfig(t *testing.T) {
461-
t.Run("When GetCurrentConfig is called then current settings are returned", func(t *testing.T) {
467+
t.Run("When GetCurrentConfig is called, should return the current settings", func(t *testing.T) {
462468
sut := maroto.New(config.NewBuilder().
463469
WithMaxGridSize(20).
464470
Build())

test/maroto/maroto_add_row.json

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"type": "maroto",
3+
"details": {
4+
"chunk_workers": 1,
5+
"config_max_grid_sum": 12,
6+
"config_provider_type": "gofpdf",
7+
"generation_mode": "sequential",
8+
"maroto_dimension_height": 20,
9+
"maroto_dimension_width": 20,
10+
"prop_font_color": "RGB(0, 0, 0)",
11+
"prop_font_family": "arial",
12+
"prop_font_size": 10
13+
},
14+
"nodes": [
15+
{
16+
"type": "page",
17+
"nodes": [
18+
{
19+
"value": 19,
20+
"type": "row",
21+
"nodes": [
22+
{
23+
"value": 0,
24+
"type": "col",
25+
"details": {
26+
"is_max": true
27+
}
28+
}
29+
]
30+
},
31+
{
32+
"value": 1,
33+
"type": "row",
34+
"nodes": [
35+
{
36+
"value": 0,
37+
"type": "col",
38+
"details": {
39+
"is_max": true
40+
}
41+
}
42+
]
43+
},
44+
{
45+
"value": 0,
46+
"type": "row",
47+
"nodes": [
48+
{
49+
"value": 12,
50+
"type": "col"
51+
}
52+
]
53+
}
54+
]
55+
}
56+
]
57+
}

0 commit comments

Comments
 (0)