|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package group |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestValidateName(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + input string |
| 17 | + expectErr bool |
| 18 | + }{ |
| 19 | + // Valid cases |
| 20 | + {"valid_simple_name", "teamalpha", false}, |
| 21 | + {"valid_with_spaces", "team alpha", false}, |
| 22 | + {"valid_with_dash_and_underscore", "team-alpha_123", false}, |
| 23 | + |
| 24 | + // Empty or whitespace-only |
| 25 | + {"empty_string", "", true}, |
| 26 | + {"only_spaces", " ", true}, |
| 27 | + |
| 28 | + // Invalid characters |
| 29 | + {"invalid_special_characters", "team@alpha!", true}, |
| 30 | + {"invalid_unicode", "团队🚀", true}, |
| 31 | + |
| 32 | + // Null byte |
| 33 | + {"null_byte", "team\x00alpha", true}, |
| 34 | + |
| 35 | + // Leading/trailing whitespace |
| 36 | + {"leading_space", " teamalpha", true}, |
| 37 | + {"trailing_space", "teamalpha ", true}, |
| 38 | + |
| 39 | + // Consecutive spaces |
| 40 | + {"consecutive_spaces_middle", "team alpha", true}, |
| 41 | + {"consecutive_spaces_start", " teamalpha", true}, |
| 42 | + {"consecutive_spaces_end", "teamalpha ", true}, |
| 43 | + |
| 44 | + // Uppercase letters |
| 45 | + {"uppercase_letters", "TeamAlpha", true}, |
| 46 | + |
| 47 | + // Borderline valid |
| 48 | + {"single_char", "t", false}, |
| 49 | + {"max_typical", "alpha team 2025 - squad_01", false}, |
| 50 | + } |
| 51 | + |
| 52 | + for _, tc := range tests { |
| 53 | + t.Run(tc.name, func(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + err := ValidateName(tc.input) |
| 56 | + if tc.expectErr { |
| 57 | + assert.Error(t, err, "Expected error for input: %q", tc.input) |
| 58 | + } else { |
| 59 | + assert.NoError(t, err, "Did not expect error for input: %q", tc.input) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
0 commit comments