github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/gotemplate/functional_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package gotemplate
    21  
    22  import (
    23  	"reflect"
    24  	"testing"
    25  )
    26  
    27  func Test_regexStringSubmatch(t *testing.T) {
    28  	type args struct {
    29  		regex string
    30  		s     string
    31  	}
    32  	tests := []struct {
    33  		name    string
    34  		args    args
    35  		want    []string
    36  		wantErr bool
    37  	}{{
    38  		name: "test",
    39  		args: args{
    40  			regex: `^(\d+)K$`,
    41  			s:     "123K",
    42  		},
    43  		want:    []string{"123K", "123"},
    44  		wantErr: false,
    45  	}, {
    46  		name: "test",
    47  		args: args{
    48  			regex: `^(\d+)M$`,
    49  			s:     "123",
    50  		},
    51  		want:    nil,
    52  		wantErr: false,
    53  	}}
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			got, err := regexStringSubmatch(tt.args.regex, tt.args.s)
    57  			if (err != nil) != tt.wantErr {
    58  				t.Errorf("regexStringSubmatch() error = %v, wantErr %v", err, tt.wantErr)
    59  				return
    60  			}
    61  			if !reflect.DeepEqual(got, tt.want) {
    62  				t.Errorf("regexStringSubmatch() got = %v, want %v", got, tt.want)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func Test_fromYAML(t *testing.T) {
    69  	type args struct {
    70  		str string
    71  	}
    72  	tests := []struct {
    73  		name    string
    74  		args    args
    75  		want    map[string]interface{}
    76  		wantErr bool
    77  	}{{
    78  		name: "test",
    79  		args: args{
    80  			str: ``,
    81  		},
    82  		want: map[string]interface{}{},
    83  	}, {
    84  		name: "test",
    85  		args: args{
    86  			str: `efg`,
    87  		},
    88  		want:    map[string]interface{}{},
    89  		wantErr: true,
    90  	}, {
    91  		name: "test",
    92  		args: args{
    93  			str: `a: 
    94                      b: "c"
    95                      c: "d"
    96  `,
    97  		},
    98  		want: map[string]interface{}{
    99  			"a": map[interface{}]interface{}{
   100  				"b": "c",
   101  				"c": "d",
   102  			},
   103  		},
   104  	}}
   105  	for _, tt := range tests {
   106  		t.Run(tt.name, func(t *testing.T) {
   107  			got, err := fromYAML(tt.args.str)
   108  			if (err != nil) != tt.wantErr {
   109  				t.Errorf("fromYAML() error = %v, wantErr %v", err, tt.wantErr)
   110  				return
   111  			}
   112  			if !reflect.DeepEqual(got, tt.want) {
   113  				t.Errorf("fromYAML() got = %v, want %v", got, tt.want)
   114  			}
   115  		})
   116  	}
   117  }
   118  
   119  func Test_fromYAMLArray(t *testing.T) {
   120  	type args struct {
   121  		str string
   122  	}
   123  	tests := []struct {
   124  		name    string
   125  		args    args
   126  		want    []interface{}
   127  		wantErr bool
   128  	}{{
   129  		name: "test",
   130  		args: args{
   131  			str: ``,
   132  		},
   133  		want: nil,
   134  	}, {
   135  		name: "test",
   136  		args: args{
   137  			str: `abc: efg`,
   138  		},
   139  		wantErr: true,
   140  	}, {
   141  		name: "test",
   142  		args: args{
   143  			str: `
   144  - a
   145  - b
   146  - c
   147  `,
   148  		},
   149  		want: []interface{}{
   150  			"a",
   151  			"b",
   152  			"c",
   153  		},
   154  	}}
   155  	for _, tt := range tests {
   156  		t.Run(tt.name, func(t *testing.T) {
   157  			got, err := fromYAMLArray(tt.args.str)
   158  			if (err != nil) != tt.wantErr {
   159  				t.Errorf("fromYAMLArray() error = %v, wantErr %v", err, tt.wantErr)
   160  				return
   161  			}
   162  			if !reflect.DeepEqual(got, tt.want) {
   163  				t.Errorf("fromYAMLArray() got = %v, want %v", got, tt.want)
   164  			}
   165  		})
   166  	}
   167  }