github.com/zmb3/gogetdoc@v0.0.0-20190228002656-b37376c5da6a/ident_test.go (about)

     1  package main
     2  
     3  import (
     4  	"go/token"
     5  	"path/filepath"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  
    10  	"golang.org/x/tools/go/packages/packagestest"
    11  )
    12  
    13  func TestIdent(t *testing.T) {
    14  	dir := filepath.Join(".", "testdata", "package")
    15  	mods := []packagestest.Module{
    16  		{Name: "somepkg", Files: packagestest.MustCopyFileTree(dir)},
    17  	}
    18  
    19  	packagestest.TestAll(t, func(t *testing.T, exporter packagestest.Exporter) {
    20  		if exporter == packagestest.Modules && !modulesSupported() {
    21  			t.Skip("Skipping modules test on", runtime.Version())
    22  		}
    23  		exported := packagestest.Export(t, exporter, mods)
    24  		defer exported.Cleanup()
    25  
    26  		teardown := setup(exported.Config)
    27  		defer teardown()
    28  
    29  		getDoc := func(p token.Position) *Doc {
    30  			t.Helper()
    31  			doc, docErr := Run(p.Filename, p.Offset, nil)
    32  			if docErr != nil {
    33  				t.Fatal(docErr)
    34  			}
    35  			return doc
    36  		}
    37  
    38  		pcmp := func(want, got string) {
    39  			t.Helper()
    40  			if !strings.HasPrefix(got, want) {
    41  				if len(got) > 64 {
    42  					got = got[:64]
    43  				}
    44  				t.Errorf("expected prefix %q in %q", want, got)
    45  			}
    46  		}
    47  
    48  		cmp := func(want, got string) {
    49  			t.Helper()
    50  			if got != want {
    51  				t.Errorf("want %q, got %q", want, got)
    52  			}
    53  		}
    54  
    55  		if expectErr := exported.Expect(map[string]interface{}{
    56  			"doc":  func(p token.Position, doc string) { pcmp(doc, getDoc(p).Doc) },
    57  			"pkg":  func(p token.Position, pkg string) { cmp(pkg, getDoc(p).Pkg) },
    58  			"decl": func(p token.Position, decl string) { cmp(decl, getDoc(p).Decl) },
    59  			"const": func(p token.Position, val string) {
    60  				d := getDoc(p)
    61  				needle := "Constant Value: " + val
    62  				if !strings.Contains(d.Doc, needle) {
    63  					t.Errorf("Expected %q in %q", needle, d.Doc)
    64  				}
    65  			},
    66  			"exported": func(p token.Position) {
    67  				for _, showUnexported := range []bool{true, false} {
    68  					*showUnexportedFields = showUnexported
    69  					d := getDoc(p)
    70  					hasUnexportedField := strings.Contains(d.Decl, "notVisible")
    71  					if hasUnexportedField != *showUnexportedFields {
    72  						t.Errorf("show unexported fields is %v, but got %q", showUnexported, d.Decl)
    73  					}
    74  				}
    75  			},
    76  		}); expectErr != nil {
    77  			t.Fatal(expectErr)
    78  		}
    79  	})
    80  }
    81  
    82  func TestVendoredCode(t *testing.T) {
    83  	dir := filepath.Join(".", "testdata", "withvendor")
    84  	mods := []packagestest.Module{
    85  		{Name: "main", Files: packagestest.MustCopyFileTree(dir)},
    86  	}
    87  
    88  	exported := packagestest.Export(t, packagestest.GOPATH, mods)
    89  	defer exported.Cleanup()
    90  
    91  	teardown := setup(exported.Config)
    92  	defer teardown()
    93  
    94  	filename := exported.File("main", "main.go")
    95  	getDoc := func(p token.Position) *Doc {
    96  		t.Helper()
    97  		doc, docErr := Run(filename, p.Offset, nil)
    98  		if docErr != nil {
    99  			t.Fatal(docErr)
   100  		}
   101  		return doc
   102  	}
   103  
   104  	compare := func(want, got string) {
   105  		if want != got {
   106  			t.Errorf("want %q, got %q", want, got)
   107  		}
   108  	}
   109  
   110  	if expectErr := exported.Expect(map[string]interface{}{
   111  		"import": func(p token.Position, path string) { compare(path, getDoc(p).Import) },
   112  		"decl":   func(p token.Position, decl string) { compare(decl, getDoc(p).Decl) },
   113  		"doc":    func(p token.Position, doc string) { compare(doc, getDoc(p).Doc) },
   114  	}); expectErr != nil {
   115  		t.Fatal(expectErr)
   116  	}
   117  }