49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
package storage
|
|
|
|
import "testing"
|
|
|
|
func TestFitsWithin(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
w, h, maxSide int
|
|
want bool
|
|
}{
|
|
{"未设置上限时恒为真", 8000, 8000, 0, true},
|
|
{"负上限等同于不限制", 8000, 8000, -1, true},
|
|
{"宽高均在限内", 512, 512, 512, true},
|
|
{"刚好等于上限", 1600, 1200, 1600, true},
|
|
{"仅超宽", 1921, 1080, 1920, false},
|
|
{"仅超高(竖图长边此前不受约束)", 1080, 10000, 1920, false},
|
|
{"双向超限", 4000, 5000, 1600, false},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
if got := fitsWithin(c.w, c.h, c.maxSide); got != c.want {
|
|
t.Fatalf("fitsWithin(%d, %d, %d) = %v, want %v", c.w, c.h, c.maxSide, got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// 各用途档位必须与其展示场景相称:头像按前端 cropper 输出上限兜底,
|
|
// 背景保留铺满宽度所需尺寸,通用图取折中值。
|
|
func TestEncodeSpecTiers(t *testing.T) {
|
|
if avatarEncodeSpec.MaxSide != 512 {
|
|
t.Errorf("avatar MaxSide = %d, want 512", avatarEncodeSpec.MaxSide)
|
|
}
|
|
if backgroundEncodeSpec.MaxSide != 1920 {
|
|
t.Errorf("background MaxSide = %d, want 1920", backgroundEncodeSpec.MaxSide)
|
|
}
|
|
if GeneralEncodeSpec.MaxSide != 1600 {
|
|
t.Errorf("general MaxSide = %d, want 1600", GeneralEncodeSpec.MaxSide)
|
|
}
|
|
for name, s := range map[string]EncodeSpec{
|
|
"avatar": avatarEncodeSpec, "background": backgroundEncodeSpec, "general": GeneralEncodeSpec,
|
|
} {
|
|
if s.Quality <= 0 || s.Quality > 100 {
|
|
t.Errorf("%s Quality = %v, out of range (0,100]", name, s.Quality)
|
|
}
|
|
}
|
|
}
|