|
| 1 | +package strings |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestDeDupeStrSlice(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + input []string |
| 12 | + expected []string |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "Empty slice", |
| 16 | + input: []string{}, |
| 17 | + expected: []string{}, |
| 18 | + }, |
| 19 | + { |
| 20 | + name: "Single element", |
| 21 | + input: []string{"foo"}, |
| 22 | + expected: []string{"foo"}, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "Duplicate elements", |
| 26 | + input: []string{"foo", "foo"}, |
| 27 | + expected: []string{"foo"}, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "Multiple elements", |
| 31 | + input: []string{"foo", "bar", "foo", "baz", "bar"}, |
| 32 | + expected: []string{"foo", "bar", "baz"}, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + for _, tt := range tests { |
| 37 | + t.Run(tt.name, func(t *testing.T) { |
| 38 | + actual := DeDupeStrSlice(tt.input) |
| 39 | + if len(actual) != len(tt.expected) { |
| 40 | + t.Fatalf("expected %v, got %v", tt.expected, actual) |
| 41 | + } |
| 42 | + if !slices.Equal(actual, tt.expected) { |
| 43 | + t.Fatalf("expected %v, got %v", tt.expected, actual) |
| 44 | + } |
| 45 | + }) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestSanitize(t *testing.T) { |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + input string |
| 53 | + expected string |
| 54 | + }{ |
| 55 | + { |
| 56 | + name: "Empty string", |
| 57 | + input: "", |
| 58 | + expected: "", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "Lowercase", |
| 62 | + input: "FOO", |
| 63 | + expected: "foo", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "Trim whitespace", |
| 67 | + input: " foo ", |
| 68 | + expected: "foo", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "Replace non-alphanumeric characters", |
| 72 | + input: "foo-bar_baz", |
| 73 | + expected: "foo-bar-baz", |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "Remove consecutive & leading/trailing dashes", |
| 77 | + input: "! Example---string with **multiple** non-alphanumeric---characters---! ", |
| 78 | + expected: "example-string-with-multiple-non-alphanumeric-characters", |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.name, func(t *testing.T) { |
| 84 | + actual := Sanitize(tt.input) |
| 85 | + if actual != tt.expected { |
| 86 | + t.Fatalf("expected %v, got %v", tt.expected, actual) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments