@@ -218,3 +218,201 @@ func TestTextOutputIsSorted(t *testing.T) {
218218 assert .NoError (t , err )
219219 assert .Equal (t , "# Source: A\n \n \n (No annotations found)\n --\n # Source: B\n \n \n (No annotations found)\n --\n # Source: C\n \n \n (No annotations found)\n --\n " , buffy .String ())
220220}
221+
222+ func TestOutputText (t * testing.T ) {
223+ cases := []struct {
224+ name string
225+ allData map [string ][]* ast.AnnotationsRef
226+ template string
227+ expected string
228+ expectErr bool
229+ errMsg string
230+ }{
231+ {
232+ name : "successful output with multiple sources" ,
233+ allData : map [string ][]* ast.AnnotationsRef {
234+ "source1" : {
235+ {
236+ Path : ast.Ref {
237+ ast .VarTerm ("data" ),
238+ ast .StringTerm ("policy" ),
239+ ast .StringTerm ("test" ),
240+ ast .StringTerm ("deny" ),
241+ },
242+ Annotations : & ast.Annotations {
243+ Scope : "rule" ,
244+ Title : "Test Rule" ,
245+ Description : "Test Description" ,
246+ },
247+ },
248+ },
249+ "source2" : {
250+ {
251+ Path : ast.Ref {
252+ ast .VarTerm ("data" ),
253+ ast .StringTerm ("policy" ),
254+ ast .StringTerm ("warn" ),
255+ },
256+ Annotations : & ast.Annotations {
257+ Scope : "rule" ,
258+ Title : "Warning Rule" ,
259+ },
260+ },
261+ },
262+ },
263+ template : "text" ,
264+ expected : "# Source: source1\n \n policy.test. (deny)\n Test Rule\n Test Description\n --\n # Source: source2\n \n policy. (warn)\n Warning Rule\n \n --\n " ,
265+ expectErr : false ,
266+ },
267+ {
268+ name : "successful output with no annotations" ,
269+ allData : map [string ][]* ast.AnnotationsRef {
270+ "source1" : {
271+ {
272+ Path : ast.Ref {
273+ ast .VarTerm ("data" ),
274+ ast .StringTerm ("policy" ),
275+ ast .StringTerm ("test" ),
276+ ast .StringTerm ("deny" ),
277+ },
278+ // No annotations
279+ },
280+ },
281+ },
282+ template : "text" ,
283+ expected : "# Source: source1\n \n policy.test.deny\n (No annotations found)\n --\n " ,
284+ expectErr : false ,
285+ },
286+ {
287+ name : "failed output with invalid template" ,
288+ allData : map [string ][]* ast.AnnotationsRef {
289+ "source1" : {
290+ {
291+ Path : ast.Ref {
292+ ast .VarTerm ("data" ),
293+ ast .StringTerm ("policy" ),
294+ ast .StringTerm ("test" ),
295+ ast .StringTerm ("deny" ),
296+ },
297+ Annotations : & ast.Annotations {
298+ Scope : "rule" ,
299+ Title : "Test Rule" ,
300+ },
301+ },
302+ },
303+ },
304+ template : "invalid-template" ,
305+ expected : "" ,
306+ expectErr : true ,
307+ errMsg : "no template" ,
308+ },
309+ }
310+
311+ for _ , c := range cases {
312+ t .Run (c .name , func (t * testing.T ) {
313+ buf := new (bytes.Buffer )
314+ err := OutputText (buf , c .allData , c .template )
315+
316+ if c .expectErr {
317+ assert .Error (t , err , "Expected error for invalid input" )
318+ if c .errMsg != "" {
319+ assert .Contains (t , err .Error (), c .errMsg , "Error message should contain expected text" )
320+ }
321+ } else {
322+ assert .NoError (t , err , "Expected no error for valid input" )
323+ assert .Equal (t , c .expected , buf .String (), "Output should match expected" )
324+ }
325+ })
326+ }
327+ }
328+
329+ func TestRenderAnn (t * testing.T ) {
330+ cases := []struct {
331+ name string
332+ annotations * ast.AnnotationsRef
333+ tmplName string
334+ expected string
335+ expectErr bool
336+ errMsg string
337+ }{
338+ {
339+ name : "successful render with text template" ,
340+ annotations : & ast.AnnotationsRef {
341+ Path : ast.Ref {
342+ ast .VarTerm ("data" ),
343+ ast .StringTerm ("policy" ),
344+ ast .StringTerm ("test" ),
345+ ast .StringTerm ("deny" ),
346+ },
347+ Annotations : & ast.Annotations {
348+ Scope : "rule" ,
349+ Title : "Test Rule" ,
350+ Description : "Test Description" ,
351+ Custom : map [string ]interface {}{
352+ "short_name" : "test_rule" ,
353+ },
354+ },
355+ },
356+ tmplName : "text" ,
357+ expected : "policy.test.test_rule (deny)\n https://conforma.dev/docs/policy/packages/release_test.html#test__test_rule\n Test Rule\n Test Description\n --\n " ,
358+ expectErr : false ,
359+ },
360+ {
361+ name : "successful render with names template" ,
362+ annotations : & ast.AnnotationsRef {
363+ Path : ast.Ref {
364+ ast .VarTerm ("data" ),
365+ ast .StringTerm ("policy" ),
366+ ast .StringTerm ("test" ),
367+ ast .StringTerm ("warn" ),
368+ },
369+ Annotations : & ast.Annotations {
370+ Scope : "rule" ,
371+ Title : "Warning Rule" ,
372+ Custom : map [string ]interface {}{
373+ "short_name" : "warning_rule" ,
374+ },
375+ },
376+ },
377+ tmplName : "names" ,
378+ expected : "policy.test.warning_rule\n " ,
379+ expectErr : false ,
380+ },
381+ {
382+ name : "failed render with invalid template" ,
383+ annotations : & ast.AnnotationsRef {
384+ Path : ast.Ref {
385+ ast .VarTerm ("data" ),
386+ ast .StringTerm ("policy" ),
387+ ast .StringTerm ("test" ),
388+ ast .StringTerm ("deny" ),
389+ },
390+ Annotations : & ast.Annotations {
391+ Scope : "rule" ,
392+ Title : "Test Rule" ,
393+ },
394+ },
395+ tmplName : "invalid-template" ,
396+ expected : "" ,
397+ expectErr : true ,
398+ errMsg : "no template" ,
399+ },
400+ }
401+
402+ for _ , c := range cases {
403+ t .Run (c .name , func (t * testing.T ) {
404+ buf := new (bytes.Buffer )
405+ err := renderAnn (buf , c .annotations , c .tmplName )
406+
407+ if c .expectErr {
408+ assert .Error (t , err , "Expected error for invalid template" )
409+ if c .errMsg != "" {
410+ assert .Contains (t , err .Error (), c .errMsg , "Error message should contain expected text" )
411+ }
412+ } else {
413+ assert .NoError (t , err , "Expected no error for valid template" )
414+ assert .Equal (t , c .expected , buf .String (), "Output should match expected" )
415+ }
416+ })
417+ }
418+ }
0 commit comments