Skip to content

Commit

Permalink
added corner cases for comments.go in astbuilder package (#3134)
Browse files Browse the repository at this point in the history
* added corner cases for comments.go in astbuilder package
* added corner cases to the comments
* updated test case

Co-authored-by: Bevan Arps <bevan.arps@microsoft.com>
  • Loading branch information
khareyash05 and theunrepentantgeek authored Jul 17, 2023
1 parent 6fbd782 commit e748005
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions v2/tools/generator/internal/astbuilder/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func AddComment(commentList *dst.Decorations, comment string) {

// formatComment splits the supplied comment string up ready for use as a documentation comment
func formatComment(comment string, width int) []string {
if comment == "" {
return []string{}
}
// Remove markdown bolding
text := strings.ReplaceAll(comment, "**", "")

Expand Down Expand Up @@ -122,6 +125,10 @@ func docCommentWrap(lines []string, width int) []string {
func WordWrap(text string, width int) []string {
var result []string

if width == 0 && text == "" {
return []string{}
}

start := 0
for start < len(text) {
finish := findBreakPoint(text, start, width)
Expand Down
3 changes: 3 additions & 0 deletions v2/tools/generator/internal/astbuilder/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestDocumentationCommentFormatting(t *testing.T) {
{"**foo**\nbar", []string{"foo", "bar"}},
{"foo\n**bar**", []string{"foo", "bar"}},
{"foo\n**bar**\nbaz", []string{"foo", "bar", "baz"}},
{"", []string{}},
// Expect long lines to be wrapped
{
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
Expand Down Expand Up @@ -70,6 +71,8 @@ func TestWordWrap(t *testing.T) {
{"this is a simple line of text", 16, []string{"this is a simple ", "line of text"}},
{"this is a simple line of text", 20, []string{"this is a simple ", "line of text"}},
{"this is a simple line of text", 21, []string{"this is a simple line ", "of text"}},
{"", 0, []string{}},
{"this is a sample text", 0, []string{"this ", "is ", "a ", "sample ", "text"}},
}

for _, c := range cases {
Expand Down

0 comments on commit e748005

Please sign in to comment.