| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package integration |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"golang.org/x/tools/gopls/internal/protocol" |
| 13 |
) |
| 14 |
|
| 15 |
func TestProgressUpdating(t *testing.T) { |
| 16 |
a := &Awaiter{ |
| 17 |
state: State{ |
| 18 |
work: 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 := a.onWorkDoneProgressCreate(ctx, &protocol.WorkDoneProgressCreateParams{ |
| 25 |
Token: "foo", |
| 26 |
}); err != nil { |
| 27 |
t.Fatal(err) |
| 28 |
} |
| 29 |
if err := a.onWorkDoneProgressCreate(ctx, &protocol.WorkDoneProgressCreateParams{ |
| 30 |
Token: "bar", |
| 31 |
}); err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
updates := []struct { |
| 35 |
token string |
| 36 |
value any |
| 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: varOf[uint32](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 := a.onProgress(ctx, &unmarshaled); err != nil { |
| 57 |
t.Fatal(err) |
| 58 |
} |
| 59 |
} |
| 60 |
if got, want := a.state.completedWork["foo work"], uint64(1); got != want { |
| 61 |
t.Errorf(`completedWork["foo work"] = %d, want %d`, got, want) |
| 62 |
} |
| 63 |
got := *a.state.work["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 |
} |
| 69 |
|
| 70 |
|
| 71 |
func varOf[T any](x T) *T { return &x } |