You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/parser/g4/ast/apiparser_test.go

100 lines
2.7 KiB
Go

package ast
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
func Test_ImportCycle(t *testing.T) {
const (
mainFilename = "main.api"
subAFilename = "a.api"
subBFilename = "b.api"
mainSrc = `import "./a.api"`
subASrc = `import "./b.api"`
subBSrc = `import "./a.api"`
)
var err error
dir := pathx.MustTempDir()
defer os.RemoveAll(dir)
mainPath := filepath.Join(dir, mainFilename)
err = ioutil.WriteFile(mainPath, []byte(mainSrc), 0o777)
require.NoError(t, err)
subAPath := filepath.Join(dir, subAFilename)
err = ioutil.WriteFile(subAPath, []byte(subASrc), 0o777)
require.NoError(t, err)
subBPath := filepath.Join(dir, subBFilename)
err = ioutil.WriteFile(subBPath, []byte(subBSrc), 0o777)
require.NoError(t, err)
_, err = NewParser().Parse(mainPath)
assert.ErrorIs(t, err, ErrImportCycleNotAllowed)
}
func Test_MultiImportedShouldAllowed(t *testing.T) {
const (
mainFilename = "main.api"
subAFilename = "a.api"
subBFilename = "b.api"
mainSrc = "import \"./b.api\"\n" +
"import \"./a.api\"\n" +
"type Main { b B `json:\"b\"`}"
subASrc = "import \"./b.api\"\n" +
"type A { b B `json:\"b\"`}\n"
subBSrc = `type B{}`
)
var err error
dir := pathx.MustTempDir()
defer os.RemoveAll(dir)
mainPath := filepath.Join(dir, mainFilename)
err = ioutil.WriteFile(mainPath, []byte(mainSrc), 0o777)
require.NoError(t, err)
subAPath := filepath.Join(dir, subAFilename)
err = ioutil.WriteFile(subAPath, []byte(subASrc), 0o777)
require.NoError(t, err)
subBPath := filepath.Join(dir, subBFilename)
err = ioutil.WriteFile(subBPath, []byte(subBSrc), 0o777)
require.NoError(t, err)
_, err = NewParser().Parse(mainPath)
assert.NoError(t, err)
}
func Test_RedundantDeclarationShouldNotBeAllowed(t *testing.T) {
const (
mainFilename = "main.api"
subAFilename = "a.api"
subBFilename = "b.api"
mainSrc = "import \"./a.api\"\n" +
"import \"./b.api\"\n"
subASrc = `import "./b.api"
type A{}`
subBSrc = `type A{}`
)
var err error
dir := pathx.MustTempDir()
defer os.RemoveAll(dir)
mainPath := filepath.Join(dir, mainFilename)
err = ioutil.WriteFile(mainPath, []byte(mainSrc), 0o777)
require.NoError(t, err)
subAPath := filepath.Join(dir, subAFilename)
err = ioutil.WriteFile(subAPath, []byte(subASrc), 0o777)
require.NoError(t, err)
subBPath := filepath.Join(dir, subBFilename)
err = ioutil.WriteFile(subBPath, []byte(subBSrc), 0o777)
require.NoError(t, err)
_, err = NewParser().Parse(mainPath)
assert.Error(t, err)
assert.Contains(t, err.Error(), "duplicate type declaration")
}