parser_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package parser
  2. import (
  3. "sort"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestDefaultProtoParse(t *testing.T) {
  9. p := NewDefaultProtoParser()
  10. data, err := p.Parse("./test.proto")
  11. assert.Nil(t, err)
  12. assert.Equal(t, "base.proto", func() string {
  13. ip := data.Import[0]
  14. return ip.Filename
  15. }())
  16. assert.Equal(t, "test", data.Package.Name)
  17. assert.Equal(t, true, data.GoPackage == "go")
  18. assert.Equal(t, true, data.PbPackage == "_go")
  19. assert.Equal(t, []string{"TestMessage", "TestReply", "TestReq"}, func() []string {
  20. var list []string
  21. for _, item := range data.Message {
  22. list = append(list, item.Name)
  23. }
  24. sort.Strings(list)
  25. return list
  26. }())
  27. assert.Equal(t, true, func() bool {
  28. s := data.Service
  29. if s.Name != "TestService" {
  30. return false
  31. }
  32. rpcOne := s.RPC[0]
  33. return rpcOne.Name == "TestRpcOne" && rpcOne.RequestType == "TestReq" && rpcOne.ReturnsType == "TestReply"
  34. }())
  35. }
  36. func TestDefaultProtoParseCaseInvalidRequestType(t *testing.T) {
  37. p := NewDefaultProtoParser()
  38. _, err := p.Parse("./test_invalid_request.proto")
  39. assert.True(t, true, func() bool {
  40. return strings.Contains(err.Error(), "request type must defined in")
  41. }())
  42. }
  43. func TestDefaultProtoParseCaseInvalidResponseType(t *testing.T) {
  44. p := NewDefaultProtoParser()
  45. _, err := p.Parse("./test_invalid_response.proto")
  46. assert.True(t, true, func() bool {
  47. return strings.Contains(err.Error(), "response type must defined in")
  48. }())
  49. }
  50. func TestDefaultProtoParseError(t *testing.T) {
  51. p := NewDefaultProtoParser()
  52. _, err := p.Parse("./nil.proto")
  53. assert.NotNil(t, err)
  54. }
  55. func TestDefaultProtoParse_Option(t *testing.T) {
  56. p := NewDefaultProtoParser()
  57. data, err := p.Parse("./test_option.proto")
  58. assert.Nil(t, err)
  59. assert.Equal(t, "github.com/tal-tech/go-zero", data.GoPackage)
  60. assert.Equal(t, "go_zero", data.PbPackage)
  61. }
  62. func TestDefaultProtoParse_Option2(t *testing.T) {
  63. p := NewDefaultProtoParser()
  64. data, err := p.Parse("./test_option2.proto")
  65. assert.Nil(t, err)
  66. assert.Equal(t, "stream", data.GoPackage)
  67. assert.Equal(t, "stream", data.PbPackage)
  68. }