1// Copyright 2017 The Bazel Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package resolve_test 6 7import ( 8 "strings" 9 "testing" 10 11 "go.starlark.net/internal/chunkedfile" 12 "go.starlark.net/resolve" 13 "go.starlark.net/starlarktest" 14 "go.starlark.net/syntax" 15) 16 17func setOptions(src string) { 18 resolve.AllowGlobalReassign = option(src, "globalreassign") 19 resolve.AllowRecursion = option(src, "recursion") 20 resolve.AllowSet = option(src, "set") 21 resolve.LoadBindsGlobally = option(src, "loadbindsglobally") 22} 23 24func option(chunk, name string) bool { 25 return strings.Contains(chunk, "option:"+name) 26} 27 28func TestResolve(t *testing.T) { 29 defer setOptions("") 30 filename := starlarktest.DataFile("resolve", "testdata/resolve.star") 31 for _, chunk := range chunkedfile.Read(filename, t) { 32 f, err := syntax.Parse(filename, chunk.Source, 0) 33 if err != nil { 34 t.Error(err) 35 continue 36 } 37 38 // A chunk may set options by containing e.g. "option:recursion". 39 setOptions(chunk.Source) 40 41 if err := resolve.File(f, isPredeclared, isUniversal); err != nil { 42 for _, err := range err.(resolve.ErrorList) { 43 chunk.GotError(int(err.Pos.Line), err.Msg) 44 } 45 } 46 chunk.Done() 47 } 48} 49 50func TestDefVarargsAndKwargsSet(t *testing.T) { 51 source := "def f(*args, **kwargs): pass\n" 52 file, err := syntax.Parse("foo.star", source, 0) 53 if err != nil { 54 t.Fatal(err) 55 } 56 if err := resolve.File(file, isPredeclared, isUniversal); err != nil { 57 t.Fatal(err) 58 } 59 fn := file.Stmts[0].(*syntax.DefStmt).Function.(*resolve.Function) 60 if !fn.HasVarargs { 61 t.Error("HasVarargs not set") 62 } 63 if !fn.HasKwargs { 64 t.Error("HasKwargs not set") 65 } 66} 67 68func TestLambdaVarargsAndKwargsSet(t *testing.T) { 69 resolve.AllowLambda = true 70 source := "f = lambda *args, **kwargs: 0\n" 71 file, err := syntax.Parse("foo.star", source, 0) 72 if err != nil { 73 t.Fatal(err) 74 } 75 if err := resolve.File(file, isPredeclared, isUniversal); err != nil { 76 t.Fatal(err) 77 } 78 lam := file.Stmts[0].(*syntax.AssignStmt).RHS.(*syntax.LambdaExpr).Function.(*resolve.Function) 79 if !lam.HasVarargs { 80 t.Error("HasVarargs not set") 81 } 82 if !lam.HasKwargs { 83 t.Error("HasKwargs not set") 84 } 85} 86 87func isPredeclared(name string) bool { return name == "M" } 88 89func isUniversal(name string) bool { return name == "U" || name == "float" } 90