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

     1  package commentutil
     2  
     3  import (
     4  	"io"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestGetCodeFenceLength(t *testing.T) {
    10  	tests := []struct {
    11  		in   string
    12  		want int
    13  	}{
    14  		{
    15  			in:   "",
    16  			want: 3,
    17  		},
    18  		{
    19  			in:   "`inline code`",
    20  			want: 3,
    21  		},
    22  		{
    23  			in:   "``foo`bar``",
    24  			want: 3,
    25  		},
    26  		{
    27  			in:   "func main() {\nprintln(\"Hello World\")\n}\n",
    28  			want: 3,
    29  		},
    30  		{
    31  			in:   "```\nLook! You can see my backticks.\n```\n",
    32  			want: 4,
    33  		},
    34  		{
    35  			in:   "```go\nfunc main() {\nprintln(\"Hello World\")\n}\n```",
    36  			want: 4,
    37  		},
    38  		{
    39  			in:   "```go\nfunc main() {\nprintln(\"Hello World\")\n}\n`````",
    40  			want: 6,
    41  		},
    42  		{
    43  			in:   "`````\n````\n```",
    44  			want: 6,
    45  		},
    46  	}
    47  
    48  	for _, tt := range tests {
    49  		want := tt.want
    50  		if got := GetCodeFenceLength(tt.in); got != want {
    51  			t.Errorf("got unexpected length.\ngot:\n%d\nwant:\n%d", got, want)
    52  		}
    53  	}
    54  }
    55  
    56  // A version of strings.Builder without WriteByte
    57  type Builder struct {
    58  	strings.Builder
    59  	io.ByteWriter // conflicts with and hides strings.Builder's WriteByte.
    60  }
    61  
    62  func TestWriteCodeFence(t *testing.T) {
    63  	var buf Builder
    64  	err := WriteCodeFence(&buf, 10)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	got := buf.String()
    69  	want := "``````````"
    70  	if got != want {
    71  		t.Errorf("got %q, want %q", got, want)
    72  	}
    73  }
    74  
    75  func TestWriteCodeFenceWriteByte(t *testing.T) {
    76  	var buf strings.Builder
    77  	err := WriteCodeFence(&buf, 10)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	got := buf.String()
    82  	want := "``````````"
    83  	if got != want {
    84  		t.Errorf("got %q, want %q", got, want)
    85  	}
    86  }