Skip to content

Commit de1d9ee

Browse files
authored
Merge pull request #44 from xushiwei/test
README
2 parents 4c90958 + fa8961f commit de1d9ee

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

README.md

+68
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ yap - Yet Another Go/Go+ HTTP Web Framework
77
[![Coverage Status](https://codecov.io/gh/goplus/yap/branch/main/graph/badge.svg)](https://codecov.io/gh/goplus/yap)
88
[![GoDoc](https://pkg.go.dev/badge/github.com/goplus/yap.svg)](https://pkg.go.dev/github.com/goplus/yap)
99

10+
### How to use in Go+
11+
12+
This repo contains two Go+ classfiles: `yap` (a HTTP Web Framework) and `yaptest` (a HTTP Test Framework).
13+
14+
The classfile `yap` has the file suffix `_yap.gox`. And the classfile `yaptest` has the file suffix `_ytest.gox`.
15+
16+
Before using `yap` or `yaptest`, you need to add `github.com/goplus/yap` to `go.mod` by using `go get`:
17+
18+
```sh
19+
go get github.com/goplus/yap@latest
20+
```
21+
22+
Then find `require github.com/goplus/yap` statement in `go.mod` and add `//gop:class` at the end of the line:
23+
24+
```go.mod
25+
require github.com/goplus/yap v0.7.2 //gop:class
26+
```
27+
1028
### Router and Parameters
1129

1230
demo in Go ([hello.go](demo/hello/hello.go)):
@@ -102,3 +120,53 @@ get "/p/:id", ctx => {
102120

103121
run ":8080"
104122
```
123+
124+
### YAP Test Framework
125+
126+
Suppose we have a web server named `foo` ([demo/foo/foo_yap.gox](ytest/demo/foo/foo_yap.gox)):
127+
128+
```go
129+
get "/p/:id", ctx => {
130+
ctx.json {
131+
"id": ctx.param("id"),
132+
}
133+
}
134+
135+
run ":8080"
136+
```
137+
138+
Then we create a yaptest file ([demo/foo/foo_ytest.gox](ytest/demo/foo/foo_ytest.gox)):
139+
140+
```go
141+
mock "foo.com", new(foo)
142+
143+
run "test get /p/$id", => {
144+
id := "123"
145+
get "http://foo.com/p/${id}"
146+
ret 200
147+
json {
148+
"id": id,
149+
}
150+
}
151+
```
152+
153+
The directive `mock` creates the `foo` server by [mockhttp](https://pkg.go.dev/github.com/qiniu/x/mockhttp). Then we call the directive `run` to run a subtest.
154+
155+
You can change the directive `mock` to `testServer` (see [demo/foo/bar_ytest.gox](ytest/demo/foo/bar_ytest.gox)), and keep everything else unchanged:
156+
157+
```go
158+
testServer "foo.com", new(foo)
159+
160+
run "test get /p/$id", => {
161+
id := "123"
162+
get "http://foo.com/p/${id}"
163+
ret 200
164+
json {
165+
"id": id,
166+
}
167+
}
168+
```
169+
170+
The directive `testServer` creates the `foo` server by [net/http/httptest](https://pkg.go.dev/net/http/httptest#NewServer) and obtained a random port as the service address. Then it calls the directive [host](https://pkg.go.dev/github.com/goplus/yap/ytest#App.Host) to map the random service address to `foo.com`. This makes all other code no need to changed.
171+
172+
For more details, see [yaptest - Go+ HTTP Test Framework](ytest).

ytest/demo/foo/bar_ytest.gox

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
testServer "foo.com", new(foo)
22

3-
run "get /p/$id", => {
3+
run "test get /p/$id", => {
44
id := "123"
55
get "http://foo.com/p/${id}"
66
ret 200

ytest/demo/foo/foo_ytest.gox

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mock "foo.com", new(foo)
22

3-
run "get /p/$id", => {
3+
run "test get /p/$id", => {
44
id := "123"
55
get "http://foo.com/p/${id}"
66
ret 200

ytest/demo/foo/gop_autogen_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (this *case_bar) Main() {
1616
//line ytest/demo/foo/bar_ytest.gox:1:1
1717
this.TestServer("foo.com", new(foo))
1818
//line ytest/demo/foo/bar_ytest.gox:3:1
19-
this.Run("get /p/$id", func() {
19+
this.Run("test get /p/$id", func() {
2020
//line ytest/demo/foo/bar_ytest.gox:4:1
2121
id := "123"
2222
//line ytest/demo/foo/bar_ytest.gox:5:1
@@ -32,7 +32,7 @@ func (this *case_foo) Main() {
3232
//line ytest/demo/foo/foo_ytest.gox:1:1
3333
this.Mock("foo.com", new(foo))
3434
//line ytest/demo/foo/foo_ytest.gox:3:1
35-
this.Run("get /p/$id", func() {
35+
this.Run("test get /p/$id", func() {
3636
//line ytest/demo/foo/foo_ytest.gox:4:1
3737
id := "123"
3838
//line ytest/demo/foo/foo_ytest.gox:5:1

0 commit comments

Comments
 (0)