123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package test
- import (
- "sort"
- "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 importAccept = func(p *api.ApiParserParser, visitor *ast.ApiVisitor) interface{} {
- return p.ImportSpec().Accept(visitor)
- }
- func TestImport(t *testing.T) {
- t.Run("matched", func(t *testing.T) {
- v, err := parser.Accept(importAccept, `import "foo.api"`)
- assert.Nil(t, err)
- list := v.([]*ast.ImportExpr)
- for _, each := range list {
- assert.True(t, each.Equal(&ast.ImportExpr{
- Import: ast.NewTextExpr("import"),
- Value: ast.NewTextExpr(`"foo.api"`),
- }))
- }
- })
- t.Run("matched block", func(t *testing.T) {
- v, err := parser.Accept(importAccept, `
- import (
- /**foo*/
- "foo.api"
- /**bar*/
- "bar.api"
- /**foobar*/
- "foo/bar.api"/**foobar*/
- )
- `)
- assert.Nil(t, err)
- list := v.([]*ast.ImportExpr)
- expected := []*ast.ImportExpr{
- {
- Import: ast.NewTextExpr("import"),
- Value: ast.NewTextExpr(`"foo.api"`),
- DocExpr: []ast.Expr{
- ast.NewTextExpr("/**foo*/"),
- },
- },
- {
- Import: ast.NewTextExpr("import"),
- Value: ast.NewTextExpr(`"bar.api"`),
- DocExpr: []ast.Expr{
- ast.NewTextExpr("/**bar*/"),
- },
- },
- {
- Import: ast.NewTextExpr("import"),
- Value: ast.NewTextExpr(`"foo/bar.api"`),
- DocExpr: []ast.Expr{
- ast.NewTextExpr("/**foobar*/"),
- },
- CommentExpr: ast.NewTextExpr("/**foobar*/"),
- },
- }
- sort.Slice(list, func(i, j int) bool {
- return list[i].Value.Line() < list[j].Value.Line()
- })
- sort.Slice(expected, func(i, j int) bool {
- return expected[i].Value.Line() < expected[j].Value.Line()
- })
- assert.True(t, len(list) == len(expected))
- for index, each := range list {
- assert.True(t, each.Equal(expected[index]))
- }
- })
- t.Run("matched doc", func(t *testing.T) {
- v, err := parser.Accept(importAccept, `
- /**doc*/
- import "foo.api" /**line doc*/`)
- assert.Nil(t, err)
- list := v.([]*ast.ImportExpr)
- for _, each := range list {
- assert.True(t, each.Equal(&ast.ImportExpr{
- Import: ast.NewTextExpr("import"),
- Value: ast.NewTextExpr(`"foo.api"`),
- DocExpr: []ast.Expr{
- ast.NewTextExpr("/**doc*/"),
- },
- CommentExpr: ast.NewTextExpr("/**line doc*/"),
- }))
- }
- })
- t.Run("matched comment", func(t *testing.T) {
- v, err := parser.Accept(importAccept, `
- // comment block
- import "foo.api" // line comment`)
- assert.Nil(t, err)
- list := v.([]*ast.ImportExpr)
- for _, each := range list {
- assert.True(t, each.Equal(&ast.ImportExpr{
- Import: ast.NewTextExpr("import"),
- Value: ast.NewTextExpr(`"foo.api"`),
- DocExpr: []ast.Expr{
- ast.NewTextExpr("// comment block"),
- },
- CommentExpr: ast.NewTextExpr("// line comment"),
- }))
- }
- })
- t.Run("mismatched import", func(t *testing.T) {
- _, err := parser.Accept(importAccept, `
- "foo.api"`)
- assert.Error(t, err)
- _, err = parser.Accept(importAccept, `
- impor "foo.api"`)
- assert.Error(t, err)
- })
- t.Run("mismatched value", func(t *testing.T) {
- _, err := parser.Accept(importAccept, `
- import "foo"`)
- assert.Error(t, err)
- _, err = parser.Accept(importAccept, `
- import ""`)
- assert.Error(t, err)
- _, err = parser.Accept(importAccept, `
- import `)
- assert.Error(t, err)
- })
- }
|