diff --git a/Makefile b/Makefile index 2bf756cb..bccef879 100644 --- a/Makefile +++ b/Makefile @@ -21,10 +21,15 @@ fmt: .PHONY: lint lint: golangci-lint run --config=.golangci.yml ./... + make mock-lint + +.PHONY: mock-lint +mock-lint: + bash shell/mock-check.sh .PHONY: install install: - bash install.sh + bash shell/install.sh .PHONY: docs docs: diff --git a/internal/cache/mutexcache_test.go b/internal/cache/mutexcache_test.go index 7c0911de..229c0b70 100644 --- a/internal/cache/mutexcache_test.go +++ b/internal/cache/mutexcache_test.go @@ -28,7 +28,7 @@ func TestMutexCache_AddImage(t *testing.T) { value := "value1" img := &entity.Image{} - innerMock := &mocks.Cache{} + innerMock := mocks.NewCache(t) innerMock.EXPECT().AddImage(value, img) sut := cache.NewMutexDecorator(innerMock) @@ -47,7 +47,7 @@ func TestMutexCache_GetImage(t *testing.T) { imgToReturn := &entity.Image{} errToReturn := errors.New("any error") - innerMock := &mocks.Cache{} + innerMock := mocks.NewCache(t) innerMock.EXPECT().GetImage(value, ext).Return(imgToReturn, errToReturn) sut := cache.NewMutexDecorator(innerMock) @@ -67,7 +67,7 @@ func TestMutexCache_LoadImage(t *testing.T) { ext := extension.Jpg errToReturn := errors.New("any error") - innerMock := &mocks.Cache{} + innerMock := mocks.NewCache(t) innerMock.EXPECT().LoadImage(value, ext).Return(errToReturn) sut := cache.NewMutexDecorator(innerMock) diff --git a/internal/providers/gofpdf/cellwriter/bordercolorstyler_test.go b/internal/providers/gofpdf/cellwriter/bordercolorstyler_test.go index 2a389344..1f3514b4 100644 --- a/internal/providers/gofpdf/cellwriter/bordercolorstyler_test.go +++ b/internal/providers/gofpdf/cellwriter/bordercolorstyler_test.go @@ -38,7 +38,7 @@ func TestBorderColorStyler_Apply(t *testing.T) { cfg := &entity.Config{} var nilCellProp *props.Cell - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, nilCellProp) sut := cellwriter.NewBorderColorStyler(nil) @@ -57,7 +57,7 @@ func TestBorderColorStyler_Apply(t *testing.T) { cfg := &entity.Config{} prop := &props.Cell{} - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, prop) sut := cellwriter.NewBorderColorStyler(nil) @@ -78,10 +78,10 @@ func TestBorderColorStyler_Apply(t *testing.T) { BorderColor: &props.Color{Red: 140, Green: 100, Blue: 80}, } - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, prop) - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetDrawColor(prop.BorderColor.Red, prop.BorderColor.Green, prop.BorderColor.Blue) fpdf.EXPECT().SetDrawColor(0, 0, 0) diff --git a/internal/providers/gofpdf/cellwriter/borderlinestyler_test.go b/internal/providers/gofpdf/cellwriter/borderlinestyler_test.go index 78f6a5e8..b340827e 100644 --- a/internal/providers/gofpdf/cellwriter/borderlinestyler_test.go +++ b/internal/providers/gofpdf/cellwriter/borderlinestyler_test.go @@ -38,7 +38,7 @@ func TestBorderLineStyler_Apply(t *testing.T) { cfg := &entity.Config{} var nilCellProp *props.Cell - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, nilCellProp) sut := cellwriter.NewBorderLineStyler(nil) @@ -59,7 +59,7 @@ func TestBorderLineStyler_Apply(t *testing.T) { LineStyle: linestyle.Solid, } - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, prop) sut := cellwriter.NewBorderLineStyler(nil) @@ -78,7 +78,7 @@ func TestBorderLineStyler_Apply(t *testing.T) { cfg := &entity.Config{} prop := &props.Cell{} - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, prop) sut := cellwriter.NewBorderLineStyler(nil) @@ -99,10 +99,10 @@ func TestBorderLineStyler_Apply(t *testing.T) { LineStyle: linestyle.Dashed, } - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, prop) - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetDashPattern([]float64{1, 1}, 0.0) fpdf.EXPECT().SetDashPattern([]float64{1, 0}, 0.0) diff --git a/internal/providers/gofpdf/cellwriter/borderthicknessstyler_test.go b/internal/providers/gofpdf/cellwriter/borderthicknessstyler_test.go index f88d36dd..2fd37f2c 100644 --- a/internal/providers/gofpdf/cellwriter/borderthicknessstyler_test.go +++ b/internal/providers/gofpdf/cellwriter/borderthicknessstyler_test.go @@ -38,7 +38,7 @@ func TestBorderThicknessStyler_Apply(t *testing.T) { cfg := &entity.Config{} var nilCellProp *props.Cell - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, nilCellProp) sut := cellwriter.NewBorderThicknessStyler(nil) @@ -57,7 +57,7 @@ func TestBorderThicknessStyler_Apply(t *testing.T) { cfg := &entity.Config{} prop := &props.Cell{} - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, prop) sut := cellwriter.NewBorderThicknessStyler(nil) @@ -78,10 +78,10 @@ func TestBorderThicknessStyler_Apply(t *testing.T) { BorderThickness: 1.0, } - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, prop) - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetLineWidth(prop.BorderThickness) fpdf.EXPECT().SetLineWidth(linestyle.DefaultLineThickness) diff --git a/internal/providers/gofpdf/cellwriter/cellwriter_test.go b/internal/providers/gofpdf/cellwriter/cellwriter_test.go index 0c4d4b2e..2b58a1f3 100644 --- a/internal/providers/gofpdf/cellwriter/cellwriter_test.go +++ b/internal/providers/gofpdf/cellwriter/cellwriter_test.go @@ -28,7 +28,7 @@ func TestCellWriter_Apply(t *testing.T) { config := &entity.Config{} width := 100.0 height := 200.0 - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().CellFormat(width, height, "", "", 0, "C", false, 0, "") sut := cellwriter.NewCellWriter(fpdf) @@ -46,7 +46,7 @@ func TestCellWriter_Apply(t *testing.T) { } width := 100.0 height := 200.0 - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().CellFormat(width, height, "", "1", 0, "C", false, 0, "") sut := cellwriter.NewCellWriter(fpdf) @@ -63,7 +63,7 @@ func TestCellWriter_Apply(t *testing.T) { prop := fixture.CellProp() width := 100.0 height := 200.0 - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().CellFormat(width, height, "", "L", 0, "C", true, 0, "") sut := cellwriter.NewCellWriter(fpdf) @@ -82,7 +82,7 @@ func TestCellWriter_Apply(t *testing.T) { prop := fixture.CellProp() width := 100.0 height := 200.0 - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().CellFormat(width, height, "", "1", 0, "C", true, 0, "") sut := cellwriter.NewCellWriter(fpdf) diff --git a/internal/providers/gofpdf/cellwriter/fillcolorstyler_test.go b/internal/providers/gofpdf/cellwriter/fillcolorstyler_test.go index f54679eb..c18ec0bf 100644 --- a/internal/providers/gofpdf/cellwriter/fillcolorstyler_test.go +++ b/internal/providers/gofpdf/cellwriter/fillcolorstyler_test.go @@ -37,7 +37,7 @@ func TestFillColorStyle_Apply(t *testing.T) { cfg := &entity.Config{} var nilCellProp *props.Cell - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, nilCellProp) sut := cellwriter.NewFillColorStyler(nil) @@ -56,7 +56,7 @@ func TestFillColorStyle_Apply(t *testing.T) { cfg := &entity.Config{} prop := &props.Cell{} - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, prop) sut := cellwriter.NewFillColorStyler(nil) @@ -77,10 +77,10 @@ func TestFillColorStyle_Apply(t *testing.T) { BackgroundColor: &props.Color{Red: 100, Green: 150, Blue: 170}, } - inner := &mocks.CellWriter{} + inner := mocks.NewCellWriter(t) inner.EXPECT().Apply(width, height, cfg, prop) - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetFillColor(prop.BackgroundColor.Red, prop.BackgroundColor.Green, prop.BackgroundColor.Blue) fpdf.EXPECT().SetFillColor(255, 255, 255) diff --git a/internal/providers/gofpdf/font_test.go b/internal/providers/gofpdf/font_test.go index 85000608..b28bc416 100644 --- a/internal/providers/gofpdf/font_test.go +++ b/internal/providers/gofpdf/font_test.go @@ -19,7 +19,7 @@ func TestNewFont(t *testing.T) { family := fontfamily.Arial style := fontstyle.Bold - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetFont(family, string(style), size) // Act @@ -40,7 +40,7 @@ func TestFont_GetHeight(t *testing.T) { family := fontfamily.Arial style := fontstyle.Bold - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetFont(family, string(style), size) font := gofpdf.NewFont(fpdf, size, family, style) @@ -57,7 +57,7 @@ func TestFont_SetFamily(t *testing.T) { family := fontfamily.Arial style := fontstyle.Bold - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetFont(family, string(style), size) fpdf.EXPECT().SetFont(fontfamily.Helvetica, string(style), size) font := gofpdf.NewFont(fpdf, size, family, style) @@ -75,7 +75,7 @@ func TestFont_SetStyle(t *testing.T) { family := fontfamily.Arial style := fontstyle.Bold - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetFont(family, string(style), size) fpdf.EXPECT().SetFontStyle(string(fontstyle.BoldItalic)) font := gofpdf.NewFont(fpdf, size, family, style) @@ -93,7 +93,7 @@ func TestFont_SetSize(t *testing.T) { family := fontfamily.Arial style := fontstyle.Bold - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetFont(family, string(style), size) fpdf.EXPECT().SetFontSize(14.0) font := gofpdf.NewFont(fpdf, size, family, style) @@ -112,7 +112,7 @@ func TestFont_SetColor(t *testing.T) { family := fontfamily.Arial style := fontstyle.Bold - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetFont(family, string(style), size) font := gofpdf.NewFont(fpdf, size, family, style) color := &props.Color{Red: 0, Green: 0, Blue: 0} @@ -129,7 +129,7 @@ func TestFont_SetColor(t *testing.T) { family := fontfamily.Arial style := fontstyle.Bold - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetFont(family, string(style), size) fpdf.EXPECT().SetTextColor(200, 200, 200) font := gofpdf.NewFont(fpdf, size, family, style) diff --git a/internal/providers/gofpdf/image_test.go b/internal/providers/gofpdf/image_test.go index d0a1dea9..662d2a43 100644 --- a/internal/providers/gofpdf/image_test.go +++ b/internal/providers/gofpdf/image_test.go @@ -17,7 +17,7 @@ import ( ) func TestNewImage(t *testing.T) { - image := gofpdf2.NewImage(&mocks.Fpdf{}, &mocks.Math{}) + image := gofpdf2.NewImage(mocks.NewFpdf(t), mocks.NewMath(t)) assert.NotNil(t, image) assert.Equal(t, fmt.Sprintf("%T", image), "*gofpdf.image") @@ -35,10 +35,10 @@ func TestImage_Add(t *testing.T) { ImageType: string(img.Extension), } - pdf := &mocks.Fpdf{} + pdf := mocks.NewFpdf(t) pdf.EXPECT().RegisterImageOptionsReader(mock.Anything, options, bytes.NewReader(img.Bytes)).Return(nil) - image := gofpdf2.NewImage(pdf, &mocks.Math{}) + image := gofpdf2.NewImage(pdf, mocks.NewMath(t)) // Act err := image.Add(&img, &cell, &margins, &rect, img.Extension, true) @@ -57,7 +57,7 @@ func TestImage_Add(t *testing.T) { ImageType: string(img.Extension), } - pdf := &mocks.Fpdf{} + pdf := mocks.NewFpdf(t) pdf.EXPECT().RegisterImageOptionsReader(mock.Anything, options, bytes.NewReader(img.Bytes)).Return(&gofpdf.ImageInfoType{}) pdf.EXPECT().Image(mock.Anything, 30.0, 35.0, 98.0, mock.Anything, true, "", 0, "") @@ -83,7 +83,7 @@ func TestImage_Add(t *testing.T) { ImageType: string(img.Extension), } - pdf := &mocks.Fpdf{} + pdf := mocks.NewFpdf(t) pdf.EXPECT().RegisterImageOptionsReader(mock.Anything, options, bytes.NewReader(img.Bytes)).Return(&gofpdf.ImageInfoType{}) pdf.EXPECT().Image(mock.Anything, 21.0, mock.Anything, 98.0, mock.Anything, true, "", 0, "") diff --git a/internal/providers/gofpdf/provider_test.go b/internal/providers/gofpdf/provider_test.go index a55710a3..eb300a89 100644 --- a/internal/providers/gofpdf/provider_test.go +++ b/internal/providers/gofpdf/provider_test.go @@ -40,7 +40,7 @@ func TestProvider_AddText(t *testing.T) { cell := &entity.Cell{} prop := fixture.TextProp() - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add(txtContent, cell, &prop) dep := &gofpdf.Dependencies{ @@ -60,7 +60,7 @@ func TestProvider_GetTextHeight(t *testing.T) { fontHeightToReturn := 10.0 prop := fixture.FontProp() - font := &mocks.Font{} + font := mocks.NewFont(t) font.EXPECT().GetHeight(prop.Family, prop.Style, prop.Size).Return(fontHeightToReturn) dep := &gofpdf.Dependencies{ @@ -81,7 +81,7 @@ func TestProvider_AddLine(t *testing.T) { cell := &entity.Cell{} prop := fixture.LineProp() - line := &mocks.Line{} + line := mocks.NewLine(t) line.EXPECT().Add(cell, &prop) dep := &gofpdf.Dependencies{ @@ -103,13 +103,13 @@ func TestProvider_AddMatrixCode(t *testing.T) { cell := &entity.Cell{} prop := fixture.RectProp() - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(nil, errors.New("anyError1")) - code := &mocks.Code{} + code := mocks.NewCode(t) code.EXPECT().GenDataMatrix(codeContent).Return(nil, errors.New("anyError2")) - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add("could not generate matrixcode", cell, merror.DefaultErrorText) dep := &gofpdf.Dependencies{ @@ -135,11 +135,11 @@ func TestProvider_AddMatrixCode(t *testing.T) { img := &entity.Image{Bytes: []byte{1, 2, 3}} - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(img, nil) cache.EXPECT().AddImage(codeContent, img) - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add("could not add matrixcode to document", cell, merror.DefaultErrorText) cfg := &entity.Config{ @@ -151,10 +151,10 @@ func TestProvider_AddMatrixCode(t *testing.T) { }, } - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, &prop, extension.Jpg, false).Return(errors.New("anyError")) - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().ClearError() dep := &gofpdf.Dependencies{ @@ -183,7 +183,7 @@ func TestProvider_AddMatrixCode(t *testing.T) { img := &entity.Image{Bytes: []byte{1, 2, 3}} - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(img, nil) cache.EXPECT().AddImage(codeContent, img) @@ -196,16 +196,12 @@ func TestProvider_AddMatrixCode(t *testing.T) { }, } - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, &prop, extension.Jpg, false).Return(nil) - fpdf := &mocks.Fpdf{} - fpdf.EXPECT().ClearError() - dep := &gofpdf.Dependencies{ Cache: cache, Image: image, - Fpdf: fpdf, Cfg: cfg, } @@ -228,13 +224,13 @@ func TestProvider_AddQrCode(t *testing.T) { cell := &entity.Cell{} prop := fixture.RectProp() - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(nil, errors.New("anyError1")) - code := &mocks.Code{} + code := mocks.NewCode(t) code.EXPECT().GenQr(codeContent).Return(nil, errors.New("anyError2")) - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add("could not generate qrcode", cell, merror.DefaultErrorText) dep := &gofpdf.Dependencies{ @@ -260,11 +256,11 @@ func TestProvider_AddQrCode(t *testing.T) { img := &entity.Image{Bytes: []byte{1, 2, 3}} - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(img, nil) cache.EXPECT().AddImage(codeContent, img) - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add("could not add qrcode to document", cell, merror.DefaultErrorText) cfg := &entity.Config{ @@ -276,10 +272,10 @@ func TestProvider_AddQrCode(t *testing.T) { }, } - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, &prop, extension.Jpg, false).Return(errors.New("anyError")) - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().ClearError() dep := &gofpdf.Dependencies{ @@ -308,7 +304,7 @@ func TestProvider_AddQrCode(t *testing.T) { img := &entity.Image{Bytes: []byte{1, 2, 3}} - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(codeContent, extension.Jpg).Return(img, nil) cache.EXPECT().AddImage(codeContent, img) @@ -321,16 +317,12 @@ func TestProvider_AddQrCode(t *testing.T) { }, } - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, &prop, extension.Jpg, false).Return(nil) - fpdf := &mocks.Fpdf{} - fpdf.EXPECT().ClearError() - dep := &gofpdf.Dependencies{ Cache: cache, Image: image, - Fpdf: fpdf, Cfg: cfg, } @@ -353,13 +345,13 @@ func TestProvider_AddBarCode(t *testing.T) { cell := &entity.Cell{} prop := fixture.BarcodeProp() - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(codeContent+"code128", extension.Jpg).Return(nil, errors.New("anyError1")) - code := &mocks.Code{} + code := mocks.NewCode(t) code.EXPECT().GenBar(codeContent, cell, &prop).Return(nil, errors.New("anyError2")) - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add("could not generate barcode", cell, merror.DefaultErrorText) dep := &gofpdf.Dependencies{ @@ -385,11 +377,11 @@ func TestProvider_AddBarCode(t *testing.T) { img := &entity.Image{Bytes: []byte{1, 2, 3}} - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(codeContent+"code128", extension.Jpg).Return(img, nil) cache.EXPECT().AddImage(codeContent+"code128", img) - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add("could not add barcode to document", cell, merror.DefaultErrorText) cfg := &entity.Config{ @@ -401,10 +393,10 @@ func TestProvider_AddBarCode(t *testing.T) { }, } - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, prop.ToRectProp(), extension.Jpg, false).Return(errors.New("anyError")) - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().ClearError() dep := &gofpdf.Dependencies{ @@ -433,7 +425,7 @@ func TestProvider_AddBarCode(t *testing.T) { img := &entity.Image{Bytes: []byte{1, 2, 3}} - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(codeContent+"code128", extension.Jpg).Return(img, nil) cache.EXPECT().AddImage(codeContent+"code128", img) @@ -446,16 +438,12 @@ func TestProvider_AddBarCode(t *testing.T) { }, } - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, prop.ToRectProp(), extension.Jpg, false).Return(nil) - fpdf := &mocks.Fpdf{} - fpdf.EXPECT().ClearError() - dep := &gofpdf.Dependencies{ Cache: cache, Image: image, - Fpdf: fpdf, Cfg: cfg, } @@ -477,7 +465,7 @@ func TestProvider_AddBarCode(t *testing.T) { img := &entity.Image{Bytes: []byte{1, 2, 3}} - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(codeContent+"ean", extension.Jpg).Return(img, nil) cache.EXPECT().AddImage(codeContent+"ean", img) @@ -490,16 +478,12 @@ func TestProvider_AddBarCode(t *testing.T) { }, } - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, prop.ToRectProp(), extension.Jpg, false).Return(nil) - fpdf := &mocks.Fpdf{} - fpdf.EXPECT().ClearError() - dep := &gofpdf.Dependencies{ Cache: cache, Image: image, - Fpdf: fpdf, Cfg: cfg, } @@ -519,7 +503,7 @@ func TestProvider_CreateRow(t *testing.T) { // Arrange height := 10.0 - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().Ln(height) dep := &gofpdf.Dependencies{ @@ -542,7 +526,7 @@ func TestProvider_CreateCol(t *testing.T) { cfg := &entity.Config{} prop := fixture.CellProp() - cellWriter := &mocks.CellWriter{} + cellWriter := mocks.NewCellWriter(t) cellWriter.EXPECT().Apply(width, height, cfg, &prop) dep := &gofpdf.Dependencies{ @@ -575,7 +559,7 @@ func TestProvider_SetProtection(t *testing.T) { OwnerPassword: "ownerPassword", } - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetProtection(byte(p.Type), p.UserPassword, p.OwnerPassword) dep := &gofpdf.Dependencies{ @@ -594,7 +578,7 @@ func TestProvider_SetProtection(t *testing.T) { func TestProvider_SetCompression(t *testing.T) { // Arrange - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetCompression(true) dep := &gofpdf.Dependencies{ @@ -624,7 +608,7 @@ func TestProvider_SetMetadata(t *testing.T) { // Arrange timeNow := time.Now() - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetAuthor("author", true) fpdf.EXPECT().SetCreator("creator", true) fpdf.EXPECT().SetSubject("subject", true) @@ -668,7 +652,7 @@ func TestProvider_SetMetadata(t *testing.T) { func TestProvider_GenerateBytes(t *testing.T) { // Arrange - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().Output(mock.Anything).Return(errors.New("anyError")) dep := &gofpdf.Dependencies{ @@ -691,7 +675,7 @@ func TestProvider_AddImageFromBytes(t *testing.T) { prop := fixture.RectProp() cell := &entity.Cell{} - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add("could not parse image bytes", cell, merror.DefaultErrorText) dep := &gofpdf.Dependencies{ @@ -724,13 +708,13 @@ func TestProvider_AddImageFromBytes(t *testing.T) { }, } - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add("could not add image to document", cell, merror.DefaultErrorText) - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, &prop, img.Extension, false).Return(errors.New("anyError")) - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().ClearError() dep := &gofpdf.Dependencies{ @@ -768,7 +752,7 @@ func TestProvider_AddImageFromBytes(t *testing.T) { }, } - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, &prop, img.Extension, false).Return(nil) dep := &gofpdf.Dependencies{ @@ -792,7 +776,7 @@ func TestProvider_AddBackgroundImageFromBytes(t *testing.T) { prop := fixture.RectProp() cell := &entity.Cell{} - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add("could not parse image bytes", cell, merror.DefaultErrorText) dep := &gofpdf.Dependencies{ @@ -825,13 +809,13 @@ func TestProvider_AddBackgroundImageFromBytes(t *testing.T) { }, } - text := &mocks.Text{} + text := mocks.NewText(t) text.EXPECT().Add("could not add image to document", cell, merror.DefaultErrorText) - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, &prop, img.Extension, true).Return(errors.New("anyError")) - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().ClearError() fpdf.EXPECT().SetHomeXY() @@ -871,10 +855,10 @@ func TestProvider_AddBackgroundImageFromBytes(t *testing.T) { }, } - image := &mocks.Image{} + image := mocks.NewImage(t) image.EXPECT().Add(img, cell, cfg.Margins, &prop, img.Extension, true).Return(nil) - fpdf := &mocks.Fpdf{} + fpdf := mocks.NewFpdf(t) fpdf.EXPECT().SetHomeXY() dep := &gofpdf.Dependencies{ @@ -901,7 +885,7 @@ func TestProvider_AddBackgroundImageFromBytes(t *testing.T) { cell := &entity.Cell{} prop := fixture.RectProp() - cache := &mocks.Cache{} + cache := mocks.NewCache(t) cache.EXPECT().GetImage(file, extension.Jpg) dep := &gofpdf.Dependencies{ diff --git a/internal/providers/gofpdf/text_test.go b/internal/providers/gofpdf/text_test.go index a1bc1adb..7ab9f391 100644 --- a/internal/providers/gofpdf/text_test.go +++ b/internal/providers/gofpdf/text_test.go @@ -11,511 +11,8 @@ import ( ) func TestNewText(t *testing.T) { - text := gofpdf.NewText(&mocks.Fpdf{}, &mocks.Math{}, &mocks.Font{}) + text := gofpdf.NewText(mocks.NewFpdf(t), mocks.NewMath(t), mocks.NewFont(t)) assert.NotNil(t, text) assert.Equal(t, fmt.Sprintf("%T", text), "*gofpdf.text") } - -/*func TestText_GetLinesQuantity_WhenStringSmallerThanLimits(t *testing.T) { - // Arrange - pdf := &mocks.Fpdf{} - pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(text string) string { - return text - }) - pdf.On("GetStringWidth", mock.Anything).Return(8.0) - - fontstyle := &mocks.DefaultFont{} - fontstyle.On("SetFont", mock.Anything, mock.Anything, mock.Anything).Return(nil) - - sut := internal.NewText(pdf, nil, fontstyle) - - // Act - lines := sut.GetLinesQuantity("AnyText With Spaces", props.Text{}, 2) - - // Assert - assert.Equal(t, lines, 4) -} - -func TestText_GetLinesQuantity_WhenHasOneWord(t *testing.T) { - // Arrange - pdf := &mocks.Fpdf{} - pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(text string) string { - return text - }) - pdf.On("GetStringWidth", mock.Anything).Return(15.0) - - fontstyle := &mocks.DefaultFont{} - fontstyle.On("SetFont", mock.Anything, mock.Anything, mock.Anything).Return(nil) - - sut := internal.NewText(pdf, nil, fontstyle) - - // Act - lines := sut.GetLinesQuantity("OneWord", props.Text{}, 2) - - // Assert - assert.Equal(t, lines, 1) -} - -func TestText_GetLinesQuantity_WhenExtrapolate(t *testing.T) { - // Arrange - pdf := &mocks.Fpdf{} - pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(text string) string { - return text - }) - pdf.On("GetStringWidth", mock.Anything).Return(15.0) - - fontstyle := &mocks.DefaultFont{} - fontstyle.On("SetFont", mock.Anything, mock.Anything, mock.Anything).Return(nil) - - sut := internal.NewText(pdf, nil, fontstyle) - - // Act - lines := sut.GetLinesQuantity("Many words", props.Text{Extrapolate: true}, 2) - - // Assert - assert.Equal(t, lines, 1) -} - -func TestText_GetLinesQuantity_WhenHasToBreakLines(t *testing.T) { - // Arrange - pdf := &mocks.Fpdf{} - pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(text string) string { - return text - }) - pdf.On("GetStringWidth", mock.Anything).Return(15.0) - - math := &mocks.Math{} - math.On("GetWidthPerCol", mock.Anything).Return(10.0) - - fontstyle := &mocks.DefaultFont{} - fontstyle.On("SetFont", mock.Anything, mock.Anything, mock.Anything).Return(nil) - - sut := internal.NewText(pdf, math, fontstyle) - - // Act - lines := sut.GetLinesQuantity("Many words", props.Text{}, 2) - - // Assert - assert.Equal(t, lines, 3) -} - -func TestText_Add(t *testing.T) { - cases := []struct { - name string - text string - align consts.Align - family string - color color.Color - pdf func() *mocks.Fpdf - fontstyle func() *mocks.DefaultFont - cell func() *internal.Cell - assertPdf func(t *testing.T, pdf *mocks.Fpdf) - assertFont func(t *testing.T, fontstyle *mocks.DefaultFont) - }{ - { - "Left Align", - "TextHelper1", - align.Type, - consts.Arial, - color.Color{Red: 0, Green: 0, Blue: 0}, - func() *mocks.Fpdf { - _pdf := &mocks.Fpdf{} - _pdf.On("GetStringWidth", mock.Anything).Return(12.0) - _pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) - _pdf.On("Text", mock.Anything, mock.Anything, mock.Anything) - _pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value }) - return _pdf - }, - func() *mocks.DefaultFont { - _font := &mocks.DefaultFont{} - _font.On("GetScaleFactor").Return(1.0) - _font.On("GetFont").Return(consts.Arial, consts.Bold, 1.0) - _font.On("SetFont", mock.Anything, mock.Anything, mock.Anything) - _font.On("GetColor").Return(color.Color{Red: 0, Green: 0, Blue: 0}) - _font.On("SetColor", mock.Anything) - return _font - }, - func() *internal.Cell { - return nil - }, - func(t *testing.T, _pdf *mocks.Fpdf) { - _pdf.AssertNotCalled(t, "GetStringWidth") - - _pdf.AssertNumberOfCalls(t, "GetMargins", 1) - - _pdf.AssertNumberOfCalls(t, "Text", 1) - _pdf.AssertCalled(t, "Text", 11.0, 16.0, "TextHelper1") - }, - func(t *testing.T, _font *mocks.DefaultFont) { - _font.AssertNumberOfCalls(t, "SetFont", 1) - _font.AssertCalled(t, "SetFont", consts.Arial, consts.BoldItalic, 16.0) - _font.AssertNumberOfCalls(t, "GetColor", 1) - _font.AssertNumberOfCalls(t, "SetColor", 2) - _font.AssertCalled(t, "SetColor", color.Color{Red: 0, Green: 0, Blue: 0}) - }, - }, - { - "Custom DefaultFont", - "TextHelper1", - align.Type, - "CustomFont", - color.Color{Red: 0, Green: 0, Blue: 0}, - func() *mocks.Fpdf { - _pdf := &mocks.Fpdf{} - _pdf.On("GetStringWidth", mock.Anything).Return(12.0) - _pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) - _pdf.On("Text", mock.Anything, mock.Anything, mock.Anything) - _pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value }) - return _pdf - }, - func() *mocks.DefaultFont { - _font := &mocks.DefaultFont{} - _font.On("GetScaleFactor").Return(1.0) - _font.On("GetFont").Return(consts.Arial, consts.Bold, 1.0) - _font.On("SetFont", mock.Anything, mock.Anything, mock.Anything) - _font.On("GetColor").Return(color.Color{Red: 0, Green: 0, Blue: 0}) - _font.On("SetColor", mock.Anything) - return _font - }, - func() *internal.Cell { - return nil - }, - func(t *testing.T, _pdf *mocks.Fpdf) { - _pdf.AssertNotCalled(t, "GetStringWidth") - - _pdf.AssertNumberOfCalls(t, "GetMargins", 1) - - _pdf.AssertNumberOfCalls(t, "Text", 1) - _pdf.AssertCalled(t, "Text", 11.0, 16.0, "TextHelper1") - }, - func(t *testing.T, _font *mocks.DefaultFont) { - _font.AssertNumberOfCalls(t, "SetFont", 1) - _font.AssertCalled(t, "SetFont", "CustomFont", consts.BoldItalic, 16.0) - _font.AssertNumberOfCalls(t, "GetColor", 1) - _font.AssertNumberOfCalls(t, "SetColor", 2) - _font.AssertCalled(t, "SetColor", color.Color{Red: 0, Green: 0, Blue: 0}) - }, - }, - // nolint:dupl // better this way - { - "Center Align", - "TextHelper2", - consts.Center, - consts.Arial, - color.Color{Red: 0, Green: 0, Blue: 0}, - func() *mocks.Fpdf { - _pdf := &mocks.Fpdf{} - _pdf.On("GetStringWidth", mock.Anything).Return(12.0) - _pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) - _pdf.On("Text", mock.Anything, mock.Anything, mock.Anything) - _pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value }) - return _pdf - }, - func() *mocks.DefaultFont { - _font := &mocks.DefaultFont{} - _font.On("GetScaleFactor").Return(1.0) - _font.On("GetFont").Return(consts.Arial, consts.Bold, 1.0) - _font.On("SetFont", mock.Anything, mock.Anything, mock.Anything) - _font.On("GetColor").Return(color.Color{Red: 0, Green: 0, Blue: 0}) - _font.On("SetColor", mock.Anything) - return _font - }, - func() *internal.Cell { - return nil - }, - func(t *testing.T, _pdf *mocks.Fpdf) { - _pdf.AssertNumberOfCalls(t, "GetStringWidth", 1) - _pdf.AssertCalled(t, "GetStringWidth", "TextHelper2") - - _pdf.AssertNumberOfCalls(t, "GetMargins", 1) - - _pdf.AssertNumberOfCalls(t, "Text", 1) - _pdf.AssertCalled(t, "Text", 12.5, 16.0, "TextHelper2") - }, - func(t *testing.T, _font *mocks.DefaultFont) { - _font.AssertNumberOfCalls(t, "SetFont", 1) - _font.AssertCalled(t, "SetFont", consts.Arial, consts.BoldItalic, 16.0) - _font.AssertNumberOfCalls(t, "GetColor", 1) - _font.AssertNumberOfCalls(t, "SetColor", 2) - _font.AssertCalled(t, "SetColor", color.Color{Red: 0, Green: 0, Blue: 0}) - }, - }, - // nolint:dupl // better this way - { - "Right Align", - "TextHelper3", - consts.Right, - consts.Arial, - color.Color{Red: 0, Green: 0, Blue: 0}, - func() *mocks.Fpdf { - _pdf := &mocks.Fpdf{} - _pdf.On("GetStringWidth", mock.Anything).Return(12.0) - _pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) - _pdf.On("Text", mock.Anything, mock.Anything, mock.Anything) - _pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value }) - return _pdf - }, - func() *mocks.DefaultFont { - _font := &mocks.DefaultFont{} - _font.On("GetScaleFactor").Return(1.0) - _font.On("GetFont").Return(consts.Arial, consts.Bold, 1.0) - _font.On("SetFont", mock.Anything, mock.Anything, mock.Anything) - _font.On("GetColor").Return(color.Color{Red: 0, Green: 0, Blue: 0}) - _font.On("SetColor", mock.Anything) - return _font - }, func() *internal.Cell { - return nil - }, - func(t *testing.T, _pdf *mocks.Fpdf) { - _pdf.AssertNumberOfCalls(t, "GetStringWidth", 1) - _pdf.AssertCalled(t, "GetStringWidth", "TextHelper3") - - _pdf.AssertNumberOfCalls(t, "GetMargins", 1) - - _pdf.AssertNumberOfCalls(t, "Text", 1) - _pdf.AssertCalled(t, "Text", 14.0, 16.0, "TextHelper3") - }, - func(t *testing.T, _font *mocks.DefaultFont) { - _font.AssertNumberOfCalls(t, "SetFont", 1) - _font.AssertCalled(t, "SetFont", consts.Arial, consts.BoldItalic, 16.0) - _font.AssertNumberOfCalls(t, "GetColor", 1) - _font.AssertNumberOfCalls(t, "SetColor", 2) - _font.AssertCalled(t, "SetColor", color.Color{Red: 0, Green: 0, Blue: 0}) - }, - }, - // nolint:dupl // better this way - { - "Right Align", - "TextHelper4", - consts.Right, - consts.Arial, - color.Color{Red: 0, Green: 0, Blue: 0}, - func() *mocks.Fpdf { - _pdf := &mocks.Fpdf{} - _pdf.On("GetStringWidth", mock.Anything).Return(12.0) - _pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) - _pdf.On("Text", mock.Anything, mock.Anything, mock.Anything) - _pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value }) - return _pdf - }, - func() *mocks.DefaultFont { - _font := &mocks.DefaultFont{} - _font.On("GetScaleFactor").Return(1.0) - _font.On("GetFont").Return(consts.Arial, consts.Bold, 1.0) - _font.On("SetFont", mock.Anything, mock.Anything, mock.Anything) - _font.On("GetColor").Return(color.Color{Red: 0, Green: 0, Blue: 0}) - _font.On("SetColor", mock.Anything) - return _font - }, func() *internal.Cell { - return nil - }, - func(t *testing.T, _pdf *mocks.Fpdf) { - _pdf.AssertNumberOfCalls(t, "GetStringWidth", 1) - _pdf.AssertCalled(t, "GetStringWidth", "TextHelper4") - - _pdf.AssertNumberOfCalls(t, "GetMargins", 1) - - _pdf.AssertNumberOfCalls(t, "Text", 1) - _pdf.AssertCalled(t, "Text", 14.0, 16.0, "TextHelper4") - }, - func(t *testing.T, _font *mocks.DefaultFont) { - _font.AssertNumberOfCalls(t, "SetFont", 1) - _font.AssertCalled(t, "SetFont", consts.Arial, consts.BoldItalic, 16.0) - _font.AssertNumberOfCalls(t, "GetColor", 1) - _font.AssertNumberOfCalls(t, "SetColor", 2) - _font.AssertCalled(t, "SetColor", color.Color{Red: 0, Green: 0, Blue: 0}) - }, - }, - { - "Bigger than cell width", - "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.", - consts.Center, - consts.Arial, - color.Color{Red: 0, Green: 0, Blue: 0}, - func() *mocks.Fpdf { - _pdf := &mocks.Fpdf{} - _pdf.On("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.").Return(900.0) - _pdf.On("GetStringWidth", mock.Anything).Return(20.0) - _pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) - _pdf.On("Text", mock.Anything, mock.Anything, mock.Anything) - _pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value }) - return _pdf - }, - func() *mocks.DefaultFont { - _font := &mocks.DefaultFont{} - _font.On("GetScaleFactor").Return(1.0) - _font.On("GetFont").Return(consts.Arial, consts.Bold, 1.0) - _font.On("SetFont", mock.Anything, mock.Anything, mock.Anything) - _font.On("GetColor").Return(color.Color{Red: 0, Green: 0, Blue: 0}) - _font.On("SetColor", mock.Anything) - return _font - }, func() *internal.Cell { - return nil - }, - func(t *testing.T, _pdf *mocks.Fpdf) { - _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", 92) - - _pdf.AssertNumberOfCalls(t, "Text", 92) - }, - func(t *testing.T, _font *mocks.DefaultFont) { - _font.AssertNumberOfCalls(t, "SetFont", 1) - _font.AssertCalled(t, "SetFont", consts.Arial, consts.BoldItalic, 16.0) - _font.AssertNumberOfCalls(t, "GetColor", 1) - _font.AssertNumberOfCalls(t, "SetColor", 2) - _font.AssertCalled(t, "SetColor", color.Color{Red: 0, Green: 0, Blue: 0}) - }, - }, - { - "Customizable col width", - "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.", - consts.Center, - consts.Arial, - color.Color{Red: 0, Green: 0, Blue: 0}, - func() *mocks.Fpdf { - _pdf := &mocks.Fpdf{} - _pdf.On("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."). - Return(900.0) - _pdf.On("GetStringWidth", mock.Anything).Return(20.0) - _pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) - _pdf.On("Text", mock.Anything, mock.Anything, mock.Anything) - _pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value }) - return _pdf - }, - func() *mocks.DefaultFont { - _font := &mocks.DefaultFont{} - _font.On("GetScaleFactor").Return(1.0) - _font.On("GetFont").Return(consts.Arial, consts.Bold, 1.0) - _font.On("SetFont", mock.Anything, mock.Anything, mock.Anything) - _font.On("GetColor").Return(color.Color{Red: 0, Green: 0, Blue: 0}) - _font.On("SetColor", mock.Anything) - return _font - }, - func() *internal.Cell { - return &internal.Cell{ - X: 1.0, - Y: 5.0, - Width: 25.0, - } - }, - func(t *testing.T, _pdf *mocks.Fpdf) { - _pdf.AssertNumberOfCalls(t, "GetStringWidth", 274) - _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", 91) - - _pdf.AssertNumberOfCalls(t, "Text", 91) - }, - func(t *testing.T, _font *mocks.DefaultFont) { - _font.AssertNumberOfCalls(t, "SetFont", 1) - _font.AssertCalled(t, "SetFont", consts.Arial, consts.BoldItalic, 16.0) - _font.AssertNumberOfCalls(t, "GetColor", 1) - _font.AssertNumberOfCalls(t, "SetColor", 2) - _font.AssertCalled(t, "SetColor", color.Color{Red: 0, Green: 0, Blue: 0}) - }, - }, - { - "Custom DefaultFont Color", - "CustomFontColor", - align.Type, - consts.Arial, - color.Color{Red: 20, Green: 20, Blue: 20}, - func() *mocks.Fpdf { - _pdf := &mocks.Fpdf{} - _pdf.On("GetStringWidth", mock.Anything).Return(12.0) - _pdf.On("GetMargins").Return(10.0, 10.0, 10.0, 10.0) - _pdf.On("Text", mock.Anything, mock.Anything, mock.Anything) - _pdf.On("UnicodeTranslatorFromDescriptor", mock.Anything).Return(func(value string) string { return value }) - return _pdf - }, - func() *mocks.DefaultFont { - _font := &mocks.DefaultFont{} - _font.On("GetScaleFactor").Return(1.0) - _font.On("GetFont").Return(consts.Arial, consts.Bold, 1.0) - _font.On("SetFont", mock.Anything, mock.Anything, mock.Anything) - _font.On("GetColor").Return(color.Color{Red: 0, Green: 0, Blue: 0}) - _font.On("SetColor", mock.Anything) - return _font - }, - func() *internal.Cell { - return nil - }, - func(t *testing.T, _pdf *mocks.Fpdf) { - _pdf.AssertNotCalled(t, "GetStringWidth") - - _pdf.AssertNumberOfCalls(t, "GetMargins", 1) - - _pdf.AssertNumberOfCalls(t, "Text", 1) - _pdf.AssertCalled(t, "Text", 11.0, 16.0, "CustomFontColor") - }, - func(t *testing.T, _font *mocks.DefaultFont) { - _font.AssertNumberOfCalls(t, "SetFont", 1) - _font.AssertCalled(t, "SetFont", consts.Arial, consts.BoldItalic, 16.0) - _font.AssertNumberOfCalls(t, "GetColor", 1) - _font.AssertNumberOfCalls(t, "SetColor", 2) - _font.AssertCalled(t, "SetColor", color.Color{Red: 0, Green: 0, Blue: 0}) - _font.AssertCalled(t, "SetColor", color.Color{Red: 20, Green: 20, Blue: 20}) - }, - }, - } - - for _, c := range cases { - // Arrange - _pdf := c.pdf() - _font := c.fontstyle() - - text := internal.NewText(_pdf, nil, _font) - - var cell internal.Cell - if c.cell() == nil { - cell = internal.Cell{ - X: 1.0, - Y: 5.0, - Width: 15.0, - } - } else { - cell = *c.cell() - } - - // Act - text.Add(c.text, cell, props.Text{Family: c.family, Style: consts.BoldItalic, Size: 16.0, Align: c.align, Color: c.color}) - - // Assert - c.assertPdf(t, _pdf) - c.assertFont(t, _font) - } -} -*/ diff --git a/metricsdecorator_test.go b/metricsdecorator_test.go index 9185ff63..d8d290e3 100644 --- a/metricsdecorator_test.go +++ b/metricsdecorator_test.go @@ -27,9 +27,9 @@ func TestMetricsDecorator_AddPages(t *testing.T) { // Arrange pg := page.New() - docToReturn := &mocks.Document{} + docToReturn := mocks.NewDocument(t) docToReturn.EXPECT().GetBytes().Return([]byte{1, 2, 3}) - inner := &mocks.Maroto{} + inner := mocks.NewMaroto(t) inner.EXPECT().AddPages(pg) inner.EXPECT().Generate().Return(docToReturn, nil) @@ -57,9 +57,9 @@ func TestMetricsDecorator_AddRow(t *testing.T) { // Arrange col := col.New(12) - docToReturn := &mocks.Document{} + docToReturn := mocks.NewDocument(t) docToReturn.EXPECT().GetBytes().Return([]byte{1, 2, 3}) - inner := &mocks.Maroto{} + inner := mocks.NewMaroto(t) inner.EXPECT().AddRow(10.0, col).Return(nil) inner.EXPECT().Generate().Return(docToReturn, nil) @@ -87,9 +87,9 @@ func TestMetricsDecorator_AddRows(t *testing.T) { // Arrange row := row.New(10).Add(col.New(12)) - docToReturn := &mocks.Document{} + docToReturn := mocks.NewDocument(t) docToReturn.EXPECT().GetBytes().Return([]byte{1, 2, 3}) - inner := &mocks.Maroto{} + inner := mocks.NewMaroto(t) inner.EXPECT().AddRows(row) inner.EXPECT().Generate().Return(docToReturn, nil) @@ -117,9 +117,9 @@ func TestMetricsDecorator_GetStructure(t *testing.T) { // Arrange row := row.New(10).Add(col.New(12)) - docToReturn := &mocks.Document{} + docToReturn := mocks.NewDocument(t) docToReturn.EXPECT().GetBytes().Return([]byte{1, 2, 3}) - inner := &mocks.Maroto{} + inner := mocks.NewMaroto(t) inner.EXPECT().AddRows(row) inner.EXPECT().GetStructure().Return(&node.Node[core.Structure]{}) inner.EXPECT().Generate().Return(docToReturn, nil) @@ -147,7 +147,7 @@ func TestMetricsDecorator_GetStructure(t *testing.T) { } func TestMetricsDecorator_FitlnCurrentPage(t *testing.T) { - inner := &mocks.Maroto{} + inner := mocks.NewMaroto(t) inner.EXPECT().FitlnCurrentPage(10.0).Return(true) inner.EXPECT().FitlnCurrentPage(20.0).Return(false) diff --git a/pkg/components/code/barcode_test.go b/pkg/components/code/barcode_test.go index a39d14c5..48f4ebbb 100644 --- a/pkg/components/code/barcode_test.go +++ b/pkg/components/code/barcode_test.go @@ -69,7 +69,7 @@ func TestBarcode_Render(t *testing.T) { prop := fixture.BarcodeProp() sut := code.NewBar(codeValue, prop) - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().AddBarCode(codeValue, &cell, &prop) // Act diff --git a/pkg/components/code/matrixcode_test.go b/pkg/components/code/matrixcode_test.go index b551acd4..bca9287b 100644 --- a/pkg/components/code/matrixcode_test.go +++ b/pkg/components/code/matrixcode_test.go @@ -69,7 +69,7 @@ func TestMatrixCode_Render(t *testing.T) { prop := fixture.RectProp() sut := code.NewMatrix(codeValue, prop) - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().AddMatrixCode(codeValue, &cell, &prop) // Act diff --git a/pkg/components/code/qrcode_test.go b/pkg/components/code/qrcode_test.go index 0fd22a30..f6b0ee92 100644 --- a/pkg/components/code/qrcode_test.go +++ b/pkg/components/code/qrcode_test.go @@ -69,7 +69,7 @@ func TestQrCode_Render(t *testing.T) { prop := fixture.RectProp() sut := code.NewQr(codeValue, prop) - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().AddQrCode(codeValue, &cell, &prop) // Act diff --git a/pkg/components/col/col_test.go b/pkg/components/col/col_test.go index b872812d..d1332688 100644 --- a/pkg/components/col/col_test.go +++ b/pkg/components/col/col_test.go @@ -77,9 +77,9 @@ func TestCol_Render(t *testing.T) { cell := fixture.CellEntity() style := &props.Cell{} - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) - component := &mocks.Component{} + component := mocks.NewComponent(t) component.EXPECT().Render(provider, &cell) component.EXPECT().SetConfig(cfg) @@ -100,10 +100,10 @@ func TestCol_Render(t *testing.T) { cell := fixture.CellEntity() style := &props.Cell{} - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().CreateCol(cell.Width, cell.Height, cfg, style) - component := &mocks.Component{} + component := mocks.NewComponent(t) component.EXPECT().Render(provider, &cell) component.EXPECT().SetConfig(cfg) diff --git a/pkg/components/image/bytesimage_test.go b/pkg/components/image/bytesimage_test.go index 4a3a80ea..938fa6f6 100644 --- a/pkg/components/image/bytesimage_test.go +++ b/pkg/components/image/bytesimage_test.go @@ -70,7 +70,7 @@ func TestBytesImage_Render(t *testing.T) { prop := fixture.RectProp() sut := image.NewFromBytes(bytes, ext, prop) - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().AddImageFromBytes(bytes, &cell, &prop, ext) // Act diff --git a/pkg/components/image/fileimage_test.go b/pkg/components/image/fileimage_test.go index 8856e810..29a12c34 100644 --- a/pkg/components/image/fileimage_test.go +++ b/pkg/components/image/fileimage_test.go @@ -69,7 +69,7 @@ func TestFileImage_Render(t *testing.T) { prop := fixture.RectProp() sut := image.NewFromFile(path, prop) - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().AddImageFromFile(path, &cell, &prop) // Act diff --git a/pkg/components/line/line_test.go b/pkg/components/line/line_test.go index 380e7c52..f08a0e61 100644 --- a/pkg/components/line/line_test.go +++ b/pkg/components/line/line_test.go @@ -68,7 +68,7 @@ func TestLine_Render(t *testing.T) { prop := fixture.LineProp() sut := line.New(prop) - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().AddLine(&cell, &prop) // Act diff --git a/pkg/components/page/page_test.go b/pkg/components/page/page_test.go index ba6b5622..93adfb7f 100644 --- a/pkg/components/page/page_test.go +++ b/pkg/components/page/page_test.go @@ -50,8 +50,8 @@ func TestPage_Render(t *testing.T) { prop.Pattern = "" cfg := &entity.Config{} - provider := &mocks.Provider{} - row := &mocks.Row{} + provider := mocks.NewProvider(t) + row := mocks.NewRow(t) row.EXPECT().Render(provider, cell) row.EXPECT().GetHeight().Return(10.0) row.EXPECT().SetConfig(cfg) @@ -82,9 +82,9 @@ func TestPage_Render(t *testing.T) { rectProp := &props.Rect{} rectProp.MakeValid() - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().AddBackgroundImageFromBytes(cfg.BackgroundImage.Bytes, &cell, rectProp, cfg.BackgroundImage.Extension) - row := &mocks.Row{} + row := mocks.NewRow(t) row.EXPECT().Render(provider, cell) row.EXPECT().GetHeight().Return(10.0) row.EXPECT().SetConfig(cfg) @@ -115,10 +115,10 @@ func TestPage_Render(t *testing.T) { rectProp := &props.Rect{} rectProp.MakeValid() - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().AddBackgroundImageFromBytes(cfg.BackgroundImage.Bytes, &cell, rectProp, cfg.BackgroundImage.Extension) provider.EXPECT().AddText("0 / 0", &cell, prop.GetNumberTextProp(cell.Height)) - row := &mocks.Row{} + row := mocks.NewRow(t) row.EXPECT().Render(provider, cell) row.EXPECT().GetHeight().Return(10.0) row.EXPECT().SetConfig(cfg) @@ -154,7 +154,7 @@ func TestPage_SetNumber(t *testing.T) { func TestPage_GetRows(t *testing.T) { t.Run("when called get rows, should return rows correctly", func(t *testing.T) { // Arrange - row := &mocks.Row{} + row := mocks.NewRow(t) sut := page.New() sut.Add(row) diff --git a/pkg/components/row/row_test.go b/pkg/components/row/row_test.go index f5dd7a3e..440d1a62 100644 --- a/pkg/components/row/row_test.go +++ b/pkg/components/row/row_test.go @@ -55,10 +55,10 @@ func TestRow_GetStructure(t *testing.T) { } cell := fixture.CellEntity() - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().CreateRow(cell.Height) - col := &mocks.Col{} + col := mocks.NewCol(t) col.EXPECT().Render(provider, cell, true) col.EXPECT().SetConfig(cfg) col.EXPECT().GetSize().Return(12) @@ -82,11 +82,11 @@ func TestRow_GetStructure(t *testing.T) { cell := fixture.CellEntity() prop := fixture.CellProp() - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().CreateRow(cell.Height) provider.EXPECT().CreateCol(cell.Width, cell.Height, cfg, &prop) - col := &mocks.Col{} + col := mocks.NewCol(t) col.EXPECT().Render(provider, cell, false) col.EXPECT().SetConfig(cfg) col.EXPECT().GetSize().Return(12) diff --git a/pkg/components/signature/signature_test.go b/pkg/components/signature/signature_test.go index 69f65c57..d12f9b6b 100644 --- a/pkg/components/signature/signature_test.go +++ b/pkg/components/signature/signature_test.go @@ -69,7 +69,7 @@ func TestSignature_Render(t *testing.T) { prop := fixture.SignatureProp() sut := signature.New(label, prop) - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.On("AddText", mock.Anything, mock.Anything, mock.Anything).Return(10.0) provider.On("GetTextHeight", mock.Anything).Return(10.0) provider.On("AddLine", mock.Anything, mock.Anything) diff --git a/pkg/components/text/text_test.go b/pkg/components/text/text_test.go index cdcb290d..d751a812 100644 --- a/pkg/components/text/text_test.go +++ b/pkg/components/text/text_test.go @@ -69,7 +69,7 @@ func TestText_Render(t *testing.T) { prop := fixture.TextProp() sut := text.New(value, prop) - provider := &mocks.Provider{} + provider := mocks.NewProvider(t) provider.EXPECT().AddText(value, &cell, &prop) sut.SetConfig(&entity.Config{}) diff --git a/pull_request_template.md b/pull_request_template.md index cea6849b..d1823ee0 100644 --- a/pull_request_template.md +++ b/pull_request_template.md @@ -9,11 +9,13 @@ **Checklist** -> check with "x", if applied to your change +> check with "x", **ONLY IF APPLIED** to your change - [ ] All methods associated with structs has ```func ( *struct) method() {}``` name style. - [ ] Wrote unit tests for new/changed features. - [ ] Followed the unit test ```when,should``` naming pattern. +- [ ] All mocks created with ```m := mocks.NewConstructor(t)```. +- [ ] All mocks using ```m.EXPECT().MethodName()``` method to mock methods. - [ ] Updated docs/doc.go and docs/* - [ ] Updated example_test.go - [ ] Updated README.md diff --git a/install.sh b/shell/install.sh similarity index 88% rename from install.sh rename to shell/install.sh index 69354248..057dd783 100644 --- a/install.sh +++ b/shell/install.sh @@ -9,4 +9,4 @@ sudo cp $GOPATH/bin/gofumpt /usr/local/bin/ curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.43.0 sudo cp $GOPATH/bin/golangci-lint /usr/local/bin/ -go install github.com/vektra/mockery/v2@v2.33.3 \ No newline at end of file +go install github.com/vektra/mockery/v2@latest \ No newline at end of file diff --git a/shell/mock-check.sh b/shell/mock-check.sh new file mode 100644 index 00000000..08b2cd7b --- /dev/null +++ b/shell/mock-check.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +RED='\033[0;31m' +CYAN='\033[0;36m' +NC='\033[0m' + +mockOccurrences="`grep -n --include \*.go --exclude-dir=vendor -R mocks.`" +mocksNotInImports="`echo "$mockOccurrences" | grep -v "/mocks"`" +mocksCreatedWithoutNew="`echo "$mocksNotInImports" | grep -v "New"`" + +if [ -z "$mocksCreatedWithoutNew" ]; +then + exit 0 +else + printf "${RED}ERROR:${NC} there are mocks that doesn't follow the constructor pattern ${CYAN}m := mocks.NewConstructor(t)${NC}:\n" + printf "${RED}$mocksCreatedWithoutNew" + exit 1 +fi \ No newline at end of file