Skip to content

Commit

Permalink
Add tests on maroto level (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfercher authored Oct 10, 2023
1 parent 0cecd01 commit bb2e738
Show file tree
Hide file tree
Showing 50 changed files with 1,940 additions and 410 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ always when a new page appear, in this case, a header may have many rows, lines
* With `go get`:

```bash
go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.39
go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.40
```


Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#template-engines) [![Branch](https://img.shields.io/badge/V2-Branch-pink)](https://github.com/johnfercher/maroto/tree/v2) [![Roadmap](https://img.shields.io/badge/V2-Roadmap-purple)](https://github.com/users/johnfercher/projects/1) [![Discussion](https://img.shields.io/badge/V2-Discussion-blue)](https://github.com/johnfercher/maroto/issues/257) [![Release Notes](https://img.shields.io/badge/Release-Notes-cyan)](https://github.com/johnfercher/maroto/releases) [![Visits Badge](https://badges.pufler.dev/visits/johnfercher/maroto)](https://badges.pufler.dev)

#### Maroto`v2.0.0-alpha.39`is here! Try out:
#### Maroto`v2.0.0-alpha.40`is here! Try out:

* Installation with`go get`:

```bash
go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.39
go get github.com/johnfercher/maroto/v2/pkg@v2.0.0-alpha.40
```

The public API was completely redesigned with the aim of enhancing the
Expand Down
3 changes: 2 additions & 1 deletion maroto.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ func (m *maroto) GetStructure() *node.Node[core.Structure] {
m.fillPageToAddNew()

str := core.Structure{
Type: "maroto",
Type: "maroto",
Details: m.config.ToMap(),
}
node := node.New(str)

Expand Down
35 changes: 33 additions & 2 deletions pkg/components/col/col_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNew(t *testing.T) {
// Assert
test.New(t).Assert(c.GetStructure()).Equals("components/cols/new_zero_size.json")
})
t.Run("when size is defined, should use not use max", func(t *testing.T) {
t.Run("when size is defined, should not use max", func(t *testing.T) {
// Act
c := col.New(12)

Expand All @@ -36,6 +36,14 @@ func TestNew(t *testing.T) {
// Assert
test.New(t).Assert(c.GetStructure()).Equals("components/cols/new_with_components.json")
})
t.Run("when has component, should retrieve components", func(t *testing.T) {
// Act
prop := fixture.CellProp()
c := col.New(12).WithStyle(&prop)

// Assert
test.New(t).Assert(c.GetStructure()).Equals("components/cols/new_with_props.json")
})
}

func TestCol_GetSize(t *testing.T) {
Expand Down Expand Up @@ -63,7 +71,30 @@ func TestCol_GetSize(t *testing.T) {
}

func TestCol_Render(t *testing.T) {
t.Run("should call provider correctly", func(t *testing.T) {
t.Run("when not createCell, should call provider correctly", func(t *testing.T) {
// Arrange
cfg := &entity.Config{}
cell := fixture.CellEntity()
style := &props.Cell{}

provider := &mocks.Provider{}

component := &mocks.Component{}
component.EXPECT().Render(provider, &cell)
component.EXPECT().SetConfig(cfg)

sut := col.New(12).Add(component)
sut.WithStyle(style)
sut.SetConfig(cfg)

// Act
sut.Render(provider, cell, false)

// Assert
component.AssertNumberOfCalls(t, "Render", 1)
component.AssertNumberOfCalls(t, "SetConfig", 1)
})
t.Run("when createCell, should call provider correctly", func(t *testing.T) {
// Arrange
cfg := &entity.Config{}
cell := fixture.CellEntity()
Expand Down
129 changes: 129 additions & 0 deletions pkg/components/list/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package list_test

import (
"fmt"
"testing"

"github.com/johnfercher/maroto/v2/internal/fixture"
"github.com/johnfercher/maroto/v2/pkg/components/list"
"github.com/johnfercher/maroto/v2/pkg/components/page"
"github.com/johnfercher/maroto/v2/pkg/components/row"
"github.com/johnfercher/maroto/v2/pkg/components/text"
"github.com/johnfercher/maroto/v2/pkg/core"
"github.com/johnfercher/maroto/v2/pkg/test"
"github.com/stretchr/testify/assert"
)

type anyType struct {
Key string
Value string
}

func (a anyType) GetHeader() core.Row {
r := row.New(10).Add(
text.NewCol(6, "Key"),
text.NewCol(6, "Value"),
)

return r
}

func (a anyType) GetContent(i int) core.Row {
r := row.New(10).Add(
text.NewCol(6, a.Key),
text.NewCol(6, a.Value),
)

if i%2 == 0 {
cell := fixture.CellProp()
r.WithStyle(&cell)
}

return r
}

func TestBuild(t *testing.T) {
t.Run("when arr is empty, should return error", func(t *testing.T) {
// Act
r, err := list.Build[anyType](nil)

// Assert
assert.NotNil(t, err)
assert.Nil(t, r)
})
t.Run("when arr is not empty, should return rows", func(t *testing.T) {
// Arrange
arr := buildList(10)

// Act
r, err := list.Build(arr)
p := page.New().Add(r...)

// Assert
assert.Nil(t, err)
test.New(t).Assert(p.GetStructure()).Equals("components/list/build.json")
})
}

func TestBuildFromPointer(t *testing.T) {
t.Run("when arr is empty, should return error", func(t *testing.T) {
// Arrange
arr := buildPointerList(0)

// Act
r, err := list.BuildFromPointer(arr)

// Assert
assert.NotNil(t, err)
assert.Nil(t, r)
})
t.Run("when arr is not empty, should return rows", func(t *testing.T) {
// Arrange
arr := buildPointerList(10)

// Act
r, _ := list.BuildFromPointer(arr)
p := page.New().Add(r...)

// Assert
test.New(t).Assert(p.GetStructure()).Equals("components/list/build_from_pointer.json")
})
t.Run("when arr is has a nil element, should return error", func(t *testing.T) {
// Arrange
arr := buildPointerList(10)
arr[5] = nil

// Act
r, err := list.BuildFromPointer(arr)

// Assert
assert.NotNil(t, err)
assert.Nil(t, r)
})
}

func buildList(qtd int) []anyType {
var arr []anyType

for i := 0; i < qtd; i++ {
arr = append(arr, anyType{
Key: fmt.Sprintf("key(%d)", i),
Value: fmt.Sprintf("value(%d)", i),
})
}

return arr
}

func buildPointerList(qtd int) []*anyType {
var arr []*anyType

for i := 0; i < qtd; i++ {
arr = append(arr, &anyType{
Key: fmt.Sprintf("key(%d)", i),
Value: fmt.Sprintf("value(%d)", i),
})
}

return arr
}
110 changes: 104 additions & 6 deletions pkg/components/row/row_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,116 @@
package row_test

import (
"fmt"
"testing"

"github.com/johnfercher/maroto/v2/internal/fixture"
"github.com/johnfercher/maroto/v2/mocks"
"github.com/johnfercher/maroto/v2/pkg/components/col"
"github.com/johnfercher/maroto/v2/pkg/components/row"
"github.com/johnfercher/maroto/v2/pkg/core/entity"
"github.com/johnfercher/maroto/v2/pkg/test"
"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
// Act
sut := row.New(10)
t.Run("when there is no cols", func(t *testing.T) {
// Act
r := row.New(10)

// Assert
assert.NotNil(t, sut)
assert.Equal(t, "*row.row", fmt.Sprintf("%T", sut))
// Assert
test.New(t).Assert(r.GetStructure()).Equals("components/rows/new_empty_col.json")
})
t.Run("when has component, should retrieve components", func(t *testing.T) {
// Act
r := row.New(12).Add(col.New(12))

// Assert
test.New(t).Assert(r.GetStructure()).Equals("components/rows/new_filled_col.json")
})
t.Run("when has prop, should apply correctly", func(t *testing.T) {
// Act
prop := fixture.CellProp()
r := row.New(12).WithStyle(&prop)

// Assert
test.New(t).Assert(r.GetStructure()).Equals("components/rows/new_col_with_prop.json")
})
}

func TestRow_GetHeight(t *testing.T) {
t.Run("should return height correctly", func(t *testing.T) {
// Act
r := row.New(10)

// Assert
assert.Equal(t, 10.0, r.GetHeight())
})
}

func TestRow_GetStructure(t *testing.T) {
t.Run("when there is no style, should call provider correctly", func(t *testing.T) {
// Arrange
cfg := &entity.Config{
MaxGridSize: 12,
}
cell := fixture.CellEntity()

provider := &mocks.Provider{}
provider.EXPECT().CreateRow(cell.Height)

col := &mocks.Col{}
col.EXPECT().Render(provider, cell, true)
col.EXPECT().SetConfig(cfg)
col.EXPECT().GetSize().Return(12)

sut := row.New(cell.Height).Add(col)
sut.SetConfig(cfg)

// Act
sut.Render(provider, cell)

// Assert
provider.AssertNumberOfCalls(t, "CreateRow", 1)
col.AssertNumberOfCalls(t, "Render", 1)
col.AssertNumberOfCalls(t, "SetConfig", 1)
})
t.Run("when there is style, should call provider correctly", func(t *testing.T) {
// Arrange
cfg := &entity.Config{
MaxGridSize: 12,
}
cell := fixture.CellEntity()
prop := fixture.CellProp()

provider := &mocks.Provider{}
provider.EXPECT().CreateRow(cell.Height)
provider.EXPECT().CreateCol(cell.Width, cell.Height, cfg, &prop)

col := &mocks.Col{}
col.EXPECT().Render(provider, cell, false)
col.EXPECT().SetConfig(cfg)
col.EXPECT().GetSize().Return(12)

sut := row.New(cell.Height).Add(col).WithStyle(&prop)
sut.SetConfig(cfg)

// Act
sut.Render(provider, cell)

// Assert
provider.AssertNumberOfCalls(t, "CreateCol", 1)
provider.AssertNumberOfCalls(t, "CreateRow", 1)
col.AssertNumberOfCalls(t, "Render", 1)
col.AssertNumberOfCalls(t, "SetConfig", 1)
})
}

func TestRow_SetConfig(t *testing.T) {
t.Run("should call correctly", func(t *testing.T) {
// Arrange
sut := row.New(10)

// Act
sut.SetConfig(nil)
})
}
2 changes: 1 addition & 1 deletion pkg/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (b *builder) WithCreationDate(time time.Time) Builder {
return b
}

b.metadata.CreationDate = time
b.metadata.CreationDate = &time

return b
}
Expand Down
Loading

0 comments on commit bb2e738

Please sign in to comment.