go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/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  	"go.starlark.net/internal/chunkedfile"
    12  	"go.starlark.net/resolve"
    13  	"go.starlark.net/starlarktest"
    14  	"go.starlark.net/syntax"
    15  )
    16  
    17  // A test may enable non-standard options by containing (e.g.) "option:recursion".
    18  func getOptions(src string) *syntax.FileOptions {
    19  	return &syntax.FileOptions{
    20  		Set:               option(src, "set"),
    21  		While:             option(src, "while"),
    22  		TopLevelControl:   option(src, "toplevelcontrol"),
    23  		GlobalReassign:    option(src, "globalreassign"),
    24  		LoadBindsGlobally: option(src, "loadbindsglobally"),
    25  		Recursion:         option(src, "recursion"),
    26  	}
    27  }
    28  
    29  func option(chunk, name string) bool {
    30  	return strings.Contains(chunk, "option:"+name)
    31  }
    32  
    33  func TestResolve(t *testing.T) {
    34  	filename := starlarktest.DataFile("resolve", "testdata/resolve.star")
    35  	for _, chunk := range chunkedfile.Read(filename, t) {
    36  		// A chunk may set options by containing e.g. "option:recursion".
    37  		opts := getOptions(chunk.Source)
    38  
    39  		f, err := opts.Parse(filename, chunk.Source, 0)
    40  		if err != nil {
    41  			t.Error(err)
    42  			continue
    43  		}
    44  
    45  		if err := resolve.File(f, isPredeclared, isUniversal); err != nil {
    46  			for _, err := range err.(resolve.ErrorList) {
    47  				chunk.GotError(int(err.Pos.Line), err.Msg)
    48  			}
    49  		}
    50  		chunk.Done()
    51  	}
    52  }
    53  
    54  func TestDefVarargsAndKwargsSet(t *testing.T) {
    55  	source := "def f(*args, **kwargs): pass\n"
    56  	file, err := syntax.Parse("foo.star", source, 0)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if err := resolve.File(file, isPredeclared, isUniversal); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	fn := file.Stmts[0].(*syntax.DefStmt).Function.(*resolve.Function)
    64  	if !fn.HasVarargs {
    65  		t.Error("HasVarargs not set")
    66  	}
    67  	if !fn.HasKwargs {
    68  		t.Error("HasKwargs not set")
    69  	}
    70  }
    71  
    72  func TestLambdaVarargsAndKwargsSet(t *testing.T) {
    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" }