Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnferchermeli committed Mar 24, 2020
1 parent fb0bf84 commit b335028
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 109 deletions.
6 changes: 3 additions & 3 deletions internal/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestSignature_AddSpaceFor_DefaultMargins(t *testing.T) {
pdf.AssertNumberOfCalls(t, "Line", 1)
pdf.AssertCalled(t, "Line", 16.0, 15.0, 13.0, 15.0)
text.AssertNumberOfCalls(t, "Add", 1)
text.AssertCalled(t, "Add", "label", props.Text{Size: 10.0}, 5.0, 2.0, 5.0)
text.AssertCalled(t, "Add", "label", props.Text{Size: 10.0}, 7.0, 2.0, 5.0)
}

func TestSignature_AddSpaceFor_NotDefaultMargins(t *testing.T) {
Expand All @@ -60,7 +60,7 @@ func TestSignature_AddSpaceFor_NotDefaultMargins(t *testing.T) {

// Assert
pdf.AssertNumberOfCalls(t, "Line", 1)
pdf.AssertCalled(t, "Line", 124.0, 10.0, 166.0, 10.0)
pdf.AssertCalled(t, "Line", 26.0, 15.0, 23.0, 15.0)
text.AssertNumberOfCalls(t, "Add", 1)
text.AssertCalled(t, "Add", "label", props.Text{Size: 10.0}, 5.0, 2.0, 5.0)
text.AssertCalled(t, "Add", "label", props.Text{Size: 10.0}, 7.0, 2.0, 5.0)
}
6 changes: 6 additions & 0 deletions internal/tablelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func TestTableList_Create_Happy(t *testing.T) {
marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil)
marotoGrid.On("Line", mock.Anything).Return(nil)
marotoGrid.On("SetBackgroundColor", mock.Anything).Return(nil)
marotoGrid.On("GetPageMargins").Return(10.0, 10.0, 10.0, 10.0)
marotoGrid.On("GetPageSize").Return(200.0, 600.0)

sut := internal.NewTableList(text, font)
sut.BindGrid(marotoGrid)
Expand Down Expand Up @@ -131,6 +133,8 @@ func TestTableList_Create_HappyWithBackgroundColor(t *testing.T) {
marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil)
marotoGrid.On("Line", mock.Anything).Return(nil)
marotoGrid.On("SetBackgroundColor", mock.Anything).Return(nil)
marotoGrid.On("GetPageMargins").Return(10.0, 10.0, 10.0, 10.0)
marotoGrid.On("GetPageSize").Return(200.0, 600.0)

sut := internal.NewTableList(text, font)
sut.BindGrid(marotoGrid)
Expand Down Expand Up @@ -175,6 +179,8 @@ func TestTableList_Create_Happy_Without_Line(t *testing.T) {
marotoGrid := &mocks.Maroto{}
marotoGrid.On("Row", mock.Anything, mock.Anything).Return(nil)
marotoGrid.On("Line", mock.Anything).Return(nil)
marotoGrid.On("GetPageMargins").Return(10.0, 10.0, 10.0, 10.0)
marotoGrid.On("GetPageSize").Return(200.0, 600.0)

sut := internal.NewTableList(text, font)
sut.BindGrid(marotoGrid)
Expand Down
82 changes: 12 additions & 70 deletions internal/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,16 @@ func TestText_GetLinesQuantity_WhenStringSmallerThanLimits(t *testing.T) {
})
pdf.On("GetStringWidth", mock.Anything).Return(8.0)

math := &mocks.Math{}
math.On("GetWidthPerCol", mock.Anything).Return(10.0)

font := &mocks.Font{}
font.On("SetFont", mock.Anything, mock.Anything, mock.Anything).Return(nil)

sut := internal.NewText(pdf, math, font)
sut := internal.NewText(pdf, nil, font)

// Act
lines := sut.GetLinesQuantity("AnyText With Spaces", props.Text{}, 2)

// Assert
assert.Equal(t, lines, 1)
assert.Equal(t, lines, 4)
}

func TestText_GetLinesQuantity_WhenHasOneWord(t *testing.T) {
Expand All @@ -49,13 +46,10 @@ func TestText_GetLinesQuantity_WhenHasOneWord(t *testing.T) {
})
pdf.On("GetStringWidth", mock.Anything).Return(15.0)

math := &mocks.Math{}
math.On("GetWidthPerCol", mock.Anything).Return(10.0)

font := &mocks.Font{}
font.On("SetFont", mock.Anything, mock.Anything, mock.Anything).Return(nil)

sut := internal.NewText(pdf, math, font)
sut := internal.NewText(pdf, nil, font)

// Act
lines := sut.GetLinesQuantity("OneWord", props.Text{}, 2)
Expand All @@ -72,13 +66,10 @@ func TestText_GetLinesQuantity_WhenExtrapolate(t *testing.T) {
})
pdf.On("GetStringWidth", mock.Anything).Return(15.0)

math := &mocks.Math{}
math.On("GetWidthPerCol", mock.Anything).Return(10.0)

font := &mocks.Font{}
font.On("SetFont", mock.Anything, mock.Anything, mock.Anything).Return(nil)

sut := internal.NewText(pdf, math, font)
sut := internal.NewText(pdf, nil, font)

// Act
lines := sut.GetLinesQuantity("Many words", props.Text{Extrapolate: true}, 2)
Expand Down Expand Up @@ -116,10 +107,8 @@ func TestText_Add(t *testing.T) {
text string
align consts.Align
pdf func() *mocks.Pdf
math func() *mocks.Math
font func() *mocks.Font
assertPdf func(t *testing.T, pdf *mocks.Pdf)
assertMath func(t *testing.T, math *mocks.Math)
assertFont func(t *testing.T, font *mocks.Font)
}{
{
Expand All @@ -134,11 +123,6 @@ func TestText_Add(t *testing.T) {
_pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value })
return _pdf
},
func() *mocks.Math {
_math := &mocks.Math{}
_math.On("GetWidthPerCol", mock.Anything).Return(123.0)
return _math
},
func() *mocks.Font {
_font := &mocks.Font{}
_font.On("GetScaleFactor").Return(1.0)
Expand All @@ -152,11 +136,7 @@ func TestText_Add(t *testing.T) {
_pdf.AssertNumberOfCalls(t, "GetMargins", 1)

_pdf.AssertNumberOfCalls(t, "Text", 1)
_pdf.AssertCalled(t, "Text", 133.0, 15.0, "TextHelper1")
},
func(t *testing.T, _math *mocks.Math) {
_math.AssertNumberOfCalls(t, "GetWidthPerCol", 1)
_math.AssertCalled(t, "GetWidthPerCol", 15.0)
_pdf.AssertCalled(t, "Text", 11.0, 16.0, "TextHelper1")
},
func(t *testing.T, _font *mocks.Font) {
_font.AssertNumberOfCalls(t, "SetFont", 1)
Expand All @@ -175,11 +155,6 @@ func TestText_Add(t *testing.T) {
_pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value })
return _pdf
},
func() *mocks.Math {
_math := &mocks.Math{}
_math.On("GetWidthPerCol", mock.Anything).Return(123.0)
return _math
},
func() *mocks.Font {
_font := &mocks.Font{}
_font.On("GetScaleFactor").Return(1.0)
Expand All @@ -194,11 +169,7 @@ func TestText_Add(t *testing.T) {
_pdf.AssertNumberOfCalls(t, "GetMargins", 1)

_pdf.AssertNumberOfCalls(t, "Text", 1)
_pdf.AssertCalled(t, "Text", 188.5, 15.0, "TextHelper2")
},
func(t *testing.T, _math *mocks.Math) {
_math.AssertNumberOfCalls(t, "GetWidthPerCol", 1)
_math.AssertCalled(t, "GetWidthPerCol", 15.0)
_pdf.AssertCalled(t, "Text", 12.5, 16.0, "TextHelper2")
},
func(t *testing.T, _font *mocks.Font) {
_font.AssertNumberOfCalls(t, "SetFont", 1)
Expand All @@ -217,11 +188,6 @@ func TestText_Add(t *testing.T) {
_pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value })
return _pdf
},
func() *mocks.Math {
_math := &mocks.Math{}
_math.On("GetWidthPerCol", mock.Anything).Return(123.0)
return _math
},
func() *mocks.Font {
_font := &mocks.Font{}
_font.On("GetScaleFactor").Return(1.0)
Expand All @@ -236,11 +202,7 @@ func TestText_Add(t *testing.T) {
_pdf.AssertNumberOfCalls(t, "GetMargins", 1)

_pdf.AssertNumberOfCalls(t, "Text", 1)
_pdf.AssertCalled(t, "Text", 244.0, 15.0, "TextHelper3")
},
func(t *testing.T, _math *mocks.Math) {
_math.AssertNumberOfCalls(t, "GetWidthPerCol", 1)
_math.AssertCalled(t, "GetWidthPerCol", 15.0)
_pdf.AssertCalled(t, "Text", 14.0, 16.0, "TextHelper3")
},
func(t *testing.T, _font *mocks.Font) {
_font.AssertNumberOfCalls(t, "SetFont", 1)
Expand All @@ -259,11 +221,6 @@ func TestText_Add(t *testing.T) {
_pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value })
return _pdf
},
func() *mocks.Math {
_math := &mocks.Math{}
_math.On("GetWidthPerCol", mock.Anything).Return(123.0)
return _math
},
func() *mocks.Font {
_font := &mocks.Font{}
_font.On("GetScaleFactor").Return(1.0)
Expand All @@ -278,11 +235,7 @@ func TestText_Add(t *testing.T) {
_pdf.AssertNumberOfCalls(t, "GetMargins", 1)

_pdf.AssertNumberOfCalls(t, "Text", 1)
_pdf.AssertCalled(t, "Text", 244.0, 15.0, "TextHelper4")
},
func(t *testing.T, _math *mocks.Math) {
_math.AssertNumberOfCalls(t, "GetWidthPerCol", 1)
_math.AssertCalled(t, "GetWidthPerCol", 15.0)
_pdf.AssertCalled(t, "Text", 14.0, 16.0, "TextHelper4")
},
func(t *testing.T, _font *mocks.Font) {
_font.AssertNumberOfCalls(t, "SetFont", 1)
Expand All @@ -302,11 +255,6 @@ func TestText_Add(t *testing.T) {
_pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value })
return _pdf
},
func() *mocks.Math {
_math := &mocks.Math{}
_math.On("GetWidthPerCol", mock.Anything).Return(123.0)
return _math
},
func() *mocks.Font {
_font := &mocks.Font{}
_font.On("GetScaleFactor").Return(1.0)
Expand All @@ -315,16 +263,12 @@ func TestText_Add(t *testing.T) {
return _font
},
func(t *testing.T, _pdf *mocks.Pdf) {
_pdf.AssertNumberOfCalls(t, "GetStringWidth", 199)
_pdf.AssertNumberOfCalls(t, "GetStringWidth", 275)
_pdf.AssertCalled(t, "GetStringWidth", "Lorem Ipsum is simply dummy textá of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")

_pdf.AssertNumberOfCalls(t, "GetMargins", 16)
_pdf.AssertNumberOfCalls(t, "GetMargins", 92)

_pdf.AssertNumberOfCalls(t, "Text", 16)
},
func(t *testing.T, _math *mocks.Math) {
_math.AssertNumberOfCalls(t, "GetWidthPerCol", 1)
_math.AssertCalled(t, "GetWidthPerCol", 15.0)
_pdf.AssertNumberOfCalls(t, "Text", 92)
},
func(t *testing.T, _font *mocks.Font) {
_font.AssertNumberOfCalls(t, "SetFont", 1)
Expand All @@ -336,17 +280,15 @@ func TestText_Add(t *testing.T) {
for _, c := range cases {
// Arrange
_pdf := c.pdf()
_math := c.math()
_font := c.font()

text := internal.NewText(_pdf, _math, _font)
text := internal.NewText(_pdf, nil, _font)

// Act
text.Add(c.text, props.Text{Family: consts.Arial, Style: consts.BoldItalic, Size: 16.0, Align: c.align}, 5.0, 1, 15.0)

// Assert
c.assertPdf(t, _pdf)
c.assertMath(t, _math)
c.assertFont(t, _font)
}
}
Loading

0 comments on commit b335028

Please sign in to comment.