github.com/verrazzano/verrazzano@v1.7.0/pkg/string/comma_separated_test.go (about)

     1  // Copyright (C) 2021, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package string
     5  
     6  import "testing"
     7  
     8  // TestCommaSeparatedContainsString tests the CommaSeparatedStringContains function
     9  func TestCommaSeparatedContainsString(t *testing.T) {
    10  	type args struct {
    11  		commaSeparated string
    12  		s              string
    13  	}
    14  	tests := []struct {
    15  		name string
    16  		args args
    17  		want bool
    18  	}{
    19  		{
    20  			name: "Test empty string",
    21  			args: args{
    22  				commaSeparated: "",
    23  				s:              "test",
    24  			},
    25  			want: false,
    26  		},
    27  		{
    28  			name: "Test match without any commas",
    29  			args: args{
    30  				commaSeparated: "test",
    31  				s:              "test",
    32  			},
    33  			want: true,
    34  		},
    35  		{
    36  			name: "Test match with commas",
    37  			args: args{
    38  				commaSeparated: "test1,test2,test3",
    39  				s:              "test2",
    40  			},
    41  			want: true,
    42  		},
    43  		{
    44  			name: "Test no match with commas",
    45  			args: args{
    46  				commaSeparated: "test1,test2,test3",
    47  				s:              "test",
    48  			},
    49  			want: false,
    50  		},
    51  	}
    52  
    53  	// Process each test case
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			if got := CommaSeparatedStringContains(tt.args.commaSeparated, tt.args.s); got != tt.want {
    57  				t.Errorf("CommaSeparatedStringContains() = %v, want %v", got, tt.want)
    58  			}
    59  		})
    60  	}
    61  }
    62  
    63  // TestCommaSeparatedAppendString tests the AppendToCommaSeparatedString function
    64  func TestCommaSeparatedAppendString(t *testing.T) {
    65  	type args struct {
    66  		commaSeparated string
    67  		s              string
    68  	}
    69  	tests := []struct {
    70  		name string
    71  		args args
    72  		want string
    73  	}{
    74  		{
    75  			name: "Test append to empty string",
    76  			args: args{
    77  				commaSeparated: "",
    78  				s:              "test",
    79  			},
    80  			want: "test",
    81  		},
    82  		{
    83  			name: "Test append to string without commas",
    84  			args: args{
    85  				commaSeparated: "test1",
    86  				s:              "test2",
    87  			},
    88  			want: "test1,test2",
    89  		},
    90  		{
    91  			name: "Test append to string with commas",
    92  			args: args{
    93  				commaSeparated: "test1,test2,test3",
    94  				s:              "test4",
    95  			},
    96  			want: "test1,test2,test3,test4",
    97  		},
    98  	}
    99  
   100  	// Process each test case
   101  	for _, tt := range tests {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			if got := AppendToCommaSeparatedString(tt.args.commaSeparated, tt.args.s); got != tt.want {
   104  				t.Errorf("AppendToCommaSeparatedString() = %v, want %v", got, tt.want)
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  // TestCommaSeparatedRemoveString tests the RemoveFromCommaSeparatedString function
   111  func TestCommaSeparatedRemoveString(t *testing.T) {
   112  	type args struct {
   113  		commaSeparated string
   114  		s              string
   115  	}
   116  	tests := []struct {
   117  		name string
   118  		args args
   119  		want string
   120  	}{
   121  		{
   122  			name: "Test remove from empty string",
   123  			args: args{
   124  				commaSeparated: "",
   125  				s:              "test",
   126  			},
   127  			want: "",
   128  		},
   129  		{
   130  			name: "Test remove from string that does not contain the value",
   131  			args: args{
   132  				commaSeparated: "test1,test2",
   133  				s:              "test",
   134  			},
   135  			want: "test1,test2",
   136  		},
   137  		{
   138  			name: "Test remove from string that does contains the value but no commas",
   139  			args: args{
   140  				commaSeparated: "test1",
   141  				s:              "test1",
   142  			},
   143  			want: "",
   144  		},
   145  		{
   146  			name: "Test remove from string that does contains the value with commas",
   147  			args: args{
   148  				commaSeparated: "test1,test2,test3",
   149  				s:              "test2",
   150  			},
   151  			want: "test1,test3",
   152  		},
   153  	}
   154  
   155  	// Process each test case
   156  	for _, tt := range tests {
   157  		t.Run(tt.name, func(t *testing.T) {
   158  			if got := RemoveFromCommaSeparatedString(tt.args.commaSeparated, tt.args.s); got != tt.want {
   159  				t.Errorf("RemoveFromCommaSeparatedString() = %v, want %v", got, tt.want)
   160  			}
   161  		})
   162  	}
   163  }