Skip to content

Commit

Permalink
Merge pull request #1825 from xushiwei/q
Browse files Browse the repository at this point in the history
gop/doc: TestFromTestdata
  • Loading branch information
xushiwei committed Mar 23, 2024
2 parents aea48cf + b2591df commit 0a7dbfe
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 124 deletions.
20 changes: 20 additions & 0 deletions doc/_testdata/gopoFn/in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package foo

const GopPackage = true

const Gopo_Add = "AddInt,,AddString"

// Add doc
func Add__1(a, b float64) float64 {
return 0
}

// AddInt doc
func AddInt(a, b int) int {
return 0
}

// AddString doc
func AddString(a, b string) string {
return ""
}
20 changes: 20 additions & 0 deletions doc/_testdata/gopoFn/out.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
== Func Add ==
Doc: AddInt doc

func Add(a, b int) int
== Func Add ==
Doc: Add doc

func Add(a, b float64) float64
== Func Add ==
Doc: AddString doc

func Add(a, b string) string
== Func AddInt ==
Doc: AddInt doc

func AddInt(a, b int) int
== Func AddString ==
Doc: AddString doc

func AddString(a, b string) string
17 changes: 17 additions & 0 deletions doc/_testdata/gopoMethod/in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package foo

const GopPackage = true

const Gopo__T__Gop_Add = ".AddInt,AddString"

type T int

// AddInt doc
func (p T) AddInt(b int) *T {
return nil
}

// AddString doc
func AddString(this *T, b string) *T {
return nil
}
20 changes: 20 additions & 0 deletions doc/_testdata/gopoMethod/out.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
== Type T ==
- Func AddString -
Doc: AddString doc

func AddString(this *T, b string) *T
- Method AddInt -
Recv: T
Doc: AddInt doc

func (p T) AddInt(b int) *T
- Method Gop_Add -
Recv: T
Doc: AddInt doc

func (p T) Gop_Add(b int) *T
- Method Gop_Add -
Recv: *T
Doc: AddString doc

func (this *T) Gop_Add(b string) *T
7 changes: 7 additions & 0 deletions doc/_testdata/overloadFn/in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

const GopPackage = true

// Bar doc
func Bar__0() {
}
4 changes: 4 additions & 0 deletions doc/_testdata/overloadFn/out.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
== Func Bar ==
Doc: Bar doc

func Bar()
13 changes: 13 additions & 0 deletions doc/_testdata/overloadMethod/in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package foo

const GopPackage = true

type T int

// Bar doc 1
func (p *T) Bar__0() {
}

// Bar doc 2
func (t T) Bar__1() {
}
11 changes: 11 additions & 0 deletions doc/_testdata/overloadMethod/out.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
== Type T ==
- Method Bar -
Recv: *T
Doc: Bar doc 1

func (p *T) Bar()
- Method Bar -
Recv: T
Doc: Bar doc 2

func (t T) Bar()
158 changes: 34 additions & 124 deletions doc/z_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"go/format"
"go/parser"
"go/token"
"os"
"path"
"strings"
"testing"
)
Expand Down Expand Up @@ -61,128 +63,6 @@ func TestDocRecv(t *testing.T) {
}
}

func TestGopoMethod(t *testing.T) {
testPkg(t, `
package foo
const GopPackage = true
const Gopo__T__Gop_Add = ".AddInt,AddString"
type T int
// AddInt doc
func (p T) AddInt(b int) *T {}
// AddString doc
func AddString(this *T, b string) *T {}
`, `== Type T ==
- Func AddString -
Doc: AddString doc
func AddString(this *T, b string) *T
- Method AddInt -
Recv: T
Doc: AddInt doc
func (p T) AddInt(b int) *T
- Method Gop_Add -
Recv: T
Doc: AddInt doc
func (p T) Gop_Add(b int) *T
- Method Gop_Add -
Recv: *T
Doc: AddString doc
func (this *T) Gop_Add(b string) *T
`)
}

func TestGopoFn(t *testing.T) {
testPkg(t, `
package foo
const GopPackage = true
const Gopo_Add = "AddInt,,AddString"
// Add doc
func Add__1(a, b float64) float64 {}
// AddInt doc
func AddInt(a, b int) int {}
// AddString doc
func AddString(a, b string) string {}
`, `== Func Add ==
Doc: AddInt doc
func Add(a, b int) int
== Func Add ==
Doc: Add doc
func Add(a, b float64) float64
== Func Add ==
Doc: AddString doc
func Add(a, b string) string
== Func AddInt ==
Doc: AddInt doc
func AddInt(a, b int) int
== Func AddString ==
Doc: AddString doc
func AddString(a, b string) string
`)
}

func TestOverloadFn(t *testing.T) {
testPkg(t, `
package foo
const GopPackage = true
// Bar doc
func Bar__0() {
}
`, `== Func Bar ==
Doc: Bar doc
func Bar()
`)
}

func TestOverloadMethod(t *testing.T) {
testPkg(t, `
package foo
const GopPackage = true
type T int
// Bar doc 1
func (p *T) Bar__0() {
}
// Bar doc 2
func (t T) Bar__1() {
}
`, `== Type T ==
- Method Bar -
Recv: *T
Doc: Bar doc 1
func (p *T) Bar()
- Method Bar -
Recv: T
Doc: Bar doc 2
func (t T) Bar()
`)
}

func printVal(parts []string, format string, val any) []string {
return append(parts, fmt.Sprintf(format, val))
}
Expand Down Expand Up @@ -237,10 +117,10 @@ func printPkg(fset *token.FileSet, in *doc.Package) string {
return strings.Join(append(parts, ""), "\n")
}

func testPkg(t *testing.T, in, expected string) {
func testPkg(t *testing.T, filename string, src any, expected string) {
t.Helper()
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "foo.go", in, parser.ParseComments)
f, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
if err != nil {
t.Fatal(err)
}
Expand All @@ -253,3 +133,33 @@ func testPkg(t *testing.T, in, expected string) {
t.Fatalf("got:\n%s\nexpected:\n%s\n", ret, expected)
}
}

func testFromDir(t *testing.T, sel, relDir string) {
dir, err := os.Getwd()
if err != nil {
t.Fatal("Getwd failed:", err)
}
dir = path.Join(dir, relDir)
fis, err := os.ReadDir(dir)
if err != nil {
t.Fatal("ReadDir failed:", err)
}
for _, fi := range fis {
name := fi.Name()
if strings.HasPrefix(name, "_") || !fi.IsDir() || (sel != "" && !strings.Contains(name, sel)) {
continue
}
t.Run(name, func(t *testing.T) {
testDir := dir + "/" + name
out, e := os.ReadFile(testDir + "/out.expect")
if e != nil {
t.Fatal(e)
}
testPkg(t, testDir+"/in.go", nil, string(out))
})
}
}

func TestFromTestdata(t *testing.T) {
testFromDir(t, "", "./_testdata")
}

0 comments on commit 0a7dbfe

Please sign in to comment.