123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package test
- import (
- "testing"
- "git.i2edu.net/i2/go-zero/tools/goctl/api/parser/g4/ast"
- "git.i2edu.net/i2/go-zero/tools/goctl/api/parser/g4/gen/api"
- "github.com/stretchr/testify/assert"
- )
- var infoAccept = func(p *api.ApiParserParser, visitor *ast.ApiVisitor) interface{} {
- return p.InfoSpec().Accept(visitor)
- }
- func TestInfo(t *testing.T) {
- t.Run("matched", func(t *testing.T) {
- v, err := parser.Accept(infoAccept, `
- info(
- title: foo
- )
- `)
- assert.Nil(t, err)
- info := v.(*ast.InfoExpr)
- assert.True(t, info.Equal(&ast.InfoExpr{
- Info: ast.NewTextExpr("info"),
- Kvs: []*ast.KvExpr{
- {
- Key: ast.NewTextExpr("title"),
- Value: ast.NewTextExpr("foo"),
- },
- },
- }))
- v, err = parser.Accept(infoAccept, `
- info(
- title: 中文(bar)
- )
- `)
- assert.Nil(t, err)
- info = v.(*ast.InfoExpr)
- assert.True(t, info.Equal(&ast.InfoExpr{
- Info: ast.NewTextExpr("info"),
- Kvs: []*ast.KvExpr{
- {
- Key: ast.NewTextExpr("title"),
- Value: ast.NewTextExpr("中文(bar)"),
- },
- },
- }))
- v, err = parser.Accept(infoAccept, `
- info(
- foo: "new
- line"
- )
- `)
- assert.Nil(t, err)
- info = v.(*ast.InfoExpr)
- assert.True(t, info.Equal(&ast.InfoExpr{
- Info: ast.NewTextExpr("info"),
- Kvs: []*ast.KvExpr{
- {
- Key: ast.NewTextExpr("foo"),
- Value: ast.NewTextExpr(`"new
- line"`),
- },
- },
- }))
- })
- t.Run("matched doc", func(t *testing.T) {
- v, err := parser.Accept(infoAccept, `
- // doc
- info( // comment
- // foo
- title: foo // bar
- )
- `)
- assert.Nil(t, err)
- info := v.(*ast.InfoExpr)
- assert.True(t, info.Equal(&ast.InfoExpr{
- Info: ast.NewTextExpr("info"),
- Kvs: []*ast.KvExpr{
- {
- Key: ast.NewTextExpr("title"),
- Value: ast.NewTextExpr("foo"),
- DocExpr: []ast.Expr{
- ast.NewTextExpr("// foo"),
- },
- CommentExpr: ast.NewTextExpr("// bar"),
- },
- },
- }))
- v, err = parser.Accept(infoAccept, `
- /**doc block*/
- info( /**line block*/
- /**foo*/
- title: foo /*bar**/
- )
- `)
- assert.Nil(t, err)
- info = v.(*ast.InfoExpr)
- assert.True(t, info.Equal(&ast.InfoExpr{
- Info: ast.NewTextExpr("info"),
- Kvs: []*ast.KvExpr{
- {
- Key: ast.NewTextExpr("title"),
- Value: ast.NewTextExpr("foo"),
- DocExpr: []ast.Expr{
- ast.NewTextExpr("/**foo*/"),
- },
- CommentExpr: ast.NewTextExpr("/*bar**/"),
- },
- },
- }))
- v, err = parser.Accept(infoAccept, `
- info(
- // doc
- title: foo
- // doc
- author: bar
- )
- `)
- assert.Nil(t, err)
- info = v.(*ast.InfoExpr)
- assert.True(t, info.Equal(&ast.InfoExpr{
- Info: ast.NewTextExpr("info"),
- Kvs: []*ast.KvExpr{
- {
- Key: ast.NewTextExpr("title"),
- Value: ast.NewTextExpr("foo"),
- DocExpr: []ast.Expr{
- ast.NewTextExpr("// doc"),
- },
- },
- {
- Key: ast.NewTextExpr("author"),
- Value: ast.NewTextExpr("bar"),
- DocExpr: []ast.Expr{
- ast.NewTextExpr("// doc"),
- },
- },
- },
- }))
- })
- t.Run("mismatched", func(t *testing.T) {
- _, err := parser.Accept(infoAccept, `
- info(
- title
- )
- `)
- assert.Error(t, err)
- _, err = parser.Accept(infoAccept, `
- info(
- :title
- )
- `)
- assert.Error(t, err)
- _, err = parser.Accept(infoAccept, `
- info(
- foo bar
- )
- `)
- assert.Error(t, err)
- _, err = parser.Accept(infoAccept, `
- info(
- foo : new
- line
- )
- `)
- assert.Error(t, err)
- _, err = parser.Accept(infoAccept, `
- info()
- `)
- assert.Error(t, err)
- })
- }
|