github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/resources/images/config_test.go (about) 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]interface{}{ 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]interface{}{} 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]interface{}{ 49 "quality": 123, 50 }) 51 c.Assert(err, qt.Not(qt.IsNil)) 52 53 _, err = DecodeConfig(map[string]interface{}{ 54 "resampleFilter": "asdf", 55 }) 56 c.Assert(err, qt.Not(qt.IsNil)) 57 58 _, err = DecodeConfig(map[string]interface{}{ 59 "anchor": "asdf", 60 }) 61 c.Assert(err, qt.Not(qt.IsNil)) 62 63 imagingConfig, err = DecodeConfig(map[string]interface{}{ 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]interface{}{ 71 "exif": map[string]interface{}{ 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 in string 84 expect interface{} 85 }{ 86 {"300x400", newImageConfig(300, 400, 75, 0, "box", "smart", "")}, 87 {"300x400 #fff", newImageConfig(300, 400, 75, 0, "box", "smart", "fff")}, 88 {"100x200 bottomRight", newImageConfig(100, 200, 75, 0, "box", "BottomRight", "")}, 89 {"10x20 topleft Lanczos", newImageConfig(10, 20, 75, 0, "Lanczos", "topleft", "")}, 90 {"linear left 10x r180", newImageConfig(10, 0, 75, 180, "linear", "left", "")}, 91 {"x20 riGht Cosine q95", newImageConfig(0, 20, 95, 0, "cosine", "right", "")}, 92 93 {"", false}, 94 {"foo", false}, 95 } { 96 97 cfg, err := DecodeConfig(nil) 98 if err != nil { 99 t.Fatal(err) 100 } 101 result, err := DecodeImageConfig("resize", this.in, cfg, PNG) 102 if b, ok := this.expect.(bool); ok && !b { 103 if err == nil { 104 t.Errorf("[%d] parseImageConfig didn't return an expected error", i) 105 } 106 } else { 107 if err != nil { 108 t.Fatalf("[%d] err: %s", i, err) 109 } 110 if fmt.Sprint(result) != fmt.Sprint(this.expect) { 111 t.Fatalf("[%d] got\n%v\n but expected\n%v", i, result, this.expect) 112 } 113 } 114 } 115 } 116 117 func newImageConfig(width, height, quality, rotate int, filter, anchor, bgColor string) ImageConfig { 118 var c ImageConfig = GetDefaultImageConfig("resize", ImagingConfig{}) 119 c.TargetFormat = PNG 120 c.Hint = 2 121 c.Width = width 122 c.Height = height 123 c.Quality = quality 124 c.qualitySetForImage = quality != 75 125 c.Rotate = rotate 126 c.BgColorStr = bgColor 127 c.BgColor, _ = hexStringToColor(bgColor) 128 129 if filter != "" { 130 filter = strings.ToLower(filter) 131 if v, ok := imageFilters[filter]; ok { 132 c.Filter = v 133 c.FilterStr = filter 134 } 135 } 136 137 if anchor != "" { 138 if anchor == smartCropIdentifier { 139 c.AnchorStr = anchor 140 } else { 141 anchor = strings.ToLower(anchor) 142 if v, ok := anchorPositions[anchor]; ok { 143 c.Anchor = v 144 c.AnchorStr = anchor 145 } 146 } 147 } 148 149 return c 150 }