Skip to content

Commit

Permalink
Merge pull request #40 from xushiwei/test
Browse files Browse the repository at this point in the history
yaptest demo: use TestServer to test
  • Loading branch information
xushiwei committed Jan 30, 2024
2 parents 0306e66 + 4d0cab4 commit 3aed443
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ytest/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (p *App) TestServer(host string, app yap.AppType) {
app.InitYap()
app.SetLAS(func(addr string, h http.Handler) error {
svr := httptest.NewServer(h)
p.Host(host, svr.URL)
p.Host("http://"+host, svr.URL)
return nil
})
app.(interface{ MainEntry() }).MainEntry()
Expand All @@ -78,7 +78,7 @@ func (p *App) RunMock(host string, h http.Handler) {
// 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)
p.Host("http://"+host, svr.URL)
}

// Gopt_App_TestMain is required by Go+ compiler as the TestMain entry of a YAP testing project.
Expand Down
10 changes: 10 additions & 0 deletions ytest/demo/foo/bar_ytest.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
testServer "foo.com", new(foo)

run "get /p/$id", => {
id := "123"
get "http://foo.com/p/${id}"
ret 200
json {
"id": "12",
}
}
22 changes: 22 additions & 0 deletions ytest/demo/foo/gop_autogen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,28 @@ import (
"testing"
)

type case_bar struct {
ytest.Case
}
type case_foo struct {
ytest.Case
}
//line ytest/demo/foo/bar_ytest.gox:1
func (this *case_bar) Main() {
//line ytest/demo/foo/bar_ytest.gox:1:1
this.TestServer("foo.com", new(foo))
//line ytest/demo/foo/bar_ytest.gox:3:1
this.Run("get /p/$id", func() {
//line ytest/demo/foo/bar_ytest.gox:4:1
id := "123"
//line ytest/demo/foo/bar_ytest.gox:5:1
this.Get("http://foo.com/p/" + id)
//line ytest/demo/foo/bar_ytest.gox:6:1
this.RetWith(200)
//line ytest/demo/foo/bar_ytest.gox:7:1
this.Json(map[string]string{"id": "12"})
})
}
//line ytest/demo/foo/foo_ytest.gox:1
func (this *case_foo) Main() {
//line ytest/demo/foo/foo_ytest.gox:1:1
Expand All @@ -24,6 +43,9 @@ func (this *case_foo) Main() {
this.Json(map[string]string{"id": "12"})
})
}
func Test_bar(t *testing.T) {
ytest.Gopt_Case_TestMain(new(case_bar), t)
}
func Test_foo(t *testing.T) {
ytest.Gopt_Case_TestMain(new(case_foo), t)
}

0 comments on commit 3aed443

Please sign in to comment.