Skip to content

Commit

Permalink
Merge pull request #107 from xushiwei/q
Browse files Browse the repository at this point in the history
ytest: match doc
  • Loading branch information
xushiwei authored Mar 12, 2024
2 parents ae114c5 + 584e94e commit d30157e
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ yap - Yet Another Go/Go+ HTTP Web Framework
[![GitHub release](https://img.shields.io/github/v/tag/goplus/yap.svg?label=release)](https://github.com/goplus/yap/releases)
[![Coverage Status](https://codecov.io/gh/goplus/yap/branch/main/graph/badge.svg)](https://codecov.io/gh/goplus/yap)
[![GoDoc](https://pkg.go.dev/badge/github.com/goplus/yap.svg)](https://pkg.go.dev/github.com/goplus/yap)
[![Language](https://img.shields.io/badge/language-Go+-blue.svg)](https://github.com/goplus/gop)

This repo contains three [Go+ classfiles](https://github.com/goplus/gop/blob/main/doc/classfile.md): `yap` (a HTTP Web Framework), `yaptest` (a HTTP Test Framework) and `ydb` (a Go+ Database Framework).

Expand Down Expand Up @@ -36,7 +37,7 @@ gop get github.com/goplus/yap@latest

Create a file named [get.yap](demo/classfile2_hello/get.yap) with the following content:

```coffee
```go
html `<html><body>Hello, YAP!</body></html>`
```

Expand Down
25 changes: 23 additions & 2 deletions ytest/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
yaptest - Go+ HTTP Test Framework
=====
[![Language](https://img.shields.io/badge/language-Go+-blue.svg)](https://github.com/goplus/gop)
[![GitHub release](https://img.shields.io/github/v/tag/goplus/gop.svg?label=Go%2b+release)](https://github.com/goplus/gop/releases)
[![Discord](https://img.shields.io/badge/Discord-online-success.svg?logo=discord&logoColor=white)](https://discord.gg/mYjWCJDcAr)
[![GoDoc](https://pkg.go.dev/badge/github.com/goplus/yap/ytest.svg)](https://pkg.go.dev/github.com/goplus/yap/ytest)

yaptest is a web server testing framework. This classfile has the file suffix `_ytest.gox`.

Expand Down Expand Up @@ -48,7 +52,7 @@ json {
The directive `testServer` creates the web 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.


### match
## match

This is almost the core concept in `yaptest`. It matches two objects.

Expand Down Expand Up @@ -103,11 +107,28 @@ Unbound variables are allowed in `<ExpectedObject>`, but cannot appear in `<Sour

If a variable in `<ExpectedObject>` has not been bound, it will be bound according to the value of the corresponding `<SourceObject>`; if the variable has been bound, the values on both sides must match.

The cornerstone of `yaptest` is matching grammar. Let's look at the next example you saw at the beginning:
The cornerstone of `yaptest` is matching grammar. Let's look at [the example](demo/match/hello/hello_yapt.gox) you saw at the beginning:

```go
id := "123"
get "http://foo.com/p/${id}"

ret 200
json {
"id": id,
}
```

It is [equivalent to](demo/match/diveinto/hello_yapt.gox):

```go
id := "123"
get "http://foo.com/p/${id}"

send // send request
match 200, resp.code // assert resp.code == 200
match "application/json", resp.header.get("Content-Type")
match { // assert resp.body.id == id
"id": id,
}, resp.body
```
3 changes: 3 additions & 0 deletions ytest/demo/match/diveinto/get_p_#id.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json {
"id": ${id},
}
69 changes: 69 additions & 0 deletions ytest/demo/match/diveinto/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions ytest/demo/match/diveinto/hello_yapt.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mock "foo.com", new(AppV2)

id := "123"
get "http://foo.com/p/${id}"

send // send request
match 200, resp.code // assert resp.code == 200
match "application/json", resp.header.get("Content-Type")
match { // assert resp.body.id == id
"id": id,
}, resp.body

echo "OK"
2 changes: 2 additions & 0 deletions ytest/demo/match/hello/hello_yapt.gox
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ mock "foo.com", new(AppV2)

id := "123"
get "http://foo.com/p/${id}"

ret 200
json {
"id": id,
}

echo "OK"

0 comments on commit d30157e

Please sign in to comment.