github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/regtest/env_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 regtest
     6  
     7  import (
     8  	"context"
     9  	"encoding/json"
    10  	"testing"
    11  
    12  	"github.com/powerman/golang-tools/internal/lsp/protocol"
    13  )
    14  
    15  func TestProgressUpdating(t *testing.T) {
    16  	e := &Env{
    17  		state: State{
    18  			outstandingWork: make(map[protocol.ProgressToken]*workProgress),
    19  			startedWork:     make(map[string]uint64),
    20  			completedWork:   make(map[string]uint64),
    21  		},
    22  	}
    23  	ctx := context.Background()
    24  	if err := e.onWorkDoneProgressCreate(ctx, &protocol.WorkDoneProgressCreateParams{
    25  		Token: "foo",
    26  	}); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	if err := e.onWorkDoneProgressCreate(ctx, &protocol.WorkDoneProgressCreateParams{
    30  		Token: "bar",
    31  	}); err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	updates := []struct {
    35  		token string
    36  		value interface{}
    37  	}{
    38  		{"foo", protocol.WorkDoneProgressBegin{Kind: "begin", Title: "foo work"}},
    39  		{"bar", protocol.WorkDoneProgressBegin{Kind: "begin", Title: "bar work"}},
    40  		{"foo", protocol.WorkDoneProgressEnd{Kind: "end"}},
    41  		{"bar", protocol.WorkDoneProgressReport{Kind: "report", Percentage: 42}},
    42  	}
    43  	for _, update := range updates {
    44  		params := &protocol.ProgressParams{
    45  			Token: update.token,
    46  			Value: update.value,
    47  		}
    48  		data, err := json.Marshal(params)
    49  		if err != nil {
    50  			t.Fatal(err)
    51  		}
    52  		var unmarshaled protocol.ProgressParams
    53  		if err := json.Unmarshal(data, &unmarshaled); err != nil {
    54  			t.Fatal(err)
    55  		}
    56  		if err := e.onProgress(ctx, &unmarshaled); err != nil {
    57  			t.Fatal(err)
    58  		}
    59  	}
    60  	if _, ok := e.state.outstandingWork["foo"]; ok {
    61  		t.Error("got work entry for \"foo\", want none")
    62  	}
    63  	got := *e.state.outstandingWork["bar"]
    64  	want := workProgress{title: "bar work", percent: 42}
    65  	if got != want {
    66  		t.Errorf("work progress for \"bar\": %v, want %v", got, want)
    67  	}
    68  }