Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yaptest: RunMock/RunTestServer; demo/basic #38

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (p *App) initApp() {
p.Engine = New()
}

// InitYapApp initialize a YAP application.
func (p *App) InitYapApp(fs ...fs.FS) {
// InitYap initialize a YAP application.
func (p *App) InitYap(fs ...fs.FS) {
if p.Engine == nil {
p.initApp()
}
Expand Down Expand Up @@ -99,7 +99,9 @@ func (p App) Static__2(pattern string, fs http.FileSystem, allowRedirect ...bool
// Gopt_App_Main is required by Go+ compiler as the entry of a YAP project.
func Gopt_App_Main(app interface{ initApp() }) {
app.initApp()
app.(interface{ MainEntry() }).MainEntry()
if me, ok := app.(interface{ MainEntry() }); ok {
me.MainEntry()
}
}

const (
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/goplus/yap

go 1.18

require github.com/qiniu/x v1.13.2
require github.com/qiniu/x v1.13.3-0.20240129201727-4013f184c5c7
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/qiniu/x v1.13.2 h1:mgWOfB9Rpk6AEtlBoObZVxH+b2FHSntYrxc4KX5Ta98=
github.com/qiniu/x v1.13.2/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
github.com/qiniu/x v1.13.3-0.20240129201727-4013f184c5c7 h1:rzpGloMVgWexXti9yRXMH8aAdjNy8rZrlAQdKvzZeMg=
github.com/qiniu/x v1.13.3-0.20240129201727-4013f184c5c7/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
14 changes: 10 additions & 4 deletions yap.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,20 @@ func (p *Engine) Handle(pattern string, f func(ctx *Context)) {
})
}

// Run listens on the TCP network address addr and then calls
// Serve with handler to handle requests on incoming connections.
// Accepted connections are configured to enable TCP keep-alives.
func (p *Engine) Run(addr string, mws ...func(h http.Handler) http.Handler) error {
// Handler returns the main entry that responds to HTTP requests.
func (p *Engine) Handler(mws ...func(h http.Handler) http.Handler) http.Handler {
h := http.Handler(p)
for _, mw := range mws {
h = mw(h)
}
return h
}

// Run listens on the TCP network address addr and then calls
// Serve with handler to handle requests on incoming connections.
// Accepted connections are configured to enable TCP keep-alives.
func (p *Engine) Run(addr string, mws ...func(h http.Handler) http.Handler) error {
h := p.Handler(mws...)
return http.ListenAndServe(addr, h)
}

Expand Down
2 changes: 1 addition & 1 deletion ytest/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ type Case struct {
DefaultHeader http.Header
}

func New() *Case {
func NewCase() *Case {
return &Case{}
}

Expand Down
16 changes: 16 additions & 0 deletions ytest/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import (
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/qiniu/x/mockhttp"
)

const (
Expand All @@ -42,6 +45,19 @@ func (p *App) initApp() *App {
return p
}

// RunMock runs a HTTP server by mockhttp.
func (p *App) RunMock(host string, h http.Handler) {
tr := mockhttp.NewTransport()
p.transport = tr
tr.ListenAndServe(host, h)
}

// RunTestServer runs a HTTP server by httptest.Server.
func (p *App) RunTestServer(host string, h http.Handler) {
svr := httptest.NewServer(h)
p.Host(host, svr.URL)
}

// Gopt_App_TestMain is required by Go+ compiler as the TestMain entry of a YAP testing project.
func Gopt_App_TestMain(app interface{ initApp() *App }, m *testing.M) {
app.initApp()
Expand Down
70 changes: 35 additions & 35 deletions ytest/demo/_example/gop_autogen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,78 +9,78 @@ import (
type case_example struct {
ytest.Case
}
//line ytest/demo/example/example_ytest.gox:1
//line ytest/demo/_example/example_ytest.gox:1
func (this *case_example) Main() {
//line ytest/demo/example/example_ytest.gox:1:1
//line ytest/demo/_example/example_ytest.gox:1:1
this.Host("https://example.com", "http://localhost:8080")
//line ytest/demo/example/example_ytest.gox:2:1
//line ytest/demo/_example/example_ytest.gox:2:1
testauth := ytest.Oauth2("...")
//line ytest/demo/example/example_ytest.gox:4:1
//line ytest/demo/_example/example_ytest.gox:4:1
this.DefaultHeader.Set("User-Agent", "yaptest/0.7")
//line ytest/demo/example/example_ytest.gox:6:1
//line ytest/demo/_example/example_ytest.gox:6:1
this.Run("urlWithVar", func() {
//line ytest/demo/example/example_ytest.gox:7:1
//line ytest/demo/_example/example_ytest.gox:7:1
id := "123"
//line ytest/demo/example/example_ytest.gox:8:1
//line ytest/demo/_example/example_ytest.gox:8:1
this.Get("https://example.com/p/" + id)
//line ytest/demo/example/example_ytest.gox:9:1
//line ytest/demo/_example/example_ytest.gox:9:1
this.Send()
//line ytest/demo/example/example_ytest.gox:10:1
//line ytest/demo/_example/example_ytest.gox:10:1
fmt.Println("code:", this.Resp().Code())
//line ytest/demo/example/example_ytest.gox:11:1
//line ytest/demo/_example/example_ytest.gox:11:1
fmt.Println("body:", this.Resp().Body())
})
//line ytest/demo/example/example_ytest.gox:14:1
//line ytest/demo/_example/example_ytest.gox:14:1
this.Run("matchWithVar", func() {
//line ytest/demo/example/example_ytest.gox:15:1
//line ytest/demo/_example/example_ytest.gox:15:1
code := ytest.Gopx_Var_Cast__0[int]()
//line ytest/demo/example/example_ytest.gox:16:1
//line ytest/demo/_example/example_ytest.gox:16:1
id := "123"
//line ytest/demo/example/example_ytest.gox:17:1
//line ytest/demo/_example/example_ytest.gox:17:1
this.Get("https://example.com/p/" + id)
//line ytest/demo/example/example_ytest.gox:18:1
//line ytest/demo/_example/example_ytest.gox:18:1
this.RetWith(code)
//line ytest/demo/example/example_ytest.gox:19:1
//line ytest/demo/_example/example_ytest.gox:19:1
fmt.Println("code:", code)
//line ytest/demo/example/example_ytest.gox:20:1
//line ytest/demo/_example/example_ytest.gox:20:1
ytest.Match__4(code, 200)
})
//line ytest/demo/example/example_ytest.gox:23:1
//line ytest/demo/_example/example_ytest.gox:23:1
this.Run("postWithAuth", func() {
//line ytest/demo/example/example_ytest.gox:24:1
//line ytest/demo/_example/example_ytest.gox:24:1
id := "123"
//line ytest/demo/example/example_ytest.gox:25:1
//line ytest/demo/_example/example_ytest.gox:25:1
title := "title"
//line ytest/demo/example/example_ytest.gox:26:1
//line ytest/demo/_example/example_ytest.gox:26:1
author := "author"
//line ytest/demo/example/example_ytest.gox:27:1
//line ytest/demo/_example/example_ytest.gox:27:1
this.Post("https://example.com/p/" + id)
//line ytest/demo/example/example_ytest.gox:28:1
//line ytest/demo/_example/example_ytest.gox:28:1
this.Auth(testauth)
//line ytest/demo/example/example_ytest.gox:29:1
//line ytest/demo/_example/example_ytest.gox:29:1
this.Json(map[string]string{"title": title, "author": author})
//line ytest/demo/example/example_ytest.gox:33:1
//line ytest/demo/_example/example_ytest.gox:33:1
this.RetWith(200)
//line ytest/demo/example/example_ytest.gox:34:1
//line ytest/demo/_example/example_ytest.gox:34:1
fmt.Println("body:", this.Resp().Body())
})
//line ytest/demo/example/example_ytest.gox:37:1
//line ytest/demo/_example/example_ytest.gox:37:1
this.Run("matchJsonObject", func() {
//line ytest/demo/example/example_ytest.gox:38:1
//line ytest/demo/_example/example_ytest.gox:38:1
title := ytest.Gopx_Var_Cast__0[string]()
//line ytest/demo/example/example_ytest.gox:39:1
//line ytest/demo/_example/example_ytest.gox:39:1
author := ytest.Gopx_Var_Cast__0[string]()
//line ytest/demo/example/example_ytest.gox:40:1
//line ytest/demo/_example/example_ytest.gox:40:1
id := "123"
//line ytest/demo/example/example_ytest.gox:41:1
//line ytest/demo/_example/example_ytest.gox:41:1
this.Get("https://example.com/p/" + id)
//line ytest/demo/example/example_ytest.gox:42:1
//line ytest/demo/_example/example_ytest.gox:42:1
this.RetWith(200)
//line ytest/demo/example/example_ytest.gox:43:1
//line ytest/demo/_example/example_ytest.gox:43:1
this.Json(map[string]*ytest.Var__0[string]{"title": title, "author": author})
//line ytest/demo/example/example_ytest.gox:47:1
//line ytest/demo/_example/example_ytest.gox:47:1
fmt.Println("title:", title)
//line ytest/demo/example/example_ytest.gox:48:1
//line ytest/demo/_example/example_ytest.gox:48:1
fmt.Println("author:", author)
})
}
Expand Down
13 changes: 13 additions & 0 deletions ytest/demo/basic/foo.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "github.com/goplus/yap"

var (
yap.App
)

initYap

get "/p/:id", ctx => {
ctx.json {
"id": ctx.param("id"),
}
}

Check warning on line 13 in ytest/demo/basic/foo.gox

View check run for this annotation

Codecov / codecov/patch

ytest/demo/basic/foo.gox#L13

Added line #L13 was not covered by tests
12 changes: 12 additions & 0 deletions ytest/demo/basic/foo_ytest.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server := new(foo)
server.main
runMock "foo.com", server

run "get /p/$id", => {
id := "123"
get "http://foo.com/p/${id}"
ret 200
json {
"id": "12",
}
}
21 changes: 21 additions & 0 deletions ytest/demo/basic/gop_autogen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "github.com/goplus/yap"

const _ = true

type foo struct {
yap.App
}
//line ytest/demo/basic/foo.gox:7
func (this *foo) Main() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[staticcheck] receiver name should be a reflection of its identity; don't use generic names such as "this" or "self" (ST1006)

//line ytest/demo/basic/foo.gox:7:1
this.InitYap()

Check warning on line 13 in ytest/demo/basic/gop_autogen.go

View check run for this annotation

Codecov / codecov/patch

ytest/demo/basic/gop_autogen.go#L13

Added line #L13 was not covered by tests
//line ytest/demo/basic/foo.gox:9:1
this.Get("/p/:id", func(ctx *yap.Context) {
//line ytest/demo/basic/foo.gox:10:1
ctx.Json__1(map[string]string{"id": ctx.Param("id")})
})
}
func main() {
}
33 changes: 33 additions & 0 deletions ytest/demo/basic/gop_autogen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"github.com/goplus/yap/ytest"
"testing"
)

type case_foo struct {
ytest.Case
}
//line ytest/demo/basic/foo_ytest.gox:1
func (this *case_foo) Main() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[staticcheck] receiver name should be a reflection of its identity; don't use generic names such as "this" or "self" (ST1006)

//line ytest/demo/basic/foo_ytest.gox:1:1
server := new(foo)
//line ytest/demo/basic/foo_ytest.gox:2:1
server.Main()
//line ytest/demo/basic/foo_ytest.gox:3:1
this.RunMock("foo.com", server)
//line ytest/demo/basic/foo_ytest.gox:5:1
this.Run("get /p/$id", func() {
//line ytest/demo/basic/foo_ytest.gox:6:1
id := "123"
//line ytest/demo/basic/foo_ytest.gox:7:1
this.Get("http://foo.com/p/" + id)
//line ytest/demo/basic/foo_ytest.gox:8:1
this.RetWith(200)
//line ytest/demo/basic/foo_ytest.gox:9:1
this.Json(map[string]string{"id": "12"})
})
}
func Test_foo(t *testing.T) {
ytest.Gopt_Case_TestMain(new(case_foo), t)
}
5 changes: 0 additions & 5 deletions ytest/demo/mixyap/_main.yunit.gox

This file was deleted.