github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/cue/ast/astutil/util_test.go (about) 1 // Copyright 2020 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package astutil 16 17 import ( 18 "path" 19 "testing" 20 21 "github.com/joomcode/cue/cue/ast" 22 "github.com/google/go-cmp/cmp" 23 ) 24 25 func TestImportInfo(t *testing.T) { 26 testCases := []struct { 27 name string 28 path string 29 want ImportInfo 30 }{ 31 {"", "a.b/bar", ImportInfo{ 32 Ident: "bar", 33 PkgName: "bar", 34 ID: "a.b/bar", 35 Dir: "a.b/bar", 36 }}, 37 {"foo", "a.b/bar", ImportInfo{ 38 Ident: "foo", 39 PkgName: "bar", 40 ID: "a.b/bar", 41 Dir: "a.b/bar", 42 }}, 43 {"", "a.b/bar:foo", ImportInfo{ 44 Ident: "foo", 45 PkgName: "foo", 46 ID: "a.b/bar:foo", 47 Dir: "a.b/bar", 48 }}, 49 {"", "strings", ImportInfo{ 50 Ident: "strings", 51 PkgName: "strings", 52 ID: "strings", 53 Dir: "strings", 54 }}, 55 } 56 for _, tc := range testCases { 57 t.Run(path.Join(tc.name, tc.path), func(t *testing.T) { 58 var ident *ast.Ident 59 if tc.name != "" { 60 ident = ast.NewIdent(tc.name) 61 } 62 got, err := ParseImportSpec(&ast.ImportSpec{ 63 Name: ident, 64 Path: ast.NewString(tc.path), 65 }) 66 if err != nil { 67 t.Fatal(err) 68 } 69 if !cmp.Equal(got, tc.want) { 70 t.Error(cmp.Diff(got, tc.want)) 71 } 72 }) 73 } 74 }