github.com/greenpau/go-authcrunch@v1.1.4/pkg/util/cfg/encoder_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 cfg 16 17 import ( 18 "fmt" 19 "github.com/greenpau/go-authcrunch/internal/tests" 20 "testing" 21 ) 22 23 func TestEncodeArgs(t *testing.T) { 24 var testcases = []struct { 25 name string 26 args []string 27 want map[string]interface{} 28 shouldErr bool 29 err error 30 }{ 31 { 32 name: "test encoder function", 33 args: []string{ 34 "authp admin", 35 "authp viewer", 36 "authp editor", 37 }, 38 want: map[string]interface{}{ 39 "args": `"authp admin" "authp viewer" "authp editor"`, 40 }, 41 }, 42 } 43 for _, tc := range testcases { 44 t.Run(tc.name, func(t *testing.T) { 45 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 46 encodedArgs := EncodeArgs(tc.args) 47 got := make(map[string]interface{}) 48 got["args"] = encodedArgs 49 tests.EvalObjectsWithLog(t, "encoder", tc.want, got, msgs) 50 }) 51 } 52 } 53 54 func TestDecodeArgs(t *testing.T) { 55 var testcases = []struct { 56 name string 57 args string 58 want map[string]interface{} 59 shouldErr bool 60 err error 61 }{ 62 { 63 name: "test decoder function", 64 args: "foo,bar foo", 65 want: map[string]interface{}{ 66 "args": []string{ 67 "foo,bar", 68 "foo", 69 }, 70 }, 71 }, 72 } 73 for _, tc := range testcases { 74 t.Run(tc.name, func(t *testing.T) { 75 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 76 decodedArgs, err := DecodeArgs(tc.args) 77 if tests.EvalErrWithLog(t, err, "decoder", tc.shouldErr, tc.err, msgs) { 78 return 79 } 80 got := make(map[string]interface{}) 81 got["args"] = decodedArgs 82 tests.EvalObjectsWithLog(t, "decoder", tc.want, got, msgs) 83 }) 84 } 85 }