github.com/greenpau/go-authcrunch@v1.1.4/internal/tests/data_test.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tests 16 17 import ( 18 "fmt" 19 20 "testing" 21 ) 22 23 func TestUnpackDict(t *testing.T) { 24 25 testcases := []struct { 26 name string 27 input interface{} 28 want map[string]interface{} 29 shouldErr bool 30 err error 31 disabled bool 32 }{ 33 { 34 name: "test unpack json string", 35 disabled: false, 36 input: `{ 37 "foo": { 38 "bar": "baz" 39 } 40 }`, 41 want: map[string]interface{}{ 42 "foo": map[string]interface{}{ 43 "bar": "baz", 44 }, 45 }, 46 }, 47 { 48 name: "test malformed json string", 49 disabled: false, 50 input: `{ 51 { 52 }`, 53 shouldErr: true, 54 err: fmt.Errorf("invalid character '{' looking for beginning of object key string"), 55 }, 56 { 57 name: "test unpack map", 58 disabled: false, 59 input: map[string]interface{}{ 60 "foo": map[string]interface{}{ 61 "bar": "baz", 62 }, 63 }, 64 want: map[string]interface{}{ 65 "foo": map[string]interface{}{ 66 "bar": "baz", 67 }, 68 }, 69 }, 70 { 71 name: "test unpack non map", 72 disabled: false, 73 input: 123, 74 shouldErr: true, 75 err: fmt.Errorf("json: cannot unmarshal number into Go value of type map[string]interface {}"), 76 }, 77 } 78 for _, tc := range testcases { 79 t.Run(tc.name, func(t *testing.T) { 80 if tc.disabled { 81 return 82 } 83 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 84 msgs = append(msgs, fmt.Sprintf("input:\n%v", tc.input)) 85 got, err := UnpackDict(tc.input) 86 if EvalErrWithLog(t, err, "UnpackDict", tc.shouldErr, tc.err, msgs) { 87 return 88 } 89 EvalObjectsWithLog(t, "UnpackDict", tc.want, got, msgs) 90 }) 91 } 92 }