config_test.go (4621B)
1 // Copyright 2019 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 "fmt" 18 "strings" 19 "testing" 20 21 qt "github.com/frankban/quicktest" 22 ) 23 24 func TestDecodeConfig(t *testing.T) { 25 c := qt.New(t) 26 m := map[string]any{ 27 "quality": 42, 28 "resampleFilter": "NearestNeighbor", 29 "anchor": "topLeft", 30 } 31 32 imagingConfig, err := DecodeConfig(m) 33 34 c.Assert(err, qt.IsNil) 35 imaging := imagingConfig.Cfg 36 c.Assert(imaging.Quality, qt.Equals, 42) 37 c.Assert(imaging.ResampleFilter, qt.Equals, "nearestneighbor") 38 c.Assert(imaging.Anchor, qt.Equals, "topleft") 39 40 m = map[string]any{} 41 42 imagingConfig, err = DecodeConfig(m) 43 c.Assert(err, qt.IsNil) 44 imaging = imagingConfig.Cfg 45 c.Assert(imaging.ResampleFilter, qt.Equals, "box") 46 c.Assert(imaging.Anchor, qt.Equals, "smart") 47 48 _, err = DecodeConfig(map[string]any{ 49 "quality": 123, 50 }) 51 c.Assert(err, qt.Not(qt.IsNil)) 52 53 _, err = DecodeConfig(map[string]any{ 54 "resampleFilter": "asdf", 55 }) 56 c.Assert(err, qt.Not(qt.IsNil)) 57 58 _, err = DecodeConfig(map[string]any{ 59 "anchor": "asdf", 60 }) 61 c.Assert(err, qt.Not(qt.IsNil)) 62 63 imagingConfig, err = DecodeConfig(map[string]any{ 64 "anchor": "Smart", 65 }) 66 imaging = imagingConfig.Cfg 67 c.Assert(err, qt.IsNil) 68 c.Assert(imaging.Anchor, qt.Equals, "smart") 69 70 imagingConfig, err = DecodeConfig(map[string]any{ 71 "exif": map[string]any{ 72 "disableLatLong": true, 73 }, 74 }) 75 c.Assert(err, qt.IsNil) 76 imaging = imagingConfig.Cfg 77 c.Assert(imaging.Exif.DisableLatLong, qt.Equals, true) 78 c.Assert(imaging.Exif.ExcludeFields, qt.Equals, "GPS|Exif|Exposure[M|P|B]|Contrast|Resolution|Sharp|JPEG|Metering|Sensing|Saturation|ColorSpace|Flash|WhiteBalance") 79 } 80 81 func TestDecodeImageConfig(t *testing.T) { 82 for i, this := range []struct { 83 action string 84 in string 85 expect any 86 }{ 87 {"resize", "300x400", newImageConfig("resize", 300, 400, 75, 0, "box", "smart", "")}, 88 {"resize", "300x400 #fff", newImageConfig("resize", 300, 400, 75, 0, "box", "smart", "fff")}, 89 {"resize", "100x200 bottomRight", newImageConfig("resize", 100, 200, 75, 0, "box", "BottomRight", "")}, 90 {"resize", "10x20 topleft Lanczos", newImageConfig("resize", 10, 20, 75, 0, "Lanczos", "topleft", "")}, 91 {"resize", "linear left 10x r180", newImageConfig("resize", 10, 0, 75, 180, "linear", "left", "")}, 92 {"resize", "x20 riGht Cosine q95", newImageConfig("resize", 0, 20, 95, 0, "cosine", "right", "")}, 93 {"crop", "300x400", newImageConfig("crop", 300, 400, 75, 0, "box", "smart", "")}, 94 {"fill", "300x400", newImageConfig("fill", 300, 400, 75, 0, "box", "smart", "")}, 95 {"fit", "300x400", newImageConfig("fit", 300, 400, 75, 0, "box", "smart", "")}, 96 97 {"resize", "", false}, 98 {"resize", "foo", false}, 99 {"crop", "100x", false}, 100 {"fill", "100x", false}, 101 {"fit", "100x", false}, 102 {"foo", "100x", false}, 103 } { 104 105 cfg, err := DecodeConfig(nil) 106 if err != nil { 107 t.Fatal(err) 108 } 109 result, err := DecodeImageConfig(this.action, this.in, cfg, PNG) 110 if b, ok := this.expect.(bool); ok && !b { 111 if err == nil { 112 t.Errorf("[%d] parseImageConfig didn't return an expected error", i) 113 } 114 } else { 115 if err != nil { 116 t.Fatalf("[%d] err: %s", i, err) 117 } 118 if fmt.Sprint(result) != fmt.Sprint(this.expect) { 119 t.Fatalf("[%d] got\n%v\n but expected\n%v", i, result, this.expect) 120 } 121 } 122 } 123 } 124 125 func newImageConfig(action string, width, height, quality, rotate int, filter, anchor, bgColor string) ImageConfig { 126 var c ImageConfig = GetDefaultImageConfig(action, ImagingConfig{}) 127 c.TargetFormat = PNG 128 c.Hint = 2 129 c.Width = width 130 c.Height = height 131 c.Quality = quality 132 c.qualitySetForImage = quality != 75 133 c.Rotate = rotate 134 c.BgColorStr = bgColor 135 c.BgColor, _ = hexStringToColor(bgColor) 136 137 if filter != "" { 138 filter = strings.ToLower(filter) 139 if v, ok := imageFilters[filter]; ok { 140 c.Filter = v 141 c.FilterStr = filter 142 } 143 } 144 145 if anchor != "" { 146 if anchor == smartCropIdentifier { 147 c.AnchorStr = anchor 148 } else { 149 anchor = strings.ToLower(anchor) 150 if v, ok := anchorPositions[anchor]; ok { 151 c.Anchor = v 152 c.AnchorStr = anchor 153 } 154 } 155 } 156 157 return c 158 }