Skip to content

Commit

Permalink
Fix/Nil pointer in register header (#474)
Browse files Browse the repository at this point in the history
* test: Validate adding auto-height rows

* Fix: Handle error when creating components with auto height

Add configuration to component before calculating height
  • Loading branch information
Fernando-hub527 authored Sep 17, 2024
1 parent 53f06b8 commit 3b2fae8
Show file tree
Hide file tree
Showing 8 changed files with 4,470 additions and 0 deletions.
1 change: 1 addition & 0 deletions maroto.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ func (m *Maroto) processPage(pages []core.Page) ([]byte, error) {
func (m *Maroto) getRowsHeight(rows ...core.Row) float64 {
var height float64
for _, r := range rows {
r.SetConfig(m.config)
height += r.GetHeight(m.provider, &m.cell)
}

Expand Down
75 changes: 75 additions & 0 deletions maroto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/johnfercher/maroto/v2/pkg/components/code"
"github.com/johnfercher/maroto/v2/pkg/components/text"

"github.com/johnfercher/maroto/v2/pkg/components/col"
"github.com/johnfercher/maroto/v2/pkg/components/page"
Expand Down Expand Up @@ -166,6 +167,34 @@ func TestMaroto_AddRows(t *testing.T) {
// Assert
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_rows_3.json")
})

t.Run("when autoRow is sent, should set autoRow", func(t *testing.T) {
// Arrange
sut := maroto.New()

// Act
for i := 0; i < 20; i++ {
sut.AddRows(row.New().Add(text.NewCol(12, "teste")))
}

// Assert
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_rows_5.json")
})
}

func TestMaroto_AddAutoRow(t *testing.T) {
t.Run("When 100 automatic rows are sent, it should create 2 pages", func(t *testing.T) {
// Arrange
sut := maroto.New()

// Act
for i := 0; i < 150; i++ {
sut.AddAutoRow(text.NewCol(12, "teste"))
}

// Assert
test.New(t).Assert(sut.GetStructure()).Equals("maroto_add_auto_row_1.json")
})
}

func TestMaroto_AddPages(t *testing.T) {
Expand Down Expand Up @@ -385,6 +414,20 @@ func TestMaroto_FitlnCurrentPage(t *testing.T) {
sut.AddPages(page.New().Add(rows...))
assert.True(t, sut.FitlnCurrentPage(40))
})
t.Run("when it have content with an automatic height of 10 and the height sent fits the current page, it should return true",
func(t *testing.T) {
sut := maroto.New(config.NewBuilder().
WithDimensions(210.0, 297.0).
Build())

var rows []core.Row
for i := 0; i < 10; i++ {
rows = append(rows, row.New().Add(text.NewCol(12, "teste")))
}

sut.AddPages(page.New().Add(rows...))
assert.True(t, sut.FitlnCurrentPage(40))
})
}

func TestMaroto_GetCurrentConfig(t *testing.T) {
Expand Down Expand Up @@ -423,6 +466,22 @@ func TestMaroto_RegisterHeader(t *testing.T) {
assert.Nil(t, err)
test.New(t).Assert(sut.GetStructure()).Equals("header.json")
})
t.Run("when autoRow is sent, should set autoRow", func(t *testing.T) {
sut := maroto.New()

err := sut.RegisterHeader(text.NewAutoRow("header"))

var rows []core.Row
for i := 0; i < 5; i++ {
rows = append(rows, row.New(100).Add(col.New(12)))
}

sut.AddRows(rows...)

// Assert
assert.Nil(t, err)
test.New(t).Assert(sut.GetStructure()).Equals("header_auto_row.json")
})
}

// nolint:dupl // dupl is good here
Expand Down Expand Up @@ -451,4 +510,20 @@ func TestMaroto_RegisterFooter(t *testing.T) {
assert.Nil(t, err)
test.New(t).Assert(sut.GetStructure()).Equals("footer.json")
})
t.Run("when autoRow is sent, should set autoRow", func(t *testing.T) {
sut := maroto.New()

err := sut.RegisterFooter(text.NewAutoRow("header"))

var rows []core.Row
for i := 0; i < 5; i++ {
rows = append(rows, row.New(100).Add(col.New(12)))
}

sut.AddRows(rows...)

// Assert
assert.Nil(t, err)
test.New(t).Assert(sut.GetStructure()).Equals("footer_auto_row.json")
})
}
4 changes: 4 additions & 0 deletions test/maroto/examples/billing.json
Original file line number Diff line number Diff line change
Expand Up @@ -2009,7 +2009,9 @@
"type": "text",
"details": {
"prop_align": "L",
"prop_breakline_strategy": "empty_space_strategy",
"prop_color": "RGB(10, 10, 150)",
"prop_font_family": "arial",
"prop_font_size": 8,
"prop_font_style": "BI",
"prop_top": 13
Expand All @@ -2020,7 +2022,9 @@
"type": "text",
"details": {
"prop_align": "L",
"prop_breakline_strategy": "empty_space_strategy",
"prop_color": "RGB(10, 10, 150)",
"prop_font_family": "arial",
"prop_font_size": 8,
"prop_font_style": "BI",
"prop_top": 16
Expand Down
9 changes: 9 additions & 0 deletions test/maroto/examples/footer.json
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@
"type": "text",
"details": {
"prop_align": "C",
"prop_breakline_strategy": "empty_space_strategy",
"prop_color": "RGB(0, 0, 0)",
"prop_font_family": "arial",
"prop_font_size": 10,
"prop_font_style": "B"
}
Expand Down Expand Up @@ -1333,6 +1336,9 @@
"type": "text",
"details": {
"prop_align": "C",
"prop_breakline_strategy": "empty_space_strategy",
"prop_color": "RGB(0, 0, 0)",
"prop_font_family": "arial",
"prop_font_size": 10,
"prop_font_style": "B"
}
Expand Down Expand Up @@ -1424,6 +1430,9 @@
"type": "text",
"details": {
"prop_align": "C",
"prop_breakline_strategy": "empty_space_strategy",
"prop_color": "RGB(0, 0, 0)",
"prop_font_family": "arial",
"prop_font_size": 10,
"prop_font_style": "B"
}
Expand Down
193 changes: 193 additions & 0 deletions test/maroto/footer_auto_row.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
{
"type": "maroto",
"details": {
"chunk_workers": 1,
"config_margin_bottom": 20.0025,
"config_margin_left": 10,
"config_margin_right": 10,
"config_margin_top": 10,
"config_max_grid_sum": 12,
"config_provider_type": "gofpdf",
"generation_mode": "sequential",
"maroto_dimension_height": 297,
"maroto_dimension_width": 210,
"prop_font_color": "RGB(0, 0, 0)",
"prop_font_family": "arial",
"prop_font_size": 10
},
"nodes": [
{
"type": "page",
"nodes": [
{
"value": 100,
"type": "row",
"nodes": [
{
"value": 12,
"type": "col"
}
]
},
{
"value": 100,
"type": "row",
"nodes": [
{
"value": 12,
"type": "col"
}
]
},
{
"value": 63.469722222222224,
"type": "row",
"nodes": [
{
"value": 12,
"type": "col"
}
]
},
{
"value": 3.527777777777778,
"type": "row",
"nodes": [
{
"value": 0,
"type": "col",
"details": {
"is_max": true
},
"nodes": [
{
"value": "header",
"type": "text",
"details": {
"prop_align": "L",
"prop_breakline_strategy": "empty_space_strategy",
"prop_color": "RGB(0, 0, 0)",
"prop_font_family": "arial",
"prop_font_size": 10
}
}
]
}
]
}
]
},
{
"type": "page",
"nodes": [
{
"value": 100,
"type": "row",
"nodes": [
{
"value": 12,
"type": "col"
}
]
},
{
"value": 100,
"type": "row",
"nodes": [
{
"value": 12,
"type": "col"
}
]
},
{
"value": 63.469722222222224,
"type": "row",
"nodes": [
{
"value": 12,
"type": "col"
}
]
},
{
"value": 3.527777777777778,
"type": "row",
"nodes": [
{
"value": 0,
"type": "col",
"details": {
"is_max": true
},
"nodes": [
{
"value": "header",
"type": "text",
"details": {
"prop_align": "L",
"prop_breakline_strategy": "empty_space_strategy",
"prop_color": "RGB(0, 0, 0)",
"prop_font_family": "arial",
"prop_font_size": 10
}
}
]
}
]
}
]
},
{
"type": "page",
"nodes": [
{
"value": 100,
"type": "row",
"nodes": [
{
"value": 12,
"type": "col"
}
]
},
{
"value": 163.46972222222223,
"type": "row",
"nodes": [
{
"value": 12,
"type": "col"
}
]
},
{
"value": 3.527777777777778,
"type": "row",
"nodes": [
{
"value": 0,
"type": "col",
"details": {
"is_max": true
},
"nodes": [
{
"value": "header",
"type": "text",
"details": {
"prop_align": "L",
"prop_breakline_strategy": "empty_space_strategy",
"prop_color": "RGB(0, 0, 0)",
"prop_font_family": "arial",
"prop_font_size": 10
}
}
]
}
]
}
]
}
]
}
Loading

0 comments on commit 3b2fae8

Please sign in to comment.