|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func Test_newFileList(t *testing.T) { |
| 9 | + type args struct { |
| 10 | + paths []string |
| 11 | + } |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + args args |
| 15 | + want *fileList |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "nil paths", |
| 19 | + args: args{paths: nil}, |
| 20 | + want: &fileList{patterns: map[string]struct{}{}}, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "empty paths", |
| 24 | + args: args{paths: []string{}}, |
| 25 | + want: &fileList{patterns: map[string]struct{}{}}, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "have paths", |
| 29 | + args: args{paths: []string{"*_test.go"}}, |
| 30 | + want: &fileList{patterns: map[string]struct{}{ |
| 31 | + "*_test.go": struct{}{}, |
| 32 | + }}, |
| 33 | + }, |
| 34 | + } |
| 35 | + for _, tt := range tests { |
| 36 | + if got := newFileList(tt.args.paths...); !reflect.DeepEqual(got, tt.want) { |
| 37 | + t.Errorf("%q. newFileList() = %v, want %v", tt.name, got, tt.want) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func Test_fileList_String(t *testing.T) { |
| 43 | + type fields struct { |
| 44 | + patterns []string |
| 45 | + } |
| 46 | + tests := []struct { |
| 47 | + name string |
| 48 | + fields fields |
| 49 | + want string |
| 50 | + }{ |
| 51 | + { |
| 52 | + name: "nil patterns", |
| 53 | + fields: fields{patterns: nil}, |
| 54 | + want: "", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "empty patterns", |
| 58 | + fields: fields{patterns: []string{}}, |
| 59 | + want: "", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "one pattern", |
| 63 | + fields: fields{patterns: []string{"foo"}}, |
| 64 | + want: "foo", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "two patterns", |
| 68 | + fields: fields{patterns: []string{"foo", "bar"}}, |
| 69 | + want: "foo, bar", |
| 70 | + }, |
| 71 | + } |
| 72 | + for _, tt := range tests { |
| 73 | + f := newFileList(tt.fields.patterns...) |
| 74 | + if got := f.String(); got != tt.want { |
| 75 | + t.Errorf("%q. fileList.String() = %v, want %v", tt.name, got, tt.want) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func Test_fileList_Set(t *testing.T) { |
| 81 | + type fields struct { |
| 82 | + patterns []string |
| 83 | + } |
| 84 | + type args struct { |
| 85 | + path string |
| 86 | + } |
| 87 | + tests := []struct { |
| 88 | + name string |
| 89 | + fields fields |
| 90 | + args args |
| 91 | + want map[string]struct{} |
| 92 | + wantErr bool |
| 93 | + }{ |
| 94 | + { |
| 95 | + name: "add empty path", |
| 96 | + fields: fields{patterns: nil}, |
| 97 | + args: args{path: ""}, |
| 98 | + want: map[string]struct{}{}, |
| 99 | + wantErr: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "add path to nil patterns", |
| 103 | + fields: fields{patterns: nil}, |
| 104 | + args: args{path: "foo"}, |
| 105 | + want: map[string]struct{}{ |
| 106 | + "foo": struct{}{}, |
| 107 | + }, |
| 108 | + wantErr: false, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "add path to empty patterns", |
| 112 | + fields: fields{patterns: []string{}}, |
| 113 | + args: args{path: "foo"}, |
| 114 | + want: map[string]struct{}{ |
| 115 | + "foo": struct{}{}, |
| 116 | + }, |
| 117 | + wantErr: false, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "add path to populated patterns", |
| 121 | + fields: fields{patterns: []string{"foo"}}, |
| 122 | + args: args{path: "bar"}, |
| 123 | + want: map[string]struct{}{ |
| 124 | + "foo": struct{}{}, |
| 125 | + "bar": struct{}{}, |
| 126 | + }, |
| 127 | + wantErr: false, |
| 128 | + }, |
| 129 | + } |
| 130 | + for _, tt := range tests { |
| 131 | + f := newFileList(tt.fields.patterns...) |
| 132 | + if err := f.Set(tt.args.path); (err != nil) != tt.wantErr { |
| 133 | + t.Errorf("%q. fileList.Set() error = %v, wantErr %v", tt.name, err, tt.wantErr) |
| 134 | + } |
| 135 | + if !reflect.DeepEqual(f.patterns, tt.want) { |
| 136 | + t.Errorf("%q. got state fileList.patterns = %v, want state %v", tt.name, f.patterns, tt.want) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func Test_fileList_Contains(t *testing.T) { |
| 142 | + type fields struct { |
| 143 | + patterns []string |
| 144 | + } |
| 145 | + type args struct { |
| 146 | + path string |
| 147 | + } |
| 148 | + tests := []struct { |
| 149 | + name string |
| 150 | + fields fields |
| 151 | + args args |
| 152 | + want bool |
| 153 | + }{ |
| 154 | + { |
| 155 | + name: "nil patterns", |
| 156 | + fields: fields{patterns: nil}, |
| 157 | + args: args{path: "foo"}, |
| 158 | + want: false, |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "empty patterns", |
| 162 | + fields: fields{patterns: nil}, |
| 163 | + args: args{path: "foo"}, |
| 164 | + want: false, |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "one pattern, no wildcard, no match", |
| 168 | + fields: fields{patterns: []string{"foo"}}, |
| 169 | + args: args{path: "bar"}, |
| 170 | + want: false, |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "one pattern, no wildcard, match", |
| 174 | + fields: fields{patterns: []string{"foo"}}, |
| 175 | + args: args{path: "foo"}, |
| 176 | + want: true, |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "one pattern, wildcard prefix, match", |
| 180 | + fields: fields{patterns: []string{"*foo"}}, |
| 181 | + args: args{path: "foo"}, |
| 182 | + want: true, |
| 183 | + }, |
| 184 | + { |
| 185 | + name: "one pattern, wildcard suffix, match", |
| 186 | + fields: fields{patterns: []string{"foo*"}}, |
| 187 | + args: args{path: "foo"}, |
| 188 | + want: true, |
| 189 | + }, |
| 190 | + { |
| 191 | + name: "one pattern, wildcard both ends, match", |
| 192 | + fields: fields{patterns: []string{"*foo*"}}, |
| 193 | + args: args{path: "foo"}, |
| 194 | + want: true, |
| 195 | + }, |
| 196 | + { |
| 197 | + name: "default test match 1", |
| 198 | + fields: fields{patterns: []string{"*_test.go"}}, |
| 199 | + args: args{path: "foo_test.go"}, |
| 200 | + want: true, |
| 201 | + }, |
| 202 | + { |
| 203 | + name: "default test match 2", |
| 204 | + fields: fields{patterns: []string{"*_test.go"}}, |
| 205 | + args: args{path: "bar/foo_test.go"}, |
| 206 | + want: true, |
| 207 | + }, |
| 208 | + { |
| 209 | + name: "default test match 3", |
| 210 | + fields: fields{patterns: []string{"*_test.go"}}, |
| 211 | + args: args{path: "/bar/foo_test.go"}, |
| 212 | + want: true, |
| 213 | + }, |
| 214 | + { |
| 215 | + name: "default test match 4", |
| 216 | + fields: fields{patterns: []string{"*_test.go"}}, |
| 217 | + args: args{path: "baz/bar/foo_test.go"}, |
| 218 | + want: true, |
| 219 | + }, |
| 220 | + { |
| 221 | + name: "default test match 5", |
| 222 | + fields: fields{patterns: []string{"*_test.go"}}, |
| 223 | + args: args{path: "/baz/bar/foo_test.go"}, |
| 224 | + want: true, |
| 225 | + }, |
| 226 | + { |
| 227 | + name: "many patterns, no match", |
| 228 | + fields: fields{patterns: []string{"*_one.go", "*_two.go"}}, |
| 229 | + args: args{path: "/baz/bar/foo_test.go"}, |
| 230 | + want: false, |
| 231 | + }, |
| 232 | + { |
| 233 | + name: "many patterns, match", |
| 234 | + fields: fields{patterns: []string{"*_one.go", "*_two.go", "*_test.go"}}, |
| 235 | + args: args{path: "/baz/bar/foo_test.go"}, |
| 236 | + want: true, |
| 237 | + }, |
| 238 | + } |
| 239 | + for _, tt := range tests { |
| 240 | + f := newFileList(tt.fields.patterns...) |
| 241 | + if got := f.Contains(tt.args.path); got != tt.want { |
| 242 | + t.Errorf("%q. fileList.Contains() = %v, want %v", tt.name, got, tt.want) |
| 243 | + } |
| 244 | + } |
| 245 | +} |
0 commit comments