| 1 |
// Copyright 2026 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package images |
| 15 |
|
| 16 |
import ( |
| 17 |
"image" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
) |
| 22 |
|
| 23 |
func TestExpandCropRectToMinSize(t *testing.T) { |
| 24 |
c := qt.New(t) |
| 25 |
|
| 26 |
for _, test := range []struct { |
| 27 |
name string |
| 28 |
r image.Rectangle |
| 29 |
bounds image.Rectangle |
| 30 |
width int |
| 31 |
height int |
| 32 |
expectedRect image.Rectangle |
| 33 |
}{ |
| 34 |
{ |
| 35 |
name: "top left", |
| 36 |
r: image.Rect(0, 0, 899, 560), |
| 37 |
bounds: image.Rect(0, 0, 900, 562), |
| 38 |
width: 900, |
| 39 |
height: 561, |
| 40 |
expectedRect: image.Rect(0, 0, 900, 561), |
| 41 |
}, |
| 42 |
{ |
| 43 |
name: "bottom right", |
| 44 |
r: image.Rect(1, 2, 900, 562), |
| 45 |
bounds: image.Rect(0, 0, 900, 562), |
| 46 |
width: 900, |
| 47 |
height: 561, |
| 48 |
expectedRect: image.Rect(0, 1, 900, 562), |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "centered", |
| 52 |
r: image.Rect(10, 10, 109, 109), |
| 53 |
bounds: image.Rect(0, 0, 200, 200), |
| 54 |
width: 101, |
| 55 |
height: 101, |
| 56 |
expectedRect: image.Rect(9, 9, 110, 110), |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "source too small", |
| 60 |
r: image.Rect(0, 0, 899, 560), |
| 61 |
bounds: image.Rect(0, 0, 899, 560), |
| 62 |
width: 900, |
| 63 |
height: 561, |
| 64 |
expectedRect: image.Rect(0, 0, 899, 560), |
| 65 |
}, |
| 66 |
} { |
| 67 |
c.Run(test.name, func(c *qt.C) { |
| 68 |
got := expandCropRectToMinSize(test.r, test.bounds, test.width, test.height) |
| 69 |
c.Assert(got, qt.Equals, test.expectedRect) |
| 70 |
}) |
| 71 |
} |
| 72 |
} |