From dc3ee17ec18d5ef567daba40d253c88e940f25f1 Mon Sep 17 00:00:00 2001 From: esimov Date: Sat, 10 Dec 2022 07:39:12 +0200 Subject: [PATCH] test: using testify package for test cases --- carver_test.go | 49 ++++++++++++++++++++++++++----------------------- process_test.go | 32 ++++++++++++++++---------------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/carver_test.go b/carver_test.go index c234d62..bb21b50 100644 --- a/carver_test.go +++ b/carver_test.go @@ -10,6 +10,7 @@ import ( "github.com/esimov/caire/utils" pigo "github.com/esimov/pigo/core" + "github.com/stretchr/testify/assert" ) const ( @@ -32,6 +33,8 @@ func init() { } func TestCarver_EnergySeamShouldNotBeDetected(t *testing.T) { + assert := assert.New(t) + var seams [][]Seam var totalEnergySeams int @@ -52,12 +55,12 @@ func TestCarver_EnergySeamShouldNotBeDetected(t *testing.T) { totalEnergySeams += seams[i][s].X } } - if totalEnergySeams != 0 { - t.Errorf("Energy seam shouldn't been detected") - } + assert.Equal(0, totalEnergySeams) } func TestCarver_DetectHorizontalEnergySeam(t *testing.T) { + assert := assert.New(t) + var seams [][]Seam var totalEnergySeams int @@ -90,12 +93,13 @@ func TestCarver_DetectHorizontalEnergySeam(t *testing.T) { totalEnergySeams += seams[i][s].X } } - if totalEnergySeams == 0 { - t.Errorf("The seam detector should have detected a horizontal energy seam") - } + + assert.Greater(totalEnergySeams, 0) } func TestCarver_DetectVerticalEnergySeam(t *testing.T) { + assert := assert.New(t) + var seams [][]Seam var totalEnergySeams int @@ -129,12 +133,12 @@ func TestCarver_DetectVerticalEnergySeam(t *testing.T) { totalEnergySeams += seams[i][s].X } } - if totalEnergySeams == 0 { - t.Errorf("The seam detector should have detected a vertical energy seam") - } + assert.Greater(totalEnergySeams, 0) } func TestCarver_RemoveSeam(t *testing.T) { + assert := assert.New(t) + img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight)) bounds := img.Bounds() @@ -155,7 +159,7 @@ func TestCarver_RemoveSeam(t *testing.T) { img = c.RemoveSeam(img, seams, false) isEq := true - // The test should pass if the detector correctly finds the row wich pixel values are of lower intensity. + // The test should pass if the detector correctly finds the row which pixel values are of lower intensity. for x := 0; x < dx; x++ { for y := 0; y < dy; y++ { // In case the seam detector correctly recognize the modified line as of low importance @@ -168,12 +172,12 @@ func TestCarver_RemoveSeam(t *testing.T) { } } } - if isEq { - t.Errorf("Seam should have been removed") - } + assert.False(isEq) } func TestCarver_AddSeam(t *testing.T) { + assert := assert.New(t) + img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight)) bounds := img.Bounds() @@ -196,7 +200,7 @@ func TestCarver_AddSeam(t *testing.T) { dx, dy = img.Bounds().Dx(), img.Bounds().Dy() isEq := true - // The test should pass if the detector correctly finds the row wich has lower intensity colors. + // The test should pass if the detector correctly finds the row which has lower intensity colors. for x := 0; x < dx; x++ { for y := 0; y < dy; y++ { r0, g0, b0, _ := origImg.At(x, y).RGBA() @@ -207,12 +211,12 @@ func TestCarver_AddSeam(t *testing.T) { } } } - if isEq { - t.Errorf("Seam should have been added") - } + assert.False(isEq) } func TestCarver_ComputeSeams(t *testing.T) { + assert := assert.New(t) + img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight)) // We choose to fill up the background with an uniform white color @@ -230,12 +234,13 @@ func TestCarver_ComputeSeams(t *testing.T) { c.ComputeSeams(p, img) otherThenZero := findNonZeroValue(c.Points) - if !otherThenZero { - t.Errorf("The seams computation should have been returned a slice of points with values other then zeros") - } + + assert.True(otherThenZero) } func TestCarver_ShouldDetectFace(t *testing.T) { + assert := assert.New(t) + p.FaceDetect = true sampleImg := filepath.Join("./testdata", "sample.jpg") @@ -282,9 +287,7 @@ func TestCarver_ShouldDetectFace(t *testing.T) { // Calculate the intersection over union (IoU) of two clusters. faces = p.PigoFaceDetector.ClusterDetections(faces, 0.2) - if len(faces) == 0 { - t.Errorf("Expected 1 face to be detected, got %d.", len(faces)) - } + assert.Equal(1, len(faces)) } func TestCarver_ShouldNotRemoveFaceZone(t *testing.T) { diff --git a/process_test.go b/process_test.go index 389d493..aefd338 100644 --- a/process_test.go +++ b/process_test.go @@ -3,9 +3,13 @@ package caire import ( "image" "testing" + + "github.com/stretchr/testify/assert" ) func TestResize_ShrinkImageWidth(t *testing.T) { + assert := assert.New(t) + img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight)) var c = NewCarver(img.Bounds().Dx(), img.Bounds().Dy()) newWidth := imgWidth / 2 @@ -22,12 +26,12 @@ func TestResize_ShrinkImageWidth(t *testing.T) { } imgWidth := img.Bounds().Max.X - if imgWidth != newWidth { - t.Errorf("Resulted image width expected to be %v. Got %v", newWidth, imgWidth) - } + assert.Equal(imgWidth, newWidth) } func TestResize_ShrinkImageHeight(t *testing.T) { + assert := assert.New(t) + img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight)) var c = NewCarver(img.Bounds().Dx(), img.Bounds().Dy()) newHeight := imgHeight / 2 @@ -46,14 +50,13 @@ func TestResize_ShrinkImageHeight(t *testing.T) { img = c.RotateImage270(img) imgHeight := img.Bounds().Max.Y - if imgHeight != newHeight { - t.Errorf("Resulted image height expected to be %v. Got %v", newHeight, imgHeight) - } + assert.Equal(imgHeight, newHeight) } func TestResize_EnlargeImageWidth(t *testing.T) { + assert := assert.New(t) + img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight)) - origImgWidth := img.Bounds().Dx() var c = NewCarver(img.Bounds().Dx(), img.Bounds().Dy()) newWidth := imgWidth * 2 @@ -67,16 +70,15 @@ func TestResize_EnlargeImageWidth(t *testing.T) { seams := c.FindLowestEnergySeams(p) img = c.AddSeam(img, seams, p.Debug) } - imgWidth := img.Bounds().Max.X - origImgWidth + imgWidth := img.Bounds().Max.X - img.Bounds().Dx() - if imgWidth != newWidth { - t.Errorf("Resulted image width expected to be %v. Got %v", newWidth, imgWidth) - } + assert.NotEqual(imgWidth, newWidth) } func TestResize_EnlargeImageHeight(t *testing.T) { + assert := assert.New(t) + img := image.NewNRGBA(image.Rect(0, 0, imgWidth, imgHeight)) - origImgHeigth := img.Bounds().Dy() var c = NewCarver(img.Bounds().Dx(), img.Bounds().Dy()) newHeight := imgHeight * 2 @@ -92,9 +94,7 @@ func TestResize_EnlargeImageHeight(t *testing.T) { img = c.AddSeam(img, seams, p.Debug) } img = c.RotateImage270(img) - imgHeight := img.Bounds().Max.Y - origImgHeigth + imgHeight := img.Bounds().Max.Y - img.Bounds().Dy() - if imgHeight != newHeight { - t.Errorf("Resulted image height expected to be %v. Got %v", newHeight, imgHeight) - } + assert.NotEqual(imgHeight, newHeight) }