-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgodoc_test.go
More file actions
941 lines (848 loc) · 23.7 KB
/
Copy pathgodoc_test.go
File metadata and controls
941 lines (848 loc) · 23.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
package godoc_test
import (
"context"
"errors"
"strings"
"testing"
"time"
"go.dw1.io/godoc"
)
type testGodoc struct {
godoc.Godoc
}
func newTestGodoc(opts ...godoc.Option) testGodoc {
return testGodoc{Godoc: godoc.New(opts...)}
}
func (g testGodoc) Load(importPath, sel, version string) (godoc.Result, error) {
return g.Godoc.Load(importPath, sel, version)
}
func TestNew(t *testing.T) {
g := godoc.New(godoc.WithGOOS("linux"), godoc.WithGOARCH("amd64"), godoc.WithWorkdir("/tmp"))
// Fields are unexported, so we can't check them directly
// But we can verify the instance was created
_ = g
}
func TestNewDefaultDir(t *testing.T) {
g := godoc.New()
// Fields are unexported, so we can't check workdir directly
// But we can verify the instance was created with default
_ = g
}
func TestLoadPackage(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "", "")
if err != nil {
t.Fatalf("Failed to load fmt: %v", err)
}
pkgDoc, ok := result.(godoc.PackageDoc)
if !ok {
t.Errorf("Expected PackageDoc, got %T", result)
}
if pkgDoc.ImportPath != "fmt" {
t.Errorf("Expected ImportPath fmt, got %s", pkgDoc.ImportPath)
}
if pkgDoc.Name != "fmt" {
t.Errorf("Expected Name fmt, got %s", pkgDoc.Name)
}
if pkgDoc.DocText == "" {
t.Errorf("Expected non-empty DocText")
}
}
func TestLoadSymbolFunction(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "Printf", "")
if err != nil {
t.Fatalf("Failed to load fmt.Printf: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
if symDoc.Kind != "func" {
t.Errorf("Expected Kind func, got %s", symDoc.Kind)
}
if symDoc.Name != "Printf" {
t.Errorf("Expected Name Printf, got %s", symDoc.Name)
}
if symDoc.Package != "fmt" {
t.Errorf("Expected Package fmt, got %s", symDoc.Package)
}
if symDoc.FuncDoc == nil {
t.Fatalf("Expected FuncDoc for function symbol")
}
if len(symDoc.Returns) < 2 {
t.Errorf("Expected Printf to have at least two return values, got %d", len(symDoc.Returns))
} else {
if symDoc.Returns[0].Type != "int" {
t.Errorf("Expected first return type int, got %s", symDoc.Returns[0].Type)
}
if symDoc.Returns[1].Type != "error" {
t.Errorf("Expected second return type error, got %s", symDoc.Returns[1].Type)
}
}
}
func TestLoadSymbolType(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "Stringer", "")
if err != nil {
t.Fatalf("Failed to load fmt.Stringer: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
if symDoc.Kind != "type" {
t.Errorf("Expected Kind type, got %s", symDoc.Kind)
}
if symDoc.Name != "Stringer" {
t.Errorf("Expected Name Stringer, got %s", symDoc.Name)
}
if symDoc.TypeDoc == nil {
t.Fatalf("Expected TypeDoc for type symbol")
}
if symDoc.TypeDoc.Name != "Stringer" {
t.Errorf("Expected embedded TypeDoc name Stringer, got %s", symDoc.TypeDoc.Name)
}
if len(symDoc.Methods) == 0 {
t.Fatalf("Expected TypeDoc methods for Stringer")
}
if symDoc.Methods[0].Name != "String" {
t.Errorf("Expected first method String, got %s", symDoc.Methods[0].Name)
}
}
func TestLoadSymbolMethod(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("net/http", "Request.ParseForm", "")
if err != nil {
t.Fatalf("Failed to load net/http.Request.ParseForm: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
if symDoc.Kind != "method" {
t.Errorf("Expected Kind method, got %s", symDoc.Kind)
}
if symDoc.Name != "ParseForm" {
t.Errorf("Expected Name ParseForm, got %s", symDoc.Name)
}
if symDoc.Receiver != "Request" {
t.Errorf("Expected Receiver Request, got %s", symDoc.Receiver)
}
if symDoc.FuncDoc == nil {
t.Fatalf("Expected FuncDoc for method symbol")
}
if len(symDoc.Returns) != 1 {
t.Errorf("Expected ParseForm to return single value, got %d", len(symDoc.Returns))
} else if symDoc.Returns[0].Type != "error" {
t.Errorf("Expected ParseForm to return error, got %s", symDoc.Returns[0].Type)
}
}
func TestLoadSymbolConst(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("net/http", "StatusOK", "")
if err != nil {
t.Fatalf("Failed to load net/http.StatusOK: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
if symDoc.Kind != "const" {
t.Errorf("Expected Kind const, got %s", symDoc.Kind)
}
if symDoc.Name != "StatusOK" {
t.Errorf("Expected Name StatusOK, got %s", symDoc.Name)
}
}
func TestLoadInvalidPackage(t *testing.T) {
g := godoc.New()
_, err := g.Load("invalid/package", "", "")
if err == nil {
t.Errorf("Expected error for invalid package")
}
}
func TestLoadNonExistentSymbol(t *testing.T) {
g := godoc.New()
_, err := g.Load("fmt", "NonExistent", "")
if err == nil {
t.Errorf("Expected error for non-existent symbol")
}
}
func TestLoadEmptyImportPath(t *testing.T) {
g := godoc.New()
_, err := g.Load("", "", "")
if err == nil {
t.Errorf("Expected error for empty import path")
}
}
func TestLoadSymbolWithDot(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("os", "File.Read", "")
if err != nil {
t.Fatalf("Failed to load os.File.Read: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
if symDoc.Kind != "method" {
t.Errorf("Expected Kind method, got %s", symDoc.Kind)
}
if symDoc.Name != "Read" {
t.Errorf("Expected Name Read, got %s", symDoc.Name)
}
if symDoc.Receiver != "File" {
t.Errorf("Expected Receiver File, got %s", symDoc.Receiver)
}
}
func TestStructFieldsIncluded(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("net/http", "", "")
if err != nil {
t.Fatalf("Failed to load net/http: %v", err)
}
pkgDoc, ok := result.(godoc.PackageDoc)
if !ok {
t.Fatalf("Expected PackageDoc, got %T", result)
}
var requestDoc *godoc.TypeDoc
for i := range pkgDoc.Types {
if pkgDoc.Types[i].Name == "Request" {
requestDoc = &pkgDoc.Types[i]
break
}
}
if requestDoc == nil {
t.Fatalf("Expected Request type in net/http")
}
if len(requestDoc.Fields) == 0 {
t.Fatalf("Expected struct fields for net/http.Request")
}
var methodField *godoc.FieldDoc
for i := range requestDoc.Fields {
if requestDoc.Fields[i].Name == "Method" {
methodField = &requestDoc.Fields[i]
break
}
}
if methodField == nil {
t.Fatalf("Expected Method field on net/http.Request")
}
if methodField.Type != "string" {
t.Fatalf("Expected Method field type string, got %s", methodField.Type)
}
}
func TestExtractArgs(t *testing.T) {
// This is internal, but we can test via Load
g := newTestGodoc()
result, err := g.Load("fmt", "Sprintf", "")
if err != nil {
t.Fatalf("Failed to load fmt.Sprintf: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
if symDoc.FuncDoc == nil {
t.Fatalf("Expected FuncDoc for Sprintf")
}
if len(symDoc.Args) < 1 {
t.Errorf("Expected at least one arg for Sprintf")
}
// Check first arg is format string
if symDoc.Args[0].Type != "string" {
t.Errorf("Expected first arg type string, got %s", symDoc.Args[0].Type)
}
if len(symDoc.Returns) != 1 {
t.Errorf("Expected Sprintf to return one value, got %d", len(symDoc.Returns))
} else if symDoc.Returns[0].Type != "string" {
t.Errorf("Expected Sprintf return type string, got %s", symDoc.Returns[0].Type)
}
}
func TestPackageDocJSON(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "", "")
if err != nil {
t.Fatalf("Failed to load fmt: %v", err)
}
if _, ok := result.(godoc.PackageDoc); !ok {
t.Errorf("Expected PackageDoc, got %T", result)
}
data, err := result.MarshalJSON()
if err != nil {
t.Errorf("Failed to marshal PackageDoc via Result.MarshalJSON: %v", err)
}
if len(data) == 0 {
t.Errorf("Expected non-empty JSON")
}
}
func TestSymbolDocJSON(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "Printf", "")
if err != nil {
t.Fatalf("Failed to load fmt.Printf: %v", err)
}
if _, ok := result.(godoc.SymbolDoc); !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
data, err := result.MarshalJSON()
if err != nil {
t.Errorf("Failed to marshal SymbolDoc via Result.MarshalJSON: %v", err)
}
if len(data) == 0 {
t.Errorf("Expected non-empty JSON")
}
}
func TestTextAndHTML(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "", "")
if err != nil {
t.Fatalf("Failed to load fmt: %v", err)
}
text := result.Text()
html := result.HTML()
if text == "" {
t.Errorf("Expected non-empty Text")
}
if html == "" {
t.Errorf("Expected non-empty HTML")
}
}
func TestLoadWithVersion(t *testing.T) {
g := newTestGodoc()
// This might require a remote package, but for test, use local
result, err := g.Load("fmt", "", "v1.21.0")
if err != nil {
t.Fatalf("Failed to load fmt with version: %v", err)
}
// Just check it loads
_ = result
}
func TestLoadRemotePackage(t *testing.T) {
g := newTestGodoc()
// Use a small remote package
result, err := g.Load("github.com/stretchr/testify/assert", "", "")
if err != nil {
t.Skipf("Skipping remote test: %v", err)
}
pkgDoc, ok := result.(godoc.PackageDoc)
if !ok {
t.Errorf("Expected PackageDoc, got %T", result)
}
if !strings.Contains(pkgDoc.ImportPath, "testify") {
t.Errorf("Expected ImportPath to contain testify, got %s", pkgDoc.ImportPath)
}
}
func TestLoadSymbolFromRemote(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("github.com/stretchr/testify/assert", "Equal", "")
if err != nil {
t.Skipf("Skipping remote test: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
if symDoc.Name != "Equal" {
t.Errorf("Expected Name Equal, got %s", symDoc.Name)
}
}
func TestValidateInputs(t *testing.T) {
// Internal function, test via Load
g := godoc.New()
_, err := g.Load("", "symbol", "")
if err == nil {
t.Errorf("Expected error for empty import path")
}
}
func TestLoadCurrentPackage(t *testing.T) {
g := newTestGodoc()
result, err := g.Load(".", "", "")
if err != nil {
t.Fatalf("Failed to load current package: %v", err)
}
pkgDoc, ok := result.(godoc.PackageDoc)
if !ok {
t.Errorf("Expected PackageDoc, got %T", result)
}
if pkgDoc.Name == "" {
t.Errorf("Expected non-empty package name")
}
}
func TestCacheFunctionality(t *testing.T) {
g := newTestGodoc()
// First load
startFirst := time.Now()
result1, err1 := g.Load("fmt", "", "")
firstDuration := time.Since(startFirst)
if err1 != nil {
t.Fatalf("Failed first load: %v", err1)
}
// Second load (should use cache)
startSecond := time.Now()
result2, err2 := g.Load("fmt", "", "")
secondDuration := time.Since(startSecond)
if err2 != nil {
t.Fatalf("Failed second load: %v", err2)
}
// Results should be identical
pkgDoc1, ok1 := result1.(godoc.PackageDoc)
pkgDoc2, ok2 := result2.(godoc.PackageDoc)
if !ok1 || !ok2 {
t.Errorf("Expected PackageDoc for both results")
}
if pkgDoc1.ImportPath != pkgDoc2.ImportPath {
t.Errorf("Cached result differs: %s vs %s", pkgDoc1.ImportPath, pkgDoc2.ImportPath)
}
delta := firstDuration - secondDuration
t.Logf("cache load timings: cold=%s warm=%s delta=%s", firstDuration, secondDuration, delta)
}
func TestCacheWithVersion(t *testing.T) {
g := newTestGodoc()
// Load with version
startFirst := time.Now()
result1, err1 := g.Load("fmt", "", "latest")
firstDuration := time.Since(startFirst)
if err1 != nil {
t.Fatalf("Failed load with version: %v", err1)
}
// Load again with same version (should use cache)
startSecond := time.Now()
result2, err2 := g.Load("fmt", "", "latest")
secondDuration := time.Since(startSecond)
if err2 != nil {
t.Fatalf("Failed second load with version: %v", err2)
}
// Results should be identical
pkgDoc1, ok1 := result1.(godoc.PackageDoc)
pkgDoc2, ok2 := result2.(godoc.PackageDoc)
if !ok1 || !ok2 {
t.Errorf("Expected PackageDoc for both results")
}
if pkgDoc1.DocText != pkgDoc2.DocText {
t.Errorf("Cached result with version differs")
}
delta := firstDuration - secondDuration
t.Logf("cache load timings (versioned): cold=%s warm=%s delta=%s", firstDuration, secondDuration, delta)
}
func TestStdLibWithGoVersion(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("os", "", "")
if err != nil {
t.Fatalf("Failed to load os: %v", err)
}
pkgDoc, ok := result.(godoc.PackageDoc)
if !ok {
t.Errorf("Expected PackageDoc, got %T", result)
}
if pkgDoc.ImportPath != "os" {
t.Errorf("Expected ImportPath os, got %s", pkgDoc.ImportPath)
}
// Verify it has documentation
if pkgDoc.DocText == "" {
t.Errorf("Expected non-empty DocText for std lib")
}
}
func TestLoadWithGOOSGOARCH(t *testing.T) {
g := newTestGodoc(godoc.WithGOOS("linux"), godoc.WithGOARCH("amd64"))
result, err := g.Load("runtime", "", "")
if err != nil {
t.Fatalf("Failed to load runtime with GOOS/GOARCH: %v", err)
}
pkgDoc, ok := result.(godoc.PackageDoc)
if !ok {
t.Errorf("Expected PackageDoc, got %T", result)
}
if pkgDoc.Name != "runtime" {
t.Errorf("Expected Name runtime, got %s", pkgDoc.Name)
}
}
func TestLoadSymbolVar(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("os", "Args", "")
if err != nil {
t.Fatalf("Failed to load os.Args: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
if symDoc.Kind != "var" {
t.Errorf("Expected Kind var, got %s", symDoc.Kind)
}
if symDoc.Name != "Args" {
t.Errorf("Expected Name Args, got %s", symDoc.Name)
}
}
func TestLoadComplexSymbol(t *testing.T) {
g := newTestGodoc()
// Test loading a method from a complex type
result, err := g.Load("net/http", "Client.Do", "")
if err != nil {
t.Fatalf("Failed to load net/http.Client.Do: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
if symDoc.Kind != "method" {
t.Errorf("Expected Kind method, got %s", symDoc.Kind)
}
if symDoc.Name != "Do" {
t.Errorf("Expected Name Do, got %s", symDoc.Name)
}
if symDoc.Receiver != "Client" {
t.Errorf("Expected Receiver Client, got %s", symDoc.Receiver)
}
}
func TestLoadInvalidSymbolFormat(t *testing.T) {
g := godoc.New()
_, err := g.Load("fmt", "Printf.", "")
if err == nil {
t.Errorf("Expected error for invalid symbol format")
}
}
func TestLoadEmptySymbol(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "", "")
if err != nil {
t.Fatalf("Failed to load fmt with empty symbol: %v", err)
}
// Should return PackageDoc when symbol is empty
_, ok := result.(godoc.PackageDoc)
if !ok {
t.Errorf("Expected PackageDoc for empty symbol, got %T", result)
}
}
func TestCacheDifferentVersions(t *testing.T) {
g := newTestGodoc()
// Load with empty version
result1, err1 := g.Load("fmt", "", "")
if err1 != nil {
t.Fatalf("Failed load with empty version: %v", err1)
}
// Load with specific version (if fmt supports it)
result2, err2 := g.Load("fmt", "", "latest")
if err2 != nil {
t.Fatalf("Failed load with specific version: %v", err2)
}
// Results should be the same for std lib
pkgDoc1, ok1 := result1.(godoc.PackageDoc)
pkgDoc2, ok2 := result2.(godoc.PackageDoc)
if !ok1 || !ok2 {
t.Errorf("Expected PackageDoc for both results")
}
if pkgDoc1.ImportPath != pkgDoc2.ImportPath {
t.Errorf("Results should be the same for std lib: %s vs %s", pkgDoc1.ImportPath, pkgDoc2.ImportPath)
}
}
func TestRemotePackageCaching(t *testing.T) {
g := newTestGodoc()
// First load of remote package
result1, err1 := g.Load("github.com/stretchr/testify/assert", "", "")
if err1 != nil {
t.Skipf("Skipping remote caching test: %v", err1)
}
// Second load (should use cache)
result2, err2 := g.Load("github.com/stretchr/testify/assert", "", "")
if err2 != nil {
t.Fatalf("Failed second remote load: %v", err2)
}
// Results should be identical
pkgDoc1, ok1 := result1.(godoc.PackageDoc)
pkgDoc2, ok2 := result2.(godoc.PackageDoc)
if !ok1 || !ok2 {
t.Errorf("Expected PackageDoc for both results")
}
if pkgDoc1.Name != pkgDoc2.Name {
t.Errorf("Cached remote result differs: %s vs %s", pkgDoc1.Name, pkgDoc2.Name)
}
}
func TestLoadWithCustomDir(t *testing.T) {
g := newTestGodoc(godoc.WithWorkdir("."))
result, err := g.Load("fmt", "", "")
if err != nil {
t.Fatalf("Failed to load fmt with custom dir: %v", err)
}
pkgDoc, ok := result.(godoc.PackageDoc)
if !ok {
t.Errorf("Expected PackageDoc, got %T", result)
}
if pkgDoc.ImportPath != "fmt" {
t.Errorf("Expected ImportPath fmt, got %s", pkgDoc.ImportPath)
}
}
func TestSymbolDocArgsVariadic(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "Printf", "")
if err != nil {
t.Fatalf("Failed to load fmt.Printf: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
// Printf has variadic args
if symDoc.FuncDoc == nil {
t.Fatalf("Expected FuncDoc for Printf")
}
if len(symDoc.Args) < 2 {
t.Errorf("Expected at least 2 args for Printf")
}
// Check if any arg is variadic (starts with ...)
hasVariadic := false
for _, arg := range symDoc.Args {
if strings.HasPrefix(arg.Type, "...") {
hasVariadic = true
break
}
}
if !hasVariadic {
t.Errorf("Expected variadic args for Printf")
}
}
func TestSymbolDocGenericArgs(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("slices", "Insert", "")
if err != nil {
t.Fatalf("Failed to load slices.Insert: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Fatalf("Expected SymbolDoc, got %T", result)
}
if symDoc.FuncDoc == nil {
t.Fatalf("Expected FuncDoc for slices.Insert")
}
if len(symDoc.Args) != 3 {
t.Fatalf("Expected 3 args for slices.Insert, got %d", len(symDoc.Args))
}
if symDoc.Args[0].Type != "S" {
t.Errorf("expected first argument type S, got %q", symDoc.Args[0].Type)
}
if symDoc.Args[1].Type != "int" {
t.Errorf("expected second argument type int, got %q", symDoc.Args[1].Type)
}
if symDoc.Args[2].Type != "...E" {
t.Errorf("expected third argument type ...E, got %q", symDoc.Args[2].Type)
}
}
func TestPackageDocSynopsis(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "", "")
if err != nil {
t.Fatalf("Failed to load fmt: %v", err)
}
pkgDoc, ok := result.(godoc.PackageDoc)
if !ok {
t.Errorf("Expected PackageDoc, got %T", result)
}
if pkgDoc.Synopsis == "" {
t.Errorf("Expected non-empty Synopsis")
}
}
func TestLoadNonExistentRemotePackage(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
g := godoc.New(
godoc.WithContext(ctx),
)
_, err := g.Load("github.com/nonexistent/package/that/does/not/exist", "", "")
if err == nil {
t.Errorf("Expected error for non-existent remote package")
}
}
func TestSymbolDocHTML(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "Printf", "")
if err != nil {
t.Fatalf("Failed to load fmt.Printf: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
// Test HTML generation
html := symDoc.HTML()
if html == "" {
t.Errorf("Expected non-empty HTML")
}
// Test that HTML is cached (call again)
html2 := symDoc.HTML()
if html != html2 {
t.Errorf("HTML should be cached and identical")
}
}
func TestSymbolDocText(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "Printf", "")
if err != nil {
t.Fatalf("Failed to load fmt.Printf: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
// Test Text method
text := symDoc.Text()
if text == "" {
t.Errorf("Expected non-empty text from SymbolDoc.Text()")
}
// Verify it matches the DocText field
if text != symDoc.DocText {
t.Errorf("Text() should return DocText field")
}
}
func TestSymbolDocTextAndHTML(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("fmt", "Stringer", "")
if err != nil {
t.Fatalf("Failed to load fmt.Stringer: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
// Test both Text and HTML methods
text := symDoc.Text()
html := symDoc.HTML()
if text == "" {
t.Errorf("Expected non-empty text")
}
if html == "" {
t.Errorf("Expected non-empty HTML")
}
// Test that HTML is generated and cached
html2 := symDoc.HTML()
if html != html2 {
t.Errorf("HTML should be cached and identical on second call")
}
}
func TestLocalModuleCaching(t *testing.T) {
g := newTestGodoc()
// Load current package multiple times to test local caching
result1, err1 := g.Load(".", "", "")
if err1 != nil {
t.Fatalf("Failed first load of current package: %v", err1)
}
result2, err2 := g.Load(".", "", "")
if err2 != nil {
t.Fatalf("Failed second load of current package: %v", err2)
}
// Results should be identical
pkgDoc1, ok1 := result1.(godoc.PackageDoc)
pkgDoc2, ok2 := result2.(godoc.PackageDoc)
if !ok1 || !ok2 {
t.Errorf("Expected PackageDoc for both results")
}
if pkgDoc1.Name != pkgDoc2.Name {
t.Errorf("Cached local result differs: %s vs %s", pkgDoc1.Name, pkgDoc2.Name)
}
}
func TestLoadWithDifferentGOOSGOARCH(t *testing.T) {
g := newTestGodoc(godoc.WithGOOS("windows"), godoc.WithGOARCH("amd64"))
result, err := g.Load("runtime", "", "")
if err != nil {
t.Fatalf("Failed to load runtime with different GOOS/GOARCH: %v", err)
}
pkgDoc, ok := result.(godoc.PackageDoc)
if !ok {
t.Errorf("Expected PackageDoc, got %T", result)
}
if pkgDoc.Name != "runtime" {
t.Errorf("Expected Name runtime, got %s", pkgDoc.Name)
}
}
func TestCacheConcurrency(t *testing.T) {
g := newTestGodoc()
// Test concurrent access to cache
done := make(chan bool, 10)
for range 10 {
go func() {
result, err := g.Load("fmt", "", "")
if err != nil {
t.Errorf("Concurrent load failed: %v", err)
done <- false
return
}
_ = result
done <- true
}()
}
// Wait for all goroutines
for i := 0; i < 10; i++ {
if !<-done {
t.Errorf("One of the concurrent loads failed")
}
}
}
func TestLoadSymbolFromComplexPackage(t *testing.T) {
g := newTestGodoc()
// Test loading from a package with many symbols
result, err := g.Load("net/http", "Server", "")
if err != nil {
t.Fatalf("Failed to load net/http.Server: %v", err)
}
symDoc, ok := result.(godoc.SymbolDoc)
if !ok {
t.Errorf("Expected SymbolDoc, got %T", result)
}
if symDoc.Kind != "type" {
t.Errorf("Expected Kind type, got %s", symDoc.Kind)
}
if symDoc.Name != "Server" {
t.Errorf("Expected Name Server, got %s", symDoc.Name)
}
}
func TestPackageDocTypesMethods(t *testing.T) {
g := newTestGodoc()
result, err := g.Load("net/http", "", "")
if err != nil {
t.Fatalf("Failed to load net/http: %v", err)
}
pkgDoc, ok := result.(godoc.PackageDoc)
if !ok {
t.Errorf("Expected PackageDoc, got %T", result)
}
// Check that we have types with methods
hasTypeWithMethods := false
for _, typ := range pkgDoc.Types {
if len(typ.Methods) > 0 {
hasTypeWithMethods = true
break
}
}
if !hasTypeWithMethods {
t.Errorf("Expected at least one type with methods")
}
}
func TestLoadWithWhitespaceVersion(t *testing.T) {
g := newTestGodoc()
// Test version with whitespace
result, err := g.Load("fmt", "", " v1.21.0 ")
if err != nil {
t.Fatalf("Failed to load with whitespace version: %v", err)
}
_ = result
}
func TestLoadImportPathParentTraversal(t *testing.T) {
g := godoc.New()
_, err := g.Load("../fmt", "", "")
if err == nil {
t.Fatalf("expected error for parent traversal import path")
}
if !errors.Is(err, godoc.ErrInvalidImportPath) {
t.Fatalf("expected ErrInvalidImportPath, got %v", err)
}
}
func TestLoadImportPathAbsolute(t *testing.T) {
g := godoc.New()
_, err := g.Load("/fmt", "", "")
if err == nil {
t.Fatalf("expected error for absolute import path")
}
if !errors.Is(err, godoc.ErrInvalidImportPath) {
t.Fatalf("expected ErrInvalidImportPath, got %v", err)
}
}