github.com/visualfc/goembed@v0.3.3/load_test.go (about) 1 package goembed_test 2 3 import ( 4 "go/ast" 5 "go/parser" 6 "go/token" 7 "os" 8 "testing" 9 10 "github.com/visualfc/goembed" 11 12 embedparser "github.com/visualfc/goembed/parser" 13 ) 14 15 func parserFile(fset *token.FileSet, src string) (*ast.File, error) { 16 file, err := parser.ParseFile(fset, "./main.go", src, parser.ParseComments) 17 if err != nil { 18 return nil, err 19 } 20 return file, nil 21 } 22 23 func parserEmbed(fset *token.FileSet, f *ast.File) ([]*goembed.Embed, error) { 24 eps, err := embedparser.ParseEmbed(fset, []*ast.File{f}) 25 if err != nil { 26 return nil, err 27 } 28 return goembed.CheckEmbed(eps.PatternPos, fset, []*ast.File{f}) 29 } 30 31 func load(src string) ([]*goembed.File, error) { 32 fset := token.NewFileSet() 33 f, err := parserFile(fset, src) 34 if err != nil { 35 return nil, err 36 } 37 ems, err := parserEmbed(fset, f) 38 if err != nil { 39 return nil, err 40 } 41 r := goembed.NewResolve() 42 wd, _ := os.Getwd() 43 for _, em := range ems { 44 _, err := r.Load(wd, fset, em) 45 if err != nil { 46 return nil, err 47 } 48 } 49 return r.Files(), nil 50 } 51 52 func testError(src string, want string, t *testing.T) { 53 _, err := load(src) 54 if err == nil { 55 t.Fatalf("must have error: %v", want) 56 } 57 if err.Error() != want { 58 t.Fatalf("\nwant %v\nhave %v", want, err) 59 } 60 } 61 62 type File struct { 63 Name string 64 Data string 65 } 66 67 func testLoad(src string, data []*File, t *testing.T) { 68 files, err := load(src) 69 if err != nil { 70 t.Fatalf("load error: %v", err) 71 } 72 if len(files) != len(data) { 73 t.Fatalf("load files error:\n want %v\n have %v", data, files) 74 } 75 for i, v := range files { 76 if v.Name != data[i].Name { 77 t.Fatalf("\nwant %v, have %v", data[i].Name, v.Name) 78 } 79 if string(v.Data) != data[i].Data { 80 t.Fatalf("\nwant %v, have %v", data[i].Data, string(v.Data)) 81 } 82 } 83 } 84 85 func TestErrorMisImportEmbed(t *testing.T) { 86 src := `package main 87 88 //go:embed testata/data1.txt 89 var data string 90 91 func main() { 92 } 93 ` 94 testError(src, `./main.go:3:3: go:embed only allowed in Go files that import "embed"`, t) 95 } 96 97 func TestErrorMultipleVars(t *testing.T) { 98 src := `package main 99 100 import _ "embed" 101 102 //go:embed testdata/data1.txt 103 var data, data2 string 104 105 func main() { 106 } 107 ` 108 testError(src, `./main.go:5:3: go:embed cannot apply to multiple vars`, t) 109 } 110 111 func TestErrorWithInitializer(t *testing.T) { 112 src := `package main 113 114 import _ "embed" 115 116 //go:embed testdata/data1.txt 117 var data string = "hello" 118 119 func main() { 120 } 121 ` 122 testError(src, `./main.go:5:3: go:embed cannot apply to var with initializer`, t) 123 } 124 125 func TestErrorVarType(t *testing.T) { 126 src := `package main 127 128 import _ "embed" 129 130 //go:embed testdata/data1.txt 131 var data [128]byte 132 133 func main() { 134 } 135 ` 136 testError(src, `./main.go:6:5: go:embed cannot apply to var of type [128]byte`, t) 137 } 138 139 func TestErrorVarType2(t *testing.T) { 140 src := `package main 141 142 import _ "embed" 143 144 //go:embed testdata/data1.txt 145 var data [][]byte 146 147 func main() { 148 } 149 ` 150 testError(src, `./main.go:6:5: go:embed cannot apply to var of type [][]byte`, t) 151 } 152 153 func TestErrorVarType3(t *testing.T) { 154 src := `package main 155 156 import _ "embed" 157 158 //go:embed testdata/data1.txt 159 var data map[int]int 160 161 func main() { 162 } 163 ` 164 testError(src, `./main.go:6:5: go:embed cannot apply to var of type map[int]int`, t) 165 } 166 167 func TestErrorMisplaced(t *testing.T) { 168 src := `package main 169 170 import _ "embed" 171 172 //go:embed testdata/data1.txt 173 //var data string 174 175 func main() { 176 } 177 ` 178 testError(src, `./main.go:5:3: misplaced go:embed directive`, t) 179 } 180 181 func TestErrorMultipleFiles(t *testing.T) { 182 src := `package main 183 184 import _ "embed" 185 186 //go:embed testdata/data1.txt testdata/data2.txt 187 var data string 188 189 func main() { 190 } 191 ` 192 testError(src, `./main.go:6:5: invalid go:embed: multiple files for type string`, t) 193 } 194 195 func TestErrorMultipleFiles2(t *testing.T) { 196 src := `package main 197 198 import _ "embed" 199 200 //go:embed testdata 201 var data string 202 203 func main() { 204 } 205 ` 206 testError(src, `./main.go:6:5: invalid go:embed: multiple files for type string`, t) 207 } 208 209 func TestLoadString(t *testing.T) { 210 src := `package main 211 212 import _ "embed" 213 214 //go:embed testdata/data1.txt 215 var data string 216 217 func main() { 218 } 219 ` 220 testLoad(src, []*File{{"testdata/data1.txt", "hello data1"}}, t) 221 } 222 223 func TestLoadString2(t *testing.T) { 224 src := `package main 225 226 import _ "embed" 227 228 //go:embed testdata/data1.txt 229 var data T 230 type T string 231 232 func main() { 233 } 234 ` 235 testLoad(src, []*File{{"testdata/data1.txt", "hello data1"}}, t) 236 } 237 238 func TestLoadString3(t *testing.T) { 239 src := `package main 240 241 import _ "embed" 242 243 var ( 244 //go:embed testdata/data1.txt 245 data string 246 ) 247 248 func main() { 249 } 250 ` 251 testLoad(src, []*File{{"testdata/data1.txt", "hello data1"}}, t) 252 } 253 254 func TestLoadBytes(t *testing.T) { 255 src := `package main 256 257 import _ "embed" 258 259 //go:embed testdata/data1.txt 260 var data []byte 261 262 func main() { 263 } 264 ` 265 testLoad(src, []*File{{"testdata/data1.txt", "hello data1"}}, t) 266 } 267 268 func TestLoadBytes2(t *testing.T) { 269 src := `package main 270 271 import _ "embed" 272 273 //go:embed testdata/data1.txt 274 var data T 275 type T []byte 276 277 func main() { 278 } 279 ` 280 testLoad(src, []*File{{"testdata/data1.txt", "hello data1"}}, t) 281 } 282 283 func TestLoadBytes3(t *testing.T) { 284 src := `package main 285 286 import _ "embed" 287 288 //go:embed testdata/data1.txt 289 var data []T 290 type T byte 291 292 func main() { 293 } 294 ` 295 testLoad(src, []*File{{"testdata/data1.txt", "hello data1"}}, t) 296 } 297 298 func TestLoadFiles(t *testing.T) { 299 src := `package main 300 301 import "embed" 302 303 //go:embed testdata/data1.txt 304 //go:embed testdata/data2.txt 305 var data embed.FS 306 307 func main() { 308 } 309 ` 310 testLoad(src, []*File{{"testdata/data1.txt", "hello data1"}, {"testdata/data2.txt", "hello data2"}}, t) 311 } 312 313 func TestLoadFiles2(t *testing.T) { 314 src := `package main 315 316 import em "embed" 317 318 //go:embed testdata/data1.txt 319 //go:embed testdata/data2.txt 320 var data em.FS 321 322 func main() { 323 } 324 ` 325 testLoad(src, []*File{{"testdata/data1.txt", "hello data1"}, {"testdata/data2.txt", "hello data2"}}, t) 326 } 327 328 func TestLoadFiles3(t *testing.T) { 329 src := `package main 330 331 import . "embed" 332 333 //go:embed testdata/data1.txt 334 //go:embed testdata/data2.txt 335 var data FS 336 337 func main() { 338 } 339 ` 340 testLoad(src, []*File{{"testdata/data1.txt", "hello data1"}, {"testdata/data2.txt", "hello data2"}}, t) 341 } 342 343 func TestLoadOne(t *testing.T) { 344 src := `package main 345 346 import _ "embed" 347 348 //go:embed testdata/one 349 var data string 350 351 func main() { 352 } 353 ` 354 testLoad(src, []*File{{"testdata/one/data.txt", "hello data"}}, t) 355 } 356 357 func TestLoadTwo(t *testing.T) { 358 src := `package main 359 360 import _ "embed" 361 362 //go:embed testdata/two/data1.txt 363 var data1 string 364 365 //go:embed testdata/two/data2.txt 366 var data1 string 367 368 func main() { 369 } 370 ` 371 testLoad(src, []*File{{"testdata/two/data1.txt", "sub data1"}, {"testdata/two/data2.txt", "sub data2"}}, t) 372 }