Skip to content

Commit

Permalink
Add text unit tests and refactor color to use consts and reference (#336
Browse files Browse the repository at this point in the history
)
  • Loading branch information
johnfercher authored Oct 9, 2023
1 parent ac4ec81 commit 2cb4639
Show file tree
Hide file tree
Showing 26 changed files with 350 additions and 87 deletions.
2 changes: 1 addition & 1 deletion docs/assets/examples/billing/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
Size: 9,
Style: fontstyle.Bold,
Align: align.Center,
Color: props.NewWhite(),
Color: &props.WhiteColor,
}),
).WithStyle(&props.Cell{BackgroundColor: darkGrayColor})

Expand Down
12 changes: 3 additions & 9 deletions docs/assets/examples/line/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ import (
"github.com/johnfercher/maroto/v2/pkg/config"
)

var redColor = props.Color{
Red: 255,
Green: 0,
Blue: 0,
}

func main() {
cfg := config.NewBuilder().
WithDebug(true).
Expand All @@ -41,7 +35,7 @@ func main() {

m.AddRow(40,
line.NewCol(2, props.Line{Thickness: 0.5}),
line.NewCol(4, props.Line{Color: redColor}),
line.NewCol(4, props.Line{Color: &props.RedColor}),
line.NewCol(6, props.Line{Orientation: orientation.Vertical}),
)

Expand All @@ -53,8 +47,8 @@ func main() {

m.AddRow(40,
line.NewCol(2, props.Line{Style: linestyle.Dashed}),
line.NewCol(4, props.Line{Color: redColor, Style: linestyle.Dashed, Thickness: 0.8, Orientation: orientation.Vertical, OffsetPercent: 70, SizePercent: 70}),
line.NewCol(6, props.Line{Color: redColor, Style: linestyle.Dashed, Thickness: 0.8, Orientation: orientation.Horizontal, OffsetPercent: 40, SizePercent: 40}),
line.NewCol(4, props.Line{Color: &props.RedColor, Style: linestyle.Dashed, Thickness: 0.8, Orientation: orientation.Vertical, OffsetPercent: 70, SizePercent: 70}),
line.NewCol(6, props.Line{Color: &props.RedColor, Style: linestyle.Dashed, Thickness: 0.8, Orientation: orientation.Horizontal, OffsetPercent: 40, SizePercent: 40}),
)

document, err := m.Generate()
Expand Down
36 changes: 35 additions & 1 deletion internal/fixture/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,47 @@ package fixture

import (
"github.com/johnfercher/go-tree/node"
"github.com/johnfercher/maroto/v2/pkg/consts/align"
"github.com/johnfercher/maroto/v2/pkg/consts/border"
"github.com/johnfercher/maroto/v2/pkg/consts/breakline"
"github.com/johnfercher/maroto/v2/pkg/consts/fontfamily"
"github.com/johnfercher/maroto/v2/pkg/consts/fontstyle"
"github.com/johnfercher/maroto/v2/pkg/consts/linestyle"
"github.com/johnfercher/maroto/v2/pkg/consts/orientation"
"github.com/johnfercher/maroto/v2/pkg/core"
"github.com/johnfercher/maroto/v2/pkg/core/entity"
"github.com/johnfercher/maroto/v2/pkg/props"
)

func TextProp() props.Text {
fontProp := FontProp()
prop := props.Text{
Top: 12,
Left: 3,
Family: fontProp.Family,
Style: fontProp.Style,
Size: fontProp.Size,
Align: align.Right,
BreakLineStrategy: breakline.DashStrategy,
VerticalPadding: 20,
Color: fontProp.Color,
}
prop.MakeValid(&fontProp)
return prop
}

func FontProp() props.Font {
colorProp := ColorProp()
prop := props.Font{
Family: fontfamily.Helvetica,
Style: fontstyle.Bold,
Size: 14,
Color: &colorProp,
}
prop.MakeValid(fontfamily.Arial)
return prop
}

func BarcodeProp() props.Barcode {
prop := props.Barcode{
Top: 10,
Expand Down Expand Up @@ -85,8 +118,9 @@ func ColorProp() props.Color {
}

func LineProp() props.Line {
colorProp := ColorProp()
prop := props.Line{
Color: ColorProp(),
Color: &colorProp,
Style: linestyle.Dashed,
Thickness: 1.1,
Orientation: orientation.Vertical,
Expand Down
2 changes: 1 addition & 1 deletion internal/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type line struct {
func NewLine(pdf fpdf.Fpdf) *line {
return &line{
pdf: pdf,
defaultColor: props.NewBlack(),
defaultColor: &props.BlackColor,
defaultThickness: linestyle.DefaultLineThickness,
}
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/components/col/col.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ func (c *col) GetSize() int {
}

func (c *col) GetStructure() *node.Node[core.Structure] {
detailsMap := c.style.ToMap()

str := core.Structure{
Type: "col",
Value: c.size,
Details: detailsMap,
Details: c.style.ToMap(),
}

node := node.New(str)
Expand Down
5 changes: 3 additions & 2 deletions pkg/components/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ func NewRow(height float64, value string, ps ...props.Text) core.Row {

func (t *text) GetStructure() *node.Node[core.Structure] {
str := core.Structure{
Type: "text",
Value: t.value,
Type: "text",
Value: t.value,
Details: t.prop.ToMap(),
}

return node.New(str)
Expand Down
88 changes: 81 additions & 7 deletions pkg/components/text/text_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,92 @@
package text_test

import (
"fmt"
"testing"

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

func TestNew(t *testing.T) {
// Act
sut := text.New("label")
t.Run("when prop is not sent, should use default", func(t *testing.T) {
// Act
sut := text.New("code")

// Assert
assert.NotNil(t, sut)
assert.Equal(t, "*text.text", fmt.Sprintf("%T", sut))
// Assert
test.New(t).Assert(sut.GetStructure()).Equals("new_text_default_prop.json")
})
t.Run("when prop is sent, should use the provided", func(t *testing.T) {
// Act
sut := text.New("code", fixture.TextProp())

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

func TestNewCol(t *testing.T) {
t.Run("when prop is not sent, should use default", func(t *testing.T) {
// Act
sut := text.NewCol(12, "code")

// Assert
test.New(t).Assert(sut.GetStructure()).Equals("new_text_col_default_prop.json")
})
t.Run("when prop is sent, should use the provided", func(t *testing.T) {
// Act
sut := text.NewCol(12, "code", fixture.TextProp())

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

func TestNewRow(t *testing.T) {
t.Run("when prop is not sent, should use default", func(t *testing.T) {
// Act
sut := text.NewRow(10, "code")

// Assert
test.New(t).Assert(sut.GetStructure()).Equals("new_text_row_default_prop.json")
})
t.Run("when prop is sent, should use the provided", func(t *testing.T) {
// Act
sut := text.NewRow(10, "code", fixture.TextProp())

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

func TestText_Render(t *testing.T) {
t.Run("should call provider correctly", func(t *testing.T) {
// Arrange
value := "textValue"
cell := fixture.CellEntity()
prop := fixture.TextProp()
sut := text.New(value, prop)

provider := &mocks.Provider{}
provider.EXPECT().AddText(value, &cell, &prop)
sut.SetConfig(&entity.Config{})

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

// Assert
provider.AssertNumberOfCalls(t, "AddText", 1)
})
}

func TestText_SetConfig(t *testing.T) {
t.Run("should call correctly", func(t *testing.T) {
// Arrange
sut := text.New("textValue")

// 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 @@ -76,7 +76,7 @@ func NewBuilder() Builder {
Size: pagesize.DefaultFontSize,
Family: fontfamily.Arial,
Style: fontstyle.Normal,
Color: props.NewBlack(),
Color: &props.BlackColor,
},
metadata: &entity.Metadata{},
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/props/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ type Cell struct {

func (c *Cell) ToMap() map[string]interface{} {
if c == nil {
return make(map[string]interface{})
return nil
}

return map[string]interface{}{
"cell_prop_backgrond_color": c.BackgroundColor.ToString(),
"cell_prop_border_color": c.BorderColor.ToString(),
m := map[string]interface{}{
"cell_prop_border_type": c.BorderType,
"cell_prop_border_thickness": c.BorderThickness,
"cell_prop_border_line_style": c.LineStyle,
}

if c.BackgroundColor != nil {
m["cell_prop_backgrond_color"] = c.BackgroundColor.ToString()
}

if c.BorderColor != nil {
m["cell_prop_border_color"] = c.BorderColor.ToString()
}

return m
}
26 changes: 8 additions & 18 deletions pkg/props/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ package props

import "fmt"

var (
WhiteColor = Color{Red: 255, Green: 255, Blue: 255}
BlackColor = Color{Red: 0, Green: 0, Blue: 0}
RedColor = Color{Red: 255, Green: 0, Blue: 0}
GreenColor = Color{Red: 0, Green: 255, Blue: 0}
BlueColor = Color{Red: 0, Green: 0, Blue: 255}
)

// Color represents a color in the RGB (Red, Green, Blue) space,
// is possible mix values, when all values are 0 the result color is black
// when all values are 255 the result color is white.
Expand All @@ -27,21 +35,3 @@ func (c *Color) ToString() string {
func (c *Color) IsWhite() bool {
return c.Red == 255 && c.Green == 255 && c.Blue == 255
}

// NewWhite return a Color with all components (red, green and blue) as 255.
func NewWhite() *Color {
return &Color{
Red: 255,
Green: 255,
Blue: 255,
}
}

// NewBlack return a Color with all components (red, green and blue) as 0.
func NewBlack() *Color {
return &Color{
Red: 0,
Green: 0,
Blue: 0,
}
}
12 changes: 6 additions & 6 deletions pkg/props/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ import (
"github.com/stretchr/testify/assert"
)

func TestNewWhite(t *testing.T) {
func TestWhiteColor(t *testing.T) {
// Act
white := props.NewWhite()
white := props.WhiteColor

// Assert
assert.Equal(t, 255, white.Red)
assert.Equal(t, 255, white.Green)
assert.Equal(t, 255, white.Blue)
}

func TestNewBlack(t *testing.T) {
func TestBlackColor(t *testing.T) {
// Act
black := props.NewBlack()
black := props.BlackColor

// Assert
assert.Equal(t, 0, black.Red)
assert.Equal(t, 0, black.Green)
assert.Equal(t, 0, black.Blue)
}

func TestColor_IsWhite(t *testing.T) {
/*func TestColor_IsWhite(t *testing.T) {
// Act
white := props.NewWhite()
// Assert
assert.True(t, white.IsWhite())
}
}*/
2 changes: 1 addition & 1 deletion pkg/props/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// Line represents properties from a Line inside a cell.
type Line struct {
// Color define the line color.
Color Color
Color *Color
// Style define the line style (solid or dashed).
Style linestyle.Type
// Thickness define the line thicknesl.
Expand Down
Loading

0 comments on commit 2cb4639

Please sign in to comment.