refactor tests for trimEdges function to improve readability and consistency

This commit is contained in:
Vetle Leinonen-Roeim 2025-03-30 18:24:07 +02:00
parent 209bcc54a3
commit df9676c2b6

View file

@ -377,21 +377,24 @@ func TestTransformImage(t *testing.T) {
} }
func TestTrimBordersOfSameColor(t *testing.T) { func TestTrimBordersOfSameColor(t *testing.T) {
w := color.NRGBA{255, 255, 255, 255}
r := color.NRGBA{255, 0, 0, 255}
src := newImage(4, 4, src := newImage(4, 4,
color.NRGBA{255, 255, 255, 255}, color.NRGBA{255, 255, 255, 255}, color.NRGBA{255, 255, 255, 255}, color.NRGBA{255, 255, 255, 255}, w, w, w, w,
color.NRGBA{255, 255, 255, 255}, color.NRGBA{255, 0, 0, 255}, color.NRGBA{255, 0, 0, 255}, color.NRGBA{255, 255, 255, 255}, w, r, r, w,
color.NRGBA{255, 255, 255, 255}, color.NRGBA{255, 0, 0, 255}, color.NRGBA{255, 0, 0, 255}, color.NRGBA{255, 255, 255, 255}, w, r, r, w,
color.NRGBA{255, 255, 255, 255}, color.NRGBA{255, 255, 255, 255}, color.NRGBA{255, 255, 255, 255}, color.NRGBA{255, 255, 255, 255}, w, w, w, w,
) )
want := newImage(2, 2, want := newImage(2, 2,
color.NRGBA{255, 0, 0, 255}, color.NRGBA{255, 0, 0, 255}, r, r,
color.NRGBA{255, 0, 0, 255}, color.NRGBA{255, 0, 0, 255}, r, r,
) )
got := trimEdges(src) got := trimEdges(src)
if !reflect.DeepEqual(got, want) { // Compare pixel data
t.Errorf("trimEdges() = %v, want %v", got, want) if !compareImages(got, want) {
t.Errorf("trimEdges() pixel data does not match expected result")
} }
} }
@ -405,39 +408,39 @@ func TestTrimEdgesSingleColorImage(t *testing.T) {
// Apply the trimEdges function // Apply the trimEdges function
got := trimEdges(src) got := trimEdges(src)
// Check if the result matches the expected image // Compare pixel data
if !reflect.DeepEqual(got, want) { if !compareImages(got, want) {
t.Errorf("trimEdges() = %v, want %v", got.Bounds(), want.Bounds()) t.Errorf("trimEdges() pixel data does not match expected result")
} }
} }
func TestTrimEdgesCircle(t *testing.T) { func TestTrimEdgesCircle(t *testing.T) {
// Define colors for better readability // Define colors for better readability
white := color.NRGBA{255, 255, 255, 255} w := color.NRGBA{255, 255, 255, 255}
red := color.NRGBA{255, 0, 0, 255} r := color.NRGBA{255, 0, 0, 255}
// Create a 9x9 image with a white background and a larger red circle in the center // Create a 9x9 image with a white background and a larger red circle in the center
src := newImage(9, 9, src := newImage(9, 9,
white, white, white, white, white, white, white, white, white, w, w, w, w, w, w, w, w, w,
white, white, white, red, red, red, white, white, white, w, w, w, r, r, r, w, w, w,
white, white, red, red, red, red, red, white, white, w, w, r, r, r, r, r, w, w,
white, red, red, red, red, red, red, red, white, w, r, r, r, r, r, r, r, w,
white, red, red, red, red, red, red, red, white, w, r, r, r, r, r, r, r, w,
white, red, red, red, red, red, red, red, white, w, r, r, r, r, r, r, r, w,
white, white, red, red, red, red, red, white, white, w, w, r, r, r, r, r, w, w,
white, white, white, red, red, red, white, white, white, w, w, w, r, r, r, w, w, w,
white, white, white, white, white, white, white, white, white, w, w, w, w, w, w, w, w, w,
) )
// Expected result: a trimmed 7x7 image containing only the circle // Expected result: a trimmed 7x7 image containing only the circle
want := newImage(7, 7, want := newImage(7, 7,
white, white, red, red, red, white, white, w, w, r, r, r, w, w,
white, red, red, red, red, red, white, w, r, r, r, r, r, w,
red, red, red, red, red, red, red, r, r, r, r, r, r, r,
red, red, red, red, red, red, red, r, r, r, r, r, r, r,
red, red, red, red, red, red, red, r, r, r, r, r, r, r,
white, red, red, red, red, red, white, w, r, r, r, r, r, w,
white, white, red, red, red, white, white, w, w, r, r, r, w, w,
) )
// Apply the trimEdges function // Apply the trimEdges function
@ -451,31 +454,31 @@ func TestTrimEdgesCircle(t *testing.T) {
func TestTrimEdgesUnevenVerticalRectangle(t *testing.T) { func TestTrimEdgesUnevenVerticalRectangle(t *testing.T) {
// Define colors for better readability // Define colors for better readability
white := color.NRGBA{255, 255, 255, 255} w := color.NRGBA{255, 255, 255, 255}
red := color.NRGBA{255, 0, 0, 255} r := color.NRGBA{255, 0, 0, 255}
// Create a 9x5 image with a white background and a red diagonal shape // Create a 9x5 image with a white background and a red diagonal shape
src := newImage(5, 9, src := newImage(5, 9,
white, white, white, white, white, w, w, w, w, w,
white, white, white, red, white, w, w, w, r, w,
white, white, red, white, white, w, w, r, w, w,
white, red, white, white, white, w, r, w, w, w,
white, red, white, white, white, w, r, w, w, w,
white, red, white, white, white, w, r, w, w, w,
white, white, red, white, white, w, w, r, w, w,
white, white, white, red, white, w, w, w, r, w,
white, white, white, white, white, w, w, w, w, w,
) )
// Expected result: a trimmed 5x5 image containing only the diagonal shape // Expected result: a trimmed 5x5 image containing only the diagonal shape
want := newImage(3, 7, want := newImage(3, 7,
white, white, red, w, w, r,
white, red, white, w, r, w,
red, white, white, r, w, w,
red, white, white, r, w, w,
red, white, white, r, w, w,
white, red, white, w, r, w,
white, white, red, w, w, r,
) )
// Apply the trimEdges function // Apply the trimEdges function