Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/assets/examples/cellstyle/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ func GetMaroto() core.Maroto {
BorderType: border.Bottom,
BorderColor: &props.Color{0, 0, 200},
},
{
BackgroundColor: &props.Color{220, 220, 220},
BorderType: border.Left | border.Top,
BorderColor: &props.Color{0, 0, 200},
},
{
BackgroundColor: &props.Color{220, 220, 220},
BorderType: border.Left | border.Right,
BorderColor: &props.Color{0, 0, 200},
},
{
BackgroundColor: &props.Color{220, 220, 220},
BorderType: border.Top | border.Bottom,
BorderColor: &props.Color{0, 0, 200},
},
{
BackgroundColor: &props.Color{220, 220, 220},
BorderType: border.Left | border.Right | border.Top,
BorderColor: &props.Color{0, 0, 200},
},
}

whiteText := props.Text{
Expand Down
4 changes: 2 additions & 2 deletions internal/providers/gofpdf/cellwriter/cellwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *cellWriter) Apply(width, height float64, config *entity.Config, prop *p
bd = border.Full
}

c.fpdf.CellFormat(width, height, "", string(bd), 0, "C", false, 0, "")
c.fpdf.CellFormat(width, height, "", bd.String(), 0, "C", false, 0, "")
return
}

Expand All @@ -45,5 +45,5 @@ func (c *cellWriter) Apply(width, height float64, config *entity.Config, prop *p
bd = border.Full
}

c.fpdf.CellFormat(width, height, "", string(bd), 0, "C", prop.BackgroundColor != nil, 0, "")
c.fpdf.CellFormat(width, height, "", bd.String(), 0, "C", prop.BackgroundColor != nil, 0, "")
}
4 changes: 2 additions & 2 deletions internal/providers/gofpdf/cellwriter/cellwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestCellWriter_Apply(t *testing.T) {
width := 100.0
height := 200.0
fpdf := mocks.NewFpdf(t)
fpdf.EXPECT().CellFormat(width, height, "", "1", 0, "C", false, 0, "")
fpdf.EXPECT().CellFormat(width, height, "", "LTRB", 0, "C", false, 0, "")

sut := cellwriter.NewCellWriter(fpdf)

Expand Down Expand Up @@ -83,7 +83,7 @@ func TestCellWriter_Apply(t *testing.T) {
width := 100.0
height := 200.0
fpdf := mocks.NewFpdf(t)
fpdf.EXPECT().CellFormat(width, height, "", "1", 0, "C", true, 0, "")
fpdf.EXPECT().CellFormat(width, height, "", "LTRB", 0, "C", true, 0, "")

sut := cellwriter.NewCellWriter(fpdf)

Expand Down
60 changes: 50 additions & 10 deletions pkg/consts/border/border.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,64 @@
package border

// Type represents a border type.
type Type string
type Type int

// None is the default border type.
const None Type = 0

const (
// None is the default border type.
None Type = ""
// Full is a border type that borders all sides.
Full Type = "1"
// Left is a border type that borders the left side.
Left Type = "L"
Left Type = 1 << iota
// Top is a border type that borders the top side.
Top Type = "T"
Top
// Right is a border type that borders the right side.
Right Type = "R"
Right
// Bottom is a border type that borders the bottom side.
Bottom Type = "B"
Bottom
// Full is a border type that borders all sides.
Full = Left | Top | Right | Bottom
)

// IsValid checks if the border type is valid.
func (t Type) IsValid() bool {
return t == Full || t == Left || t == Top || t == Right || t == Bottom
return t > None && t <= Full
}

// HasLeft checks if the border type includes left border.
func (t Type) HasLeft() bool {
return t&Left != 0
}

// HasTop checks if the border type includes top border.
func (t Type) HasTop() bool {
return t&Top != 0
}

// HasRight checks if the border type includes right border.
func (t Type) HasRight() bool {
return t&Right != 0
}

// HasBottom checks if the border type includes bottom border.
func (t Type) HasBottom() bool {
return t&Bottom != 0
}

// String returns the string representation of the border type.
func (t Type) String() string {
result := ""
if t.HasLeft() {
result += "L"
}
if t.HasTop() {
result += "T"
}
if t.HasRight() {
result += "R"
}
if t.HasBottom() {
result += "B"
}

return result
}
133 changes: 131 additions & 2 deletions pkg/consts/border/border_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

func TestType_IsValid(t *testing.T) {
t.Run("When type is empty, should not be valid", func(t *testing.T) {
t.Run("When type is None, should not be valid", func(t *testing.T) {
// Arrange
borderType := border.Type("")
borderType := border.None

// Act & Assert
assert.False(t, borderType.IsValid())
Expand Down Expand Up @@ -52,3 +52,132 @@ func TestType_IsValid(t *testing.T) {
assert.True(t, borderType.IsValid())
})
}

func TestType_HasBorders(t *testing.T) {
t.Run("When type is None, should not have any border", func(t *testing.T) {
// Arrange
borderType := border.None

// Act & Assert
assert.False(t, borderType.HasLeft())
assert.False(t, borderType.HasTop())
assert.False(t, borderType.HasRight())
assert.False(t, borderType.HasBottom())
})

t.Run("When type is Full, should have all borders", func(t *testing.T) {
// Arrange
borderType := border.Full

// Act & Assert
assert.True(t, borderType.HasLeft())
assert.True(t, borderType.HasTop())
assert.True(t, borderType.HasRight())
assert.True(t, borderType.HasBottom())
})

t.Run("When type is Left, should have only left border", func(t *testing.T) {
// Arrange
borderType := border.Left

// Act & Assert
assert.True(t, borderType.HasLeft())
assert.False(t, borderType.HasTop())
assert.False(t, borderType.HasRight())
assert.False(t, borderType.HasBottom())
})

t.Run("When type is combined (Left|Top), should have left and top borders", func(t *testing.T) {
// Arrange
borderType := border.Left | border.Top

// Act & Assert
assert.True(t, borderType.HasLeft())
assert.True(t, borderType.HasTop())
assert.False(t, borderType.HasRight())
assert.False(t, borderType.HasBottom())
})

t.Run("When type is combined (Left|Right), should have left and right borders", func(t *testing.T) {
// Arrange
borderType := border.Left | border.Right

// Act & Assert
assert.True(t, borderType.HasLeft())
assert.False(t, borderType.HasTop())
assert.True(t, borderType.HasRight())
assert.False(t, borderType.HasBottom())
})

t.Run("When type is combined (Top|Bottom), should have top and bottom borders", func(t *testing.T) {
// Arrange
borderType := border.Top | border.Bottom

// Act & Assert
assert.False(t, borderType.HasLeft())
assert.True(t, borderType.HasTop())
assert.False(t, borderType.HasRight())
assert.True(t, borderType.HasBottom())
})

t.Run("When type is combined (Left|Top|Right), should have left, top and right borders", func(t *testing.T) {
// Arrange
borderType := border.Left | border.Top | border.Right

// Act & Assert
assert.True(t, borderType.HasLeft())
assert.True(t, borderType.HasTop())
assert.True(t, borderType.HasRight())
assert.False(t, borderType.HasBottom())
})
}

func TestType_String(t *testing.T) {
t.Run("When type is None, should return empty string", func(t *testing.T) {
// Arrange
borderType := border.None

// Act & Assert
assert.Equal(t, "", borderType.String())
})

t.Run("When type is Full, should return 'LTRB'", func(t *testing.T) {
// Arrange
borderType := border.Full

// Act & Assert
assert.Equal(t, "LTRB", borderType.String())
})

t.Run("When type is Left, should return 'L'", func(t *testing.T) {
// Arrange
borderType := border.Left

// Act & Assert
assert.Equal(t, "L", borderType.String())
})

t.Run("When type is combined (Left|Top), should return 'LT'", func(t *testing.T) {
// Arrange
borderType := border.Left | border.Top

// Act & Assert
assert.Equal(t, "LT", borderType.String())
})

t.Run("When type is combined (Left|Right), should return 'LR'", func(t *testing.T) {
// Arrange
borderType := border.Left | border.Right

// Act & Assert
assert.Equal(t, "LR", borderType.String())
})

t.Run("When type is combined (Left|Top|Right), should return 'LTR'", func(t *testing.T) {
// Arrange
borderType := border.Left | border.Top | border.Right

// Act & Assert
assert.Equal(t, "LTR", borderType.String())
})
}
2 changes: 1 addition & 1 deletion pkg/props/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *Cell) ToMap() map[string]interface{} {

m := make(map[string]interface{})

if c.BorderType != "" {
if c.BorderType != border.None {
m["prop_border_type"] = c.BorderType
}

Expand Down
2 changes: 1 addition & 1 deletion test/maroto/components/cols/new_with_props.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
}
}
10 changes: 5 additions & 5 deletions test/maroto/components/list/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
},
"nodes": [
{
Expand Down Expand Up @@ -94,7 +94,7 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
},
"nodes": [
{
Expand Down Expand Up @@ -153,7 +153,7 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
},
"nodes": [
{
Expand Down Expand Up @@ -212,7 +212,7 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
},
"nodes": [
{
Expand Down Expand Up @@ -271,7 +271,7 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
},
"nodes": [
{
Expand Down
10 changes: 5 additions & 5 deletions test/maroto/components/list/build_from_pointer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
},
"nodes": [
{
Expand Down Expand Up @@ -94,7 +94,7 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
},
"nodes": [
{
Expand Down Expand Up @@ -153,7 +153,7 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
},
"nodes": [
{
Expand Down Expand Up @@ -212,7 +212,7 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
},
"nodes": [
{
Expand Down Expand Up @@ -271,7 +271,7 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
},
"nodes": [
{
Expand Down
2 changes: 1 addition & 1 deletion test/maroto/components/rows/new_col_with_prop.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"prop_border_color": "RGB(200, 80, 60)",
"prop_border_line_style": "dashed",
"prop_border_thickness": 0.6,
"prop_border_type": "L"
"prop_border_type": 1
}
}
Loading
Loading