github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/strings_test.go (about) 1 // Copyright 2022 Harness, Inc. 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 yaml 16 17 import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "gopkg.in/yaml.v3" 22 ) 23 24 func TestStrings(t *testing.T) { 25 tests := []struct { 26 yaml string 27 want Stringorslice 28 }{ 29 { 30 yaml: `"hello"`, 31 want: Stringorslice{"hello"}, 32 }, 33 { 34 yaml: `[ "hello", "world" ]`, 35 want: Stringorslice{"hello", "world"}, 36 }, 37 { 38 yaml: `[ "hello", 1, true, 100.1 ]`, 39 want: Stringorslice{"hello", "1", "true", "100.1"}, 40 }, 41 { 42 yaml: `[ ]`, 43 want: nil, 44 }, 45 } 46 47 for i, test := range tests { 48 got := new(Stringorslice) 49 if err := yaml.Unmarshal([]byte(test.yaml), got); err != nil { 50 t.Error(err) 51 return 52 } 53 if diff := cmp.Diff(got, &test.want); diff != "" { 54 t.Errorf("Unexpected parsing results for test %v", i) 55 t.Log(diff) 56 } 57 } 58 } 59 60 func TestStrings_Error(t *testing.T) { 61 if err := yaml.Unmarshal([]byte("{}"), new(Stringorslice)); err == nil { 62 t.Errorf("Expect error when unmarshaling a into a string slice") 63 } 64 65 if err := yaml.Unmarshal([]byte("[{}]"), new(Stringorslice)); err == nil { 66 t.Errorf("Expect error when unmarshaling a struct slice into a string slice") 67 } 68 }