parser_test.go 1.0 KB

12345678910111213141516171819202122232425262728
  1. package parser
  2. import (
  3. "testing"
  4. "git.i2edu.net/i2/go-zero/tools/goctl/api/spec"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. 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}"
  8. func TestParseContent(t *testing.T) {
  9. sp, err := ParseContent(testApi)
  10. assert.Nil(t, err)
  11. assert.Equal(t, spec.Doc{`// syntax doc`}, sp.Syntax.Doc)
  12. assert.Equal(t, spec.Doc{`// syntax comment`}, sp.Syntax.Comment)
  13. for _, tp := range sp.Types {
  14. if tp.Name() == "Request" {
  15. assert.Equal(t, []string{`// type doc`}, tp.Documents())
  16. }
  17. }
  18. for _, e := range sp.Service.Routes() {
  19. if e.Handler == "GreetHandler" {
  20. assert.Equal(t, spec.Doc{"// handler doc"}, e.HandlerDoc)
  21. assert.Equal(t, spec.Doc{"// handler comment"}, e.HandlerComment)
  22. }
  23. }
  24. }