github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/interfaces/utils/utils_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package utils_test 21 22 import ( 23 "encoding/json" 24 "testing" 25 26 . "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/interfaces/utils" 29 ) 30 31 func Test(t *testing.T) { 32 TestingT(t) 33 } 34 35 type utilsSuite struct{} 36 37 var _ = Suite(&utilsSuite{}) 38 39 func (s *utilsSuite) TestNormalizeInterfaceAttributes(c *C) { 40 normalize := utils.NormalizeInterfaceAttributes 41 c.Assert(normalize(false), Equals, false) 42 c.Assert(normalize(nil), Equals, nil) 43 c.Assert(normalize(42), Equals, int64(42)) 44 c.Assert(normalize(3.14), Equals, float64(3.14)) 45 // Funny that, I noticed it only because of missing test coverage. 46 c.Assert(normalize(float32(3.14)), Equals, float64(3.140000104904175)) 47 c.Assert(normalize("banana"), Equals, "banana") 48 c.Assert(normalize([]interface{}{42, 3.14, "banana", json.Number("21"), json.Number("0.5")}), DeepEquals, 49 []interface{}{int64(42), float64(3.14), "banana", int64(21), float64(0.5)}) 50 c.Assert(normalize(map[string]interface{}{"i": 42, "f": 3.14, "s": "banana"}), 51 DeepEquals, map[string]interface{}{"i": int64(42), "f": float64(3.14), "s": "banana"}) 52 c.Assert(normalize(json.Number("1")), Equals, int64(1)) 53 c.Assert(normalize(json.Number("2.5")), Equals, float64(2.5)) 54 55 // Normalize doesn't mutate slices it is given 56 sliceIn := []interface{}{42} 57 sliceOut := normalize(sliceIn) 58 c.Assert(sliceIn, DeepEquals, []interface{}{42}) 59 c.Assert(sliceOut, DeepEquals, []interface{}{int64(42)}) 60 61 // Normalize doesn't mutate maps it is given 62 mapIn := map[string]interface{}{"i": 42} 63 mapOut := normalize(mapIn) 64 c.Assert(mapIn, DeepEquals, map[string]interface{}{"i": 42}) 65 c.Assert(mapOut, DeepEquals, map[string]interface{}{"i": int64(42)}) 66 } 67 68 func (s *utilsSuite) TestCopyAttributes(c *C) { 69 cpattr := utils.CopyAttributes 70 71 attrsIn := map[string]interface{}{"i": 42} 72 attrsOut := cpattr(attrsIn) 73 attrsIn["i"] = "changed" 74 c.Assert(attrsIn, DeepEquals, map[string]interface{}{"i": "changed"}) 75 c.Assert(attrsOut, DeepEquals, map[string]interface{}{"i": 42}) 76 77 attrsIn = map[string]interface{}{"ao": []interface{}{1, 2, 3}} 78 attrsOut = cpattr(attrsIn) 79 attrsIn["ao"].([]interface{})[1] = "changed" 80 c.Assert(attrsIn, DeepEquals, map[string]interface{}{"ao": []interface{}{1, "changed", 3}}) 81 c.Assert(attrsOut, DeepEquals, map[string]interface{}{"ao": []interface{}{1, 2, 3}}) 82 }