github.com/zntrio/harp/v2@v2.0.9/pkg/template/engine/internal/codec/decoder_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package codec
    19  
    20  import (
    21  	"reflect"
    22  	"testing"
    23  
    24  	fuzz "github.com/google/gofuzz"
    25  )
    26  
    27  func TestFromYAML(t *testing.T) {
    28  	type args struct {
    29  		str string
    30  	}
    31  	tests := []struct {
    32  		name string
    33  		args args
    34  		want map[string]interface{}
    35  	}{
    36  		{
    37  			name: "empty",
    38  			args: args{
    39  				str: "",
    40  			},
    41  			want: map[string]interface{}{},
    42  		},
    43  		{
    44  			name: "with error",
    45  			args: args{
    46  				str: ";",
    47  			},
    48  			want: map[string]interface{}{"Error": "error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type map[string]interface {}"},
    49  		},
    50  		{
    51  			name: "valid",
    52  			args: args{
    53  				str: "key: value",
    54  			},
    55  			want: map[string]interface{}{"key": "value"},
    56  		},
    57  	}
    58  	for _, tt := range tests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			if got := FromYAML(tt.args.str); !reflect.DeepEqual(got, tt.want) {
    61  				t.Errorf("FromYAML() = %v, want %v", got, tt.want)
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestFromYAMLArray(t *testing.T) {
    68  	type args struct {
    69  		str string
    70  	}
    71  	tests := []struct {
    72  		name string
    73  		args args
    74  		want []interface{}
    75  	}{
    76  		{
    77  			name: "empty",
    78  			args: args{
    79  				str: "",
    80  			},
    81  			want: []interface{}{},
    82  		},
    83  		{
    84  			name: "with error",
    85  			args: args{
    86  				str: ";",
    87  			},
    88  			want: []interface{}{"error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type []interface {}"},
    89  		},
    90  		{
    91  			name: "valid",
    92  			args: args{
    93  				str: "['1','2']",
    94  			},
    95  			want: []interface{}{"1", "2"},
    96  		},
    97  	}
    98  	for _, tt := range tests {
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			if got := FromYAMLArray(tt.args.str); !reflect.DeepEqual(got, tt.want) {
   101  				t.Errorf("FromYAMLArray() = %v, want %v", got, tt.want)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  func TestFromJSON(t *testing.T) {
   108  	type args struct {
   109  		str string
   110  	}
   111  	tests := []struct {
   112  		name string
   113  		args args
   114  		want map[string]interface{}
   115  	}{
   116  		{
   117  			name: "empty",
   118  			args: args{
   119  				str: "",
   120  			},
   121  			want: map[string]interface{}{"Error": "unexpected end of JSON input"},
   122  		},
   123  		{
   124  			name: "with error",
   125  			args: args{
   126  				str: ";",
   127  			},
   128  			want: map[string]interface{}{"Error": "invalid character ';' looking for beginning of value"},
   129  		},
   130  		{
   131  			name: "valid",
   132  			args: args{
   133  				str: `{"key": "value"}`,
   134  			},
   135  			want: map[string]interface{}{"key": "value"},
   136  		},
   137  	}
   138  	for _, tt := range tests {
   139  		t.Run(tt.name, func(t *testing.T) {
   140  			if got := FromJSON(tt.args.str); !reflect.DeepEqual(got, tt.want) {
   141  				t.Errorf("FromJSON() = %v, want %v", got, tt.want)
   142  			}
   143  		})
   144  	}
   145  }
   146  
   147  func TestFromJSONArray(t *testing.T) {
   148  	type args struct {
   149  		str string
   150  	}
   151  	tests := []struct {
   152  		name string
   153  		args args
   154  		want []interface{}
   155  	}{
   156  		{
   157  			name: "empty",
   158  			args: args{
   159  				str: "",
   160  			},
   161  			want: []interface{}{"unexpected end of JSON input"},
   162  		},
   163  		{
   164  			name: "with error",
   165  			args: args{
   166  				str: ";",
   167  			},
   168  			want: []interface{}{"invalid character ';' looking for beginning of value"},
   169  		},
   170  		{
   171  			name: "valid",
   172  			args: args{
   173  				str: `["1","2"]`,
   174  			},
   175  			want: []interface{}{"1", "2"},
   176  		},
   177  	}
   178  	for _, tt := range tests {
   179  		t.Run(tt.name, func(t *testing.T) {
   180  			if got := FromJSONArray(tt.args.str); !reflect.DeepEqual(got, tt.want) {
   181  				t.Errorf("FromJSONArray() = %v, want %v", got, tt.want)
   182  			}
   183  		})
   184  	}
   185  }
   186  
   187  // -----------------------------------------------------------------------------
   188  
   189  func TestFromYAML_Fuzz(t *testing.T) {
   190  	// Making sure that it never panics
   191  	for i := 0; i < 50; i++ {
   192  		f := fuzz.New()
   193  
   194  		// Prepare arguments
   195  		var input string
   196  
   197  		// Fuzz input
   198  		f.Fuzz(&input)
   199  
   200  		// Execute
   201  		FromYAML(input)
   202  	}
   203  }
   204  
   205  func TestFromYAMLArray_Fuzz(t *testing.T) {
   206  	// Making sure that it never panics
   207  	for i := 0; i < 50; i++ {
   208  		f := fuzz.New()
   209  
   210  		// Prepare arguments
   211  		var input string
   212  
   213  		// Fuzz input
   214  		f.Fuzz(&input)
   215  
   216  		// Execute
   217  		FromYAMLArray(input)
   218  	}
   219  }
   220  
   221  func TestFromJSON_Fuzz(t *testing.T) {
   222  	// Making sure that it never panics
   223  	for i := 0; i < 50; i++ {
   224  		f := fuzz.New()
   225  
   226  		// Prepare arguments
   227  		var input string
   228  
   229  		// Fuzz input
   230  		f.Fuzz(&input)
   231  
   232  		// Execute
   233  		FromJSON(input)
   234  	}
   235  }
   236  
   237  func TestFromJSONArray_Fuzz(t *testing.T) {
   238  	// Making sure that it never panics
   239  	for i := 0; i < 50; i++ {
   240  		f := fuzz.New()
   241  
   242  		// Prepare arguments
   243  		var input string
   244  
   245  		// Fuzz input
   246  		f.Fuzz(&input)
   247  
   248  		// Execute
   249  		FromJSONArray(input)
   250  	}
   251  }