github.com/k14s/starlark-go@v0.0.0-20200720175618-3a5c849cc368/resolve/resolve_test.go (about)

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