github.com/khulnasoft/codebase@v0.0.0-20231214144635-a707781cbb24/service/commentutil/commentutil_test.go (about)

     1  package commentutil
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/khulnasoft/codebase"
     8  	"github.com/khulnasoft/codebase/filter"
     9  	"github.com/khulnasoft/codebase/proto/rdf"
    10  )
    11  
    12  func TestCommentBody(t *testing.T) {
    13  	tests := []struct {
    14  		in   *codebase.Comment
    15  		want string
    16  	}{
    17  		{
    18  			in: &codebase.Comment{
    19  				ToolName: "tool-name",
    20  				Result: &filter.FilteredDiagnostic{
    21  					Diagnostic: &rdf.Diagnostic{
    22  						Message: "test message 1",
    23  					},
    24  				},
    25  			},
    26  			want: `
    27  **[tool-name]** <sub>reported by [codebase](https://github.com/khulnasoft/codebase) :dog:</sub><br>test message 1
    28  `,
    29  		},
    30  		{
    31  			in: &codebase.Comment{
    32  				Result: &filter.FilteredDiagnostic{
    33  					Diagnostic: &rdf.Diagnostic{
    34  						Message: "test message 2 (no tool)",
    35  					},
    36  				},
    37  			},
    38  			want: `
    39  <sub>reported by [codebase](https://github.com/khulnasoft/codebase) :dog:</sub><br>test message 2 (no tool)
    40  `,
    41  		},
    42  		{
    43  			in: &codebase.Comment{
    44  				ToolName: "global-tool-name",
    45  				Result: &filter.FilteredDiagnostic{
    46  					Diagnostic: &rdf.Diagnostic{
    47  						Message: "test message 3",
    48  						Source:  &rdf.Source{Name: "custom-tool-name"},
    49  					},
    50  				},
    51  			},
    52  			want: `
    53  **[custom-tool-name]** <sub>reported by [codebase](https://github.com/khulnasoft/codebase) :dog:</sub><br>test message 3
    54  `,
    55  		},
    56  		{
    57  			in: &codebase.Comment{
    58  				Result: &filter.FilteredDiagnostic{
    59  					Diagnostic: &rdf.Diagnostic{
    60  						Message:  "test message 4",
    61  						Source:   &rdf.Source{Name: "tool-name"},
    62  						Severity: rdf.Severity_WARNING,
    63  					},
    64  				},
    65  			},
    66  			want: `
    67  ⚠️ **[tool-name]** <sub>reported by [codebase](https://github.com/khulnasoft/codebase) :dog:</sub><br>test message 4
    68  `,
    69  		},
    70  		{
    71  			in: &codebase.Comment{
    72  				Result: &filter.FilteredDiagnostic{
    73  					Diagnostic: &rdf.Diagnostic{
    74  						Message: "test message 5 (code)",
    75  						Source:  &rdf.Source{Name: "tool-name"},
    76  						Code: &rdf.Code{
    77  							Value: "CODE14",
    78  						},
    79  					},
    80  				},
    81  			},
    82  			want: `
    83  **[tool-name]** <CODE14> <sub>reported by [codebase](https://github.com/khulnasoft/codebase) :dog:</sub><br>test message 5 (code)
    84  `,
    85  		},
    86  		{
    87  			in: &codebase.Comment{
    88  				Result: &filter.FilteredDiagnostic{
    89  					Diagnostic: &rdf.Diagnostic{
    90  						Message: "test message 6 (code with URL)",
    91  						Source:  &rdf.Source{Name: "tool-name"},
    92  						Code: &rdf.Code{
    93  							Value: "CODE14",
    94  							Url:   "https://example.com/#CODE14",
    95  						},
    96  					},
    97  				},
    98  			},
    99  			want: `
   100  **[tool-name]** <[CODE14](https://example.com/#CODE14)> <sub>reported by [codebase](https://github.com/khulnasoft/codebase) :dog:</sub><br>test message 6 (code with URL)
   101  `,
   102  		},
   103  	}
   104  	for _, tt := range tests {
   105  		want := strings.Trim(tt.want, "\n")
   106  		if got := MarkdownComment(tt.in); got != want {
   107  			t.Errorf("got unexpected comment.\ngot:\n%s\nwant:\n%s", got, want)
   108  		}
   109  	}
   110  }