cuelang.org/go@v0.13.0/cue/ast/importpath_test.go (about) 1 // Copyright 2025 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 ast 16 17 import ( 18 "testing" 19 20 "github.com/go-quicktest/qt" 21 ) 22 23 var parseImportPathTests = []struct { 24 testName string 25 path string 26 want ImportPath 27 wantCanonical string 28 }{{ 29 testName: "StdlibLikeWithSlash", 30 path: "stdlib/path", 31 want: ImportPath{ 32 Path: "stdlib/path", 33 Qualifier: "path", 34 }, 35 }, { 36 testName: "StdlibLikeNoSlash", 37 path: "math", 38 want: ImportPath{ 39 Path: "math", 40 Qualifier: "math", 41 }, 42 }, { 43 testName: "StdlibLikeExplicitQualifier", 44 path: "stdlib/path:other", 45 want: ImportPath{ 46 Path: "stdlib/path", 47 ExplicitQualifier: true, 48 Qualifier: "other", 49 }, 50 }, { 51 testName: "StdlibLikeExplicitQualifierNoSlash", 52 path: "math:other", 53 want: ImportPath{ 54 Path: "math", 55 ExplicitQualifier: true, 56 Qualifier: "other", 57 }, 58 }, { 59 testName: "WithMajorVersion", 60 path: "foo.com/bar@v0", 61 want: ImportPath{ 62 Path: "foo.com/bar", 63 Version: "v0", 64 Qualifier: "bar", 65 }, 66 }, { 67 testName: "WithFullVersion", 68 path: "foo.com/bar@v0.2.3:xxx", 69 want: ImportPath{ 70 Path: "foo.com/bar", 71 Version: "v0.2.3", 72 Qualifier: "xxx", 73 ExplicitQualifier: true, 74 }, 75 }, { 76 testName: "WithFullVersionNoQualifier", 77 path: "foo.com/bar@v0.2.3-foo", 78 want: ImportPath{ 79 Path: "foo.com/bar", 80 Version: "v0.2.3-foo", 81 Qualifier: "bar", 82 }, 83 }, { 84 testName: "WithLatest", 85 path: "foo.com/bar@latest", 86 want: ImportPath{ 87 Path: "foo.com/bar", 88 Version: "latest", 89 Qualifier: "bar", 90 }, 91 }, { 92 testName: "WithMajorVersionLatest", 93 path: "foo.com/bar@v1.latest", 94 want: ImportPath{ 95 Path: "foo.com/bar", 96 Version: "v1.latest", 97 Qualifier: "bar", 98 }, 99 }, { 100 testName: "WithMajorVersionNoSlash", 101 path: "main.test@v0", 102 want: ImportPath{ 103 Path: "main.test", 104 Version: "v0", 105 Qualifier: "", 106 }, 107 }, { 108 testName: "WithMajorVersionAndExplicitQualifier", 109 path: "foo.com/bar@v0:other", 110 want: ImportPath{ 111 Path: "foo.com/bar", 112 Version: "v0", 113 ExplicitQualifier: true, 114 Qualifier: "other", 115 }, 116 }, { 117 testName: "WithMajorVersionAndNoQualifier", 118 path: "foo.com/bar@v0", 119 want: ImportPath{ 120 Path: "foo.com/bar", 121 Version: "v0", 122 Qualifier: "bar", 123 }, 124 }, { 125 testName: "WithRedundantQualifier", 126 path: "foo.com/bar@v0:bar", 127 want: ImportPath{ 128 Path: "foo.com/bar", 129 Version: "v0", 130 ExplicitQualifier: true, 131 Qualifier: "bar", 132 }, 133 wantCanonical: "foo.com/bar@v0", 134 }, { 135 testName: "WithPattern", 136 path: "foo.com/bar/.../blah", 137 want: ImportPath{ 138 Path: "foo.com/bar/.../blah", 139 Version: "", 140 ExplicitQualifier: false, 141 Qualifier: "blah", 142 }, 143 wantCanonical: "foo.com/bar/.../blah", 144 }, { 145 testName: "WithPatternAtEnd", 146 path: "foo.com/bar/...", 147 want: ImportPath{ 148 Path: "foo.com/bar/...", 149 Version: "", 150 ExplicitQualifier: false, 151 Qualifier: "", 152 }, 153 wantCanonical: "foo.com/bar/...", 154 }, { 155 testName: "WithUnderscoreLastElement", 156 path: "foo.com/bar/_foo", 157 want: ImportPath{ 158 Path: "foo.com/bar/_foo", 159 Version: "", 160 ExplicitQualifier: false, 161 Qualifier: "_foo", 162 }, 163 wantCanonical: "foo.com/bar/_foo", 164 }, { 165 testName: "WithHashLastElement", 166 path: "foo.com/bar/#foo", 167 want: ImportPath{ 168 Path: "foo.com/bar/#foo", 169 Version: "", 170 ExplicitQualifier: false, 171 Qualifier: "", 172 }, 173 wantCanonical: "foo.com/bar/#foo", 174 }} 175 176 func TestParseImportPath(t *testing.T) { 177 for _, test := range parseImportPathTests { 178 t.Run(test.testName, func(t *testing.T) { 179 parts := ParseImportPath(test.path) 180 qt.Assert(t, qt.DeepEquals(parts, test.want)) 181 qt.Assert(t, qt.Equals(parts.String(), test.path)) 182 if test.wantCanonical == "" { 183 test.wantCanonical = test.path 184 } 185 qt.Assert(t, qt.Equals(parts.Canonical().String(), test.wantCanonical)) 186 }) 187 } 188 } 189 190 func TestImportPathStringAddsQualifier(t *testing.T) { 191 ip := ImportPath{ 192 Path: "foo.com/bar", 193 Version: "v0", 194 Qualifier: "baz", 195 } 196 qt.Assert(t, qt.Equals(ip.String(), "foo.com/bar@v0:baz")) 197 } 198 199 func TestImportPathStringAddsQualifierWhenNoVersion(t *testing.T) { 200 ip := ImportPath{ 201 Path: "foo.com/bar", 202 Qualifier: "baz", 203 } 204 qt.Assert(t, qt.Equals(ip.String(), "foo.com/bar:baz")) 205 }