github.com/v2fly/tools@v0.100.0/internal/lsp/fake/edit_test.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fake
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestApplyEdit(t *testing.T) {
    13  	tests := []struct {
    14  		label   string
    15  		content string
    16  		edits   []Edit
    17  		want    string
    18  		wantErr bool
    19  	}{
    20  		{
    21  			label: "empty content",
    22  		},
    23  		{
    24  			label:   "empty edit",
    25  			content: "hello",
    26  			edits:   []Edit{},
    27  			want:    "hello",
    28  		},
    29  		{
    30  			label:   "unicode edit",
    31  			content: "hello, 日本語",
    32  			edits: []Edit{{
    33  				Start: Pos{Line: 0, Column: 7},
    34  				End:   Pos{Line: 0, Column: 10},
    35  				Text:  "world",
    36  			}},
    37  			want: "hello, world",
    38  		},
    39  		{
    40  			label:   "range edit",
    41  			content: "ABC\nDEF\nGHI\nJKL",
    42  			edits: []Edit{{
    43  				Start: Pos{Line: 1, Column: 1},
    44  				End:   Pos{Line: 2, Column: 3},
    45  				Text:  "12\n345",
    46  			}},
    47  			want: "ABC\nD12\n345\nJKL",
    48  		},
    49  		{
    50  			label:   "end before start",
    51  			content: "ABC\nDEF\nGHI\nJKL",
    52  			edits: []Edit{{
    53  				End:   Pos{Line: 1, Column: 1},
    54  				Start: Pos{Line: 2, Column: 3},
    55  				Text:  "12\n345",
    56  			}},
    57  			wantErr: true,
    58  		},
    59  		{
    60  			label:   "out of bounds line",
    61  			content: "ABC\nDEF\nGHI\nJKL",
    62  			edits: []Edit{{
    63  				Start: Pos{Line: 1, Column: 1},
    64  				End:   Pos{Line: 4, Column: 3},
    65  				Text:  "12\n345",
    66  			}},
    67  			wantErr: true,
    68  		},
    69  		{
    70  			label:   "out of bounds column",
    71  			content: "ABC\nDEF\nGHI\nJKL",
    72  			edits: []Edit{{
    73  				Start: Pos{Line: 1, Column: 4},
    74  				End:   Pos{Line: 2, Column: 3},
    75  				Text:  "12\n345",
    76  			}},
    77  			wantErr: true,
    78  		},
    79  	}
    80  
    81  	for _, test := range tests {
    82  		test := test
    83  		t.Run(test.label, func(t *testing.T) {
    84  			lines := strings.Split(test.content, "\n")
    85  			newLines, err := editContent(lines, test.edits)
    86  			if (err != nil) != test.wantErr {
    87  				t.Errorf("got err %v, want error: %t", err, test.wantErr)
    88  			}
    89  			if err != nil {
    90  				return
    91  			}
    92  			if got := strings.Join(newLines, "\n"); got != test.want {
    93  				t.Errorf("got %q, want %q", got, test.want)
    94  			}
    95  		})
    96  	}
    97  }