test: server 包完善部分测试用例

This commit is contained in:
kercylan98
2024-01-15 17:27:29 +08:00
parent 22449ff5c3
commit bbf70fab02
17 changed files with 391 additions and 106 deletions

View File

@@ -5,7 +5,7 @@ import (
"strings"
)
func newComment(cg *ast.CommentGroup) *Comment {
func newComment(name string, cg *ast.CommentGroup) *Comment {
c := &Comment{}
if cg == nil {
return c
@@ -14,9 +14,10 @@ func newComment(cg *ast.CommentGroup) *Comment {
c.Comments = append(c.Comments, comment.Text)
cc := strings.TrimPrefix(strings.Replace(comment.Text, "// ", "//", 1), "//")
if i == 0 {
s := strings.SplitN(cc, " ", 2)
if len(s) == 2 {
cc = s[1]
tsc := strings.TrimSpace(cc)
if strings.HasPrefix(tsc, name) {
s := strings.TrimSpace(strings.TrimPrefix(tsc, name))
cc = s
}
}
c.Clear = append(c.Clear, cc)

View File

@@ -10,7 +10,7 @@ func newField(field *ast.Field) []*Field {
return []*Field{{
Anonymous: true,
Type: newType(field.Type),
Comments: newComment(field.Comment),
Comments: newComment("", field.Comment),
}}
} else {
var fs []*Field
@@ -22,7 +22,7 @@ func newField(field *ast.Field) []*Field {
Anonymous: false,
Name: name.String(),
Type: newType(field.Type),
Comments: newComment(field.Comment),
Comments: newComment(name.String(), field.Comment),
})
}
return fs

View File

@@ -16,7 +16,7 @@ func newFile(owner *Package, filePath string) (*File, error) {
af: af,
owner: owner,
FilePath: filePath,
Comment: newComment(af.Doc),
Comment: newComment("Package", af.Doc),
}
for _, decl := range af.Decls {
switch typ := decl.(type) {

View File

@@ -12,7 +12,7 @@ func newFunction(astFunc *ast.FuncDecl) *Function {
f := &Function{
decl: astFunc,
Name: astFunc.Name.String(),
Comments: newComment(astFunc.Doc),
Comments: newComment(astFunc.Name.String(), astFunc.Doc),
}
f.IsTest = strings.HasPrefix(f.Name, "Test")
f.IsBenchmark = strings.HasPrefix(f.Name, "Benchmark")

View File

@@ -91,7 +91,7 @@ func (p *Package) Structs() []*Struct {
}
func (p *Package) FileComments() *Comment {
var comment = newComment(nil)
var comment = newComment("", nil)
for _, file := range p.Files {
for _, c := range file.Comment.Comments {
comment.Comments = append(comment.Comments, c)

View File

@@ -8,7 +8,7 @@ func newStruct(astGen *ast.GenDecl) *Struct {
astTypeSpec := astGen.Specs[0].(*ast.TypeSpec)
s := &Struct{
Name: astTypeSpec.Name.String(),
Comments: newComment(astGen.Doc),
Comments: newComment(astTypeSpec.Name.String(), astGen.Doc),
}
s.Internal = s.Name[0] >= 97 && s.Name[0] <= 122
if astTypeSpec.TypeParams != nil {

View File

@@ -288,6 +288,7 @@ func (b *Builder) genStructs() {
if function.Internal || function.Test {
continue
}
b.newLine(fmt.Sprintf(`<span id="struct_%s_%s"></span>`, structInfo.Name, function.Name)).newLine()
b.title(4, strings.TrimSpace(fmt.Sprintf("func (%s%s) %s%s %s",
super.If(function.Struct.Type.IsPointer, "*", ""),
structInfo.Name,
@@ -311,6 +312,7 @@ func (b *Builder) genStructs() {
for _, comment := range function.Comments.Clear {
b.quote(comment)
}
b.newLine()
if example := b.p.GetExampleTest(function); example != nil {
b.newLine("**示例代码:**").newLine()
if len(example.Comments.Clear) > 0 {

View File

@@ -15,6 +15,20 @@ func Port() int {
return Int(1, 65535)
}
// UsablePort 随机返回一个可用的端口号,如果没有可用端口号则返回 -1
func UsablePort() int {
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
if err != nil {
return -1
}
cli, err := net.ListenTCP("tcp", addr)
if err != nil {
return -1
}
defer func() { _ = cli.Close() }()
return cli.Addr().(*net.TCPAddr).Port
}
// IPv4 返回一个随机产生的IPv4地址。
func IPv4() string {
return fmt.Sprintf("%d.%d.%d.%d", Int(1, 255), Int(0, 255), Int(0, 255), Int(0, 255))