chore: Embed unit test data (#1812)

* Embed unit test data

* Add testdata

Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
anqiansong
2022-04-21 21:49:09 +08:00
committed by GitHub
parent 14bf2f33f7
commit 16c61c6657
27 changed files with 388 additions and 315 deletions

View File

@@ -1,7 +1,7 @@
package test
import (
"io/ioutil"
_ "embed"
"testing"
"github.com/stretchr/testify/assert"
@@ -9,16 +9,17 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api"
)
//go:embed apis/test.api
var testApi string
var parser = ast.NewParser(ast.WithParserPrefix("test.api"), ast.WithParserDebug())
func TestApi(t *testing.T) {
fn := func(p *api.ApiParserParser, visitor *ast.ApiVisitor) interface{} {
return p.Api().Accept(visitor)
}
content, err := ioutil.ReadFile("./apis/test.api")
assert.Nil(t, err)
v, err := parser.Accept(fn, string(content))
v, err := parser.Accept(fn, testApi)
assert.Nil(t, err)
api := v.(*ast.Api)
body := &ast.Body{

View File

@@ -1,13 +1,15 @@
package parser
import (
_ "embed"
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
)
var testApi = "// syntax doc\nsyntax = \"v1\" // syntax comment\n\n// type doc\ntype Request {\n\tName string `path:\"name,options=you|me\"`\n}\n\ntype Response {\n\tMessage string `json:\"message\"`\n}\n\n// service doc\nservice greet-api {\n\t// handler doc\n\t@handler GreetHandler // handler comment\n\tget /from/:name(Request) returns (Response);\n}"
//go:embed testdata/test.api
var testApi string
func TestParseContent(t *testing.T) {
sp, err := ParseContent(testApi)

View File

@@ -0,0 +1,18 @@
// syntax doc
syntax = "v1" // syntax comment
// type doc
type Request {
Name string `path:"name,options=you|me"`
}
type Response {
Message string `json:"message"`
}
// service doc
service greet-api {
// handler doc
@handler GreetHandler // handler comment
get /from/:name(Request) returns (Response);
}