|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestConvertToTypeName(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + group string |
| 11 | + singular string |
| 12 | + expected string |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "basic conversion", |
| 16 | + group: "apps", |
| 17 | + singular: "deployment", |
| 18 | + expected: "apps_deployment", |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "empty group defaults to core", |
| 22 | + group: "", |
| 23 | + singular: "pod", |
| 24 | + expected: "core_pod", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "group with dots gets replaced with underscores", |
| 28 | + group: "networking.k8s.io", |
| 29 | + singular: "ingress", |
| 30 | + expected: "networking_k8s_io_ingress", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "single character group and singular", |
| 34 | + group: "a", |
| 35 | + singular: "b", |
| 36 | + expected: "a_b", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "numeric group and singular", |
| 40 | + group: "v1", |
| 41 | + singular: "service", |
| 42 | + expected: "v1_service", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "mixed case should be lowercased", |
| 46 | + group: "Apps", |
| 47 | + singular: "Deployment", |
| 48 | + expected: "apps_deployment", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "short group and singular within limits", |
| 52 | + group: "batch", |
| 53 | + singular: "job", |
| 54 | + expected: "batch_job", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "handles special characters in group", |
| 58 | + group: "group-with-dashes", |
| 59 | + singular: "kind", |
| 60 | + expected: "group-with-dashes_kind", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "both group and singular empty", |
| 64 | + group: "", |
| 65 | + singular: "", |
| 66 | + expected: "core_", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "dots in group get replaced properly", |
| 70 | + group: "apps.v1", |
| 71 | + singular: "deployment", |
| 72 | + expected: "apps_v1_deployment", |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "multiple dots get replaced", |
| 76 | + group: "foo.bar.baz", |
| 77 | + singular: "resource", |
| 78 | + expected: "foo_bar_baz_resource", |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.name, func(t *testing.T) { |
| 84 | + result := ConvertToTypeName(tt.group, tt.singular) |
| 85 | + if result != tt.expected { |
| 86 | + t.Errorf("ConvertToTypeName(%q, %q) = %q, want %q", tt.group, tt.singular, result, tt.expected) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestCapGroupSingularLength(t *testing.T) { |
| 93 | + tests := []struct { |
| 94 | + name string |
| 95 | + group string |
| 96 | + singular string |
| 97 | + maxLength int |
| 98 | + expected string |
| 99 | + }{ |
| 100 | + { |
| 101 | + name: "group and singular within max length", |
| 102 | + group: "apps", |
| 103 | + singular: "deployment", |
| 104 | + maxLength: 50, |
| 105 | + expected: "apps_deployment", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "empty group with capGroupSingularLength", |
| 109 | + group: "", |
| 110 | + singular: "pod", |
| 111 | + maxLength: 50, |
| 112 | + expected: "_pod", |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "short group and singular that stays within limits", |
| 116 | + group: "batch", |
| 117 | + singular: "job", |
| 118 | + maxLength: 30, |
| 119 | + expected: "batch_job", |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "exact max length boundary", |
| 123 | + group: "test", |
| 124 | + singular: "resource", |
| 125 | + maxLength: 20, |
| 126 | + expected: "est_resource", // "create_test_resources" = 21 chars, truncate 1: "est_resource" |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "successful truncation case", |
| 130 | + group: "verylonggroup", |
| 131 | + singular: "job", |
| 132 | + maxLength: 20, |
| 133 | + expected: "onggroup_job", // "create_verylonggroup_jobs" = 25 chars, truncate 5: "onggroup_job" |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "truncation case with very long singular", |
| 137 | + group: "short", |
| 138 | + singular: "verylongkindnamethatexceedseverything", |
| 139 | + maxLength: 10, |
| 140 | + expected: "ng", // Additional "s" in relation makes it one char shorter |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "maxLength zero returns original groupKind", |
| 144 | + group: "apps", |
| 145 | + singular: "deployment", |
| 146 | + maxLength: 0, |
| 147 | + expected: "apps_deployment", |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "truncation within group length", |
| 151 | + group: "exactlength", |
| 152 | + singular: "test", |
| 153 | + maxLength: 15, |
| 154 | + expected: "th_test", // "create_exactlength_tests" = 24 chars, truncate 9: "th_test" |
| 155 | + }, |
| 156 | + } |
| 157 | + |
| 158 | + for _, tt := range tests { |
| 159 | + t.Run(tt.name, func(t *testing.T) { |
| 160 | + result := capGroupSingularLength(tt.group, tt.singular, tt.maxLength) |
| 161 | + if result != tt.expected { |
| 162 | + t.Errorf("capGroupSingularLength(%q, %q, %d) = %q, want %q", tt.group, tt.singular, tt.maxLength, result, tt.expected) |
| 163 | + } |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
0 commit comments