mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-26 11:28:46 +08:00
e76f44a35b
* reactor rpc generation * update flag * update command * update command * update unit test * delete test file * optimize code * update doc * update gen pb * rename target dir * update mysql data type convert rule * add done flag * optimize req/reply parameter * optimize req/reply parameter * remove waste code * remove duplicate parameter * format code * format code * optimize naming * reactor rpcv2 to rpc * remove new line * format code * rename underline to snake * reactor getParentPackage * remove debug log * reactor background
79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
package parser
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDefaultProtoParse(t *testing.T) {
|
|
p := NewDefaultProtoParser()
|
|
data, err := p.Parse("./test.proto")
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, "base.proto", func() string {
|
|
ip := data.Import[0]
|
|
return ip.Filename
|
|
}())
|
|
assert.Equal(t, "test", data.Package.Name)
|
|
assert.Equal(t, true, data.GoPackage == "go")
|
|
assert.Equal(t, true, data.PbPackage == "_go")
|
|
assert.Equal(t, []string{"TestMessage", "TestReply", "TestReq"}, func() []string {
|
|
var list []string
|
|
for _, item := range data.Message {
|
|
list = append(list, item.Name)
|
|
}
|
|
sort.Strings(list)
|
|
return list
|
|
}())
|
|
|
|
assert.Equal(t, true, func() bool {
|
|
s := data.Service
|
|
if s.Name != "TestService" {
|
|
return false
|
|
}
|
|
rpcOne := s.RPC[0]
|
|
|
|
return rpcOne.Name == "TestRpcOne" && rpcOne.RequestType == "TestReq" && rpcOne.ReturnsType == "TestReply"
|
|
}())
|
|
}
|
|
|
|
func TestDefaultProtoParseCaseInvalidRequestType(t *testing.T) {
|
|
p := NewDefaultProtoParser()
|
|
_, err := p.Parse("./test_invalid_request.proto")
|
|
assert.True(t, true, func() bool {
|
|
return strings.Contains(err.Error(), "request type must defined in")
|
|
}())
|
|
}
|
|
|
|
func TestDefaultProtoParseCaseInvalidResponseType(t *testing.T) {
|
|
p := NewDefaultProtoParser()
|
|
_, err := p.Parse("./test_invalid_response.proto")
|
|
assert.True(t, true, func() bool {
|
|
return strings.Contains(err.Error(), "response type must defined in")
|
|
}())
|
|
}
|
|
|
|
func TestDefaultProtoParseError(t *testing.T) {
|
|
p := NewDefaultProtoParser()
|
|
_, err := p.Parse("./nil.proto")
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
func TestDefaultProtoParse_Option(t *testing.T) {
|
|
p := NewDefaultProtoParser()
|
|
data, err := p.Parse("./test_option.proto")
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, "github.com/tal-tech/go-zero", data.GoPackage)
|
|
assert.Equal(t, "go_zero", data.PbPackage)
|
|
}
|
|
|
|
func TestDefaultProtoParse_Option2(t *testing.T) {
|
|
p := NewDefaultProtoParser()
|
|
data, err := p.Parse("./test_option2.proto")
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, "stream", data.GoPackage)
|
|
assert.Equal(t, "stream", data.PbPackage)
|
|
}
|