cuelang.org/go@v0.10.1/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/google/go-cmp/cmp"
    22  
    23  	"cuelang.org/go/cue/ast"
    24  )
    25  
    26  func TestImportInfo(t *testing.T) {
    27  	testCases := []struct {
    28  		name string
    29  		path string
    30  		want ImportInfo
    31  	}{
    32  		{"", "a.b/bar", ImportInfo{
    33  			Ident:   "bar",
    34  			PkgName: "bar",
    35  			ID:      "a.b/bar",
    36  			Dir:     "a.b/bar",
    37  		}},
    38  		{"foo", "a.b/bar", ImportInfo{
    39  			Ident:   "foo",
    40  			PkgName: "bar",
    41  			ID:      "a.b/bar",
    42  			Dir:     "a.b/bar",
    43  		}},
    44  		{"", "a.b/bar:foo", ImportInfo{
    45  			Ident:   "foo",
    46  			PkgName: "foo",
    47  			ID:      "a.b/bar:foo",
    48  			Dir:     "a.b/bar",
    49  		}},
    50  		{"", "strings", ImportInfo{
    51  			Ident:   "strings",
    52  			PkgName: "strings",
    53  			ID:      "strings",
    54  			Dir:     "strings",
    55  		}},
    56  	}
    57  	for _, tc := range testCases {
    58  		t.Run(path.Join(tc.name, tc.path), func(t *testing.T) {
    59  			var ident *ast.Ident
    60  			if tc.name != "" {
    61  				ident = ast.NewIdent(tc.name)
    62  			}
    63  			got, err := ParseImportSpec(&ast.ImportSpec{
    64  				Name: ident,
    65  				Path: ast.NewString(tc.path),
    66  			})
    67  			if err != nil {
    68  				t.Fatal(err)
    69  			}
    70  			if diff := cmp.Diff(got, tc.want); diff != "" {
    71  				t.Error(diff)
    72  			}
    73  		})
    74  	}
    75  }