...

Source file src/sigs.k8s.io/yaml/err_test.go

Documentation: sigs.k8s.io/yaml

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package yaml
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  )
    23  
    24  func TestErrors(t *testing.T) {
    25  	type Into struct {
    26  		Map map[string]interface{} `json:"map"`
    27  		Int int32                  `json:"int"`
    28  	}
    29  
    30  	testcases := []struct {
    31  		Name                   string
    32  		Data                   string
    33  		UnmarshalPrefix        string
    34  		UnmarshalStrictPrefix  string
    35  		YAMLToJSONPrefix       string
    36  		YAMLToJSONStrictPrefix string
    37  	}{
    38  		{
    39  			Name:                   "unmarshal syntax",
    40  			Data:                   `map: {`,
    41  			UnmarshalPrefix:        `error converting YAML to JSON: yaml: line 1: `,
    42  			UnmarshalStrictPrefix:  `error converting YAML to JSON: yaml: line 1: `,
    43  			YAMLToJSONPrefix:       `yaml: line 1: `,
    44  			YAMLToJSONStrictPrefix: `yaml: line 1: `,
    45  		},
    46  		{
    47  			Name:                   "unmarshal type",
    48  			Data:                   `map: ""`,
    49  			UnmarshalPrefix:        `error unmarshaling JSON: while decoding JSON: json: `,
    50  			UnmarshalStrictPrefix:  `error unmarshaling JSON: while decoding JSON: json: `,
    51  			YAMLToJSONPrefix:       ``,
    52  			YAMLToJSONStrictPrefix: ``,
    53  		},
    54  		{
    55  			Name:                   "unmarshal unknown",
    56  			Data:                   `unknown: {}`,
    57  			UnmarshalPrefix:        ``,
    58  			UnmarshalStrictPrefix:  `error unmarshaling JSON: while decoding JSON: json: `,
    59  			YAMLToJSONPrefix:       ``,
    60  			YAMLToJSONStrictPrefix: ``,
    61  		},
    62  		{
    63  			Name: "unmarshal duplicate",
    64  			Data: `
    65  int: 0
    66  int: 0`,
    67  			UnmarshalPrefix:        ``,
    68  			UnmarshalStrictPrefix:  `error converting YAML to JSON: yaml: `,
    69  			YAMLToJSONPrefix:       ``,
    70  			YAMLToJSONStrictPrefix: `yaml: unmarshal errors:`,
    71  		},
    72  	}
    73  
    74  	for _, tc := range testcases {
    75  		t.Run(tc.Name, func(t *testing.T) {
    76  			v := Into{}
    77  			if err := Unmarshal([]byte(tc.Data), &v); err == nil {
    78  				if len(tc.UnmarshalPrefix) > 0 {
    79  					t.Fatal("expected err")
    80  				}
    81  			} else {
    82  				if len(tc.UnmarshalPrefix) == 0 {
    83  					t.Fatalf("unexpected err %v", err)
    84  				}
    85  				if !strings.HasPrefix(err.Error(), tc.UnmarshalPrefix) {
    86  					t.Fatalf("expected '%s' to start with '%s'", err.Error(), tc.UnmarshalPrefix)
    87  				}
    88  			}
    89  
    90  			if err := UnmarshalStrict([]byte(tc.Data), &v); err == nil {
    91  				if len(tc.UnmarshalStrictPrefix) > 0 {
    92  					t.Fatal("expected err")
    93  				}
    94  			} else {
    95  				if len(tc.UnmarshalStrictPrefix) == 0 {
    96  					t.Fatalf("unexpected err %v", err)
    97  				}
    98  				if !strings.HasPrefix(err.Error(), tc.UnmarshalStrictPrefix) {
    99  					t.Fatalf("expected '%s' to start with '%s'", err.Error(), tc.UnmarshalStrictPrefix)
   100  				}
   101  			}
   102  
   103  			if _, err := YAMLToJSON([]byte(tc.Data)); err == nil {
   104  				if len(tc.YAMLToJSONPrefix) > 0 {
   105  					t.Fatal("expected err")
   106  				}
   107  			} else {
   108  				if len(tc.YAMLToJSONPrefix) == 0 {
   109  					t.Fatalf("unexpected err %v", err)
   110  				}
   111  				if !strings.HasPrefix(err.Error(), tc.YAMLToJSONPrefix) {
   112  					t.Fatalf("expected '%s' to start with '%s'", err.Error(), tc.YAMLToJSONPrefix)
   113  				}
   114  			}
   115  
   116  			if _, err := YAMLToJSONStrict([]byte(tc.Data)); err == nil {
   117  				if len(tc.YAMLToJSONStrictPrefix) > 0 {
   118  					t.Fatal("expected err")
   119  				}
   120  			} else {
   121  				if len(tc.YAMLToJSONStrictPrefix) == 0 {
   122  					t.Fatalf("unexpected err %v", err)
   123  				}
   124  				if !strings.HasPrefix(err.Error(), tc.YAMLToJSONStrictPrefix) {
   125  					t.Fatalf("expected '%s' to start with '%s'", err.Error(), tc.YAMLToJSONStrictPrefix)
   126  				}
   127  			}
   128  		})
   129  	}
   130  }
   131  

View as plain text