...

Source file src/cuelang.org/go/cue/ast/astutil/util_test.go

Documentation: cuelang.org/go/cue/ast/astutil

     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  	"cuelang.org/go/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 diff := cmp.Diff(got, tc.want); diff != "" {
    70  				t.Error(diff)
    71  			}
    72  		})
    73  	}
    74  }
    75  

View as plain text