github.com/neilgarb/delve@v1.9.2-nobreaks/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  	in := `field"A" "fieldB" fie"l'd"C "field\"D" "yet another field"`
    25  	tgt := []string{"fieldA", "fieldB", "fiel'dC", "field\"D", "yet another field"}
    26  	out := SplitQuotedFields(in, '"')
    27  
    28  	if len(tgt) != len(out) {
    29  		t.Fatalf("expected %#v, got %#v (len mismatch)", tgt, out)
    30  	}
    31  
    32  	for i := range tgt {
    33  		if tgt[i] != out[i] {
    34  			t.Fatalf(" expected %#v, got %#v (mismatch at %d)", tgt, out, i)
    35  		}
    36  	}
    37  }
    38  
    39  func TestConfigureListByName(t *testing.T) {
    40  	type testConfig struct {
    41  		boolArg bool     `cfgName:"bool-arg"`
    42  		listArg []string `cfgName:"list-arg"`
    43  	}
    44  
    45  	type args struct {
    46  		sargs   *testConfig
    47  		cfgname string
    48  	}
    49  	tests := []struct {
    50  		name string
    51  		args args
    52  		want string
    53  	}{
    54  		{
    55  			name: "basic bool",
    56  			args: args{
    57  				sargs: &testConfig{
    58  					boolArg: true,
    59  					listArg: []string{},
    60  				},
    61  				cfgname: "bool-arg",
    62  			},
    63  			want: "bool-arg	true\n",
    64  		},
    65  		{
    66  			name: "list arg",
    67  			args: args{
    68  				sargs: &testConfig{
    69  					boolArg: true,
    70  					listArg: []string{"item 1", "item 2"},
    71  				},
    72  
    73  				cfgname: "list-arg",
    74  			},
    75  			want: "list-arg	[item 1 item 2]\n",
    76  		},
    77  		{
    78  			name: "empty",
    79  			args: args{
    80  				sargs:   &testConfig{},
    81  				cfgname: "",
    82  			},
    83  			want: "",
    84  		},
    85  		{
    86  			name: "invalid",
    87  			args: args{
    88  				sargs:   &testConfig{},
    89  				cfgname: "nonexistent",
    90  			},
    91  			want: "",
    92  		},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			if got := ConfigureListByName(tt.args.sargs, tt.args.cfgname, "cfgName"); got != tt.want {
    97  				t.Errorf("ConfigureListByName() = %v, want %v", got, tt.want)
    98  			}
    99  		})
   100  	}
   101  }