golang.org/x/tools/gopls@v0.15.3/internal/golang/completion/snippet/snippet_builder_test.go (about)

     1  // Copyright 2019 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 snippet
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestSnippetBuilder(t *testing.T) {
    12  	expect := func(expected string, fn func(*Builder)) {
    13  		t.Helper()
    14  
    15  		var b Builder
    16  		fn(&b)
    17  		if got := b.String(); got != expected {
    18  			t.Errorf("got %q, expected %q", got, expected)
    19  		}
    20  	}
    21  
    22  	expect("", func(b *Builder) {})
    23  
    24  	expect(`hi { \} \$ | " , / \\`, func(b *Builder) {
    25  		b.WriteText(`hi { } $ | " , / \`)
    26  	})
    27  
    28  	expect("${1:}", func(b *Builder) {
    29  		b.WritePlaceholder(nil)
    30  	})
    31  
    32  	expect("hi ${1:there}", func(b *Builder) {
    33  		b.WriteText("hi ")
    34  		b.WritePlaceholder(func(b *Builder) {
    35  			b.WriteText("there")
    36  		})
    37  	})
    38  
    39  	expect(`${1:id=${2:{your id\}}}`, func(b *Builder) {
    40  		b.WritePlaceholder(func(b *Builder) {
    41  			b.WriteText("id=")
    42  			b.WritePlaceholder(func(b *Builder) {
    43  				b.WriteText("{your id}")
    44  			})
    45  		})
    46  	})
    47  
    48  	expect(`${1|one,{ \} \$ \| " \, / \\,three|}`, func(b *Builder) {
    49  		b.WriteChoice([]string{"one", `{ } $ | " , / \`, "three"})
    50  	})
    51  
    52  	expect("$0 hello", func(b *Builder) {
    53  		b.WriteFinalTabstop()
    54  		b.WriteText(" hello")
    55  	})
    56  
    57  	expect(`prepended \$5 ${1:} hello`, func(b *Builder) {
    58  		b.WritePlaceholder(nil)
    59  		b.WriteText(" hello")
    60  		b.PrependText("prepended $5 ")
    61  	})
    62  }