github.com/zntrio/harp/v2@v2.0.9/pkg/template/engine/internal/codec/encoder_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  	"testing"
    22  
    23  	fuzz "github.com/google/gofuzz"
    24  )
    25  
    26  func TestToYAML(t *testing.T) {
    27  	type args struct {
    28  		v interface{}
    29  	}
    30  	tests := []struct {
    31  		name string
    32  		args args
    33  		want string
    34  	}{
    35  		{
    36  			name: "empty",
    37  			args: args{
    38  				v: map[string]string{},
    39  			},
    40  			want: "{}",
    41  		},
    42  		{
    43  			name: "object",
    44  			args: args{
    45  				v: map[string]string{
    46  					"key": "value",
    47  				},
    48  			},
    49  			want: "key: value",
    50  		},
    51  		{
    52  			name: "non-serializable",
    53  			args: args{
    54  				v: map[string]interface{}{
    55  					"key": make(chan string, 1),
    56  				},
    57  			},
    58  			want: "",
    59  		},
    60  	}
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			if got := ToYAML(tt.args.v); got != tt.want {
    64  				t.Errorf("ToYAML() = %v, want %v", got, tt.want)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func TestToTOML(t *testing.T) {
    71  	type args struct {
    72  		v interface{}
    73  	}
    74  	tests := []struct {
    75  		name string
    76  		args args
    77  		want string
    78  	}{
    79  		{
    80  			name: "empty",
    81  			args: args{
    82  				v: map[string]string{},
    83  			},
    84  			want: "",
    85  		},
    86  		{
    87  			name: "object",
    88  			args: args{
    89  				v: map[string]string{
    90  					"key": "value",
    91  				},
    92  			},
    93  			want: "key = \"value\"\n",
    94  		},
    95  		/*{
    96  			name: "non-serializable",
    97  			args: args{
    98  				v: map[string]interface{}{
    99  					"key": make(chan string, 1),
   100  				},
   101  			},
   102  			want: "",
   103  		},*/
   104  	}
   105  	for _, tt := range tests {
   106  		t.Run(tt.name, func(t *testing.T) {
   107  			if got := ToTOML(tt.args.v); got != tt.want {
   108  				t.Errorf("ToTOML() = %v, want %v", got, tt.want)
   109  			}
   110  		})
   111  	}
   112  }
   113  
   114  func TestToJSON(t *testing.T) {
   115  	type args struct {
   116  		v interface{}
   117  	}
   118  	tests := []struct {
   119  		name string
   120  		args args
   121  		want string
   122  	}{
   123  		{
   124  			name: "empty",
   125  			args: args{
   126  				v: map[string]string{},
   127  			},
   128  			want: "{}",
   129  		},
   130  		{
   131  			name: "object",
   132  			args: args{
   133  				v: map[string]string{
   134  					"key": "value",
   135  				},
   136  			},
   137  			want: `{"key":"value"}`,
   138  		},
   139  		{
   140  			name: "non-serializable",
   141  			args: args{
   142  				v: map[string]interface{}{
   143  					"key": make(chan string, 1),
   144  				},
   145  			},
   146  			want: "",
   147  		},
   148  	}
   149  	for _, tt := range tests {
   150  		t.Run(tt.name, func(t *testing.T) {
   151  			if got := ToJSON(tt.args.v); got != tt.want {
   152  				t.Errorf("ToJSON() = %v, want %v", got, tt.want)
   153  			}
   154  		})
   155  	}
   156  }
   157  
   158  // -----------------------------------------------------------------------------
   159  
   160  func TestToYAML_Fuzz(t *testing.T) {
   161  	// Making sure that it never panics
   162  	for i := 0; i < 50; i++ {
   163  		f := fuzz.New()
   164  
   165  		// Prepare arguments
   166  		var input struct {
   167  			Integer int
   168  			String  string
   169  			Map     map[string]string
   170  		}
   171  
   172  		// Fuzz input
   173  		f.Fuzz(&input)
   174  
   175  		// Execute
   176  		ToYAML(input)
   177  	}
   178  }
   179  
   180  func TestToTOML_Fuzz(t *testing.T) {
   181  	// Making sure that it never panics
   182  	for i := 0; i < 50; i++ {
   183  		f := fuzz.New()
   184  
   185  		// Prepare arguments
   186  		var input struct {
   187  			Integer int
   188  			String  string
   189  			Map     map[string]string
   190  		}
   191  
   192  		// Fuzz input
   193  		f.Fuzz(&input)
   194  
   195  		// Execute
   196  		ToTOML(input)
   197  	}
   198  }
   199  
   200  func TestToJSON_Fuzz(t *testing.T) {
   201  	// Making sure that it never panics
   202  	for i := 0; i < 50; i++ {
   203  		f := fuzz.New()
   204  
   205  		// Prepare arguments
   206  		var input struct {
   207  			Integer int
   208  			String  string
   209  			Map     map[string]string
   210  		}
   211  
   212  		// Fuzz input
   213  		f.Fuzz(&input)
   214  
   215  		// Execute
   216  		ToJSON(input)
   217  	}
   218  }