github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/metautil/normalize_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2016 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 metautil_test 21 22 import ( 23 "testing" 24 25 . "gopkg.in/check.v1" 26 27 "github.com/snapcore/snapd/metautil" 28 ) 29 30 type normalizeTestSuite struct { 31 } 32 33 var _ = Suite(&normalizeTestSuite{}) 34 35 func TestMain(t *testing.T) { TestingT(t) } 36 37 func (s *normalizeTestSuite) TestNormalize(c *C) { 38 for _, tc := range []struct { 39 v interface{} 40 exp interface{} 41 err string 42 }{ 43 {v: "foo", exp: "foo"}, 44 {v: 1, exp: int64(1)}, 45 {v: int64(1), exp: int64(1)}, 46 {v: true, exp: true}, 47 {v: 0.5, exp: float64(0.5)}, 48 {v: float32(0.5), exp: float64(0.5)}, 49 {v: float64(0.5), exp: float64(0.5)}, 50 {v: []interface{}{1, 0.5, "foo"}, exp: []interface{}{int64(1), float64(0.5), "foo"}}, 51 {v: map[string]interface{}{"foo": 1}, exp: map[string]interface{}{"foo": int64(1)}}, 52 {v: map[interface{}]interface{}{"foo": 1}, exp: map[string]interface{}{"foo": int64(1)}}, 53 { 54 v: map[interface{}]interface{}{"foo": map[interface{}]interface{}{"bar": 0.5}}, 55 exp: map[string]interface{}{"foo": map[string]interface{}{"bar": float64(0.5)}}, 56 }, 57 {v: uint(1), err: "invalid scalar: 1"}, 58 {v: map[interface{}]interface{}{2: 1}, err: "non-string key: 2"}, 59 {v: []interface{}{uint(1)}, err: "invalid scalar: 1"}, 60 {v: map[string]interface{}{"foo": uint(1)}, err: "invalid scalar: 1"}, 61 {v: map[interface{}]interface{}{"foo": uint(1)}, err: "invalid scalar: 1"}, 62 } { 63 res, err := metautil.NormalizeValue(tc.v) 64 if tc.err == "" { 65 c.Assert(err, IsNil) 66 c.Assert(res, DeepEquals, tc.exp) 67 } else { 68 c.Assert(err, ErrorMatches, tc.err) 69 } 70 } 71 }