11package lspcmd
22
33import (
4+ "bytes"
45 "context"
56 "fmt"
67 "io"
78 "log/slog"
89 "os"
10+ "strings"
911 "sync"
1012 "testing"
1113 "time"
@@ -26,7 +28,13 @@ func TestCompletion(t *testing.T) {
2628 }
2729
2830 ctx , cancel := context .WithCancel (context .Background ())
29- log := slog .New (slog .NewJSONHandler (os .Stderr , nil ))
31+ testOutput := bytes .NewBuffer (nil )
32+ log := slog .New (slog .NewJSONHandler (testOutput , nil ))
33+ defer func () {
34+ if t .Failed () {
35+ fmt .Println (testOutput .String ())
36+ }
37+ }()
3038
3139 ctx , appDir , _ , server , teardown , err := Setup (ctx , log )
3240 if err != nil {
@@ -169,7 +177,13 @@ func TestHover(t *testing.T) {
169177 }
170178
171179 ctx , cancel := context .WithCancel (context .Background ())
172- log := slog .New (slog .NewJSONHandler (os .Stderr , nil ))
180+ testOutput := bytes .NewBuffer (nil )
181+ log := slog .New (slog .NewJSONHandler (testOutput , nil ))
182+ defer func () {
183+ if t .Failed () {
184+ fmt .Println (testOutput .String ())
185+ }
186+ }()
173187
174188 ctx , appDir , _ , server , teardown , err := Setup (ctx , log )
175189 if err != nil {
@@ -212,14 +226,11 @@ func TestHover(t *testing.T) {
212226 replacement : ` <div data-testid="count">{ fmt.Sprintf("%d", count) }</div>` ,
213227 cursor : ` ^` ,
214228 assert : func (t * testing.T , actual * protocol.Hover ) (msg string , ok bool ) {
215- expectedHover := protocol.Hover {
216- Contents : protocol.MarkupContent {
217- Kind : "markdown" ,
218- Value : "```go\n package fmt\n ```\n \n ---\n \n [`fmt` on pkg.go.dev](https://pkg.go.dev/fmt)" ,
219- },
229+ if actual .Contents .Kind != "markdown" {
230+ return fmt .Sprintf ("expected hover kind to be markdown, got %q" , actual .Contents .Kind ), false
220231 }
221- if diff := lspdiff . Hover ( expectedHover , * actual ); diff != "" {
222- return fmt .Sprintf ("unexpected hover: %v \n \n : markdown: %#v" , diff , actual .Contents .Value ), false
232+ if ! strings . Contains ( actual . Contents . Value , "```go \n package fmt \n ```" ) {
233+ return fmt .Sprintf ("expected hover to contain package fmt, got %q" , actual .Contents .Value ), false
223234 }
224235 return "" , true
225236 },
@@ -229,17 +240,14 @@ func TestHover(t *testing.T) {
229240 replacement : ` <div data-testid="count">{ fmt.Sprintf("%d", count) }</div>` ,
230241 cursor : ` ^` ,
231242 assert : func (t * testing.T , actual * protocol.Hover ) (msg string , ok bool ) {
232- expectedHover := protocol.Hover {
233- Contents : protocol.MarkupContent {
234- Kind : "markdown" ,
235- Value : "```go\n func fmt.Sprintf(format string, a ...any) string\n ```\n \n ---\n \n Sprintf formats according to a format specifier and returns the resulting string.\n \n \n ---\n \n [`fmt.Sprintf` on pkg.go.dev](https://pkg.go.dev/fmt#Sprintf)" ,
236- },
237- }
238243 if actual == nil {
239244 return "expected hover to be non-nil" , false
240245 }
241- if diff := lspdiff .Hover (expectedHover , * actual ); diff != "" {
242- return fmt .Sprintf ("unexpected hover: %v" , diff ), false
246+ if actual .Contents .Kind != "markdown" {
247+ return fmt .Sprintf ("expected hover kind to be markdown, got %q" , actual .Contents .Kind ), false
248+ }
249+ if ! strings .Contains (actual .Contents .Value , "```go\n func fmt.Sprintf(format string, a ...any) string\n ```" ) {
250+ return fmt .Sprintf ("expected hover to contain fmt.Sprintf signature, got %q" , actual .Contents .Value ), false
243251 }
244252 return "" , true
245253 },
@@ -253,15 +261,6 @@ func TestHover(t *testing.T) {
253261 return "" , true
254262 },
255263 },
256- {
257- line : 19 ,
258- replacement : `var nihao = "你好"` ,
259- cursor : ` ^` , // Your text editor might not render this well, but it's the hao.
260- assert : func (t * testing.T , actual * protocol.Hover ) (msg string , ok bool ) {
261- // There's nothing to hover, just want to make sure it doesn't panic.
262- return "" , true
263- },
264- },
265264 }
266265
267266 for i , test := range tests {
@@ -329,7 +328,13 @@ func TestReferences(t *testing.T) {
329328 }
330329
331330 ctx , cancel := context .WithCancel (context .Background ())
332- log := slog .New (slog .NewJSONHandler (os .Stderr , nil ))
331+ testOutput := bytes .NewBuffer (nil )
332+ log := slog .New (slog .NewJSONHandler (testOutput , nil ))
333+ defer func () {
334+ if t .Failed () {
335+ fmt .Println (testOutput .String ())
336+ }
337+ }()
333338
334339 ctx , appDir , _ , server , teardown , err := Setup (ctx , log )
335340 if err != nil {
@@ -490,7 +495,13 @@ func TestCodeAction(t *testing.T) {
490495 }
491496
492497 ctx , cancel := context .WithCancel (context .Background ())
493- log := slog .New (slog .NewJSONHandler (os .Stderr , nil ))
498+ testOutput := bytes .NewBuffer (nil )
499+ log := slog .New (slog .NewJSONHandler (testOutput , nil ))
500+ defer func () {
501+ if t .Failed () {
502+ fmt .Println (testOutput .String ())
503+ }
504+ }()
494505
495506 ctx , appDir , _ , server , teardown , err := Setup (ctx , log )
496507 if err != nil {
@@ -613,7 +624,13 @@ func TestDocumentSymbol(t *testing.T) {
613624 }
614625
615626 ctx , cancel := context .WithCancel (context .Background ())
616- log := slog .New (slog .NewJSONHandler (os .Stderr , nil ))
627+ testOutput := bytes .NewBuffer (nil )
628+ log := slog .New (slog .NewJSONHandler (testOutput , nil ))
629+ defer func () {
630+ if t .Failed () {
631+ fmt .Println (testOutput .String ())
632+ }
633+ }()
617634
618635 ctx , appDir , _ , server , teardown , err := Setup (ctx , log )
619636 if err != nil {
0 commit comments