cue: add test for package name override
Change-Id: I8ada079b9d5d85ed5d1584a30a44138949cdbdc7
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3223
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/ast_test.go b/cue/ast_test.go
index 634cb05..96b4b63 100644
--- a/cue/ast_test.go
+++ b/cue/ast_test.go
@@ -19,8 +19,11 @@
"strings"
"testing"
+ "cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/parser"
+ "cuelang.org/go/internal"
+ "github.com/stretchr/testify/assert"
)
func TestCompile(t *testing.T) {
@@ -419,6 +422,15 @@
name: "nonexisting import package",
in: `import "doesnotexist"`,
err: `package "doesnotexist" not found`,
+ }, {
+ name: "duplicate with different name okay",
+ in: `
+ import "time"
+ import time2 "time"
+
+ a: time.Time
+ b: time2.Time
+ `,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
@@ -437,3 +449,53 @@
})
}
}
+
+func TestShadowing(t *testing.T) {
+ spec := &ast.ImportSpec{Path: mustParseExpr(`"list"`).(*ast.BasicLit)}
+ testCases := []struct {
+ file *ast.File
+ want string
+ }{{
+ file: &ast.File{Decls: []ast.Decl{
+ &ast.ImportDecl{Specs: []*ast.ImportSpec{spec}},
+ &ast.Field{
+ Label: mustParseExpr(`list`).(*ast.Ident),
+ Value: &ast.CallExpr{
+ Fun: &ast.SelectorExpr{
+ X: &ast.Ident{
+ Name: "list",
+ Node: spec,
+ },
+ Sel: ast.NewIdent("Min"),
+ },
+ },
+ },
+ }},
+ want: "import listx \"list\", list: listx.Min()",
+ }}
+ for _, tc := range testCases {
+ t.Run("", func(t *testing.T) {
+ var r Runtime
+ inst, err := r.CompileFile(tc.file)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ ctx := r.index().newContext()
+
+ n, _ := export(ctx, inst.rootStruct, options{
+ raw: true,
+ })
+ got := internal.DebugStr(n)
+ assert.Equal(t, got, tc.want)
+ })
+ }
+}
+
+func mustParseExpr(expr string) ast.Expr {
+ ex, err := parser.ParseExpr("cue", expr)
+ if err != nil {
+ panic(err)
+ }
+ return ex
+}
diff --git a/cue/build_test.go b/cue/build_test.go
index fd8e239..804e7ea 100644
--- a/cue/build_test.go
+++ b/cue/build_test.go
@@ -72,6 +72,14 @@
Number: 12
`),
}
+ pkg3 := &bimport{
+ "example.com/foo/v1:pkg3",
+ files(`
+ package pkg3
+
+ List: [1,2,3]
+ `),
+ }
testCases := []struct {
instances []*bimport
@@ -160,6 +168,29 @@
"Hello \(pkg.Number)!"`),
}),
`"Hello 12!"`,
+ }, {
+ insts(pkg3, &bimport{"",
+ files(
+ `package test
+
+ import "example.com/foo/v1:pkg3"
+
+ "Hello \(pkg3.List[1])!"`),
+ }),
+ `"Hello 2!"`,
+ }, {
+ insts(pkg3, &bimport{"",
+ files(
+ `package test
+
+ import "example.com/foo/v1:pkg3"
+
+ pkg3: 3
+
+ "Hello \(pkg3.List[1])!"`),
+ }),
+ `pkg3 redeclared as imported package name
+ previous declaration at file0.cue:5:5`,
}}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {