-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0cecd01
commit bb2e738
Showing
50 changed files
with
1,940 additions
and
410 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.