github.com/v2fly/tools@v0.100.0/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/v2fly/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  			completedWork:   make(map[string]uint64),
    20  		},
    21  	}
    22  	ctx := context.Background()
    23  	if err := e.onWorkDoneProgressCreate(ctx, &protocol.WorkDoneProgressCreateParams{
    24  		Token: "foo",
    25  	}); err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	if err := e.onWorkDoneProgressCreate(ctx, &protocol.WorkDoneProgressCreateParams{
    29  		Token: "bar",
    30  	}); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	updates := []struct {
    34  		token string
    35  		value interface{}
    36  	}{
    37  		{"foo", protocol.WorkDoneProgressBegin{Kind: "begin", Title: "foo work"}},
    38  		{"bar", protocol.WorkDoneProgressBegin{Kind: "begin", Title: "bar work"}},
    39  		{"foo", protocol.WorkDoneProgressEnd{Kind: "end"}},
    40  		{"bar", protocol.WorkDoneProgressReport{Kind: "report", Percentage: 42}},
    41  	}
    42  	for _, update := range updates {
    43  		params := &protocol.ProgressParams{
    44  			Token: update.token,
    45  			Value: update.value,
    46  		}
    47  		data, err := json.Marshal(params)
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  		var unmarshaled protocol.ProgressParams
    52  		if err := json.Unmarshal(data, &unmarshaled); err != nil {
    53  			t.Fatal(err)
    54  		}
    55  		if err := e.onProgress(ctx, &unmarshaled); err != nil {
    56  			t.Fatal(err)
    57  		}
    58  	}
    59  	if _, ok := e.state.outstandingWork["foo"]; ok {
    60  		t.Error("got work entry for \"foo\", want none")
    61  	}
    62  	got := *e.state.outstandingWork["bar"]
    63  	want := workProgress{title: "bar work", percent: 42}
    64  	if got != want {
    65  		t.Errorf("work progress for \"bar\": %v, want %v", got, want)
    66  	}
    67  }