github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/config/split_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestSplitQuotedFields(t *testing.T) {
     8  	in := `field'A' 'fieldB' fie'l\'d'C fieldD 'another field' fieldE`
     9  	tgt := []string{"fieldA", "fieldB", "fiel'dC", "fieldD", "another field", "fieldE"}
    10  	out := SplitQuotedFields(in, '\'')
    11  
    12  	if len(tgt) != len(out) {
    13  		t.Fatalf("expected %#v, got %#v (len mismatch)", tgt, out)
    14  	}
    15  
    16  	for i := range tgt {
    17  		if tgt[i] != out[i] {
    18  			t.Fatalf(" expected %#v, got %#v (mismatch at %d)", tgt, out, i)
    19  		}
    20  	}
    21  }
    22  
    23  func TestSplitDoubleQuotedFields(t *testing.T) {
    24  	tests := []struct {
    25  		name     string
    26  		in       string
    27  		expected []string
    28  	}{
    29  		{
    30  			name:     "generic test case",
    31  			in:       `field"A" "fieldB" fie"l'd"C "field\"D" "yet another field"`,
    32  			expected: []string{"fieldA", "fieldB", "fiel'dC", "field\"D", "yet another field"},
    33  		},
    34  		{
    35  			name:     "with empty string in the end",
    36  			in:       `field"A" "" `,
    37  			expected: []string{"fieldA", ""},
    38  		},
    39  		{
    40  			name:     "with empty string at the beginning",
    41  			in:       ` "" field"A"`,
    42  			expected: []string{"", "fieldA"},
    43  		},
    44  		{
    45  			name:     "lots of spaces",
    46  			in:       `    field"A"   `,
    47  			expected: []string{"fieldA"},
    48  		},
    49  		{
    50  			name:     "only empty string",
    51  			in:       ` "" "" "" """" "" `,
    52  			expected: []string{"", "", "", "", ""},
    53  		},
    54  	}
    55  	for _, tt := range tests {
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			in := tt.in
    58  			tgt := tt.expected
    59  			out := SplitQuotedFields(in, '"')
    60  			if len(tgt) != len(out) {
    61  				t.Fatalf("expected %#v, got %#v (len mismatch)", tgt, out)
    62  			}
    63  
    64  			for i := range tgt {
    65  				if tgt[i] != out[i] {
    66  					t.Fatalf(" expected %#v, got %#v (mismatch at %d)", tgt, out, i)
    67  				}
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func TestConfigureListByName(t *testing.T) {
    74  	type testConfig struct {
    75  		boolArg bool     `cfgName:"bool-arg"`
    76  		listArg []string `cfgName:"list-arg"`
    77  	}
    78  
    79  	type args struct {
    80  		sargs   *testConfig
    81  		cfgname string
    82  	}
    83  	tests := []struct {
    84  		name string
    85  		args args
    86  		want string
    87  	}{
    88  		{
    89  			name: "basic bool",
    90  			args: args{
    91  				sargs: &testConfig{
    92  					boolArg: true,
    93  					listArg: []string{},
    94  				},
    95  				cfgname: "bool-arg",
    96  			},
    97  			want: "bool-arg	true\n",
    98  		},
    99  		{
   100  			name: "list arg",
   101  			args: args{
   102  				sargs: &testConfig{
   103  					boolArg: true,
   104  					listArg: []string{"item 1", "item 2"},
   105  				},
   106  
   107  				cfgname: "list-arg",
   108  			},
   109  			want: "list-arg	[item 1 item 2]\n",
   110  		},
   111  		{
   112  			name: "empty",
   113  			args: args{
   114  				sargs:   &testConfig{},
   115  				cfgname: "",
   116  			},
   117  			want: "",
   118  		},
   119  		{
   120  			name: "invalid",
   121  			args: args{
   122  				sargs:   &testConfig{},
   123  				cfgname: "nonexistent",
   124  			},
   125  			want: "",
   126  		},
   127  	}
   128  	for _, tt := range tests {
   129  		t.Run(tt.name, func(t *testing.T) {
   130  			if got := ConfigureListByName(tt.args.sargs, tt.args.cfgname, "cfgName"); got != tt.want {
   131  				t.Errorf("ConfigureListByName() = %v, want %v", got, tt.want)
   132  			}
   133  		})
   134  	}
   135  }