github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/lsprpc/commandinterceptor_test.go (about)

     1  // Copyright 2021 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 lsprpc_test
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/powerman/golang-tools/internal/lsp/protocol"
    12  
    13  	. "github.com/powerman/golang-tools/internal/lsp/lsprpc"
    14  )
    15  
    16  func TestCommandInterceptor(t *testing.T) {
    17  	const command = "foo"
    18  	caught := false
    19  	intercept := func(_ *protocol.ExecuteCommandParams) (interface{}, error) {
    20  		caught = true
    21  		return map[string]interface{}{}, nil
    22  	}
    23  
    24  	ctx := context.Background()
    25  	env := new(TestEnv)
    26  	defer env.Shutdown(t)
    27  	mw := CommandInterceptor(command, intercept)
    28  	l, _ := env.serve(ctx, t, mw(noopBinder))
    29  	conn := env.dial(ctx, t, l.Dialer(), noopBinder, false)
    30  
    31  	params := &protocol.ExecuteCommandParams{
    32  		Command: command,
    33  	}
    34  	var res interface{}
    35  	err := conn.Call(ctx, "workspace/executeCommand", params).Await(ctx, &res)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	if !caught {
    40  		t.Errorf("workspace/executeCommand was not intercepted")
    41  	}
    42  }