github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/unstructured/parser_fsm_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 unstructured
    21  
    22  import (
    23  	"reflect"
    24  	"testing"
    25  )
    26  
    27  func TestFsmParse(t *testing.T) {
    28  	tests := []struct {
    29  		name     string
    30  		args     string
    31  		wantErr  bool
    32  		expected []string
    33  	}{{
    34  		name:     "test",
    35  		args:     "sds",
    36  		wantErr:  false,
    37  		expected: []string{"sds"},
    38  	}, {
    39  		name:     "test",
    40  		args:     "100",
    41  		wantErr:  false,
    42  		expected: []string{"100"},
    43  	}, {
    44  		name:     "test",
    45  		args:     `"efg"`,
    46  		wantErr:  false,
    47  		expected: []string{"efg"},
    48  	}, {
    49  		name:     "test",
    50  		args:     `'efg'`,
    51  		wantErr:  false,
    52  		expected: []string{"efg"},
    53  	}, {
    54  		name:     "test",
    55  		args:     `'efg""'`,
    56  		wantErr:  false,
    57  		expected: []string{"efg\"\""},
    58  	}, {
    59  		name:     "test",
    60  		args:     `'efg\' test'`,
    61  		wantErr:  false,
    62  		expected: []string{"efg' test"},
    63  	}, { // for error
    64  		name:    "test",
    65  		args:    `efg\' test`,
    66  		wantErr: true,
    67  	}, {
    68  		name:     "test",
    69  		args:     `' test'`,
    70  		expected: []string{" test"},
    71  		wantErr:  false,
    72  	}, {
    73  		name:     "test",
    74  		args:     `bind 192.168.1.100 10.0.0.1`,
    75  		expected: []string{"bind", "192.168.1.100", "10.0.0.1"},
    76  		wantErr:  false,
    77  	}, {
    78  		name:     "test",
    79  		args:     `bind 127.0.0.1 ::1 `,
    80  		expected: []string{"bind", "127.0.0.1", "::1"},
    81  		wantErr:  false,
    82  	}, {
    83  		name:     "test",
    84  		args:     `bind * -::* `,
    85  		expected: []string{"bind", "*", "-::*"},
    86  		wantErr:  false,
    87  	}}
    88  	for _, tt := range tests {
    89  		t.Run(tt.name, func(t *testing.T) {
    90  			f := &fsm{
    91  				param:           &Item{},
    92  				splitCharacters: trimChars,
    93  			}
    94  			if err := f.parse(tt.args); (err != nil) != tt.wantErr {
    95  				t.Errorf("parse() error = %v, wantErr %v", err, tt.wantErr)
    96  			}
    97  
    98  			if tt.expected != nil && !reflect.DeepEqual(f.param.Values, tt.expected) {
    99  				t.Errorf("parse() param = %v, expected %v", f.param.Values, tt.expected)
   100  			}
   101  		})
   102  	}
   103  }