@@ -16,6 +16,7 @@ limitations under the License.
1616package markers
1717
1818import (
19+ "go/ast"
1920 "testing"
2021
2122 . "github.com/onsi/gomega"
@@ -146,3 +147,106 @@ func TestExtractMarkerIdAndExpressions(t *testing.T) {
146147 })
147148 }
148149}
150+
151+ func TestExtractMarker (t * testing.T ) {
152+ type testcase struct {
153+ name string
154+ comment string
155+ shouldBeMarker bool
156+ expectedID string
157+ }
158+
159+ testcases := []testcase {
160+ {
161+ name : "valid marker - required" ,
162+ comment : "// +required" ,
163+ shouldBeMarker : true ,
164+ expectedID : "required" ,
165+ },
166+ {
167+ name : "valid marker - kubebuilder:validation:Required" ,
168+ comment : "// +kubebuilder:validation:Required" ,
169+ shouldBeMarker : true ,
170+ expectedID : "kubebuilder:validation:Required" ,
171+ },
172+ {
173+ name : "valid marker - with expressions" ,
174+ comment : "// +kubebuilder:validation:XValidation:rule=\" something\" ,message=\" haha\" " ,
175+ shouldBeMarker : true ,
176+ expectedID : "kubebuilder:validation:XValidation" ,
177+ },
178+ {
179+ name : "valid marker - with parentheses" ,
180+ comment : "// +k8s:ifEnabled(\" foo\" )=+k8s:required" ,
181+ shouldBeMarker : true ,
182+ expectedID : "k8s:ifEnabled(\" foo\" )" ,
183+ },
184+ {
185+ name : "valid marker - with single quotes and parentheses" ,
186+ comment : "// +k8s:ifEnabled('foo')=+k8s:required" ,
187+ shouldBeMarker : true ,
188+ expectedID : "k8s:ifEnabled('foo')" ,
189+ },
190+ {
191+ name : "valid marker - with backtickets and parentheses" ,
192+ comment : "// +k8s:ifEnabled(`foo`)=+k8s:required" ,
193+ shouldBeMarker : true ,
194+ expectedID : "k8s:ifEnabled(`foo`)" ,
195+ },
196+ {
197+ name : "invalid marker - markdown table border" ,
198+ comment : "// +-------+-------+-------+" ,
199+ shouldBeMarker : false ,
200+ expectedID : "" ,
201+ },
202+ {
203+ name : "invalid marker - markdown table border without pipes" ,
204+ comment : "// +----------" ,
205+ shouldBeMarker : false ,
206+ expectedID : "" ,
207+ },
208+ {
209+ name : "invalid marker - starts with special characters" ,
210+ comment : "// +!*@(#&KSDJUF:A" ,
211+ shouldBeMarker : false ,
212+ expectedID : "" ,
213+ },
214+ {
215+ name : "regular comment - no plus sign" ,
216+ comment : "// This is a regular comment" ,
217+ shouldBeMarker : false ,
218+ expectedID : "" ,
219+ },
220+ {
221+ name : "valid marker - complex nested expression" ,
222+ comment : "// +k8s:someThing(one: \" a\" , two: \" b\" )=+k8s:required" ,
223+ shouldBeMarker : true ,
224+ expectedID : "k8s:someThing(one: \" a\" , two: \" b\" )" ,
225+ },
226+ {
227+ name : "valid marker - complex nested expression with '" ,
228+ comment : "// +k8s:someThing(one: 'a', two: 'b')=+k8s:required" ,
229+ shouldBeMarker : true ,
230+ expectedID : "k8s:someThing(one: 'a', two: 'b')" ,
231+ },
232+ }
233+
234+ for _ , tc := range testcases {
235+ t .Run (tc .name , func (t * testing.T ) {
236+ g := NewWithT (t )
237+
238+ // Create a mock comment
239+ comment := & ast.Comment {
240+ Text : tc .comment ,
241+ }
242+
243+ marker := extractMarker (comment )
244+
245+ if tc .shouldBeMarker {
246+ g .Expect (marker .Identifier ).To (Equal (tc .expectedID ), "comment" , tc .comment )
247+ } else {
248+ g .Expect (marker .Identifier ).To (BeEmpty (), "comment" , tc .comment )
249+ }
250+ })
251+ }
252+ }
0 commit comments